Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/services/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/cases/fourslash/formatWithTabs.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions tests/cases/fourslash/formatWithTabs2.ts
Original file line number Diff line number Diff line change
@@ -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
];`);