mirror of
https://github.com/golang/go
synced 2024-11-06 02:26:17 -07:00
aa29eadba2
When the cursor is on a return statement or in the function declaration it will highlight the control flow for the function. It will also highlight individual fields and results if the cursor is specifically in one. Fixes #34496 Change-Id: I71d460cd174a8fbc61d119b9633c3c3ecbde2af9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/208267 Run-TryBot: Rohan Challa <rohan@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
91 lines
2.1 KiB
Go
91 lines
2.1 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"
|
|
)
|
|
|
|
// highlight implements the highlight verb for gopls
|
|
type highlight struct {
|
|
app *Application
|
|
}
|
|
|
|
func (r *highlight) Name() string { return "highlight" }
|
|
func (r *highlight) Usage() string { return "<position>" }
|
|
func (r *highlight) ShortHelp() string { return "display selected identifier's highlights" }
|
|
func (r *highlight) DetailedHelp(f *flag.FlagSet) {
|
|
fmt.Fprint(f.Output(), `
|
|
Example:
|
|
|
|
$ # 1-indexed location (:line:column or :#offset) of the target identifier
|
|
$ gopls highlight helper/helper.go:8:6
|
|
$ gopls highlight helper/helper.go:#53
|
|
|
|
gopls highlight flags are:
|
|
`)
|
|
f.PrintDefaults()
|
|
}
|
|
|
|
func (r *highlight) Run(ctx context.Context, args ...string) error {
|
|
if len(args) != 1 {
|
|
return tool.CommandLineErrorf("highlight expects 1 argument (position)")
|
|
}
|
|
|
|
conn, err := r.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
|
|
}
|
|
|
|
p := protocol.DocumentHighlightParams{
|
|
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
|
|
TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
|
|
Position: loc.Range.Start,
|
|
},
|
|
}
|
|
highlights, err := conn.DocumentHighlight(ctx, &p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var results []span.Span
|
|
for _, h := range highlights {
|
|
l := protocol.Location{Range: h.Range}
|
|
s, err := file.mapper.Span(l)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
results = append(results, s)
|
|
}
|
|
// Sort results to make tests deterministic since DocumentHighlight uses a map.
|
|
sort.SliceStable(results, func(i, j int) bool {
|
|
return span.Compare(results[i], results[j]) == -1
|
|
})
|
|
|
|
for _, s := range results {
|
|
fmt.Println(s)
|
|
}
|
|
return nil
|
|
}
|