vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Automatic indentation doesn't work well with C/C++

Open adalisk-emikhaylov opened this issue 9 months ago • 2 comments

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.98.0
  • OS Version: Arch Linux x64 6.13.5-arch1-1

I believe something was changed regarding this recently, because I didn't have those issues before, but after looking through the changelogs of the few recent versions, I can't find anything.

Steps to Reproduce:

  1. Create a new file, set the language mode to C++.
  2. Paste following code:
    int main()
    {
        if (true)
            blah();
    }
    
  3. Put the cursor immediately after ;. Press Enter. This correctly unindents (in C/C++ if without braces only affects the next single statement):

a

  1. Now if you press Enter again (which is entirely reasonable to add a one line gap), we see this: (I've shown whitespace)

b

This indentation is now wrong. Pressing enter again after that doesn't change the indentation (it's still wrong).


This is a gap in the current automatic indentation system. Instead of "on enter" rules being only applied when pressing Enter, they need to be tracked when computing the indentation.

For a moment I thought that preserving the indentation of the previous line (even if it's only whitespace, which right now is ignored) would fix it, but it's not a good solution, because there's an option to strip trailing whitespace on save, which would break this.

adalisk-emikhaylov avatar Mar 10 '25 18:03 adalisk-emikhaylov

I dug into this a bit, and the situation improves if you add "indentationRules": { "indentNextLinePattern".

This isn't a complete fix, since this chokes if you press Enter twice after if (true). That'll probably need a fix in the logic, modifying the JSON alone isn't enough.

And also if you move that one indented line of code up or down using Alt+Up/Down, the indentation gets messed up.


I think what you need to do is make onEnterRules->"beforeText": "" be able to match empty lines, because currently it looks like it doesn't do that.

HolyBlackCat avatar Apr 21 '25 18:04 HolyBlackCat

Is there a way we can experiment with changing the indentation rules without checking out a whole development build? The documentation is very poor on how indentation is modified.

alanbork avatar May 22 '25 14:05 alanbork

You can create your own extension which defines a language configuration file with the appropriate indentation rules to test out the changes.

aiday-mar avatar May 28 '25 08:05 aiday-mar

@alanbork I'm testing it out on my own extension: https://github.com/HolyBlackCat/vscode-cats-cpp/

You could test on it if you want, I guess.

adalisk-emikhaylov avatar May 28 '25 09:05 adalisk-emikhaylov

This feature request is now a candidate for our backlog. The community has 60 days to upvote the issue. If it receives 20 upvotes we will move it to our backlog. If not, we will close it. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

@alanbork I'm testing it out on my own extension: https://github.com/HolyBlackCat/vscode-cats-cpp/

You could test on it if you want, I guess.

Looks like it's color only?

alanbork avatar Jun 02 '25 16:06 alanbork

@alanbork No, it replaces the syntax too.

adalisk-emikhaylov avatar Jun 02 '25 18:06 adalisk-emikhaylov

FWIW, the indentation in C has been recently screwed in VSCode (I'm using the latest). I haven't changed any settings.

It looks like it keeps the last line indentation and does not follow the C language rules anymore. That is, it does increase indentation unless I end a statement with a curly brace (e.g. an "if" to be followed by a single statement), nor does it reduce the indentation after a single line statement that follows an "if" (for example), back to the level of "if". Pasting does not adjust the indentation properly, either (to the level at which the inserted code going). A total mess!!

al20878 avatar Jun 13 '25 00:06 al20878

Hi this could be happening because I made it so that formatOnPaste no longer controls whether to auto-indent on paste. There is a new settings called editor.autoIndentOnPaste which controls whether to auto-indent on paste. You may have to turn this on. If you see other issues, you may file new issues in VS Code.

aiday-mar avatar Jun 13 '25 08:06 aiday-mar

@aiday-mar : I only see the following "auto" setting in my editor's settings:

"editor.autoIndent": "full"

which is documented as "Controls whether the editor should automatically adjust the indentation when users type, paste, move or indent lines." So my suggestion is to revise the otherwise conflicting settings when you add new ones. And also make them "visible" through the "File->Preferences->Settings" interface. Otherwise, it's becoming impossible to "figure out" what the available means are, since there is a whole zoo of them, scattered all over undocumented.

Also, suppose I set editor.autoIndentOnPaste as suggested, it still won't address the issue with indentation of single statements that follow "if"s (it does it correctly only when "{" was entered to open a block) -- and it does not return to the previous level when that statement is ended (but again, does so correctly only when "}" is entered at the end of the block -- by un-indenting that.

al20878 avatar Jun 13 '25 13:06 al20878

Hi thanks for the comment. I forgot to mention that this setting will be made available in the next release, not the one that happened. Alternatively you may also test this behavior on VS Code Insiders nightly release and you should be able to find the setting in the UI.

You made a great point about the other setting having an incorrect description. I will amend that.

I am not sure I understand your last paragraph. Could you please do a screen recording of the issue you are seeing and add precise steps so I understand better?

aiday-mar avatar Jun 17 '25 08:06 aiday-mar