recompute character to column when comparing indentations (#12375)
recompute character to column when comparing indentations
This commit is contained in:
parent
409253145e
commit
b060107b51
|
@ -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);
|
||||
}
|
||||
|
|
16
tests/cases/fourslash/formatWithTabs.ts
Normal file
16
tests/cases/fourslash/formatWithTabs.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
/// <reference path="fourslash.ts"/>
|
||||
|
||||
////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
|
||||
];`);
|
16
tests/cases/fourslash/formatWithTabs2.ts
Normal file
16
tests/cases/fourslash/formatWithTabs2.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
/// <reference path="fourslash.ts"/>
|
||||
|
||||
////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
|
||||
];`);
|
Loading…
Reference in a new issue