dense-analysis / dense-analysis/ale

Add support for `indent` (C autoformatter)

Open
#4,817 0 comments 0 reactions 0 assignees View on GitHub
new tool
Dominant language
Vim Script
Stars
14k
Forks
1.5k
Avg merge
17h 49m
Merged PRs (30d)
1

Description

**Name:** indent
**URL:** https://www.gnu.org/software/indent/

`indent(1)` is a program for automatically formatting C code. The GNU variant supports common style shortcuts such as `-kr`.

This small UNIX trickery makes it a linter as well:

```console
$ cat > main.c <<'EOF'
#include

void printNumbers(int start, int end)

{
for (int i = start; i <= end; i++) {
if (i % 2 == 0) {
printf("%d is even.\n", i);
} else {
printf("%d is odd.\n", i);
}
}
}

int main() {
int start = 1;
int end = 10;
printNumbers(start, end);
return 0;
}
EOF
$ diff -u main.c <(indent -kr -ts1 main.c -o /dev/stdout)
--- main.c 2024-08-08 12:25:23.743927683 +0200
+++ /dev/fd/63 2024-08-08 12:25:26.142459754 +0200
@@ -1,7 +1,6 @@
#include

void printNumbers(int start, int end)
-
{
for (int i = start; i <= end; i++) {
if (i % 2 == 0) {
@@ -12,9 +11,10 @@
}
}

-int main() {
+int main()
+{
int start = 1;
int end = 10;
- printNumbers(start, end);
+ printNumbers(start, end);
return 0;
}
$ echo $?
1
$ indent -kr -ts1 main.c
$ diff -u main.c <(indent -kr -ts1 main.c -o /dev/stdout)
$ echo $?
0
```

There's no message but the diff can be processed to output a simple generic "lint error" on relevant lines which would already go a long way. It could also be processed to suggest fixes.

It can also read from stdin if so desired:

```
cat main.c | indent -kr -ts1 /dev/stdin -o /dev/stdout
```

Manuals:

- GNU: https://www.gnu.org/software/indent/manual/
- BSD: https://www.unix.com/man-page/bsd/1/indent/

Both modify in place and produce backup files if no output file is provided, this is probably undesirable so the `/dev/stdout` trickery might be preferred.

BSD `indent` does not use `-o` for output but a mere additional argument instead: GNU vs BSD should be detected to pass things around properly.

BSD indent supports much less features too, notably it does not support common styles like `-kr` and not all of what's behind `-kr` so if BSD indent is detected it could be convenient to have high level options to mimic GNU style flags when BSD indent is detected, e.g K&R would be:

```
indent -nbad -bap -nbc -br -c33 -cd33 -ncdb -ce -ci4 -cli0 -d0 -di1 -nfc1 -i4 -ip -lp -npcs -npsl -nsc -nsob main.c /dev/stdout`
```

So having a `g:ale_c_indent_style = 'kr'` would automatically (e.g by setting `g:ale_c_indent_options`) use `-kr` with GNU `indent` and the above with BSD `indent`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.