Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for indent (C autoformatter) #4817

Open
lloeki opened this issue Aug 8, 2024 · 0 comments
Open

Add support for indent (C autoformatter) #4817

lloeki opened this issue Aug 8, 2024 · 0 comments
Labels
new tool Support for new linters, fixers, etc.

Comments

@lloeki
Copy link
Contributor

lloeki commented Aug 8, 2024

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:

$ cat > main.c <<'EOF'
#include <stdio.h>

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 <stdio.h>
 
 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:

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.

@lloeki lloeki added the new tool Support for new linters, fixers, etc. label Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new tool Support for new linters, fixers, etc.
Projects
None yet
Development

No branches or pull requests

1 participant