internal/lsp/source: fix completion on final line of a document

Span treats an end of file as the beginning of the next line, which for a final line ending without a newline is incorrect and leads to completions being ignored. We adjust the ending in case range end is on a different line here.

Change-Id: Ic545dcb221493530b7e39d2be8eba57b69fb6597
Reviewed-on: https://go-review.googlesource.com/c/tools/+/249706
Run-TryBot: Danish Dua <danishdua@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Danish Dua 2020-08-21 17:16:29 -04:00
parent b72e8bb66c
commit e0bf2294bb
3 changed files with 17 additions and 0 deletions

View File

@ -42,6 +42,17 @@ func (s *Server) completion(ctx context.Context, params *protocol.CompletionPara
if err != nil {
return nil, err
}
// Span treats an end of file as the beginning of the next line, which for
// a final line ending without a newline is incorrect and leads to
// completions being ignored. We adjust the ending in case ange end is on a
// different line here.
// This should be removed after the resolution of golang/go#41029
if rng.Start.Line != rng.End.Line {
rng.End = protocol.Position{
Character: rng.Start.Character + float64(len(surrounding.Content())),
Line: rng.Start.Line,
}
}
// When using deep completions/fuzzy matching, report results as incomplete so
// client fetches updated completions after every key stroke.

View File

@ -241,6 +241,10 @@ type Selection struct {
mappedRange
}
func (p Selection) Content() string {
return p.content
}
func (p Selection) Prefix() string {
return p.content[:p.cursor-p.spanRange.Start]
}

View File

@ -114,6 +114,8 @@ func positionFromOffset(f *token.File, offset int) (string, int, int, error) {
}
pos := f.Pos(offset)
p := f.Position(pos)
// TODO(golang/go#41029): Consider returning line, column instead of line+1, 1 if
// the file's last character is not a newline.
if offset == f.Size() {
return p.Filename, p.Line + 1, 1, nil
}