File: call_hierarchy.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.1.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,588 kB
  • sloc: javascript: 2,011; asm: 1,458; sh: 174; yacc: 155; makefile: 21; ansic: 17
file content (85 lines) | stat: -rw-r--r-- 2,837 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2020 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 cmdtest

import (
	"fmt"
	"sort"
	"strings"
	"testing"

	"golang.org/x/tools/internal/lsp/protocol"
	"golang.org/x/tools/internal/lsp/tests"
	"golang.org/x/tools/internal/span"
)

func (r *runner) CallHierarchy(t *testing.T, spn span.Span, expectedCalls *tests.CallHierarchyResult) {
	collectCallSpansString := func(callItems []protocol.CallHierarchyItem) string {
		var callSpans []string
		for _, call := range callItems {
			mapper, err := r.data.Mapper(call.URI.SpanURI())
			if err != nil {
				t.Fatal(err)
			}
			callSpan, err := mapper.Span(protocol.Location{URI: call.URI, Range: call.Range})
			if err != nil {
				t.Fatal(err)
			}
			callSpans = append(callSpans, fmt.Sprint(callSpan))
		}
		// to make tests deterministic
		sort.Strings(callSpans)
		return r.Normalize(strings.Join(callSpans, "\n"))
	}

	expectIn, expectOut := collectCallSpansString(expectedCalls.IncomingCalls), collectCallSpansString(expectedCalls.OutgoingCalls)
	expectIdent := r.Normalize(fmt.Sprint(spn))

	uri := spn.URI()
	filename := uri.Filename()
	target := filename + fmt.Sprintf(":%v:%v", spn.Start().Line(), spn.Start().Column())

	got, stderr := r.NormalizeGoplsCmd(t, "call_hierarchy", target)
	if stderr != "" {
		t.Fatalf("call_hierarchy failed for %s: %s", target, stderr)
	}

	gotIn, gotIdent, gotOut := cleanCallHierarchyCmdResult(got)
	if expectIn != gotIn {
		t.Errorf("incoming calls call_hierarchy failed for %s expected:\n%s\ngot:\n%s", target, expectIn, gotIn)
	}
	if expectIdent != gotIdent {
		t.Errorf("call_hierarchy failed for %s expected:\n%s\ngot:\n%s", target, expectIdent, gotIdent)
	}
	if expectOut != gotOut {
		t.Errorf("outgoing calls call_hierarchy failed for %s expected:\n%s\ngot:\n%s", target, expectOut, gotOut)
	}

}

// parses function URI and Range from call hierarchy cmd output to
// incoming, identifier and outgoing calls (returned in that order)
// ex: "identifier: function d at .../callhierarchy/callhierarchy.go:19:6-7" -> ".../callhierarchy/callhierarchy.go:19:6-7"
func cleanCallHierarchyCmdResult(output string) (incoming, ident, outgoing string) {
	var incomingCalls, outgoingCalls []string
	for _, out := range strings.Split(output, "\n") {
		if out == "" {
			continue
		}

		callLocation := out[strings.LastIndex(out, " ")+1:]
		if strings.HasPrefix(out, "caller") {
			incomingCalls = append(incomingCalls, callLocation)
		} else if strings.HasPrefix(out, "callee") {
			outgoingCalls = append(outgoingCalls, callLocation)
		} else {
			ident = callLocation
		}
	}
	sort.Strings(incomingCalls)
	sort.Strings(outgoingCalls)
	incoming, outgoing = strings.Join(incomingCalls, "\n"), strings.Join(outgoingCalls, "\n")
	return
}