1
0
mirror of https://github.com/golang/go synced 2024-10-01 03:28:32 -06:00
go/internal/lsp/cmd/symbols.go
pjw 5091d647ee internal/lsp: reorganize the generated Go code for the lsp protocol
Code generation has been unified, so that tsprotocol.go and tsserver.go
are produced by the same program. tsprotocol.go is about 900 lines shorter,
partly from removing boilerplate comments that golint no longer requires.
(And partly by generating fewer unneeded types.)

The choice made for a union type is commented with the set of types. There
is no Go equivalent for union types, but making themn all interface{}
would replace type checking at unmarshalling with checking runtime
conversions.

Intersection types (A&B) are sometimes embedded (struct{A;B;}, and
sometimes expanded, as they have to be if A and B have fields with the
same names.

There are fewer embedded structs, which had been verbose and confusing to
initialize. They have been replaced by types whose names end in Gn.

Essentially all the generated *structs have been removed. This makes
no difference in what the client sends, and the server may send a {}
where it previously might have sent nothing. The benefit is that some
nil tests can be removed. Thus 'omitempty' in json tags is just
documentation that the element is optional in the protocol.

The files that generate this code will be submitted later, but soon.

Change-Id: I52b997d9c58de3d733fc8c6ce061e47ce2bdb100
Reviewed-on: https://go-review.googlesource.com/c/tools/+/207598
Run-TryBot: Peter Weinberger <pjw@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-11-18 19:51:19 +00:00

80 lines
1.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"
"flag"
"fmt"
"sort"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/tool"
)
// references implements the references verb for gopls
type symbols struct {
app *Application
}
func (r *symbols) Name() string { return "symbols" }
func (r *symbols) Usage() string { return "<file>" }
func (r *symbols) ShortHelp() string { return "display selected file's symbols" }
func (r *symbols) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), `
Example:
$ gopls symbols helper/helper.go
`)
f.PrintDefaults()
}
func (r *symbols) Run(ctx context.Context, args ...string) error {
if len(args) != 1 {
return tool.CommandLineErrorf("symbols expects 1 argument (position)")
}
conn, err := r.app.connect(ctx)
if err != nil {
return err
}
defer conn.terminate(ctx)
from := span.Parse(args[0])
p := protocol.DocumentSymbolParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: string(from.URI()),
},
}
symbols, err := conn.DocumentSymbol(ctx, &p)
if err != nil {
return err
}
for _, s := range symbols {
fmt.Println(symbolToString(s))
// Sort children for consistency
sort.Slice(s.Children, func(i, j int) bool {
return s.Children[i].Name < s.Children[j].Name
})
for _, c := range s.Children {
fmt.Println("\t" + symbolToString(c))
}
}
return nil
}
func symbolToString(symbol protocol.DocumentSymbol) string {
r := symbol.SelectionRange
// convert ranges to user friendly 1-based positions
position := fmt.Sprintf("%v:%v-%v:%v",
r.Start.Line+1,
r.Start.Character+1,
r.End.Line+1,
r.End.Character+1,
)
return fmt.Sprintf("%s %s %s", symbol.Name, symbol.Kind, position)
}