• Login
Community
  • Login

Looking for good C code formatter

Scheduled Pinned Locked Moved General Discussion
10 Posts 4 Posters 11.5k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B
    Bernhard Ege
    last edited by Feb 26, 2022, 10:02 AM

    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.

    E D 2 Replies Last reply Feb 26, 2022, 11:54 AM Reply Quote 0
    • E
      Ekopalypse @Bernhard Ege
      last edited by Feb 26, 2022, 11:54 AM

      @bernhard-ege

      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.

      1 Reply Last reply Reply Quote 4
      • D
        dinkumoil @Bernhard Ege
        last edited by Feb 26, 2022, 12:15 PM

        @bernhard-ege

        Another possible solution is >> AStyle << . It can be controlled via command line arguments, project specific option files or a global options file.

        M 1 Reply Last reply Feb 26, 2022, 1:38 PM Reply Quote 6
        • M
          Michael Vincent @dinkumoil
          last edited by Feb 26, 2022, 1:38 PM

          @dinkumoil said in Looking for good C code formatter:

          Another possible solution is >> AStyle <<

          @Bernhard-Ege

          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 have astyle 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.

          1 Reply Last reply Reply Quote 5
          • B
            Bernhard Ege
            last edited by Feb 27, 2022, 2:52 PM

            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.

            M 1 Reply Last reply Feb 27, 2022, 4:35 PM Reply Quote 4
            • M
              Michael Vincent @Bernhard Ege
              last edited by Feb 27, 2022, 4:35 PM

              @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:

              • Notepad++ >=8.3 compatibilities
              • Plugin Admin inclusion

              Thanks for keeping Notepad++ plugin ecosystem alive!

              Cheers.

              M 1 Reply Last reply Feb 28, 2022, 4:25 PM Reply Quote 3
              • M
                Michael Vincent @Michael Vincent
                last edited by Feb 28, 2022, 4:25 PM

                @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:

                @Bernhard-Ege

                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.

                B 2 Replies Last reply Mar 5, 2022, 7:59 AM Reply Quote 3
                • B
                  Bernhard Ege @Michael Vincent
                  last edited by Mar 5, 2022, 7:59 AM

                  @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.

                  M 1 Reply Last reply Mar 5, 2022, 12:40 PM Reply Quote 0
                  • B
                    Bernhard Ege @Michael Vincent
                    last edited by Mar 5, 2022, 8:09 AM

                    @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. :-(

                    1 Reply Last reply Reply Quote 3
                    • M
                      Michael Vincent @Bernhard Ege
                      last edited by Mar 5, 2022, 12:40 PM

                      @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.

                      1 Reply Last reply Reply Quote 2
                      7 out of 10
                      • First post
                        7/10
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors