Vscode: Incorrect indentation for single line if/for/while/etc, multiline chaining statements etc

Created on 8 Feb 2018  路  35Comments  路  Source: microsoft/vscode

I am experiencing some weird indentation issues. Not using any plugins to prettify or similar.

if (condition) {
    return; // <-- correct indentation
}
if(condition)
return; // <-- incorrect indentation



md5-8e9274922351b7cd89ca8be61f4d254b



var transformedValues =
originalValues <-- incorrect should be indented one level.
.where(condition) <-- incorrect, should be indented two levels.
.select(transform); <-- incorrect, should be indented two levels. 

function someFunction() {
    callToSomeOtherFunction(
         variableWithLongNameWhichRequiresASeparateLine,
         anotherVariableWithLongNameWhichRequiresASeparateLine);
    }  <-- incorrect should not be indented.
    function ... <-- incorrect the rest of the file is indented. 
editor-autoindent feature-request javascript

Most helpful comment

yet another trivial error:

return true ? { } : { };

i don't understand why this is labeled feature-request and not bug.

All 35 comments

Thx for picking this up! Btw, it applies to typescript as well.

class Foo {
    public bar(x: number): void {
        if(x == 0)
        return; // <-- incorrect
    }
}

also related, after typing, the code looks like this:

if (condition)
return;

but after 'Format Document' the indentation is correct:

if (condition)
  return;

but if you put the cursor after the ; and hit ENTER, the cursor is incorrectly indented:

if (condition)
  return;
  |  <-- cursor is here, should be in column zero.

image

This also applies C/C++/D, as well. Probably anything derived from C syntax.

Also applies to PHP, very very frustrating :( Anybody figured out a solution or an extension that fixes this ?

None as far as I know, the fact that this has not yet been addressed is kind of weird. Seems to me like a very fundamental part of a decent code editor.

I'm starting to wonder if this is actually a case of opinion where the responsible developers simply don't agree with this way of avoiding { } and therefore just happens to forget about it? Microsoft, pls prove me wrong on this!

There is even bug with this code:

if (condition) { some code in 1 line }
    next command wrongly intended

when is condition in comment, then it is even not working )

// if (condition) {

How is this taking so long to fix? I wanna use VSCode in place of Sublime, but c'mon :/

@rebornix can you explain why you do not consider this a bug?

here's another weirdness.

1) new file
2) set filetype to 'JS'
3) type if(true)<ENTER>

after hitting <ENTER> the cursor drops to line 2, column 1. this is clearly wrong. the cursor at this point should be indented, and only un-indented if the next keystroke is a {.

image

however...

1) new file
2) hit <ENTER> several times
3) go to line 1
4) type if(true)<ENTER>

now the cursor ends up indented correctly:

image

So I opened #62198 for a related issue but it was closed as duplicate of this one (I'm not convinced this is the exact same issue though), so I'll repeat the specifics here:

Create a javascript file containing an if statement without brackets, one line executed if the if statement is valid, then 2 unrelated lines next

var i = 1;
if (i === 1)
    console.log('log from if');

console.log('line one');
console.log('line two');
  1. invert the last 2 lines with alt+up or alt+down
  2. lines are indented under the if statement

indentation-issue-when-moving-lines

And now for the really fun part: on Mac, the same issue exists in 1.28.2, BUT works correctly in 1.29.0-insider:
oct-30-2018 16-24-12

We found the issue while trying to fix a similar problem in vscode-java, after copying the indentation rules from the js/ts server.

It seems that when I turn on the 'editor.formatOnType' setting, this issue will go away. Maybe correct indentation is implemented as a code formatting feature, and relies on specific language support.

@zhouhaoyu it still has weird behavior, like:

function Test(x, y)
{
    if (x)
        return y;

    if (y)
    {

        }
}

The setting also makes no difference in other languages, like C++.

@hleVqq I guess there's a conflict between the auto indent feature and language-specific formatting then.
Hope this could be fixed soon...

Why this is labelled as feature request and bug label was removed. It is bug at the first glance

Why this is labelled as feature request and bug label was removed. It is bug at the first glance

@stanislavdavid, I agree that, at least from the outside, this looks like a bug and not a feature request. There's an existing auto-indent feature and it doesn't work correctly, at least in some circumstances. That, to me, is the definition of a bug. I have some speculative reasons why this might be considered a feature request rather than a bug, but it would be really nice to hear from one of the folks to whom this is assigned.

It's been almost a year, now, since this issue was opened with no apparent activity outside of others commenting on new cases where it doesn't work correctly. This is an important enough issue to me that frankly, I'm just about to the point of giving up on VSCode and going back to one of the other editors that auto-indents correctly. That would be a shame, because there's a lot I like about VSCode, but this is really frustrating.

The behavior I described in https://github.com/Microsoft/vscode/issues/43244#issuecomment-436351839 is still broken in VS Code 1.33.1 (and 1.34.0-exploration) but works fine in 1.34.0-insider O_o. So there's a proper behavior that's been there in the insider builds for months but never made it to the actual release

Are assignees ever going to take ownership of this issue or will VS Code remain a broken editor forever?

I'm still looking for a fix too. This is really annoying and even basic editors do it right. If anyone finds an extension to fix this, please put it here too :/

Any available fix now? I'm experiencing the same and this is really annoying.

From the looks of it, this is never gonna be fixed. Though I can't comprehend _why_.

Yes, this is sad! Like I expressed in the beginning this is likely to be a case of opinion among a select few responsible for deciding what to do with this. And this is simply their way of enforcing that. But then again, shortly WASM will dominate the Web and we can all stop writing JS and go back to actually useful IDEs like #realVisualStudio.

Yes, this is sad! Like I expressed in the beginning this is likely to be a case of opinion among a select few responsible for deciding what to do with this. And this is simply their way of enforcing that. But then again, shortly WASM will dominate the Web and we can all stop writing JS and go back to actually useful IDEs like #realVisualStudio.

No this is not only the problem of JS... It affects all languages with this feature like C/C++ ( if I'm not misunderstanding you )

You are correct that it is an issue for most, or perhaps all, languages in VSCode. My point being that there are imo better choices of IDEs for most other languages than JS/TS, and hopefully we will soon not have to use JS/TS as extensively as we do today due to the rise of WASM.

Just started using VS Code and I cannot believe this bug has existed for so long. This is language syntax 101... regardless of your opinion on optional braces they are optional in many languages so it's a valid way to write code. Does nobody on the dev team ever run into this issue, even occasionally?

another weird javascript indentation bug:

````javascript

a(
0,
1, {
a: "b",
}
)

````

regardless of your opinion on optional braces they are optional in many languages so it's a valid way to write code

I completely agree with this. Some projects, like linux kernel even enforce this coding style on single line if, for, etc.... Hope to see a fix on this since it is rather annoying one...

lets wrote them this bug to survey in vscode which appeared to me today :-D, maybe it helps somewhere

I use VS Code at work and Visual Studio 2019 at home, recently I was thinking to moving from Visual Studio to VS Code, I like how lightweight and simple it is. However this one little thing annoys me so much that everytime I experiecing it I lose all desire to do so.

yet another trivial error:

return true ? { } : { };

i don't understand why this is labeled feature-request and not bug.

@rebornix @mjbvz take a look please

I've seen developers argue that this is not a bug, and others argue that you should use braces, and never single line indented statements (which is all wrong or absurdly opinionated).

The bizarre part is that every single other editor with auto indentation seems to do it right, including all versions of full Visual Studio, since beginning of time. I don't get it... why can't you just do what VS (full) does?

Yes, I can use another editor, and no, my problem isn't lack of effort by the devs for a free product, it's the mind blowing attitude that this is not a problem or not one worth fixing for like 5 years now.

I'm used to VS Code and otherwise love it, but it has now become second nature to fix indentation after all single-line if/else/while/for/etc. -- why is this so hard to finally fix??

It鈥檚 lazy program-management. It should be labeled as, and treated as a bug.

Could anyone please try to do some progress here? Since 2018 and it is such a simple detail, shouldn't be so difficult to solve, even less in an editor supported by Microsoft.

Isn't there some extension or some other setting which could solve this issue?

Was this page helpful?
0 / 5 - 0 ratings