lassik / emacs-format-all-the-code

Auto-format source code in many languages with one command
https://melpa.org/#/format-all
MIT License
613 stars 106 forks source link

Comment out some lines in a c preprocessor (cpp) file will cause the comment section not to be left-aligned. #178

Closed hongyi-zhao closed 2 years ago

hongyi-zhao commented 2 years ago

On Ubuntu 20.04.3 LTS, I installed the git master version of format-all as follows:

(use-package format-all
  :hook (format-all-mode . format-all-ensure-formatter)
  (prog-mode . format-all-mode)
  :config
  (setq format-all-show-errors 'errors)
  )

Then I opened the following test.cpp (c preprocessor) file in Emacs:

$ gcc -E ifdef.cxx
# 1 "ifdef.cxx"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "ifdef.cxx"

#if 0
int i = 1;
#else
int i = 2;
#endif

When I try to comment out the following part in the above file with M-;:

$ gcc -E ifdef.cxx
# 1 "ifdef.cxx"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "ifdef.cxx"

I found that this file will be formatted into the layout below:

image

As you can see, commenting out some lines in the above file will cause the comment section not to be left-aligned. I am very confused about this behavior triggered by format-all. Any hints will be appreciated.

See here for a related discussion.

Regards, HZ

lassik commented 2 years ago

I don't have any obvious solutions. What formatter are you using -- clang-format or astyle? I tried both, and I don't get the indentation problem you get.

hongyi-zhao commented 2 years ago

What formatter are you using -- clang-format or astyle?

Neither of the above tools is installed on my system. Currently, I have installed clang-format and this problem has been resolved:

$ sudo apt install clang-format

image

Check here and here for the related documents/tools/projects.