Skip to content

Conversation

@vladima
Copy link
Contributor

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:

publicstaticintConvertTabToSpace(thisstringtextSnippet,inttabSize,intinitialColumn,intendPosition){Contract.Requires(tabSize>0);Contract.Requires(endPosition>=0&&endPosition<=textSnippet.Length);intcolumn=initialColumn;// now this will calculate indentation regardless of actual content on the buffer except TABfor(inti=0;i<endPosition;i++){if(textSnippet[i]=='\t'){column+=tabSize-column%tabSize;}else{column++;}}returncolumn-initialColumn;}

Copy link
ContributorAuthor

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
ContributorAuthor

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

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

@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
ContributorAuthor

already done

vladima added a commit that referenced this pull request Nov 21, 2016
…12418) recompute character to column when comparing indentations
@microsoftmicrosoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for freeto 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

5 participants

@vladima@mhegazy@CyrusNajmabadi@msftclas