Skip to content

Commit

Permalink
internal/lsp: add handling for go.mod files in internal/lsp functions
Browse files Browse the repository at this point in the history
When we are processing a go.mod file, we are calling go/packages.load
when we should not be. It will always return 0 packages since it is
not a .go file. This CL adds branching inside each internal/lsp protocol
function and also adds a check in snapshot.PackageHandles for the file type
and returns an error. This will prevent `go list` from running on go.mod files for now.

Updates golang/go#31999

Change-Id: Ic6d0e9b7c81e1f404342b98e10b9c5387adde2ee
Reviewed-on: https://go-review.googlesource.com/c/tools/+/210757
Reviewed-by: Rebecca Stambler <[email protected]>
Run-TryBot: Rohan Challa <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
ridersofrohan committed Dec 11, 2019
1 parent 4da4485 commit ad473c0
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 12 deletions.
5 changes: 5 additions & 0 deletions internal/lsp/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func (s *snapshot) View() source.View {
}

func (s *snapshot) PackageHandles(ctx context.Context, fh source.FileHandle) ([]source.PackageHandle, error) {
// If the file is a go.mod file, go.Packages.Load will always return 0 packages.
if fh.Identity().Kind == source.Mod {
return nil, errors.Errorf("attempting to get PackageHandles of .mod file %s", fh.Identity().URI)
}

ctx = telemetry.File.With(ctx, fh.Identity().URI)
meta := s.getMetadataForURI(fh.Identity().URI)
// Determine if we need to type-check the package.
Expand Down
13 changes: 11 additions & 2 deletions internal/lsp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,17 @@ func (s *Server) completion(ctx context.Context, params *protocol.CompletionPara
if err != nil {
return nil, err
}
options.Completion.FullDocumentation = options.HoverKind == source.FullDocumentation
candidates, surrounding, err := source.Completion(ctx, snapshot, f, params.Position, options.Completion)

var candidates []source.CompletionItem
var surrounding *source.Selection
switch f.Kind() {
case source.Go:
options.Completion.FullDocumentation = options.HoverKind == source.FullDocumentation
candidates, surrounding, err = source.Completion(ctx, snapshot, f, params.Position, options.Completion)
case source.Mod:
candidates, surrounding = nil, nil
}

if err != nil {
log.Print(ctx, "no completions found", tag.Of("At", params.Position), tag.Of("Failure", err))
}
Expand Down
6 changes: 6 additions & 0 deletions internal/lsp/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func (s *Server) definition(ctx context.Context, params *protocol.DefinitionPara
if err != nil {
return nil, err
}
if f.Kind() != source.Go {
return nil, nil
}
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.WidestCheckPackageHandle)
if err != nil {
return nil, err
Expand Down Expand Up @@ -50,6 +53,9 @@ func (s *Server) typeDefinition(ctx context.Context, params *protocol.TypeDefini
if err != nil {
return nil, err
}
if f.Kind() != source.Go {
return nil, nil
}
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.WidestCheckPackageHandle)
if err != nil {
return nil, err
Expand Down
10 changes: 9 additions & 1 deletion internal/lsp/folding_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ func (s *Server) foldingRange(ctx context.Context, params *protocol.FoldingRange
if err != nil {
return nil, err
}
ranges, err := source.FoldingRange(ctx, snapshot, f, view.Options().LineFoldingOnly)

var ranges []*source.FoldingRangeInfo
switch f.Kind() {
case source.Go:
ranges, err = source.FoldingRange(ctx, snapshot, f, view.Options().LineFoldingOnly)
case source.Mod:
ranges = nil
}

if err != nil {
return nil, err
}
Expand Down
14 changes: 13 additions & 1 deletion internal/lsp/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,17 @@ func (s *Server) formatting(ctx context.Context, params *protocol.DocumentFormat
if err != nil {
return nil, err
}
return source.Format(ctx, snapshot, f)

var edits []protocol.TextEdit
switch f.Kind() {
case source.Go:
edits, err = source.Format(ctx, snapshot, f)
case source.Mod:
return nil, nil
}

if err != nil {
return nil, err
}
return edits, nil
}
10 changes: 9 additions & 1 deletion internal/lsp/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ func (s *Server) documentHighlight(ctx context.Context, params *protocol.Documen
if err != nil {
return nil, err
}
rngs, err := source.Highlight(ctx, snapshot, f, params.Position)

var rngs []protocol.Range
switch f.Kind() {
case source.Go:
rngs, err = source.Highlight(ctx, snapshot, f, params.Position)
case source.Mod:
return nil, nil
}

if err != nil {
log.Error(ctx, "no highlight", err, telemetry.URI.Of(uri))
}
Expand Down
3 changes: 3 additions & 0 deletions internal/lsp/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*prot
if err != nil {
return nil, err
}
if f.Kind() != source.Go {
return nil, nil
}
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.WidestCheckPackageHandle)
if err != nil {
return nil, nil
Expand Down
5 changes: 3 additions & 2 deletions internal/lsp/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ func (s *Server) implementation(ctx context.Context, params *protocol.Implementa
if err != nil {
return nil, err
}

if f.Kind() != source.Go {
return nil, nil
}
phs, err := snapshot.PackageHandles(ctx, snapshot.Handle(ctx, f))
if err != nil {
return nil, err
}

var (
allLocs []protocol.Location
seen = make(map[protocol.Location]bool)
Expand Down
7 changes: 5 additions & 2 deletions internal/lsp/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func (s *Server) documentLink(ctx context.Context, params *protocol.DocumentLink
return nil, err
}
fh := view.Snapshot().Handle(ctx, f)
if fh.Identity().Kind == source.Mod {
return nil, nil
}
file, m, _, err := view.Session().Cache().ParseGoHandle(fh, source.ParseFull).Parse(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -98,8 +101,8 @@ func findLinksInString(src string, pos token.Pos, view source.View, mapper *prot
end := urlIndex[1]
startPos := token.Pos(int(pos) + start)
endPos := token.Pos(int(pos) + end)
target = src[start:end]
l, err := toProtocolLink(view, mapper, target, startPos, endPos)
target = src[start:end]
l, err := toProtocolLink(view, mapper, target, startPos, endPos)
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion internal/lsp/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ func (s *Server) references(ctx context.Context, params *protocol.ReferenceParam
return nil, err
}
// Find all references to the identifier at the position.
if f.Kind() != source.Go {
return nil, nil
}
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.WidestCheckPackageHandle)
if err != nil {
return nil, err
return nil, nil
}
references, err := ident.References(ctx)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion internal/lsp/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ func (s *Server) rename(ctx context.Context, params *protocol.RenameParams) (*pr
if err != nil {
return nil, err
}
if f.Kind() != source.Go {
return nil, nil
}
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.WidestCheckPackageHandle)
if err != nil {
return nil, err
return nil, nil
}
edits, err := ident.Rename(ctx, params.NewName)
if err != nil {
Expand Down Expand Up @@ -56,6 +59,9 @@ func (s *Server) prepareRename(ctx context.Context, params *protocol.PrepareRena
if err != nil {
return nil, err
}
if f.Kind() != source.Go {
return nil, nil
}
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.WidestCheckPackageHandle)
if err != nil {
return nil, nil // ignore errors
Expand Down
3 changes: 3 additions & 0 deletions internal/lsp/signature_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func (s *Server) signatureHelp(ctx context.Context, params *protocol.SignatureHe
if err != nil {
return nil, err
}
if f.Kind() != source.Go {
return nil, nil
}
info, err := source.SignatureHelp(ctx, snapshot, f, params.Position)
if err != nil {
log.Print(ctx, "no signature help", tag.Of("At", params.Position), tag.Of("Failure", err))
Expand Down
10 changes: 9 additions & 1 deletion internal/lsp/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ func (s *Server) documentSymbol(ctx context.Context, params *protocol.DocumentSy
if err != nil {
return nil, err
}
symbols, err := source.DocumentSymbols(ctx, snapshot, f)

var symbols []protocol.DocumentSymbol
switch f.Kind() {
case source.Go:
symbols, err = source.DocumentSymbols(ctx, snapshot, f)
case source.Mod:
return []protocol.DocumentSymbol{}, nil
}

if err != nil {
log.Error(ctx, "DocumentSymbols failed", err, telemetry.URI.Of(uri))
return []protocol.DocumentSymbol{}, nil
Expand Down

0 comments on commit ad473c0

Please sign in to comment.