Skip to content

Commit

Permalink
internal/lsp/source: fix completion on final line of a document
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
dandua98 committed Aug 25, 2020
1 parent b72e8bb commit e0bf229
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/lsp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions internal/lsp/source/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
2 changes: 2 additions & 0 deletions internal/span/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit e0bf229

Please sign in to comment.