From 6f179bf48e3d2a93e09a30f28c9ed1ddeec06fba Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 18 Nov 2016 22:13:08 -0800 Subject: [PATCH 1/2] recompute character to column when comparing indentations --- src/services/formatting/formatting.ts | 10 +++++++++- tests/cases/fourslash/formatWithTabs.ts | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/formatWithTabs.ts diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 7f594ad82b1fd..712ab7bec20e1 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -886,12 +886,20 @@ 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++) { + column += sourceFile.text.charCodeAt(startLinePosition + i) === CharacterCodes.tab ? options.tabSize : 1; + } + 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 From 5c5ee58d00ba3ce16914571bd69732367eca67d0 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 18 Nov 2016 23:21:35 -0800 Subject: [PATCH 2/2] update column computation math --- src/services/formatting/formatting.ts | 7 ++++++- tests/cases/fourslash/formatWithTabs2.ts | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/formatWithTabs2.ts diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 712ab7bec20e1..bcd8cebb017e7 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -895,7 +895,12 @@ namespace ts.formatting { function characterToColumn(startLinePosition: number, characterInLine: number): number { let column = 0; for (let i = 0; i < characterInLine; i++) { - column += sourceFile.text.charCodeAt(startLinePosition + i) === CharacterCodes.tab ? options.tabSize : 1; + if (sourceFile.text.charCodeAt(startLinePosition + i) === CharacterCodes.tab) { + column += options.tabSize - column % options.tabSize; + } + else { + column++; + } } return column; } 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