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
2 changes: 2 additions & 0 deletions internal/ls/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ func (c *Converters) LineAndCharacterToPosition(script Script, lineAndCharacter
func (c *Converters) PositionToLineAndCharacter(script Script, position core.TextPos) lsproto.Position {
// UTF-8 offset to UTF-8/16 0-indexed line and character

position = min(position, core.TextPos(len(script.Text())))

lineMap := c.getLineMap(script.FileName())

line, isLineStart := slices.BinarySearch(lineMap.LineStarts, position)
Expand Down
6 changes: 6 additions & 0 deletions internal/ls/source_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ func (l *LanguageService) getMappedLocation(fileName string, fileRange core.Text
}
}
endPos := l.tryGetSourcePosition(fileName, core.TextPos(fileRange.End()))
if endPos == nil {
endPos = &sourcemap.DocumentPosition{
FileName: startPos.FileName,
Pos: startPos.Pos + fileRange.Len(),
Copy link
Member

Choose a reason for hiding this comment

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

Looks right as a port, but since this is happening, do we need to ensure this position is inside the length of the file?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure; my understanding is that the conversion to an LSP range will clamp it via the line map, but maybe not... I can grab the script and min the text length.

Copy link
Member

Choose a reason for hiding this comment

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

It looks like PositionToLineAndCharacter will crash with a slice range out of bounds when rescanning the line for the UTF-16 character offset. Maybe it’s worth adding a guard there and not worrying about it here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, I can. I'm questioning whether or not it's a good idea, though, given we don't do the same sort of bounds checks when going from an LSP position to a byte position... Could go either way. Either way it's sort of papering over something.

}
}
debug.Assert(endPos.FileName == startPos.FileName, "start and end should be in same file")
newRange := core.NewTextRange(startPos.Pos, endPos.Pos)
lspRange := l.createLspRangeFromRange(newRange, l.getScript(startPos.FileName))
Expand Down