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
10 changes: 7 additions & 3 deletions src/services/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,13 +866,17 @@ namespace ts.formatting {
}
else {
let tokenStart = sourceFile.getLineAndCharacterOfPosition(pos);
if (indentation !== tokenStart.character) {
let startLinePosition = getStartPositionOfLine(tokenStart.line, sourceFile);
let startLinePosition = getStartPositionOfLine(tokenStart.line, sourceFile);
if (indentation !== tokenStart.character || indentationIsDifferent(indentationString, startLinePosition)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just indentationString === soruceFile.text.substr(startLinePosition , indentationString.length);

recordReplace(startLinePosition, tokenStart.character, indentationString);
}
}
}

function indentationIsDifferent(indentationString: string, startLinePosition: number): boolean {
return indentationString !== sourceFile.text.substr(startLinePosition , indentationString.length);
}

function indentMultilineComment(commentRange: TextRange, indentation: number, firstLineIsIndented: boolean) {
// split comment in lines
let startLine = sourceFile.getLineAndCharacterOfPosition(commentRange.pos).line;
Expand Down Expand Up @@ -1152,4 +1156,4 @@ namespace ts.formatting {
return s;
}
}
}
}
35 changes: 35 additions & 0 deletions tests/cases/fourslash/formattingReplaceSpacesWithTabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// <reference path="fourslash.ts"/>

////module Foo {
/////*1*/class Test { }
/////*2*/ class Test { }
/////*3*/ class Test { }
/////*4*/ class Test { }
/////*5*/ class Test { }
/////*6*/ class Test { }
/////*7*/ class Test { }
////}

var options = format.copyFormatOptions();
options.ConvertTabsToSpaces = false;
var oldOptions = format.setFormatOptions(options);
try {
format.document();
goTo.marker("1");
verify.currentLineContentIs("\tclass Test { }")
goTo.marker("2");
verify.currentLineContentIs("\tclass Test { }")
goTo.marker("3");
verify.currentLineContentIs("\tclass Test { }")
goTo.marker("4");
verify.currentLineContentIs("\tclass Test { }")
goTo.marker("5");
verify.currentLineContentIs("\tclass Test { }")
goTo.marker("6");
verify.currentLineContentIs("\tclass Test { }")
goTo.marker("7");
verify.currentLineContentIs("\tclass Test { }")
}
finally {
format.setFormatOptions(oldOptions);
}
27 changes: 27 additions & 0 deletions tests/cases/fourslash/formattingReplaceTabsWithSpaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// <reference path="fourslash.ts"/>

////module Foo {
/////*1*/ class Test { }
/////*2*/ class Test { }
/////*3*/class Test { }
/////*4*/ class Test { }
/////*5*/ class Test { }
/////*6*/ class Test { }
/////*7*/ class Test { }
////}

format.document();
goTo.marker("1");
verify.currentLineContentIs(" class Test { }")
goTo.marker("2");
verify.currentLineContentIs(" class Test { }")
goTo.marker("3");
verify.currentLineContentIs(" class Test { }")
goTo.marker("4");
verify.currentLineContentIs(" class Test { }")
goTo.marker("5");
verify.currentLineContentIs(" class Test { }")
goTo.marker("6");
verify.currentLineContentIs(" class Test { }")
goTo.marker("7");
verify.currentLineContentIs(" class Test { }")