1
0
mirror of https://github.com/golang/go synced 2024-11-18 13:14:47 -07:00

internal/lsp/cmd: fix the command line query for definition

The definition command-line interface doesn't match the rest of the
commands, because I think we originally wanted to make them all
subcommands of "gopls query". Remove this, since it's no longer in use.

Change-Id: Iee923db6328774f787d539931001e438d1a2ae6e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/230299
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2020-04-27 11:41:00 -04:00
parent e0d5eebdf8
commit e56429333a
5 changed files with 17 additions and 106 deletions

View File

@ -170,6 +170,7 @@ func (app *Application) mainCommands() []tool.Application {
func (app *Application) featureCommands() []tool.Application {
return []tool.Application{
&check{app: app},
&definition{app: app},
&foldingRanges{app: app},
&format{app: app},
&highlight{app: app},
@ -178,7 +179,6 @@ func (app *Application) featureCommands() []tool.Application {
&inspect{app: app},
&links{app: app},
&prepareRename{app: app},
&query{app: app},
&references{app: app},
&rename{app: app},
&signature{app: app},

View File

@ -12,7 +12,6 @@ import (
"os"
"strings"
guru "golang.org/x/tools/cmd/guru/serial"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
@ -35,9 +34,12 @@ const (
exampleOffset = 1270
)
// definition implements the definition noun for the query command.
// definition implements the definition verb for gopls.
type definition struct {
query *query
app *Application
JSON bool `flag:"json" help:"emit output in JSON format"`
MarkdownSupported bool `flag:"markdown" help:"support markdown in responses"`
}
func (d *definition) Name() string { return "definition" }
@ -62,17 +64,17 @@ func (d *definition) Run(ctx context.Context, args ...string) error {
return tool.CommandLineErrorf("definition expects 1 argument")
}
// Plaintext makes more sense for the command line.
opts := d.query.app.options
d.query.app.options = func(o *source.Options) {
opts := d.app.options
d.app.options = func(o *source.Options) {
if opts != nil {
opts(o)
}
o.PreferredContentFormat = protocol.PlainText
if d.query.MarkdownSupported {
if d.MarkdownSupported {
o.PreferredContentFormat = protocol.Markdown
}
}
conn, err := d.query.app.connect(ctx)
conn, err := d.app.connect(ctx)
if err != nil {
return err
}
@ -120,34 +122,15 @@ func (d *definition) Run(ctx context.Context, args ...string) error {
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)
result := &Definition{
Span: definition,
Description: description,
}
if d.query.JSON {
if d.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)
}
fmt.Printf("%v: defined here as %s", result.Span, result.Description)
return nil
}

View File

@ -15,7 +15,7 @@ import (
"golang.org/x/tools/internal/tool"
)
// highlight implements the highlight verb for gopls
// highlight implements the highlight verb for gopls.
type highlight struct {
app *Application
}

View File

@ -1,71 +0,0 @@
// 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"
"flag"
"fmt"
"golang.org/x/tools/internal/tool"
)
const (
// The set of possible options that can be passed through the -emulate flag,
// which causes query to adjust its output to match that of the binary being
// emulated.
// emulateGuru tells query to emulate the output format of the guru tool.
emulateGuru = "guru"
)
// query implements the query command.
type query struct {
JSON bool `flag:"json" help:"emit output in JSON format"`
MarkdownSupported bool `flag:"markdown" help:"support markdown in responses"`
Emulate string `flag:"emulate" help:"compatibility mode, causes gopls to emulate another tool.\nvalues depend on the operation being performed"`
app *Application
}
func (q *query) Name() string { return "query" }
func (q *query) Usage() string { return "<mode> <mode args>" }
func (q *query) ShortHelp() string {
return "answer queries about go source code"
}
func (q *query) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), `
The mode argument determines the query to perform:
`)
for _, m := range q.modes() {
fmt.Fprintf(f.Output(), " %s : %v\n", m.Name(), m.ShortHelp())
}
fmt.Fprint(f.Output(), `
query flags are:
`)
f.PrintDefaults()
}
// Run takes the args after command flag processing, and invokes the correct
// query mode as specified by the first argument.
func (q *query) Run(ctx context.Context, args ...string) error {
if len(args) == 0 {
return tool.CommandLineErrorf("query must be supplied a mode")
}
mode, args := args[0], args[1:]
for _, m := range q.modes() {
if m.Name() == mode {
return tool.Run(ctx, m, args) // pass errors up the chain
}
}
return tool.CommandLineErrorf("unknown command %v", mode)
}
// modes returns the set of modes supported by the query command.
func (q *query) modes() []tool.Application {
return []tool.Application{
&definition{query: q},
}
}

View File

@ -35,13 +35,12 @@ func (r *runner) Definition(t *testing.T, spn span.Span, d tests.Definition) {
}
d.Src = span.New(d.Src.URI(), span.NewPoint(0, 0, d.Src.Start().Offset()), span.Point{})
for _, mode := range godefModes {
args := []string{"query", "-markdown"}
args := []string{"definition", "-markdown"}
tag := d.Name + "-definition"
if mode&jsonGoDef != 0 {
tag += "-json"
args = append(args, "-json")
}
args = append(args, "definition")
uri := d.Src.URI()
args = append(args, fmt.Sprint(d.Src))
got, _ := r.NormalizeGoplsCmd(t, args...)