microsoft / vscode

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

Indent/Outdent with tab key does not honor editor.autoIndent=false #63388

Open hostilefork opened 6 years ago

hostilefork commented 6 years ago

Issue Type: Bug

When I have:

    /*
     * Comment
     */

If I select it and hit tab, I get:

        /*
        * Comment
        */

If instead I had hit shift-tab, I get:

/*
    * Comment
    */

Same happens with Ctrl-] and Ctrl-[ (if those are supposed to make a difference)

I hoped turning off autoIndent would stop this, but no dice. I also turned off C++ formatting in the JSON config:

{
    "editor.autoIndent": false,
    "editor.detectIndentation": false,
    "C_Cpp.formatting": "Disabled"
}

I have done due diligence by asking (and bountying, +100) a question on StackOverflow to get an answer on what to do about this, but no one seems to know:

https://stackoverflow.com/q/53326134/preserve-spacing-on-indent-or-outdent-with-tab-in-vscode

If someone can provide a resolution here, be sure to go there and get the bounty points.

VS Code version: Code 1.29.1 (bc24f98b5f70467bc689abf41cc5550ca637088e, 2018-11-15T19:13:36.375Z) OS version: Windows_NT x64 10.0.17134

System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz (8 x 2808)| |GPU Status|2d_canvas: enabled
checker_imaging: disabled_off
flash_3d: enabled
flash_stage3d: enabled
flash_stage3d_baseline: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
native_gpu_memory_buffers: disabled_software
rasterization: enabled
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled| |Memory (System)|15.86GB (1.40GB free)| |Process Argv|| |Screen Reader|no| |VM|0%|
Extensions (3) Extension|Author (truncated)|Version ---|---|--- vscode-github|Kni|0.30.0 python|ms-|2018.10.1 cpptools|ms-|0.20.1
wasuaje commented 4 years ago

Guys Any update on this? I'm having the same issue!

tobia commented 4 years ago

Come on, I just tried it in Vim, Sublime, Emacs... no other editor has a bug like this!

For the time being, a workaround is to use the indent-one-space extension, but this should be fixed properly.

anchann commented 3 years ago

This issue also affects indentation of XML severely. For example, with indentation set at 4 spaces, starting with this text:

<Component
 class='something'
 SomeProp='blah'/>

increasing indentation by one step using the "Indent Line" command produces this:

    <Component
    class='something'
    SomeProp='blah'/>

Starting with this text

    <Component
     class='something'
     SomeProp='blah'/>

and decreasing indentation by one step using the "Outdent Line" command produces this:

<Component
    class='something'
    SomeProp='blah'/>

This style of indentation may feel strange to some people, but that's part of the whole issue — who are people, and who is VSCode, to tell others how to format their code? If I wanted my text editor to mangle my formatting, I would opt in to automated formatting.

Increasing and decreasing indentation should be a simple operation of adding or removing one fixed string (in my example, a string consisting of four spaces) to/from the beginning of each line. Yes, there are some important edge cases to take care of (e.g. what happens when one of the selected lines doesn't have enough whitespace to take away on an outdent; or e.g. ignoring the last empty line of a block of lines selection), but VSCode gets them right already (and most plugins that implement things naively do not). Why confound the simple, deterministic operation of changing the indentation level with something as complex (and often buggy, which is why people turn it off) automatic formatting?

If changing the default behaviour of these actions is somehow controversial, can you at least provide "Indent Line Trivially" and "Outdent Line Trivially" pair of actions that people who don't want their code mangled can use?

heartacker commented 3 years ago

hi All, I found that this is caused by the block comments starter word,

just now we can use /** instead of /* or /*! to work around,(but contradicting my coding rule)

https://github.com/microsoft/vscode/issues/135819#issuecomment-954671221

and anybody could identify is there anything related to : https://github.com/microsoft/vscode/blob/ff1e16eebb93af79fd6d7af1356c4003a120c563/extensions/cpp/language-configuration.json#L1-L5

heartacker commented 1 year ago

👯

chuckeng commented 1 year ago

The work around does not appear to work everywhere:

image

I think it works for method/function comments, but not comments inside a functions (and who knows where-else).

Also, we have too many lines of code already in that format. So, as a work-around it would allow me to add the *, then adjust indentation, then remove the *. But really that just saves a small bit of time. Usually we work on large sections of code, and we might miss doing this. Once its checked in, good luck changing it without a bug that requires the code to be updated again. :/

aiday-mar commented 7 months ago

Hi I am trying to reproduce this issue and am currently not able to on the latest insiders. I have no extensions installed and I see the following. I assume the issue has been fixed, and therefore will close this issue. If you encounter the issues in this thread again, please leave a comment here so I can reopen the issue.

I am using editor.autoIndent : full

https://github.com/microsoft/vscode/assets/61460952/ef39af38-d53e-4b6b-a894-4e5a0bac8776

hostilefork commented 7 months ago

Hi I am trying to reproduce this issue and am currently not able to on the latest insiders

Perhaps you misunderstood the issue...

You are showing a comment like this being indented and unindented, where the leading slash and asterisks are at column 5:

/*
* Comment
*/

That is not the situation described.

The problem occurs when the leading slash is at column 5, and the subsequent asterisks are at column 6:

/*
 * Comment
 */

The issue is that indentation of code with this arrangement to the right should put the leading slash at column 9, and the subsequent asterisks at column 10. It does not, instead putting the leading slash and asterisks all at position 9, breaking the alignment of the asterisks--which are no longer all in the same column.

As far as I can tell, this problem still exists.


Note this is a specific instance of a more general problem of relative-indentation preservation. It's not specific to comments, but also applies to things like case labels, where I use two-space indents instead of 4:

switch (something) {
  case 1:
    whatever();
    break;
  case 2:
    something_else();
    break;
}

I do not want indenting and unindenting that to lose the alignments of the case labels, but it does. e.g. if I indent I get:

    switch (something) {
    case 1:
        whatever();
        break;
    case 2:
        something_else();
        break;
    }
heartacker commented 7 months ago

@aiday-mar could you understand me image

aiday-mar commented 7 months ago

Aha I see thanks for the clarification. I reopened this issue.

aiday-mar commented 7 months ago

Hi @hostilefork I am investigating the second example in your last comment, and I noticed the following. The issue you mentioned appears to happen specifically in the case when you use indentation with 2 spaces in your code but the document has detected a general indentation with 4 spaces. If you run the command Detect Indentation from Content beforehand, the document general indentation level will be 2 and upon indentation, the correct alignment will be preserved.

I suppose in your case you would like the indentation level of 4 to be applied on the full document even if locally you were using an indentation level of 2, and for this to preserve the alignment.

https://github.com/microsoft/vscode/assets/61460952/12731eb4-a1c1-4d34-9650-4a9d5b10116b

hostilefork commented 7 months ago

I suppose in your case you would like the indentation level of 4 to be applied on the full document even if locally you were using an indentation level of 2, and for this to preserve the alignment.

Yes, that's correct--generally I want hitting the tab key to do 4 spaces.

If I want two spaces, then hitting the space bar twice is all right.

plynch-chwy commented 6 months ago

Any update on this? This is a bit of a pain point switching between editors every now and then. Vscode is acting out of the ordinary here in comparison.

billsacks commented 2 months ago

I came across this issue when struggling with something that I think is similar to https://github.com/microsoft/vscode/issues/63388#issuecomment-2025072130 and the initial comment in https://github.com/microsoft/vscode/issues/124943. I thought I'd add a slightly different example / use case to make sure this will be covered with any fixes:

I work a lot in C / C++ / Fortran code that uses different indentation amounts in different constructs. For example, the body of a function / subroutine might be indented by 2 spaces for the top-level indentation, but other blocks (bodies of conditionals, loops, etc.) are indented by 4 spaces. I'd like to have indentation set at 4 spaces so it works right in most places, and then have VSCode always indent by an additional 4 spaces relative to the previous line, no matter what column the previous line was on (ideally, this would happen both with the automatic indentation when hitting enter after starting a block, and with use of the tab key). Instead, VSCode seems to always want to indent to a "tab stop" - i.e., a multiple of 4 spaces in an absolute sense.

Here is a screen shot showing a simple example:

https://github.com/user-attachments/assets/3cf6df92-51ac-40c7-946e-fc7034c3215d

Note that indentation is set to 4 spaces here. What I want is for the x = 5; line to be indented to column 7; instead, VSCode indents it to column 5 (presumably because this is the first "tab stop" that is more indented than the line above it). In this example, the indentation ends up being fixed when typing the semicolon if I turn on "Format on type", but ideally I'd like for this to work correctly without needing to rely on a formatter.

i.e., I want indentation to work relative to the previous line, not based on tab stops that essentially assume that the previous line was itself indented to a tab stop.

Edit 2024-09-11: On further reflection, I'm not sure if this issue is an appropriate place for this comment. I have opened a new issue that duplicates and expands upon this comment: https://github.com/microsoft/vscode/issues/228286.