TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

formatOnType does not respect current line indendtiation

Open dbaeumer opened this issue 10 years ago • 34 comments

From @isidorn on January 26, 2016 15:50

  1. js workspace, have salsa enabled
  2. "editor.formatOnType": true
  3. The format on type is too aggrressive and does not respect current indentiation. This is especially noticable when trying to create object literals
  4. if salse is disabled this works nicely

Copied from original issue: Microsoft/vscode#2377

dbaeumer avatar Jan 28 '16 19:01 dbaeumer

@isidorn I can't see any weirdness. Can you please provide some steps. I tested ts and js and they behave the same. Both honoring indent setting.

Please be aware that if a indent of for example 4 spaces exist and you have insert tab and tab size > 4 then format on type from TS will stil insert 4 spaces to align the code correctly.

dbaeumer avatar Jan 28 '16 19:01 dbaeumer

From @isidorn on January 28, 2016 15:23

var t = {
  name: isidor,
// this line gets indented all the way to the start once I press enter after name property
}

dbaeumer avatar Jan 28 '16 19:01 dbaeumer

Same happens for TS.

dbaeumer avatar Jan 28 '16 19:01 dbaeumer

I debugged it and this is a tsserver behavior. When pressing enter after Isidor, VSCode inserts the right indention but the tsserver returns and edit that deletes that indention. IMO the tsserver should be smart and if Isidor is ended with , adjust the indention since there is a high likelihood that the user inserts another property.

dbaeumer avatar Jan 28 '16 19:01 dbaeumer

Sounds like this is a general issue with TypeScript also, not a Salsa issue.

billti avatar Jan 28 '16 19:01 billti

Does this also remove the space after function keyword like below? Our lint rules require space after function. But after latest vs code 0.10.10 it removes spaces on reformatting ...

var test = function() { };

mlakmal avatar Mar 09 '16 12:03 mlakmal

@mlakmal This issue is referring to the "format on enter" feature; the issue you are referring to is different. Though it may worth opening another issue for tracking.

zhengbli avatar Mar 09 '16 17:03 zhengbli

Another related issue, when adding comments as below example: screen

Prior to 0.10.10 a space is added after the * in next new line when pressing Enter. In 0.10.10, the space is removed, CMD-Z will bring it back.

johnwu5 avatar Mar 10 '16 00:03 johnwu5

@egamma is the issue 3796 in vs code related to this issue...

mlakmal avatar Mar 10 '16 00:03 mlakmal

The formatter never removed spaces from comments. i think this is the editor doing this. @egamma any thoughts?

mhegazy avatar Mar 10 '16 18:03 mhegazy

@mhegazy Is there any progress on this? We have multiple customers complaining about format on typing being usable.

jrieken avatar Apr 25 '16 07:04 jrieken

@jrieken do you have more information about this issue? are there other cases? i can not reproduce the original issue.

mhegazy avatar Apr 25 '16 18:04 mhegazy

See https://github.com/Microsoft/vscode/issues/3797 and https://github.com/Microsoft/vscode/issues/5656#issuecomment-214190434

4kochi avatar Apr 25 '16 20:04 4kochi

This issue was already fixed by #7307, for some reason I forgot to close this issue. The fix is in the nightly versions now, and will release with 2.0.

zhengbli avatar Apr 25 '16 20:04 zhengbli

@zhengbli Can you please comment on https://github.com/Microsoft/vscode/issues/3797#issuecomment-215330505

jrieken avatar Apr 28 '16 07:04 jrieken

does not look like it is fixed.

mhegazy avatar Apr 28 '16 17:04 mhegazy

from https://github.com/Microsoft/vscode/issues/3797#issuecomment-215330505 image

mhegazy avatar Apr 28 '16 17:04 mhegazy

@dbaeumer @jrieken

I am testing a fix, however I found that it may have something to do with VSCode's own formatting behavior.

For example, when I have the formatOnType set to true, I observed this: gif

While the VSCode log said:

Sending request: change (76). Response expected: no. Current queue length: 0
Arguments: {
    "file": "c:/Users/lizhe/Documents/playground/testLocalFormatting/test.ts",
    "line": 1,
    "offset": 35,
    "endLine": 1,
    "endOffset": 35,
    "insertString": "\r\n    "
}

Sending request: formatonkey (77). Response expected: yes. Current queue length: 0
Arguments: {
    "file": "c:/Users/lizhe/Documents/playground/testLocalFormatting/test.ts",
    "line": 2,
    "offset": 5,
    "key": "\n"
}

Response received: formatonkey (77). Request took 3 ms. Success: true 
Result: [
    {
        "start": {
            "line": 1,
            "offset": 16
        },
        "end": {
            "line": 1,
            "offset": 17
        },
        "newText": ""
    },
    {
        "start": {
            "line": 1,
            "offset": 31
        },
        "end": {
            "line": 1,
            "offset": 32
        },
        "newText": ""
    }
]

Sending request: change (78). Response expected: no. Current queue length: 0
Arguments: {
    "file": "c:/Users/lizhe/Documents/playground/testLocalFormatting/test.ts",
    "line": 2,
    "offset": 1,
    "endLine": 2,
    "endOffset": 5,
    "insertString": ""
}

Sending request: change (79). Response expected: no. Current queue length: 0
Arguments: {
    "file": "c:/Users/lizhe/Documents/playground/testLocalFormatting/test.ts",
    "line": 1,
    "offset": 31,
    "endLine": 1,
    "endOffset": 32,
    "insertString": ""
}

Sending request: change (80). Response expected: no. Current queue length: 0
Arguments: {
    "file": "c:/Users/lizhe/Documents/playground/testLocalFormatting/test.ts",
    "line": 1,
    "offset": 16,
    "endLine": 1,
    "endOffset": 17,
    "insertString": ""
}

It looks like the server is returning a correct change, while VSCode decides to delete the spaces on the second line. Is there any way to turn off VSCode's formatting for testing purpose?

zhengbli avatar May 05 '16 18:05 zhengbli

Indeed the TypeScript language service getIndentationAtPosition on that line gives the right result as I use it in alm:

alm

But as you can tell from the comments I struggled to get it just right originally because of my misconfigurings and stuff :) :rose:

basarat avatar May 06 '16 00:05 basarat

@alexandrudima Can you comment?

jrieken avatar May 06 '16 07:05 jrieken