github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/cmd/highlight.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cmd
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  	"sort"
    12  
    13  	"github.com/powerman/golang-tools/internal/lsp/protocol"
    14  	"github.com/powerman/golang-tools/internal/span"
    15  	"github.com/powerman/golang-tools/internal/tool"
    16  )
    17  
    18  // highlight implements the highlight verb for gopls.
    19  type highlight struct {
    20  	app *Application
    21  }
    22  
    23  func (r *highlight) Name() string      { return "highlight" }
    24  func (r *highlight) Parent() string    { return r.app.Name() }
    25  func (r *highlight) Usage() string     { return "<position>" }
    26  func (r *highlight) ShortHelp() string { return "display selected identifier's highlights" }
    27  func (r *highlight) DetailedHelp(f *flag.FlagSet) {
    28  	fmt.Fprint(f.Output(), `
    29  Example:
    30  
    31  	$ # 1-indexed location (:line:column or :#offset) of the target identifier
    32  	$ gopls highlight helper/helper.go:8:6
    33  	$ gopls highlight helper/helper.go:#53
    34  `)
    35  	printFlagDefaults(f)
    36  }
    37  
    38  func (r *highlight) Run(ctx context.Context, args ...string) error {
    39  	if len(args) != 1 {
    40  		return tool.CommandLineErrorf("highlight expects 1 argument (position)")
    41  	}
    42  
    43  	conn, err := r.app.connect(ctx)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	defer conn.terminate(ctx)
    48  
    49  	from := span.Parse(args[0])
    50  	file := conn.AddFile(ctx, from.URI())
    51  	if file.err != nil {
    52  		return file.err
    53  	}
    54  
    55  	loc, err := file.mapper.Location(from)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	p := protocol.DocumentHighlightParams{
    61  		TextDocumentPositionParams: protocol.TextDocumentPositionParams{
    62  			TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
    63  			Position:     loc.Range.Start,
    64  		},
    65  	}
    66  	highlights, err := conn.DocumentHighlight(ctx, &p)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	var results []span.Span
    72  	for _, h := range highlights {
    73  		l := protocol.Location{Range: h.Range}
    74  		s, err := file.mapper.Span(l)
    75  		if err != nil {
    76  			return err
    77  		}
    78  		results = append(results, s)
    79  	}
    80  	// Sort results to make tests deterministic since DocumentHighlight uses a map.
    81  	sort.SliceStable(results, func(i, j int) bool {
    82  		return span.Compare(results[i], results[j]) == -1
    83  	})
    84  
    85  	for _, s := range results {
    86  		fmt.Println(s)
    87  	}
    88  	return nil
    89  }