-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.sh
executable file
·144 lines (131 loc) · 5.41 KB
/
tests.sh
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#/bin/bash
set -e -o pipefail
# This script talks to gopls to go through a function's definition, identify all of the referenced symbols, and resolve their definitions.
# All of these definitions are then printed to stdout.
########################################
# Find the full definition of a function
# Params:
# string - path to the referenced symbol, e.g. pkg/path/to/file.go:123:456
# Returns:
# the full definition of the symbol, e.g. pkg/path/to/file.go:123:456
########################################
function get_definition() {
printf "Getting definition for '%s'\n" "${1}"
local path="${1}"
local definition=$(gopls definition -json "${path}")
echo "${definition}"
}
########################################
# Find the implementation of a function
# Params:
# string - path to the referenced symbol, e.g. pkg/path/to/file.go:123:456
# Returns:
# the full definition of the symbol, e.g. pkg/path/to/file.go:123:456
########################################
function get_implementation() {
printf "Getting implementation for '%s'\n" "${1}"
local path="${1}"
local implementation=$(gopls implementation "${path}")
echo "${implementation}"
}
########################################
# Find the function definition range
# Params:
# string - path to the referenced symbol, e.g. pkg/path/to/file.go:123:456
# Returns:
# string - the range where the function is defined on
########################################
function get_definition_range() {
local symbolPath="${1}"
local line_number=$(echo "${symbolPath}" | sed -E 's/.*:([0-9]+):[0-9]+/\1/')
# printf "extracted line number: '%s'\n" "${line_number}"
# split the file path based on the ':' which separates the symbol row+colume from the file path
local file_path=$(echo "${symbolPath}" | sed -E 's/(.*):[0-9]+:[0-9]+/\1/')
# printf "using filepath: '%s'\n" "${file_path}"
local foldingRange=$(gopls folding_ranges "${file_path}")
# printf "folding range: '%s'\n" "${foldingRange}"
# find the lines in foldingRange which start with the line number.
# each line will be in the format :[startline]:[startcol]-[endline]:[endcol]
local relevantLines=$(echo "${foldingRange}" | grep -E "^${line_number}:")
# from the relevant lines, find the line number which has the greatest end line number
local maxEndLine=0
local maxEndLineLine=""
# for line in "${relevantLines}"; do
# local endLine=$(echo "${line}" | sed -E 's/.*-([0-9]+):[0-9]+/\1/')
# printf "endLine: '%s'\n" "${endLine}"
# if [ "${endLine}" -gt "${maxEndLine}" ]; then
# maxEndLine="${endLine}"
# maxEndLineLine="${line}"
# fi
# done
while read -r line; do
local endLine=$(echo "${line}" | sed -E 's/.*-([0-9]+):[0-9]+/\1/')
# printf "endLine: '%s'\n" "${endLine}"
if [ "${endLine}" -gt "${maxEndLine}" ]; then
maxEndLine="${endLine}"
maxEndLineLine="${line}"
fi
done <<< "${relevantLines}"
# return the range from the start line to the end line
local startLine=$(echo "${maxEndLineLine}" | sed -E 's/([0-9]+):[0-9]+-.*/\1/')
echo "${line_number}-${maxEndLine}"
}
########################################
# Given a symbol path, e.g. pkg/path/to/file.go:123:456, return only the filepath
# Params:
# (string) - path to the referenced symbol, e.g. pkg/path/to/file.go:123:456
# Returns:
# (string) - the filepath, e.g. pkg/path/to/file.go
########################################
function get_file_path() {
local symbolPath="${1}"
local file_path=$(echo "${symbolPath}" | sed -E 's/(.*):[0-9]+:[0-9]+/\1/')
echo "${file_path}"
}
########################################
# Given a symbol path, e.g. pkg/path/to/file.go:123:456, return only the line number
# Params:
# (string) - path to the referenced symbol, e.g. pkg/path/to/file.go:123:456
# Returns:
# (string) - the line number, e.g. 123
########################################
function get_line_number() {
local symbolPath="${1}"
local line_number=$(echo "${symbolPath}" | sed -E 's/.*:([0-9]+):[0-9]+/\1/')
echo "${line_number}"
}
########################################
# Given a symbol path, e.g. pkg/path/to/file.go:123:456, return only the column number
# Params:
# (string) - path to the referenced symbol, e.g. pkg/path/to/file.go:123:456
# Returns:
# (string) - the column number, e.g. 456
########################################
function get_column_number() {
local symbolPath="${1}"
local column_number=$(echo "${symbolPath}" | sed -E 's/.*:[0-9]+:([0-9]+)/\1/')
echo "${column_number}"
}
########################################
# Given the path to a symbol, return the full definition of the symbol
# Params:
# (string) - path to the referenced symbol, e.g. pkg/path/to/file.go:123:456
# Returns:
# (string) - the full definition of the symbol, e.g. "func main() {\n\tfmt.Println(\"Hello, world!\")\n}\n
########################################
function print_definition() {
# extract the line number from the path
local symbolPath="${1}"
local line_number=$(echo "${symbolPath}" | sed -E 's/.*:([0-9]+):[0-9]+/\1/')
local definitionRange=$(get_definition_range "${symbolPath}")
local startLine=$(echo "${definitionRange}" | sed -E 's/([0-9]+)-.*/\1/')
local endLine=$(echo "${definitionRange}" | sed -E 's/[0-9]+-([0-9]+)/\1/')
# print out the filepath at the given line number
local filePath=$(get_file_path "${symbolPath}")
printf "definitionRange: '%s'\n" "${definitionRange}"
echo $(sed -n "${startLine},${endLine}p" "${filePath}")
}
# get_definition "${1}"
# get_implementation "${1}"
# print_definition "${1}"
print_definition "${1}"