Skip to content

Commit b060107

Browse files
authored
recompute character to column when comparing indentations (#12375)
recompute character to column when comparing indentations
1 parent 4092531 commit b060107

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/services/formatting/formatting.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -886,12 +886,25 @@ namespace ts.formatting {
886886
else {
887887
const tokenStart = sourceFile.getLineAndCharacterOfPosition(pos);
888888
const startLinePosition = getStartPositionOfLine(tokenStart.line, sourceFile);
889-
if (indentation !== tokenStart.character || indentationIsDifferent(indentationString, startLinePosition)) {
889+
if (indentation !== characterToColumn(startLinePosition, tokenStart.character) || indentationIsDifferent(indentationString, startLinePosition)) {
890890
recordReplace(startLinePosition, tokenStart.character, indentationString);
891891
}
892892
}
893893
}
894894

895+
function characterToColumn(startLinePosition: number, characterInLine: number): number {
896+
let column = 0;
897+
for (let i = 0; i < characterInLine; i++) {
898+
if (sourceFile.text.charCodeAt(startLinePosition + i) === CharacterCodes.tab) {
899+
column += options.tabSize - column % options.tabSize;
900+
}
901+
else {
902+
column++;
903+
}
904+
}
905+
return column;
906+
}
907+
895908
function indentationIsDifferent(indentationString: string, startLinePosition: number): boolean {
896909
return indentationString !== sourceFile.text.substr(startLinePosition, indentationString.length);
897910
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////const foo = [
4+
//// 1
5+
////];
6+
7+
const options = format.copyFormatOptions();
8+
options.IndentSize = 2;
9+
options.TabSize = 2;
10+
options.ConvertTabsToSpaces = false;
11+
format.setFormatOptions(options);
12+
format.document();
13+
verify.currentFileContentIs(
14+
`const foo = [
15+
1
16+
];`);
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////const foo = [
4+
//// 1
5+
////];
6+
7+
const options = format.copyFormatOptions();
8+
options.IndentSize = 2;
9+
options.TabSize = 2;
10+
options.ConvertTabsToSpaces = false;
11+
format.setFormatOptions(options);
12+
format.document();
13+
verify.currentFileContentIs(
14+
`const foo = [
15+
1
16+
];`);

0 commit comments

Comments
 (0)