golang.org/x/tools/gopls@v0.15.3/internal/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 "golang.org/x/tools/gopls/internal/protocol" 14 "golang.org/x/tools/internal/tool" 15 ) 16 17 // callHierarchy implements the callHierarchy verb for gopls. 18 type callHierarchy struct { 19 app *Application 20 } 21 22 func (c *callHierarchy) Name() string { return "call_hierarchy" } 23 func (c *callHierarchy) Parent() string { return c.app.Name() } 24 func (c *callHierarchy) Usage() string { return "<position>" } 25 func (c *callHierarchy) ShortHelp() string { return "display selected identifier's call hierarchy" } 26 func (c *callHierarchy) DetailedHelp(f *flag.FlagSet) { 27 fmt.Fprint(f.Output(), ` 28 Example: 29 30 $ # 1-indexed location (:line:column or :#offset) of the target identifier 31 $ gopls call_hierarchy helper/helper.go:8:6 32 $ gopls call_hierarchy helper/helper.go:#53 33 `) 34 printFlagDefaults(f) 35 } 36 37 func (c *callHierarchy) Run(ctx context.Context, args ...string) error { 38 if len(args) != 1 { 39 return tool.CommandLineErrorf("call_hierarchy expects 1 argument (position)") 40 } 41 42 conn, err := c.app.connect(ctx, nil) 43 if err != nil { 44 return err 45 } 46 defer conn.terminate(ctx) 47 48 from := parseSpan(args[0]) 49 file, err := conn.openFile(ctx, from.URI()) 50 if err != nil { 51 return err 52 } 53 54 loc, err := file.spanLocation(from) 55 if err != nil { 56 return err 57 } 58 59 p := protocol.CallHierarchyPrepareParams{ 60 TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc), 61 } 62 63 callItems, err := conn.PrepareCallHierarchy(ctx, &p) 64 if err != nil { 65 return err 66 } 67 if len(callItems) == 0 { 68 return fmt.Errorf("function declaration identifier not found at %v", args[0]) 69 } 70 71 for _, item := range callItems { 72 incomingCalls, err := conn.IncomingCalls(ctx, &protocol.CallHierarchyIncomingCallsParams{Item: item}) 73 if err != nil { 74 return err 75 } 76 for i, call := range incomingCalls { 77 // From the spec: CallHierarchyIncomingCall.FromRanges is relative to 78 // the caller denoted by CallHierarchyIncomingCall.from. 79 printString, err := callItemPrintString(ctx, conn, call.From, call.From.URI, call.FromRanges) 80 if err != nil { 81 return err 82 } 83 fmt.Printf("caller[%d]: %s\n", i, printString) 84 } 85 86 printString, err := callItemPrintString(ctx, conn, item, "", nil) 87 if err != nil { 88 return err 89 } 90 fmt.Printf("identifier: %s\n", printString) 91 92 outgoingCalls, err := conn.OutgoingCalls(ctx, &protocol.CallHierarchyOutgoingCallsParams{Item: item}) 93 if err != nil { 94 return err 95 } 96 for i, call := range outgoingCalls { 97 // From the spec: CallHierarchyOutgoingCall.FromRanges is the range 98 // relative to the caller, e.g the item passed to 99 printString, err := callItemPrintString(ctx, conn, call.To, item.URI, call.FromRanges) 100 if err != nil { 101 return err 102 } 103 fmt.Printf("callee[%d]: %s\n", i, printString) 104 } 105 } 106 107 return nil 108 } 109 110 // callItemPrintString returns a protocol.CallHierarchyItem object represented as a string. 111 // item and call ranges (protocol.Range) are converted to user friendly spans (1-indexed). 112 func callItemPrintString(ctx context.Context, conn *connection, item protocol.CallHierarchyItem, callsURI protocol.DocumentURI, calls []protocol.Range) (string, error) { 113 itemFile, err := conn.openFile(ctx, item.URI) 114 if err != nil { 115 return "", err 116 } 117 itemSpan, err := itemFile.rangeSpan(item.Range) 118 if err != nil { 119 return "", err 120 } 121 122 var callRanges []string 123 if callsURI != "" { 124 callsFile, err := conn.openFile(ctx, callsURI) 125 if err != nil { 126 return "", err 127 } 128 for _, rng := range calls { 129 call, err := callsFile.rangeSpan(rng) 130 if err != nil { 131 return "", err 132 } 133 callRange := fmt.Sprintf("%d:%d-%d", call.Start().Line(), call.Start().Column(), call.End().Column()) 134 callRanges = append(callRanges, callRange) 135 } 136 } 137 138 printString := fmt.Sprintf("function %s in %v", item.Name, itemSpan) 139 if len(calls) > 0 { 140 printString = fmt.Sprintf("ranges %s in %s from/to %s", strings.Join(callRanges, ", "), callsURI.Path(), printString) 141 } 142 return printString, nil 143 }