-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_ctags.go
More file actions
236 lines (211 loc) · 5.66 KB
/
Copy pathparser_ctags.go
File metadata and controls
236 lines (211 loc) · 5.66 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package repomap
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
)
var (
ctagsOnce sync.Once
ctagsAvailable bool
)
// CtagsAvailable reports whether ctags with JSON output support is on PATH.
func CtagsAvailable() bool {
ctagsOnce.Do(func() {
path, err := exec.LookPath("ctags")
if err != nil {
return
}
// Verify JSON output is supported (Universal Ctags, not Exuberant).
out, err := exec.Command(path, "--output-format=json", "--version").Output()
if err == nil && len(out) > 0 {
ctagsAvailable = true
}
})
return ctagsAvailable
}
// ctagsEntry is a single JSON line emitted by ctags --output-format=json.
type ctagsEntry struct {
Type string `json:"_type"`
Name string `json:"name"`
Path string `json:"path"`
Kind string `json:"kind"`
Scope string `json:"scope"`
ScopeKind string `json:"scopeKind"`
Signature string `json:"signature"`
Language string `json:"language"`
Line int `json:"line"`
}
// ParseWithCtags runs ctags once over all files and returns FileSymbols for
// each non-Go file in the list. Files must be absolute paths; root is used
// only for computing relative paths in output.
func ParseWithCtags(ctx context.Context, root string, files []FileInfo) ([]*FileSymbols, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
// Build the file list for ctags stdin (-L -).
// We only pass non-Go files; Go files use the AST parser.
absFiles := make([]string, 0, len(files))
for _, f := range files {
if f.Language != "go" {
absFiles = append(absFiles, filepath.Join(root, f.Path))
}
}
if len(absFiles) == 0 {
return nil, nil
}
input := strings.Join(absFiles, "\n")
cmd := exec.CommandContext(ctx, "ctags",
"--output-format=json",
"--fields=+S+l+K+Z+a+n",
"-L", "-",
)
cmd.Stdin = strings.NewReader(input)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("ctags: %w: %s", err, stderr.String())
}
// Index FileInfo by absolute path for fast lookup.
infoByAbs := make(map[string]FileInfo, len(files))
for _, f := range files {
infoByAbs[filepath.Join(root, f.Path)] = f
}
// symbols and members indexed by absolute path.
type pendingMember struct {
parentName string
memberName string
}
symbolsByPath := make(map[string][]*Symbol)
membersByPath := make(map[string][]pendingMember)
// track symbol pointer by name+path for later field injection
symIndex := make(map[string]*Symbol) // key: absPath+"\x00"+name
dec := json.NewDecoder(&stdout)
for dec.More() {
var e ctagsEntry
if err := dec.Decode(&e); err != nil {
continue
}
if e.Type != "tag" || e.Name == "" {
continue
}
absPath := e.Path
if !filepath.IsAbs(absPath) {
absPath = filepath.Join(root, absPath)
}
info, ok := infoByAbs[absPath]
if !ok {
continue
}
// Collect member fields to attach to parent struct/class later.
if isMemberKind(e.Kind) {
if e.Scope != "" && isScopeContainer(e.ScopeKind) {
membersByPath[absPath] = append(membersByPath[absPath], pendingMember{
parentName: e.Scope,
memberName: e.Name,
})
}
continue
}
kind := mapCtagsKind(e.Kind)
if kind == "" {
continue
}
sym := &Symbol{
Name: e.Name,
Kind: kind,
Signature: e.Signature,
Exported: isExportedForLang(e.Name, info.Language),
Line: e.Line,
}
symbolsByPath[absPath] = append(symbolsByPath[absPath], sym)
symIndex[absPath+"\x00"+e.Name] = sym
}
// Attach collected member names to parent symbols.
for absPath, members := range membersByPath {
// Group members by parent name.
grouped := make(map[string][]string)
for _, m := range members {
grouped[m.parentName] = append(grouped[m.parentName], m.memberName)
}
for parentName, fields := range grouped {
if sym, ok := symIndex[absPath+"\x00"+parentName]; ok {
sym.Signature = "{" + strings.Join(fields, ", ") + "}"
}
}
}
// Build output in input order.
result := make([]*FileSymbols, 0, len(files))
for _, f := range files {
if f.Language == "go" {
continue
}
absPath := filepath.Join(root, f.Path)
syms := symbolsByPath[absPath]
out := make([]Symbol, 0, len(syms))
for _, s := range syms {
out = append(out, *s)
}
result = append(result, &FileSymbols{
Path: f.Path,
Language: f.Language,
Symbols: out,
ParseMethod: "ctags",
})
}
return result, nil
}
// mapCtagsKind translates a ctags kind string to a Symbol Kind.
// Returns "" for kinds that should be skipped entirely.
func mapCtagsKind(k string) string {
switch k {
case "function", "f":
return "function"
case "method", "m":
return "method"
case "class", "c":
return "class"
case "struct", "s":
return "struct"
case "interface", "i":
return "interface"
case "enum", "e", "enumerator", "g":
return "enum"
case "variable", "v", "var":
return "variable"
case "constant", "d":
return "constant"
case "type", "t", "typedef":
return "type"
// module, namespace, package, member, field — skip
default:
return ""
}
}
// isMemberKind returns true for ctags kinds that represent struct/class fields.
func isMemberKind(k string) bool {
return k == "member" || k == "field"
}
// isScopeContainer returns true when the scope kind can own fields.
func isScopeContainer(k string) bool {
return k == "struct" || k == "class"
}
// isExportedForLang applies language-specific export conventions.
func isExportedForLang(name, lang string) bool {
if name == "" {
return false
}
switch lang {
case "go":
return isExported(name)
case "python":
return !strings.HasPrefix(name, "_")
default:
return true
}
}