github.com/jhump/golang-x-tools@v0.0.0-20220218190644-4958d6d39439/internal/lsp/cmd/call_hierarchy.go (about) 1 // Copyright 2020 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 "strings" 12 13 "github.com/jhump/golang-x-tools/internal/lsp/protocol" 14 "github.com/jhump/golang-x-tools/internal/span" 15 "github.com/jhump/golang-x-tools/internal/tool" 16 ) 17 18 // callHierarchy implements the callHierarchy verb for gopls. 19 type callHierarchy struct { 20 app *Application 21 } 22 23 func (c *callHierarchy) Name() string { return "call_hierarchy" } 24 func (c *callHierarchy) Parent() string { return c.app.Name() } 25 func (c *callHierarchy) Usage() string { return "<position>" } 26 func (c *callHierarchy) ShortHelp() string { return "display selected identifier's call hierarchy" } 27 func (c *callHierarchy) 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 call_hierarchy helper/helper.go:8:6 33 $ gopls call_hierarchy helper/helper.go:#53 34 `) 35 printFlagDefaults(f) 36 } 37 38 func (c *callHierarchy) Run(ctx context.Context, args ...string) error { 39 if len(args) != 1 { 40 return tool.CommandLineErrorf("call_hierarchy expects 1 argument (position)") 41 } 42 43 conn, err := c.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.CallHierarchyPrepareParams{ 61 TextDocumentPositionParams: protocol.TextDocumentPositionParams{ 62 TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI}, 63 Position: loc.Range.Start, 64 }, 65 } 66 67 callItems, err := conn.PrepareCallHierarchy(ctx, &p) 68 if err != nil { 69 return err 70 } 71 if len(callItems) == 0 { 72 return fmt.Errorf("function declaration identifier not found at %v", args[0]) 73 } 74 75 for _, item := range callItems { 76 incomingCalls, err := conn.IncomingCalls(ctx, &protocol.CallHierarchyIncomingCallsParams{Item: item}) 77 if err != nil { 78 return err 79 } 80 for i, call := range incomingCalls { 81 // From the spec: CallHierarchyIncomingCall.FromRanges is relative to 82 // the caller denoted by CallHierarchyIncomingCall.from. 83 printString, err := callItemPrintString(ctx, conn, call.From, call.From.URI, call.FromRanges) 84 if err != nil { 85 return err 86 } 87 fmt.Printf("caller[%d]: %s\n", i, printString) 88 } 89 90 printString, err := callItemPrintString(ctx, conn, item, "", nil) 91 if err != nil { 92 return err 93 } 94 fmt.Printf("identifier: %s\n", printString) 95 96 outgoingCalls, err := conn.OutgoingCalls(ctx, &protocol.CallHierarchyOutgoingCallsParams{Item: item}) 97 if err != nil { 98 return err 99 } 100 for i, call := range outgoingCalls { 101 // From the spec: CallHierarchyOutgoingCall.FromRanges is the range 102 // relative to the caller, e.g the item passed to 103 printString, err := callItemPrintString(ctx, conn, call.To, item.URI, call.FromRanges) 104 if err != nil { 105 return err 106 } 107 fmt.Printf("callee[%d]: %s\n", i, printString) 108 } 109 } 110 111 return nil 112 } 113 114 // callItemPrintString returns a protocol.CallHierarchyItem object represented as a string. 115 // item and call ranges (protocol.Range) are converted to user friendly spans (1-indexed). 116 func callItemPrintString(ctx context.Context, conn *connection, item protocol.CallHierarchyItem, callsURI protocol.DocumentURI, calls []protocol.Range) (string, error) { 117 itemFile := conn.AddFile(ctx, item.URI.SpanURI()) 118 if itemFile.err != nil { 119 return "", itemFile.err 120 } 121 itemSpan, err := itemFile.mapper.Span(protocol.Location{URI: item.URI, Range: item.Range}) 122 if err != nil { 123 return "", err 124 } 125 126 callsFile := conn.AddFile(ctx, callsURI.SpanURI()) 127 if callsURI != "" && callsFile.err != nil { 128 return "", callsFile.err 129 } 130 var callRanges []string 131 for _, rng := range calls { 132 callSpan, err := callsFile.mapper.Span(protocol.Location{URI: item.URI, Range: rng}) 133 if err != nil { 134 return "", err 135 } 136 137 spn := fmt.Sprint(callSpan) 138 callRanges = append(callRanges, fmt.Sprint(spn[strings.Index(spn, ":")+1:])) 139 } 140 141 printString := fmt.Sprintf("function %s in %v", item.Name, itemSpan) 142 if len(calls) > 0 { 143 printString = fmt.Sprintf("ranges %s in %s from/to %s", strings.Join(callRanges, ", "), callsURI.SpanURI().Filename(), printString) 144 } 145 return printString, nil 146 }