From de4a0b36f11949b6391b0d731291a2755d827c24 Mon Sep 17 00:00:00 2001 From: Ian Cottrell Date: Mon, 11 Mar 2019 16:42:38 -0400 Subject: [PATCH] internal/lsp: allow end of file byte offsets Change-Id: I46d7e07a4603f19f615fed2e37e733573ea0fe08 Reviewed-on: https://go-review.googlesource.com/c/tools/+/166880 Run-TryBot: Ian Cottrell Reviewed-by: Rebecca Stambler --- internal/lsp/lsp_test.go | 6 ++++-- internal/lsp/server.go | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go index 9206e3f490..30d04b6dc6 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 37cf6fc185..99f0fd67ac 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) {