microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.7k stars 29.45k forks source link

Weird code indentation C++ #178334

Open supernova-III opened 1 year ago

supernova-III commented 1 year ago

Issue Type: Bug

Hi there!

I have such piece of code (code indentation is 4 spaces):

int WINAPI WinMain(::HINSTANCE instance, ::HINSTANCE prev_instance,
    char *cmdline, int nshowcmd) {}

And I get weird indentation when I put my cursor between curly braces and press Enter:

int WINAPI WinMain(::HINSTANCE instance, ::HINSTANCE prev_instance,
    char *cmdline, int nshowcmd) {
        // weird caret position (8 spaces from the beginning of the line)
    } // weird brace position (4 spaces from the beginning of the line)

But I would expect

int WINAPI WinMain(::HINSTANCE instance, ::HINSTANCE prev_instance,
    char *cmdline, int nshowcmd) {
    // expected caret position
} // expected brace position

Now with the second piece of code, if I put my cursor after opening curly brace and hit enter, my cursor gets to this weird position:

int WINAPI WinMain(::HINSTANCE instance, ::HINSTANCE prev_instance,
    char *cmdline, int nshowcmd) { // 1. I'm here, pressing Enter
        // 2. my caret is here (8 spaces from the beginning of the line)
    // !. But it should be here (4 spaces from the beginning of the line)
}

It seems like it applies my indentation rules (4 spaces), but the code gets aligned to char* cmdline. So I always have to fix it manually. What should I do so that it will indent in expected way?

I'm using clang-format:

---
BasedOnStyle:  Google
IndentWidth: 4
SortIncludes: Never
AllowShortCaseLabelsOnASingleLine: true
IndentCaseBlocks: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AlignAfterOpenBracket: DontAlign

VS Code version: Code 1.67.1 (da15b6fd3ef856477bf6f4fb29ba1b7af717770d, 2022-05-06T12:37:03.389Z) OS version: Windows_NT x64 10.0.19045 Restricted Mode: No

System Info |Item|Value| |---|---| |CPUs|AMD Ryzen 7 5700U with Radeon Graphics (16 x 1797)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
oop_rasterization: enabled
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled| |Load (avg)|undefined| |Memory (System)|14.82GB (6.62GB free)| |Process Argv|. --crash-reporter-id 01a67f4c-d55c-40f4-b58a-c4b2f80741d9| |Screen Reader|no| |VM|0%|
Extensions (13) Extension|Author (truncated)|Version ---|---|--- markdown-mermaid|bie|1.15.3 vscode-glsllint|dto|1.8.0 go|gol|0.38.0 mesonbuild|mes|1.7.1 python|ms-|2022.8.1 vscode-pylance|ms-|2023.1.10 nas-vscode|rig|0.0.1 rust-analyzer|rus|0.3.1443 shader|sle|1.1.5 cmake|twx|0.0.17 vim|vsc|1.24.3 clang-format|xav|1.9.0 markdown-all-in-one|yzh|3.5.0 (2 theme extensions excluded)
vscodenpa commented 1 year ago

Thanks for creating this issue! It looks like you may be using an old version of VS Code, the latest stable release is 1.76.2. Please try upgrading to the latest version and checking whether this issue remains.

Happy Coding!

supernova-III commented 1 year ago

Thanks for creating this issue! It looks like you may be using an old version of VS Code, the latest stable release is 1.76.2. Please try upgrading to the latest version and checking whether this issue remains.

Happy Coding!

I have updated my vscode to the latest stable version. The problem still exists.

supernova-III commented 1 year ago

I found that this problem is not there if Auto Indent is disabled

supernova-III commented 6 months ago

Just for logging:

1 year passed

aiday-mar commented 6 months ago

Hi @supernova-III thank you for posting this issue. Yes this is an issue which applies to other languages as well, as it happens because indentation is generally based on the indentation of the first non-whitespace line directly above. In your case, the line directly above is indented with four spaces and so the next line is too. To fix this, which we would like to do at some point, we need to make the indentation code aware of the scopes of the code and make the indentation consistent across scope levels. Currently we are discussing using tree sitter in order to add this information.

supernova-III commented 6 months ago

@aiday-mar this is a great idea, thank you!

HuazyYang commented 2 days ago

Is there any one still tracking this problem? It's frustrating of having more indentation for functions with long parameter list and long condition chains of 'if' statement. If there is a topic about this, I want to vote for it.

supernova-III commented 2 days ago

@HuazyYang I'm still here :-) Counting years before it gets resolved.