-
Notifications
You must be signed in to change notification settings - Fork 27.9k
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
Small but infuriating undesired auto-indentation in C++ source files #36148
Comments
Tried to reproduce the issue like you described above. There is no ability to format the file without the cpptools extension. So there it must be an issue of the extension in my opinion. (When the extension does the formatting + indenting for you) |
I was able to reproduce it with --disable-extensions. I also uninstalled the C++ extension and it still reproduces : you do need to save the file with .cpp extension though. So it seems something related to the built-in C++ support. Also, the problem is not triggered by an explicit format (I didn't use "format document", just typed ENTER with the cursor right after the closing bracket. |
The langugage configuration rules for c++ are defined in an extension that is bundled with VS Code, so it should repro without any other extensions installed. Need to investigate while the setting editor.autoIndent is not honered. //CC @sean-mcmanus |
Thanks Erich, you're right: this repros on a bare bone installation. I think this is a regression, I don't remember having so much trouble with automatic indentation in earlier versions. |
Possible duplicate of #32746 |
It may be worth splitting this into two separate bugs for tracking purposes:
I opened this issue primarily to report the latter. |
1.
|
Thanks @alexandrudima ! PS. the animated screen shoots are really nice, how did you capture them? |
@tlemo I captured those on a mac, but for Windows I use LICEcap |
@alexandrudima FYI, the cpptools extension sets the configurationDefaults.[cpp]|[c].editor.autoIndent setting to false in our package.json in order to provide a good default experience for C/C++ users. |
@sean-mcmanus I've removed c/cpp indentation rules from core then ppl can get a good c/cpp experience with vanilla vscode. |
@rebornix This seems almost impossible to fix using regex rules. Would using IntelliSense with direct knowledge of scope be more feasible? |
Not sure if this is the right place to post this, but I'm also having an issue with indentation in C++ source files. I'm not using the VSCode C++ extension so I'm assuming this behaviour is coming from stock VSCode. When writing a function definition, after writing the opening brace (vscode automatically inserts the closing brace) and pressing Enter, vscode indents the next line and closing brace way too far. I've not found a setting to disable this. Setting Example: Before: void IQuicHttpStream::OnReadComplete(CompletionOnceCallback callback,
int result) {<cursor-is-here>} After pressing Enter: void IQuicHttpStream::OnReadComplete(CompletionOnceCallback callback,
int result) {
<cursor-is-here>
} Desired: void IQuicHttpStream::OnReadComplete(CompletionOnceCallback callback,
int result) {
<cursor-is-here>
} |
@DaanDeMeyer The issue you are describing repros for other languages like TypeScript as well. It looks like a duplicate of #30872 . But maybe you can upvote it and add your repro details. |
I also have this problem for other languages such as C or JavaScript. I want to control indent increase or decrease by myself. Is there any possible way to override IndentationRule? |
Hi, I have just started using vscode on ubuntu 16.04. I am having problems disabling Auto Indent. I have tried on V1.31 and also the insider version (V1.32-insider). I use the WhiteSmiths style for coding... I know this is a less common style but it is very frustrating not being able to turn the Auto Indent off. I usually use visual studio (currently 2015); I always switch the editor to the block mode and this is perfect. Is there any possible work around - or am I doing something wrong? Just disabling Auto Indenting and having me control all indenting seems like the best option for now. Is it possible I somehow have an old version? |
@DaanDeMeyer |
@naveenk2k It looks like it's still an open issue on VS Code: #30872 |
@sean-mcmanus I see. Thank you for the quick response. |
same thing when creating a class |
#30872 is closed, but I'm not sure it's the best solution to #36148 (comment). The issue isn't that auto-indent is enabled, but that it goes to the wrong place. With auto-indent off entirely, the cursor ends up in the wrong location most of the time. It's just that, in the cases where auto-indent would be catastrophically wrong, it's now only a few indent levels wrong. |
The current indent rules make it basically impossible to fix the issue reported by @DaanDeMeyer. The only workaround I can think of is implementing a formatter that fixes this "on typing". I implemented a simple proof of concept here (https://github.com/stoyannk/indent-formatter). The downside is that the formatter might cause unintended "flickering" of the code, as it runs in a different thread after the VSCode internal formatter has already put the cursor and brace in the wrong place. This item has been in the backlog since late 2020. Are there plans to address it or the formatters are the only way? |
Is the API enough to detect when there's insufficient information? Like if the line where the curly brace starts is known to be incomplete? As a hacky workaround, maybe it could report an indent of zero in those cases? It'd still be wrong, but 0 is usually closer to the correct answer than what vscode currently does, and that'd preserve autoindent for the cases it can detect. |
Looking at a more extreme / contrived example we need to be able to consider times where we both honour and ignore the existing indentation. 1 if (myArray
2 .filter(x
3 .paramWithLongName)
4 .map(x -> {
5 return x.id == 7;
6 })
7 .findAny()
8 .orElse(false)) {
9 // Do something
10 } On line 5, we indent 6 (continuation indent + regular indent) whereas on line 9 we only indent 2 (cancelling the continuation indent). I think it would be tricky to distinguish between lines 4 and 8 using regex alone. As far as I can see, we would need to not only consider the indent size, but also the reason for the indents so when we close a section, we can undo that part of the indent. We may also need to have some kind of bracket matching in place too. Here is some very hacky TS I put together to test the idea (no safety checks for incorrect order of closing and not very neat as I just wanted to check all the ways indentation might change. I am not sure how much this would effect performance either. The current solution only looks at a single line and uses regex whereas this would require the file to be parsed. Perhaps we could do something with a window of ~1000 lines and then make assumptions if we need to close an indentation which was started longer ago than that? |
Please see also #164177. |
I was not able to reproduce the original issue in this thread. The following gif shows the behavior with Screen.Recording.2024-03-28.at.12.09.41.movThe following gif shows the behavior with Screen.Recording.2024-03-28.at.12.11.32.movI have no extensions installed relative to C++. I will therefore close this issue. If you continue seeing issues, please feel free to comment on this issue so I can reopen it. |
@aiday-mar The example in #36148 (comment) still occurs. Or should that be moved now to a separate issue? |
Steps to Reproduce:
Here's a small snippet, where ^ is the cursor position (not an actual character in the file!)
If I press ENTER, the closing bracket and the cursor are auto-indented:
This is a small but extremely distracting issue in code bases which don't use brackets for single sub-statements.
Btw, "editor.autoIndent" is set to false
Reproduces without extensions: Yes/No - Yes
The text was updated successfully, but these errors were encountered: