TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

VS2015 Preview: TypeScript keeps inserting 4-space tab

Open saschanaz opened this issue 12 years ago • 30 comments

Tab option: tab2

TypeScript behavior: tab4

saschanaz avatar Nov 26 '14 08:11 saschanaz

I cannot reproduce this bug on our recent bits. Given that we've recently fixed a lot of issues in formatting and some of them were related to incorrect handling of if statements in else clauses I'd say that this bug should also be fixed. I'll add a regression test to confirm it and capture current behavior. BTW: Indent size in your settings is 2 but it looks like all code in repro has indentation 4. Is it intentional\expected?

vladima avatar Nov 26 '14 19:11 vladima

@vladima I've decided to keep 4-space indentation until this issue is fixed and you are seeing the result :D

saschanaz avatar Nov 27 '14 04:11 saschanaz

@vladima I can reproduce this bug on build from 05fd2e9fb3db8c3af0248a9d16ca198944c91a7e commit. VS 2015 Preview. Indentation rules do not applying that is VS Format Document command applying 4-space tabs indentation.

flaticols avatar Nov 27 '14 18:11 flaticols

Interesting and definitely disturbing. Can you please provide more details about the repro scenario? Everything seems to work as expected when I hit Enter at '$' position

function foo(a: boolean) {
  return new Promise<void>((resolve, reject) => {
    if (a)
      resolve();
    else
      reject(new Error());
    if (true)$

  });
}

norepro

vladima avatar Nov 27 '14 18:11 vladima

How work formatting on my machine

error_ts

I think TS formatter not inherit settings from VS in lot of situation (Ctrl + K, Ctrl + D). I'm try install VS on VM, and test on clean system and VS.

P.S. VS2013 work's great

flaticols avatar Nov 27 '14 19:11 flaticols

That is very odd - your example works like charm on my local box. Please let me know if this issue will still exist in the clean setup.

vladima avatar Nov 27 '14 19:11 vladima

@vladima nope :( Clean install give the same result. Formatting not working. Магия прям...

Win 10 VS2015 Preview TS 1.4

flaticols avatar Nov 27 '14 20:11 flaticols

@vladima I'll try to give more information

getIndentationString function in https://github.com/Microsoft/TypeScript/blob/master/src/services/formatting/indentation.ts#L42

"indentation" equal 4 and "options" parameter has this structure as value

{
  "InsertSpaceAfterCommaDelimiter": true,
  "InsertSpaceAfterSemicolonInForStatements": true,
  "InsertSpaceBeforeAndAfterBinaryOperators": true,
  "InsertSpaceAfterKeywordsInControlFlowStatements": true,
  "InsertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
  "InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
  "PlaceOpenBraceOnNewLineForFunctions": true,
  "PlaceOpenBraceOnNewLineForControlBlocks": true,
  "IndentSize": 4,
  "TabSize": 4,
  "NewLineCharacter": "\r\n",
  "ConvertTabsToSpaces": true
}

and "insertIndentation' function call getIndentationString with next prameters:

{
    "pos": 28,
    "indentation": 4
}

It was a little naive to believe that it is because of this in https://github.com/Microsoft/TypeScript/blob/44e6bcf7ffc00fd7da2ac9e5f28cfb051c19632c/src/harness/fourslash.ts#L363

this.formatCodeOptions = {
    IndentSize: 4,
    TabSize: 4,
    NewLineCharacter: sys.newLine,
    ConvertTabsToSpaces: true,
    InsertSpaceAfterCommaDelimiter: true,
    InsertSpaceAfterSemicolonInForStatements: true,
    InsertSpaceBeforeAndAfterBinaryOperators: true,
    InsertSpaceAfterKeywordsInControlFlowStatements: true,
    InsertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
    InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
    PlaceOpenBraceOnNewLineForFunctions: false,
    PlaceOpenBraceOnNewLineForControlBlocks: false,
};

Change IndentSize and TabSize influenced on open bracket in code.

I'm not really guided in TypeScript language service, but i think i do make a PR for fix this issue in next few days.

flaticols avatar Nov 27 '14 21:11 flaticols

Interesting. To summarize, in UI you've set both 'Tab Size' \ 'Indent Size' to 2 but for some reason their values that we observe on the script side are both 4. This is very odd. Can you try to set some new values for 'Tab Size' \ 'Indent Size' in UI then close and reopen VS and verify that UI still displays new values - just want to rule out some weird scenarios when user settings are not persisted properly

vladima avatar Nov 28 '14 03:11 vladima

out of curiosity: do you have any VS add-ins that might also be responsible for the code formatting (off the top of my head: ReSharper)?

vladima avatar Nov 28 '14 04:11 vladima

Tried set some values for Tab\Indent size in VS settings UI. Repoen VS - UI still displays new values. I have not installed any add-ins that might affected on formatting. Clean Windows and VS setup.

After changes values in UI, TS Language Service still set indentation to 4.

flaticols avatar Nov 28 '14 19:11 flaticols

Ok, and just to confim: you do see the same incorrect behavior on both clean install of VS2015 and when using the bleeding edge typescriptServices.js from master?

vladima avatar Nov 28 '14 22:11 vladima

Yep. And in VS2013 it's work. Correct applying indentation settings.

flaticols avatar Nov 29 '14 09:11 flaticols

I'd like to narrow down the crux of the problem. For now there are two possibilities:

  • for some reason VS passes incorrect formatting options
  • formatting options are correct but there is a bug in actual formatting logic.

For cut one of these possibilities it will be great to see what option values were actually passed from the managed side. Unfortunately for now release builds don't expose a lot of debugging features so we'll need to use an indirect way.

  • Modify typescriptServices.js and add throw new Error(options) to LanguageServiceShimObject.prototype.getFormattingEditsAfterKeystroke so ultimately source code of this function will look like:
        LanguageServiceShimObject.prototype.getFormattingEditsAfterKeystroke = function (fileName, position, key, options) {
            var _this = this;
            return this.forwardJSONCall("getFormattingEditsAfterKeystroke('" + fileName + "', " + position + ", '" + key + "')", function () {
                throw new Error(options);
                var localOptions = JSON.parse(options);
                var edits = _this.languageService.getFormattingEditsAfterKeystroke(fileName, position, key, localOptions);
                return edits;
            });
        };
  • Attach a managed debugger to the VS, open TypeScript file and type in a code that reproduces the problem. You should break into the debugger due to LanguageServiceScriptException - examine the exception and check its Message property - it should contain JSON with option values. Please paste these values here.

vladima avatar Dec 01 '14 23:12 vladima

@flaticols - I've tried to reproduce this issue on a several different environments - not luck. Can you try to obtain formatting options using the technique that was described in my previous post and paste them here so we can identify the root of the problem?

vladima avatar Dec 30 '14 21:12 vladima

@vladima I'm tried attach with WinDbg and VS Debugger to another process where opening TS project (and just file). But debugger not break on line where throwing error. What's I do wrong?

And this issue reproduced on any my machines. Work, home, friends. It's karma :(

flaticols avatar Jan 05 '15 15:01 flaticols

ok, lets try a different way to save settings

        LanguageServiceShimObject.prototype.getFormattingEditsAfterKeystroke = function (fileName, position, key, options) {
            var _this = this;
            return this.forwardJSONCall("getFormattingEditsAfterKeystroke('" + fileName + "', " + position + ", '" + key + "')", function () {
                var fso = new ActiveXObject("Scripting.FileSystemObject")
                var f = fso.CreateTextFile("<some-path-where-you-want-to-save-file>\\options.json", true);
                f.WriteLine(options);
                f.Close();
                var localOptions = JSON.parse(options);
                var edits = _this.languageService.getFormattingEditsAfterKeystroke(fileName, position, key, localOptions);
                return edits;
            });
        };

do this then paste the content of result json file here and check if content of json file matches your expectations with respect to setting values

vladima avatar Jan 05 '15 18:01 vladima

Result:

{
  "InsertSpaceAfterCommaDelimiter": true,
  "InsertSpaceAfterSemicolonInForStatements": true,
  "InsertSpaceBeforeAndAfterBinaryOperators": true,
  "InsertSpaceAfterKeywordsInControlFlowStatements": true,
  "InsertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
  "InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
  "PlaceOpenBraceOnNewLineForFunctions": false,
  "PlaceOpenBraceOnNewLineForControlBlocks": false,
  "IndentSize": 4,
  "TabSize": 4,
  "NewLineCharacter": "\r\n",
  "ConvertTabsToSpaces": true
}

For parameters IndentSize and TabSize in the VS settings is set to 2

flaticols avatar Jan 05 '15 21:01 flaticols

out of curiosity, if you try to change indentation settings for C# and type in some code will C# have new settings applied or it will fallback to defaults as well?

vladima avatar Jan 06 '15 01:01 vladima

It works perfectly on C#.

My result on TS:

{
    "InsertSpaceAfterCommaDelimiter": true,
    "InsertSpaceAfterSemicolonInForStatements": true,
    "InsertSpaceBeforeAndAfterBinaryOperators": true,
    "InsertSpaceAfterKeywordsInControlFlowStatements": true,
    "InsertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
    "InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
    "PlaceOpenBraceOnNewLineForFunctions": false,
    "PlaceOpenBraceOnNewLineForControlBlocks": false,
    "IndentSize": 4,
    "TabSize": 4,
    "NewLineCharacter": "\r\n",
    "ConvertTabsToSpaces": true
}

saschanaz avatar Jan 06 '15 05:01 saschanaz