1
0
mirror of https://github.com/golang/go synced 2024-09-30 22:48:32 -06:00

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 <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Ian Cottrell 2019-03-11 16:42:38 -04:00
parent 00c44ba9c1
commit de4a0b36f1
2 changed files with 8 additions and 4 deletions

View File

@ -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},
}

View File

@ -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) {