forked from sirnewton01/godev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
def.go
206 lines (162 loc) · 5.4 KB
/
def.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2013 Chris McGee <sirnewton_01@yahoo.ca>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
type Definition struct {
Name string
Package string
Location string
Line string
Column string
}
func definitionHandler(writer http.ResponseWriter, req *http.Request, path string, pathSegs []string) bool {
switch {
case req.Method == "POST" && len(pathSegs) > 2:
offsetStr := req.URL.Query().Get("o")
location := "/" + strings.Join(pathSegs[2:], "/")
workingDir := ""
// Find the workding directory we should launch godef in
if pathSegs[3] == "GOROOT" {
workingDir = filepath.Join(goroot+"/src/pkg", strings.Join(pathSegs[4:len(pathSegs)-1], "/"))
} else {
dirRelPath := filepath.Clean(strings.Join(pathSegs[3:len(pathSegs)-1], "/"))
for _, srcDir := range srcDirs {
dirPath := filepath.Join(srcDir, dirRelPath)
_, err := os.Stat(dirPath)
if err == nil {
workingDir = dirPath
break
}
}
}
if workingDir == "" {
ShowError(writer, 400, "Invalid resource", nil)
return true
}
cmd := exec.Command("godef", "-o="+offsetStr, "-i=true", "-t=true")
cmd.Dir = workingDir
cmd.Stdin = req.Body
output, err := cmd.Output()
if err != nil {
if strings.Contains(strings.ToLower(err.Error()), "not found") {
// Executable was not found, inform the user
ShowError(writer, 400, "Godef tool not found", err)
return true
}
}
if len(output) == 0 {
ShowJson(writer, 204, "No definition found")
return true
}
outputStr := string(output)
// First row is the file and path of the definition
outputRows := strings.Split(outputStr, "\n")
for idx, _ := range outputRows {
outputRows[idx] = strings.Replace(outputRows[idx], "\r", "", -1)
}
if len(outputRows) == 0 {
ShowJson(writer, 204, "No definition found")
return true
}
outputColumns := strings.Split(outputRows[0], ":")
// Windows path
if (outputStr[0] == 'C' || outputStr[0] == 'c') && outputStr[1] == ':' {
outputColumns[1] = string(outputStr[0]) + ":" + outputColumns[1]
outputColumns = outputColumns[1:]
}
if len(outputColumns) == 0 || len(outputColumns) > 3 {
ShowJson(writer, 204, "No definition found")
return true
}
definition := &Definition{}
if len(outputColumns) == 1 {
// Package reference
// do a quick check to see if this is a real file path
_, err := os.Stat(outputColumns[0])
if err != nil {
ShowJson(writer, 204, "No definition found")
return true
}
definition.Location = getLogicalPos(outputColumns[0])
// The package name is the package location without the '/GOROOT' and starting '/'
definition.Package = strings.Replace(definition.Location, "/GOROOT/", "", 1)
if len(definition.Package) > 0 && definition.Package[0] == '/' {
definition.Package = definition.Package[1:]
}
definition.Location = "/file" + definition.Location
// Bail out early, packages don't have any type or line information
ShowJson(writer, 200, definition)
return true
} else if len(outputColumns) == 3 {
// File, line number and column reference
_, err := strconv.ParseInt(outputColumns[1], 10, 64)
if err != nil {
ShowJson(writer, 204, "No definition found")
return true
}
_, err = strconv.ParseInt(outputColumns[2], 10, 64)
if err != nil {
ShowJson(writer, 204, "No definition found")
return true
}
// Make the path absolute
if !filepath.IsAbs(outputColumns[0]) {
outputColumns[0] = filepath.Join(workingDir, outputColumns[0])
}
// do a quick check to see if this is a real file path
_, err = os.Stat(outputColumns[0])
if err != nil {
ShowJson(writer, 204, "No definition found")
return true
}
definition.Location = "/file" + getLogicalPos(outputColumns[0])
// The package name is the location without the '/GOROOT' and starting '/'
definition.Package = strings.Replace(filepath.ToSlash(filepath.Dir(definition.Location[5:])), "/GOROOT/", "", 1)
if len(definition.Package) > 0 && definition.Package[0] == '/' {
definition.Package = definition.Package[1:]
}
definition.Line = outputColumns[1]
definition.Column = outputColumns[2]
} else if len(outputColumns) == 2 {
// Same file line number and column
_, err := strconv.ParseInt(outputColumns[0], 10, 64)
if err != nil {
ShowJson(writer, 204, "No definition found")
return true
}
_, err = strconv.ParseInt(outputColumns[1], 10, 64)
if err != nil {
ShowJson(writer, 204, "No definition found")
return true
}
definition.Location = location
// The package name is the location without the '/GOROOT' and starting '/'
definition.Package = strings.Replace(filepath.ToSlash(filepath.Dir(definition.Location[5:])), "/GOROOT/", "", 1)
if len(definition.Package) > 0 && definition.Package[0] == '/' {
definition.Package = definition.Package[1:]
}
definition.Line = outputColumns[0]
definition.Column = outputColumns[1]
} else {
// Don't recognize this output
ShowJson(writer, 204, "No definition found")
return true
}
// The next line should have a name for this item
if len(outputRows) > 1 {
outputColumns = strings.Split(outputRows[1], " ")
definition.Name = outputColumns[0]
}
ShowJson(writer, 200, definition)
return true
}
return false
}