diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 7f594ad82b1fd..bcd8cebb017e7 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -886,12 +886,25 @@ namespace ts.formatting { else { const tokenStart = sourceFile.getLineAndCharacterOfPosition(pos); const startLinePosition = getStartPositionOfLine(tokenStart.line, sourceFile); - if (indentation !== tokenStart.character || indentationIsDifferent(indentationString, startLinePosition)) { + if (indentation !== characterToColumn(startLinePosition, tokenStart.character) || indentationIsDifferent(indentationString, startLinePosition)) { recordReplace(startLinePosition, tokenStart.character, indentationString); } } } + function characterToColumn(startLinePosition: number, characterInLine: number): number { + let column = 0; + for (let i = 0; i < characterInLine; i++) { + if (sourceFile.text.charCodeAt(startLinePosition + i) === CharacterCodes.tab) { + column += options.tabSize - column % options.tabSize; + } + else { + column++; + } + } + return column; + } + function indentationIsDifferent(indentationString: string, startLinePosition: number): boolean { return indentationString !== sourceFile.text.substr(startLinePosition, indentationString.length); } diff --git a/tests/cases/fourslash/formatWithTabs.ts b/tests/cases/fourslash/formatWithTabs.ts new file mode 100644 index 0000000000000..c33808bd26acf --- /dev/null +++ b/tests/cases/fourslash/formatWithTabs.ts @@ -0,0 +1,16 @@ +/// + +////const foo = [ +//// 1 +////]; + +const options = format.copyFormatOptions(); +options.IndentSize = 2; +options.TabSize = 2; +options.ConvertTabsToSpaces = false; +format.setFormatOptions(options); +format.document(); +verify.currentFileContentIs( +`const foo = [ + 1 +];`); \ No newline at end of file diff --git a/tests/cases/fourslash/formatWithTabs2.ts b/tests/cases/fourslash/formatWithTabs2.ts new file mode 100644 index 0000000000000..cda82b9cf80cb --- /dev/null +++ b/tests/cases/fourslash/formatWithTabs2.ts @@ -0,0 +1,16 @@ +/// + +////const foo = [ +//// 1 +////]; + +const options = format.copyFormatOptions(); +options.IndentSize = 2; +options.TabSize = 2; +options.ConvertTabsToSpaces = false; +format.setFormatOptions(options); +format.document(); +verify.currentFileContentIs( +`const foo = [ + 1 +];`); \ No newline at end of file