recompute character to column when comparing indentations (#12375)

recompute character to column when comparing indentations
This commit is contained in:
Vladimir Matveev 2016-11-21 11:34:09 -08:00 committed by GitHub
parent 409253145e
commit b060107b51
3 changed files with 46 additions and 1 deletions

View file

@ -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);
}

View 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
];`);

View 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
];`);