forked from DisposaBoy/GoSublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
202 lines (175 loc) · 3.94 KB
/
common.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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"os"
"path/filepath"
"runtime"
"strings"
"unicode/utf8"
)
var (
sRuneError = eRune()
)
type jString string
func (s jString) String() string {
return string(s)
}
func (s *jString) UnmarshalJSON(p []byte) error {
if bytes.Equal(p, []byte("null")) {
return nil
}
return json.Unmarshal(p, (*string)(s))
}
type jData []byte
func (d jData) MarshalJSON() ([]byte, error) {
if len(d) == 0 {
return []byte(`""`), nil
}
buf := bytes.NewBufferString(`"base64:`)
enc := base64.NewEncoder(base64.StdEncoding, buf)
for len(d) > 0 {
r, n := utf8.DecodeRune(d)
if r == utf8.RuneError {
enc.Write(sRuneError)
} else {
enc.Write(d[:n])
}
d = d[n:]
}
enc.Close()
buf.WriteByte('"')
return buf.Bytes(), nil
}
func errStr(err error) string {
if err != nil {
return err.Error()
}
return ""
}
func envSlice(envMap map[string]string) []string {
env := []string{}
for k, v := range envMap {
env = append(env, k+"="+v)
}
if len(env) == 0 {
env = os.Environ()
}
return env
}
func defaultEnv() map[string]string {
return map[string]string{
"GOROOT": runtime.GOROOT(),
"GOARCH": runtime.GOARCH,
"GOOS": runtime.GOOS,
}
}
func orString(a ...string) string {
for _, s := range a {
if s != "" {
return s
}
}
return ""
}
func parseAstFile(fn string, s string, mode parser.Mode) (fset *token.FileSet, af *ast.File, err error) {
fset = token.NewFileSet()
var src interface{}
if s != "" {
src = s
}
if fn == "" {
fn = "<stdin>"
}
af, err = parser.ParseFile(fset, fn, src, mode)
return
}
func newPrinter(tabIndent bool, tabWidth int) *printer.Config {
mode := printer.UseSpaces
if tabIndent {
mode |= printer.TabIndent
}
return &printer.Config{
Mode: mode,
Tabwidth: tabWidth,
}
}
func printSrc(fset *token.FileSet, v interface{}, tabIndent bool, tabWidth int) (src string, err error) {
p := newPrinter(tabIndent, tabWidth)
buf := &bytes.Buffer{}
if err = p.Fprint(buf, fset, v); err == nil {
src = buf.String()
}
return
}
func fiHasGoExt(fi os.FileInfo) bool {
return strings.HasSuffix(fi.Name(), ".go")
}
func parsePkg(fset *token.FileSet, srcDir string, mode parser.Mode) (pkg *ast.Package, pkgs map[string]*ast.Package, err error) {
if pkgs, err = parser.ParseDir(fset, srcDir, fiHasGoExt, mode); pkgs != nil {
_, pkgName := filepath.Split(srcDir)
// we aren't going to support package whose name don't match the directory unless it's main
p, ok := pkgs[pkgName]
if !ok {
p, ok = pkgs["main"]
}
if ok {
pkg, err = ast.NewPackage(fset, p.Files, nil, nil)
}
}
return
}
func rootDirs(env map[string]string) []string {
dirs := []string{}
gopath := ""
if len(env) == 0 {
gopath = os.Getenv("GOPATH")
} else {
gopath = env["GOPATH"]
}
gorootBase := runtime.GOROOT()
if len(env) > 0 && env["GOROOT"] != "" {
gorootBase = env["GOROOT"]
} else if fn := os.Getenv("GOROOT"); fn != "" {
gorootBase = fn
}
goroot := filepath.Join(gorootBase, "src", "pkg")
dirsSeen := map[string]bool{}
for _, fn := range filepath.SplitList(gopath) {
if dirsSeen[fn] {
continue
}
dirsSeen[fn] = true
// goroot may be a part of gopath and we don't want that
if fn != "" && !strings.HasPrefix(fn, gorootBase) {
fn := filepath.Join(fn, "src")
if fi, err := os.Stat(fn); err == nil && fi.IsDir() {
dirs = append(dirs, fn)
}
}
}
if fi, err := os.Stat(goroot); err == nil && fi.IsDir() {
dirs = append(dirs, goroot)
}
return dirs
}
func findPkg(fset *token.FileSet, importPath string, dirs []string, mode parser.Mode) (pkg *ast.Package, pkgs map[string]*ast.Package, err error) {
for _, dir := range dirs {
srcDir := filepath.Join(dir, importPath)
if pkg, pkgs, err = parsePkg(fset, srcDir, mode); pkg != nil {
return
}
}
return
}
func eRune() []byte {
s := make([]byte, utf8.RuneLen(utf8.RuneError))
n := utf8.EncodeRune(s, utf8.RuneError)
s = s[:n]
return s
}