Repository navigation
formatOnType does not respect current line indendtiation #6700
Description
Activity
Isidor Nikolic (@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.
From Isidor Nikolic (@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 }Same happens for TS.
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.
Sounds like this is a general issue with TypeScript also, not a Salsa issue.
- changed the title
[-]Salsa: formatOnType does not respect current line indendtiation[/-][+]formatOnType does not respect current line indendtiation[/+]on Jan 28, 2016 - addedDomain: FormatterThe issue relates to the built-in formatterThe issue relates to the built-in formatter
on Mar 1, 2016 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() { };
27 remaining items
I talked to alexandrudima and the removal of the on line 2 is a feature that is controlled via the setting "editor.trimAutoWhitespace". The feature takes out whitespaces the editor inserted automatically (which is actually cool). Also the sequence of the events seems correct since we ask the TS server to format on type on the state of the document like presented in the editor.
I disabled the setting "editor.trimAutoWhitespace":false and intestingly the formatting is still incorrect. The trace looks like this in the same example in a TS file:
Sending request: change (47). Response expected: no. Current queue length: 0 Arguments: { "file": "p:/mseng/VSCode/Playgrounds/bugs/TS6700/test.ts", "line": 2, "offset": 2, "endLine": 2, "endOffset": 2, "insertString": "\r\n\t" } Sending request: formatonkey (48). Response expected: yes. Current queue length: 0 Arguments: { "file": "p:/mseng/VSCode/Playgrounds/bugs/TS6700/test.ts", "line": 3, "offset": 2, "key": "\n" } Response received: formatonkey (48). Request took 2 ms. Success: true Result: [ { "start": { "line": 2, "offset": 1 }, "end": { "line": 2, "offset": 2 }, "newText": "" }, { "start": { "line": 3, "offset": 1 }, "end": { "line": 3, "offset": 2 }, "newText": "" } ] Sending request: change (49). Response expected: no. Current queue length: 0 Arguments: { "file": "p:/mseng/VSCode/Playgrounds/bugs/TS6700/test.ts", "line": 3, "offset": 1, "endLine": 3, "endOffset": 2, "insertString": "" } Sending request: change (50). Response expected: no. Current queue length: 0 Arguments: { "file": "p:/mseng/VSCode/Playgrounds/bugs/TS6700/test.ts", "line": 2, "offset": 1, "endLine": 2, "endOffset": 2, "insertString": "" }
In this case the TS server sends two edits back to delete the tab character on line 2 and one to delete it on line 3.
Since the tsserver has an AST and therefore knows that the format happens inside a body of a function it should IMO propose an indentation one greater then the one of the function keyword.
Zhengbo Li (@zhengbli) Mohamed Hegazy (@mhegazy) can you please comment on how to proceed here.
I agree the master version does still do the wrong thing for the issue, though I was testing a fix. After I disabled the
trimAutoWhitespace, now it works as expected using my fix.I think what is happening is:
- The user hits enter, a new line is inserted with spaces added by VSCode
- VSCode sends the
formatOnTypecommand to tsserver. - Tsserver found that the current line is nothing but space. Then it doesn't touch the current line to avoid messing the current indentation, and move the format range to the previous lines, until it found a line with non-space character.
- Tsserver sends back edits. The VSCode found the current line is not touched, then it decides to trim auto spaces
- The indentation was removed.
The fix is currently at https://github.com/zhengbli/TypeScript/tree/formatOnTypeCurrentLine
Though I am finalizing it soon.Zhengbo Li (@zhengbli) can you attach a tsserver trace for this. According to alexandrudima VS Code doesn't remove any whitespaces on lines the cursor is on. Neither for remove trailing whitespace on save nor for the trim auto whitespace. So in step 4 in our example above VS Code should not remove and any indentation since that line contains the cursor.
And could you clarify what:
and move the format range to the previous lines, until it found a line with non-space character.
means.
The screen recording of the sessions with auto trim spaces enabled / disabled:

The trace log with auto trim enabled:
trace_trim.txtThe trace log with auto trim disabled:
trace_no_trim.txtThe diffing shows that in the "no_trim" case, VSCode sends an additional
changewhich deletes the indentation.and move the format range to the previous lines, until it found a line with non-space character.
To the server essentially
formatOnTypeis the same feature as generally formatting the entire document, only that for it the formatting span is only the two lines nearby. To determine where the formatting span ends, we search from the cursor position backwards, and skip all space characters. Therefore when the current line content is____|(_indicates space,|indicates the cursor), the current line will be skipped, meaning the formatting span ends at the end of last line, so the current line will not be touched.Zhengbo Li (@zhengbli) thanks for the case. I opened microsoft/vscode#6862 since this clearly looks like an editor problem.
However there is still the problem with the two new lines where the traces shows that the tsserver removes the indentation. This is a very annoying bug in VS Code and basically forces us to ask users to disable formatting on type. Are there any plans to have this fixed in 1.8.x ?
Dirk Bäumer (@dbaeumer) yes that is a problem. My new fix should take care of that, however I don't think we are going to port that to 1.8. Mohamed Hegazy (@mhegazy) may have comments on the matter.
Zhengbo Li (@zhengbli) Since this was a major issue for us we added some workaround code in the TS extension. Please ping me when this is fixed in 2.0 so that I can remove the workarounds.
Sure, thank you! The fix is now merged in master, and will ship with 2.0
- addedFixedA PR has been merged for this issueA PR has been merged for this issueand removedNeeds More InfoThe issue still hasn't been fully clarifiedThe issue still hasn't been fully clarified
on Jun 2, 2016 - locked and limited conversation to collaborators
on Jun 19, 2018
From Isidor Nikolic (@isidorn) on January 26, 2016 15:50
Copied from original issue: microsoft/vscode#2377