Looking for good C code formatter
-
I would like to know if there is a way to have Notepad++ properly reformat my C-code?
I am aware of TextFX->TextFX Edit ->Reindent C++ code but isn’t very good.
Specifically it doesn’t handle if/for statements without { }. They end up not being indented correctly.
-
Built-in - no.
But if you happen to know of a standalone tool, you can always start it from the run menu and specify the filename as$(FULL_CURRENT_PATH)
.
Clang-format seems to do a good job. The easiest way to install it would be to download an archive from here, unzip it and use it.If you are brave, you can install my attempt to build a lsp-plugin, set up a C lsp server (again, Clang is decent) and use its (plugin) built-in format call.
-
Another possible solution is >> AStyle <<. It can be controlled via command line arguments, project specific option files or a global options file.
-
@dinkumoil said in Looking for good C code formatter:
Another possible solution is >> AStyle <<
I second
astyle
- I don’t code in C/C++ much save for Notepad++ plugins and I’m not that picky about code formatting, but I do haveastyle
installed, shortcut’d in Notepad++ and use it now and again (if I remember) to format my code nicely, starndard-ly.My
.astylerc
if you like to get you started:style=allman indent=spaces=4 indent-classes indent-switches indent-namespaces indent-preproc-block break-blocks pad-oper pad-comma pad-header pad-paren-in align-pointer=middle align-reference=name break-one-line-headers remove-braces attach-return-type attach-return-type-decl convert-tabs max-code-length=120 align-pointer=name suffix=none
Of course, your mileage may vary - there are a ton of optional configuration tuning parameters and pre-programmed styles to start from. If you’re serious about it, give the AStyle Documentation a read and tune it to your liking.
Cheers.
-
Thank you for the suggestions. I found the seemingly abandoned NppAStyle plugin for Notepad++ and decided to see if it could be made runnable with Notepad++ and the latest Visual Studio (2022). After some hours or trial and error, I did succeed and NppAStyle is now working for me (haven’t made a thorough test).
If you want, you can download my code from here. It will compile with VS2022 but will require some optional packages installed via Visual Studio Installer (C++, C++).
https://github.com/galmok/NppAStyle
I haven’t modified documentation or uploaded a finished plugin (yet). I might do that, but a quick check didn’t reveal to me how I am supposed to do that and what the obligations for doing so is.
-
@bernhard-ege said in Looking for good C code formatter:
I found the seemingly abandoned NppAStyle plugin for Notepad++ and decided to see if it could be made runnable with Notepad++ and the latest Visual Studio (2022)
I’ve done the same with other abandoned plugins.
You may want to have a read here:
Thanks for keeping Notepad++ plugin ecosystem alive!
Cheers.
-
@michael-vincent said in Looking for good C code formatter:
I’ve done the same with other abandoned plugins.
You may want to have a read here:I just submitted 2 pull requests:
- remove
afxres.h
dependency to build on community - updates for N++ 8.3 and Sci_Position (actually not that many changes - maybe 6 or so over 2 files).
Cheers.
- remove
-
@michael-vincent I have a question about the afxres.h removal. The project as it is can already be completely built by VS 2022 community edition as that is what I am using. You need to install “C++ MFC for latest v143 build tools (x86 & x64)” using the Visual Studio Installer. Then it compiles without issues. In that light, should I accept that pull request?
I have to admit currently not knowing what the afxres.h to windows.h change means for the project. It could be perfectly fine, but I guess there was a reason it was depending on afxres.h?
The other pull request seems to be good changes.
-
@michael-vincent Well, now I managed to accept the pull request about afxres.h before waiting for you input. I don’t full understand why github did that as I was only looking at your other pull request. I am not comfortable using github when it does crap like that. :-(
-
@bernhard-ege said in Looking for good C code formatter:
I have a question about the afxres.h removal
I thought afxres.h was only included in paid for visual studio, not community. My bad if it is. It was failing for me and I know it generally isn’t needed and can simply be replaced with windows.h, but feel free to reject that pull request and just merge the other one which fixes the >2G file issues going forward.
If GitHub seem to merge the other one, it should be pretty easy to back it out. They were really only two changes, comment out AFXRes.h and add windows.h and the definition for IDC_STATIC
Happy to help, thanks for supporting Notepad++ plugins!!
Cheers.