mirror of
https://github.com/golang/go
synced 2024-11-19 00:54:42 -07:00
11affa06ff
If we encounter `go list` errors when loading a user's package, we should try to see if they've encountered any of our common error cases. They are: 1) a user has GO111MODULE=off, but is outside of their GOPATH, and 2) a user is in module mode but doesn't have a go.mod file. Fortunately, go/packages does a great job handling edge cases so gopls will work well for most of them. The main issue will be unresolved imports. These show up in DepErrors in `go list`, so go/packages doesn't propagate them through to the list of errors. This will require changes to go/packages. Updates golang/go#31668 Change-Id: Ibd5253b33b38caffeaad54a403c74c0b861fcc14 Reviewed-on: https://go-review.googlesource.com/c/tools/+/194018 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
145 lines
3.9 KiB
Go
145 lines
3.9 KiB
Go
// Copyright 2019 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
guru "golang.org/x/tools/cmd/guru/serial"
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/span"
|
|
"golang.org/x/tools/internal/tool"
|
|
errors "golang.org/x/xerrors"
|
|
)
|
|
|
|
// A Definition is the result of a 'definition' query.
|
|
type Definition struct {
|
|
Span span.Span `json:"span"` // span of the definition
|
|
Description string `json:"description"` // description of the denoted object
|
|
}
|
|
|
|
// These constant is printed in the help, and then used in a test to verify the
|
|
// help is still valid.
|
|
// They refer to "Set" in "flag.FlagSet" from the DetailedHelp method below.
|
|
const (
|
|
exampleLine = 44
|
|
exampleColumn = 47
|
|
exampleOffset = 1270
|
|
)
|
|
|
|
// definition implements the definition noun for the query command.
|
|
type definition struct {
|
|
query *query
|
|
}
|
|
|
|
func (d *definition) Name() string { return "definition" }
|
|
func (d *definition) Usage() string { return "<position>" }
|
|
func (d *definition) ShortHelp() string { return "show declaration of selected identifier" }
|
|
func (d *definition) DetailedHelp(f *flag.FlagSet) {
|
|
fmt.Fprintf(f.Output(), `
|
|
Example: show the definition of the identifier at syntax at offset %[1]v in this file (flag.FlagSet):
|
|
|
|
$ gopls definition internal/lsp/cmd/definition.go:%[1]v:%[2]v
|
|
$ gopls definition internal/lsp/cmd/definition.go:#%[3]v
|
|
|
|
gopls query definition flags are:
|
|
`, exampleLine, exampleColumn, exampleOffset)
|
|
f.PrintDefaults()
|
|
}
|
|
|
|
// Run performs the definition query as specified by args and prints the
|
|
// results to stdout.
|
|
func (d *definition) Run(ctx context.Context, args ...string) error {
|
|
if len(args) != 1 {
|
|
return tool.CommandLineErrorf("definition expects 1 argument")
|
|
}
|
|
conn, err := d.query.app.connect(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer conn.terminate(ctx)
|
|
from := span.Parse(args[0])
|
|
file := conn.AddFile(ctx, from.URI())
|
|
if file.err != nil {
|
|
return file.err
|
|
}
|
|
loc, err := file.mapper.Location(from)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tdpp := protocol.TextDocumentPositionParams{
|
|
TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
|
|
Position: loc.Range.Start,
|
|
}
|
|
p := protocol.DefinitionParams{
|
|
TextDocumentPositionParams: tdpp,
|
|
}
|
|
locs, err := conn.Definition(ctx, &p)
|
|
if err != nil {
|
|
return errors.Errorf("%v: %v", from, err)
|
|
}
|
|
|
|
if len(locs) == 0 {
|
|
return errors.Errorf("%v: not an identifier", from)
|
|
}
|
|
q := protocol.HoverParams{
|
|
TextDocumentPositionParams: tdpp,
|
|
}
|
|
hover, err := conn.Hover(ctx, &q)
|
|
if err != nil {
|
|
return errors.Errorf("%v: %v", from, err)
|
|
}
|
|
if hover == nil {
|
|
return errors.Errorf("%v: not an identifier", from)
|
|
}
|
|
file = conn.AddFile(ctx, span.NewURI(locs[0].URI))
|
|
if file.err != nil {
|
|
return errors.Errorf("%v: %v", from, file.err)
|
|
}
|
|
definition, err := file.mapper.Span(locs[0])
|
|
if err != nil {
|
|
return errors.Errorf("%v: %v", from, err)
|
|
}
|
|
description := strings.TrimSpace(hover.Contents.Value)
|
|
var result interface{}
|
|
switch d.query.Emulate {
|
|
case "":
|
|
result = &Definition{
|
|
Span: definition,
|
|
Description: description,
|
|
}
|
|
case emulateGuru:
|
|
pos := span.New(definition.URI(), definition.Start(), definition.Start())
|
|
result = &guru.Definition{
|
|
ObjPos: fmt.Sprint(pos),
|
|
Desc: description,
|
|
}
|
|
default:
|
|
return errors.Errorf("unknown emulation for definition: %s", d.query.Emulate)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.query.JSON {
|
|
enc := json.NewEncoder(os.Stdout)
|
|
enc.SetIndent("", "\t")
|
|
return enc.Encode(result)
|
|
}
|
|
switch d := result.(type) {
|
|
case *Definition:
|
|
fmt.Printf("%v: defined here as %s", d.Span, d.Description)
|
|
case *guru.Definition:
|
|
fmt.Printf("%s: defined here as %s", d.ObjPos, d.Desc)
|
|
default:
|
|
return errors.Errorf("no printer for type %T", result)
|
|
}
|
|
return nil
|
|
}
|