Skip to content

Incorrect LSP positions when line contains tabs #293

Description

@p-lindberg

Description:

The method DocumentUtils.offsetToLSPPos, which is used to translate document offsets into LSP positions, returns incorrect positions for lines that contain tabs. According to the specification:

A position inside a document (...) is expressed as a zero-based line and character offset.

The method instead tries to return a line and column number, which is not the same as a character offset. In doing so, it needs to compensate for tab widths:

int tabs = StringUtil.countChars(lineTextBeforeOffset, '\t');
int tabSize = getTabSize(editor);
int column = lineTextBeforeOffset.length() - tabs * (tabSize - 1);
return new Position(line, column);

This should just be

return new Position(line, lineTextBeforeOffset.length());

Additionally, the translation from offset to column is wrong in and of itself, since it subtracts the additional tab widths instead of adding them. This can lead to negative column numbers.

I'm confused as to how this issue has not been discovered or fixed yet since, by my estimation, it should wreak total havoc on all LSP functionality in files containing tabs, given that the positions come out wrong. This makes me suspect that it may have been compensated for in another layer, which is why I'm reluctant to submit a PR; I don't know if it will break other things.

Affected Versions:

0.95.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions