diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go index 9206e3f4903..30d04b6dc6d 100644 --- a/internal/lsp/lsp_test.go +++ b/internal/lsp/lsp_test.go @@ -458,13 +458,15 @@ func TestBytesOffset(t *testing.T) { {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 1}, want: 1}, {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 2}, want: 1}, {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 3}, want: 5}, - {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 4}, want: -1}, + {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 4}, want: 6}, + {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 5}, want: -1}, {text: "aaa\nbbb\n", pos: protocol.Position{Line: 0, Character: 3}, want: 3}, {text: "aaa\nbbb\n", pos: protocol.Position{Line: 0, Character: 4}, want: -1}, {text: "aaa\nbbb\n", pos: protocol.Position{Line: 1, Character: 0}, want: 4}, {text: "aaa\nbbb\n", pos: protocol.Position{Line: 1, Character: 3}, want: 7}, {text: "aaa\nbbb\n", pos: protocol.Position{Line: 1, Character: 4}, want: -1}, - {text: "aaa\nbbb\n", pos: protocol.Position{Line: 2, Character: 0}, want: -1}, + {text: "aaa\nbbb\n", pos: protocol.Position{Line: 2, Character: 0}, want: 8}, + {text: "aaa\nbbb\n", pos: protocol.Position{Line: 2, Character: 1}, want: -1}, {text: "aaa\nbbb\n\n", pos: protocol.Position{Line: 2, Character: 0}, want: 8}, } diff --git a/internal/lsp/server.go b/internal/lsp/server.go index 37cf6fc185c..99f0fd67ac1 100644 --- a/internal/lsp/server.go +++ b/internal/lsp/server.go @@ -188,10 +188,13 @@ func (s *server) DidOpen(ctx context.Context, params *protocol.DidOpenTextDocume func bytesOffset(content []byte, pos protocol.Position) int { var line, char, offset int - for len(content) > 0 { + for { if line == int(pos.Line) && char == int(pos.Character) { return offset } + if len(content) == 0 { + return -1 + } r, size := utf8.DecodeRune(content) char++ @@ -210,7 +213,6 @@ func bytesOffset(content []byte, pos protocol.Position) int { char = 0 } } - return -1 } func (s *server) applyChanges(ctx context.Context, params *protocol.DidChangeTextDocumentParams) (string, error) {