diff --git a/internal/ls/converters.go b/internal/ls/converters.go index b7730f3e5d..2fb9d262a3 100644 --- a/internal/ls/converters.go +++ b/internal/ls/converters.go @@ -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) diff --git a/internal/ls/source_map.go b/internal/ls/source_map.go index 62eafda161..c970070558 100644 --- a/internal/ls/source_map.go +++ b/internal/ls/source_map.go @@ -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(), + } + } 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))