Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 9 additions & 1 deletion src/services/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -886,12 +886,20 @@ 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++) {
column += sourceFile.text.charCodeAt(startLinePosition + i) === CharacterCodes.tab ? options.tabSize : 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem quite right. If i have <space><tab> then this math tells me i'm at (tabSize + 1) when i should be at tabSize.

Copy link
Contributor

Choose a reason for hiding this comment

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

Roslyn's algorithm:

public static int ConvertTabToSpace(this string textSnippet, int tabSize, int initialColumn, int endPosition)
        {
            Contract.Requires(tabSize > 0);
            Contract.Requires(endPosition >= 0 && endPosition <= textSnippet.Length);

            int column = initialColumn;

            // now this will calculate indentation regardless of actual content on the buffer except TAB
            for (int i = 0; i < endPosition; i++)
            {
                if (textSnippet[i] == '\t')
                {
                    column += tabSize - column % tabSize;
                }
                else
                {
                    column++;
                }
            }

            return column - initialColumn;
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fair enough, thanks for catching this

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