VS2015 Preview: TypeScript keeps inserting 4-space tab
Tab option:

TypeScript behavior:

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 I've decided to keep 4-space indentation until this issue is fixed and you are seeing the result :D
@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.
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)$
});
}

How work formatting on my machine

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
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 nope :( Clean install give the same result. Formatting not working. Магия прям...
Win 10 VS2015 Preview TS 1.4
@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.
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
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)?
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.
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?
Yep. And in VS2013 it's work. Correct applying indentation settings.
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)toLanguageServiceShimObject.prototype.getFormattingEditsAfterKeystrokeso 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 itsMessageproperty - it should contain JSON with option values. Please paste these values here.
@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 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 :(
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
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
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?
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
}