Skip to content

recompute character to column when comparing indentations #12375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2016
Merged

Conversation

vladima
Copy link
Contributor

@vladima vladima commented Nov 19, 2016

we always compute indentation as column so in order to compare it with character position in line - character position must be converted to column as well.

fixes #12175

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

@vladima
Copy link
Contributor Author

vladima commented Nov 19, 2016

failure on the CI server is not related to this PR and caused by API changes in tslint - #12376 should address it

@vladima vladima merged commit b060107 into master Nov 21, 2016
@vladima vladima deleted the vladima/12175 branch November 21, 2016 19:34
@mhegazy
Copy link
Contributor

mhegazy commented Nov 21, 2016

@vladima can you port this to release-2.1

vladima added a commit that referenced this pull request Nov 21, 2016
recompute character to column when comparing indentations
@vladima
Copy link
Contributor Author

vladima commented Nov 21, 2016

already done

vladima added a commit that referenced this pull request Nov 21, 2016
…12418)

recompute character to column when comparing indentations
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Formatter is broken when using tabs
4 participants