mirror of https://github.com/golang/go.git
internal/lsp: add handling for go.mod files in internal/lsp functions
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 <rstambler@golang.org> Run-TryBot: Rohan Challa <rohan@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
4da4485a1c
commit
ad473c03aa
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue