@codua/codeoutline-win32-x64 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DISTRIBUTION.md +98 -0
- package/LICENSE +24 -0
- package/README.md +132 -0
- package/README.zh-CN.md +132 -0
- package/THIRD_PARTY.md +32 -0
- package/build-info.json +1 -0
- package/codeoutline +4 -0
- package/codeoutline.cmd +3 -0
- package/licenses/libdeflate.txt +22 -0
- package/licenses/lua-cmsgpack.txt +22 -0
- package/licenses/luajit.txt +56 -0
- package/licenses/mbedtls.txt +553 -0
- package/licenses/rpmalloc.txt +12 -0
- package/licenses/xnet2lua.txt +24 -0
- package/licenses/yyjson.txt +21 -0
- package/package.json +1 -0
- package/scripts/codeoutline/cli.lua +32 -0
- package/scripts/codeoutline/command.lua +155 -0
- package/scripts/codeoutline/common.lua +124 -0
- package/scripts/codeoutline/control.lua +4 -0
- package/scripts/codeoutline/explore.lua +500 -0
- package/scripts/codeoutline/graph.lua +380 -0
- package/scripts/codeoutline/http.lua +156 -0
- package/scripts/codeoutline/index.lua +404 -0
- package/scripts/codeoutline/index_worker.lua +43 -0
- package/scripts/codeoutline/lang/c.lua +362 -0
- package/scripts/codeoutline/lang/cpp.lua +539 -0
- package/scripts/codeoutline/lang/csharp.lua +334 -0
- package/scripts/codeoutline/lang/go.lua +233 -0
- package/scripts/codeoutline/lang/java.lua +274 -0
- package/scripts/codeoutline/lang/js.lua +466 -0
- package/scripts/codeoutline/lang/lua.lua +218 -0
- package/scripts/codeoutline/lang/python.lua +184 -0
- package/scripts/codeoutline/lang/rust.lua +276 -0
- package/scripts/codeoutline/main.lua +104 -0
- package/scripts/codeoutline/mcp.lua +188 -0
- package/scripts/codeoutline/parse.lua +49 -0
- package/scripts/codeoutline/path.lua +36 -0
- package/scripts/codeoutline/service.lua +135 -0
- package/scripts/codeoutline/text.lua +43 -0
- package/scripts/codeoutline/version.lua +1 -0
- package/scripts/codeoutline/xscan.lua +625 -0
- package/xnet2lua/bin/xnet.exe +0 -0
- package/xnet2lua/scripts/core/share/xhttp_codec.lua +835 -0
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
-- explore.lua — answer "show me the code for X" in one call: find the
|
|
2
|
+
-- symbols a query names, connect them through the call graph, and return
|
|
3
|
+
-- their verbatim, line-numbered source within a byte budget.
|
|
4
|
+
--
|
|
5
|
+
-- local text, info = explore.run(G, idx, 'xtimer_poll xtimer_add', { budget = 16000 })
|
|
6
|
+
--
|
|
7
|
+
-- Output layout (the budget is spent top-down):
|
|
8
|
+
-- 1. summary line + the call paths found between the named symbols
|
|
9
|
+
-- 2. source of the named symbols, then of the symbols on those paths,
|
|
10
|
+
-- grouped by file, as `line<TAB>code` like the Read tool
|
|
11
|
+
-- 3. callers / callees of the named symbols (locations only)
|
|
12
|
+
-- 4. other matches that did not fit, as pointers
|
|
13
|
+
-- Big bodies are cut to head + tail; classes over the limit collapse to a
|
|
14
|
+
-- member outline; anything past the budget becomes a pointer line.
|
|
15
|
+
|
|
16
|
+
local graph = require('codeoutline.graph')
|
|
17
|
+
local text = require('codeoutline.text')
|
|
18
|
+
|
|
19
|
+
local M = {}
|
|
20
|
+
|
|
21
|
+
M.DEFAULT_BUDGET = 16000
|
|
22
|
+
M.MIN_BUDGET = 256
|
|
23
|
+
M.MAX_BUDGET = 262144
|
|
24
|
+
|
|
25
|
+
function M.validate(query, opts)
|
|
26
|
+
assert(type(query) == 'string' and query:find('%S') and #query <= 4096 and not query:find('%z'),
|
|
27
|
+
'query must contain 1-4096 bytes of non-empty text without NUL')
|
|
28
|
+
assert(text.valid(query) == query, 'query must be valid UTF-8')
|
|
29
|
+
assert(opts == nil or type(opts) == 'table', 'options must be a table')
|
|
30
|
+
local budget = M.DEFAULT_BUDGET
|
|
31
|
+
if opts and opts.budget ~= nil then budget = opts.budget end
|
|
32
|
+
assert(type(budget) == 'number' and budget >= M.MIN_BUDGET and budget <= M.MAX_BUDGET and budget % 1 == 0,
|
|
33
|
+
'budget must be an integer between 256 and 262144 bytes')
|
|
34
|
+
return budget
|
|
35
|
+
end
|
|
36
|
+
M.MAX_SEEDS = 8
|
|
37
|
+
M.PATH_DEPTH = 4
|
|
38
|
+
M.SEED_FULL_LINES = 160 -- seeds up to this many lines are shown whole
|
|
39
|
+
M.SPINE_FULL_LINES = 60
|
|
40
|
+
M.CUT_HEAD, M.CUT_TAIL = 80, 15
|
|
41
|
+
M.LIST_CAP = 10
|
|
42
|
+
M.CONTEXT_SEED_LINES = 25 -- seeds shorter than this bring their same-file callees
|
|
43
|
+
M.CONTEXT_PER_SEED = 3
|
|
44
|
+
|
|
45
|
+
local KIND_WEIGHT = {
|
|
46
|
+
['function'] = 10, method = 10, constructor = 8, class = 9, interface = 9, record = 9,
|
|
47
|
+
struct = 7, union = 6, enum = 6, typedef = 5, macro = 5, prototype = 3,
|
|
48
|
+
variable = 3, field = 2, enum_member = 2,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
-- Words that show up in questions but never name the code being asked about.
|
|
52
|
+
local STOP = {}
|
|
53
|
+
for w in ([[the a an and or of to in on for with from by is are was be how what where
|
|
54
|
+
why when which who does do did can could should would this that these those it its
|
|
55
|
+
show find explain code function method class file files call calls called caller
|
|
56
|
+
callers flow work works use used using get set make new add list all into about
|
|
57
|
+
return returns value values type types between through does]]):gmatch('%a+') do
|
|
58
|
+
STOP[w] = true
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
local VENDORED = { '/3rd/', '^3rd/', '/third_party/', '^third_party/', '/thirdparty/',
|
|
62
|
+
'/vendor/', '^vendor/', '/node_modules/', '/external/', '^external/', '/deps/', '^deps/' }
|
|
63
|
+
|
|
64
|
+
local function vendored(path)
|
|
65
|
+
for _, p in ipairs(VENDORED) do
|
|
66
|
+
if path:find(p) then return true end
|
|
67
|
+
end
|
|
68
|
+
return false
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
-- Split camelCase / snake_case into lowercase segments.
|
|
72
|
+
local function segments(name)
|
|
73
|
+
local out = {}
|
|
74
|
+
for part in name:gsub('(%l)(%u)', '%1_%2'):gmatch('[%w]+') do
|
|
75
|
+
out[#out + 1] = part:lower()
|
|
76
|
+
end
|
|
77
|
+
return out
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
local SOURCE_EXT = {}
|
|
81
|
+
for e in ('c h py pyi java lua cpp cc cxx hpp hh hxx'):gmatch('%a+') do SOURCE_EXT[e] = true end
|
|
82
|
+
|
|
83
|
+
-- Query -> candidate terms: qualified chains, identifiers, file names.
|
|
84
|
+
local function terms(query)
|
|
85
|
+
local out, seen = {}, {}
|
|
86
|
+
local function add(kind, text)
|
|
87
|
+
local key = kind .. ':' .. text
|
|
88
|
+
if not seen[key] then seen[key] = true; out[#out + 1] = { kind = kind, text = text } end
|
|
89
|
+
end
|
|
90
|
+
for chain in query:gmatch('[%a_][%w_]*[%.:%->]+[%a_][%w_%.:%->]*') do
|
|
91
|
+
if chain:match('%.%a+$') and chain:match('^[%w_/%-]+%.%a+$') and not chain:find('::') then
|
|
92
|
+
add('file', chain)
|
|
93
|
+
end
|
|
94
|
+
local parts = {}
|
|
95
|
+
for p in chain:gmatch('[%a_][%w_]*') do parts[#parts + 1] = p end
|
|
96
|
+
if #parts >= 2 then
|
|
97
|
+
add('qualified', table.concat(parts, '.'))
|
|
98
|
+
-- the chain speaks for its parts; `subprocess.run` must not also
|
|
99
|
+
-- pull in every `run` in the project as a loose word
|
|
100
|
+
for _, p in ipairs(parts) do seen['name:' .. p] = true; seen['word:' .. p] = true end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
for path in query:gmatch('[%w_%-%./]+%.%a%a?%a?%a?') do
|
|
104
|
+
local ext = path:match('%.(%a+)$')
|
|
105
|
+
if path:find('/') or (ext and SOURCE_EXT[ext:lower()]) then add('file', path) end
|
|
106
|
+
end
|
|
107
|
+
for word in query:gmatch('[%a_][%w_]*') do
|
|
108
|
+
if #word >= 3 and not STOP[word:lower()] then
|
|
109
|
+
-- "code-like" words (snake_case, camelCase, digits) name symbols;
|
|
110
|
+
-- plain lowercase words are usually English from the question.
|
|
111
|
+
local code = word:find('_') or word:find('%d') or word:find('%l%u') or word:find('^%u')
|
|
112
|
+
add(code and 'name' or 'word', word)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
return out
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
-- Rank one candidate node for a query term.
|
|
119
|
+
local function seed_score(G, id, exact)
|
|
120
|
+
local n = G.nodes[id]
|
|
121
|
+
local s = (KIND_WEIGHT[n.node.kind] or 1) + (exact and 20 or 0)
|
|
122
|
+
if vendored(G.files[n.f].path) then s = s - 6 end
|
|
123
|
+
return s
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
local function find_seeds(G, query)
|
|
127
|
+
local cands = {} -- id -> score
|
|
128
|
+
local files = {} -- file indices named in the query
|
|
129
|
+
local function consider(id, s)
|
|
130
|
+
if not cands[id] or cands[id] < s then cands[id] = s end
|
|
131
|
+
end
|
|
132
|
+
local list_terms = terms(query)
|
|
133
|
+
local has_code = false
|
|
134
|
+
for _, t in ipairs(list_terms) do
|
|
135
|
+
if t.kind ~= 'word' then has_code = true end
|
|
136
|
+
end
|
|
137
|
+
for _, t in ipairs(list_terms) do
|
|
138
|
+
if t.kind == 'word' then
|
|
139
|
+
-- Plain words count only as exact names (no case folding or fuzzy
|
|
140
|
+
-- matching, which is where English words pick up noise), and rank
|
|
141
|
+
-- below code-like terms when the query has any.
|
|
142
|
+
for _, id in ipairs(G.by_name[t.text] or {}) do
|
|
143
|
+
consider(id, seed_score(G, id, true) - (has_code and 12 or 8))
|
|
144
|
+
end
|
|
145
|
+
elseif t.kind == 'file' then
|
|
146
|
+
local want = t.text:gsub('\\', '/')
|
|
147
|
+
for f, rec in ipairs(G.files) do
|
|
148
|
+
if rec.path == want or rec.path:sub(-(#want + 1)) == '/' .. want then files[#files + 1] = f end
|
|
149
|
+
end
|
|
150
|
+
elseif t.kind == 'qualified' then
|
|
151
|
+
local last = t.text:match('([%w_]+)$')
|
|
152
|
+
local first = t.text:match('^([%w_]+)')
|
|
153
|
+
for _, id in ipairs(G.by_name[last] or {}) do
|
|
154
|
+
local q = G.nodes[id].node.qualified:gsub('[:]+', '.')
|
|
155
|
+
if q == t.text or q:sub(-(#t.text + 1)) == '.' .. t.text then
|
|
156
|
+
consider(id, seed_score(G, id, true) + 10)
|
|
157
|
+
elseif select(2, t.text:gsub('%.', '')) == 1
|
|
158
|
+
and G.files[G.nodes[id].f].path:match('([^/]+)%.[^./]+$') == first then
|
|
159
|
+
-- module.func where the module is the file: Lua's `local M`
|
|
160
|
+
-- tables and Python modules are named by their file
|
|
161
|
+
consider(id, seed_score(G, id, true) + 8)
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
else
|
|
165
|
+
local exact = G.by_name[t.text]
|
|
166
|
+
if exact then
|
|
167
|
+
-- a name defined in many places is generic; keep its best few
|
|
168
|
+
for _, id in ipairs(exact) do consider(id, seed_score(G, id, true) - math.min(#exact, 20) * 0.2) end
|
|
169
|
+
else
|
|
170
|
+
local ci = G.by_lname[t.text:lower()]
|
|
171
|
+
if ci then
|
|
172
|
+
for _, id in ipairs(ci) do consider(id, seed_score(G, id, true) - 2) end
|
|
173
|
+
elseif #t.text >= 4 then
|
|
174
|
+
-- fuzzy: every query segment appears in the name's segments
|
|
175
|
+
local want = segments(t.text)
|
|
176
|
+
local hits = 0
|
|
177
|
+
for name, ids in pairs(G.by_name) do
|
|
178
|
+
local lname = name:lower()
|
|
179
|
+
local ok = true
|
|
180
|
+
for _, w in ipairs(want) do
|
|
181
|
+
if not lname:find(w, 1, true) then ok = false; break end
|
|
182
|
+
end
|
|
183
|
+
if ok then
|
|
184
|
+
for _, id in ipairs(ids) do consider(id, seed_score(G, id, false) - #name * 0.05) end
|
|
185
|
+
hits = hits + 1
|
|
186
|
+
if hits > 200 then break end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
local list = {}
|
|
194
|
+
for id, s in pairs(cands) do list[#list + 1] = { id = id, s = s } end
|
|
195
|
+
table.sort(list, function(a, b)
|
|
196
|
+
if a.s ~= b.s then return a.s > b.s end
|
|
197
|
+
return a.id < b.id
|
|
198
|
+
end)
|
|
199
|
+
return list, files
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
-- Shortest call path a -> b (out edges), up to depth; returns id list.
|
|
203
|
+
local function path_between(G, a, b, depth)
|
|
204
|
+
local prev, frontier = { [a] = a }, { a }
|
|
205
|
+
for _ = 1, depth do
|
|
206
|
+
local next_frontier = {}
|
|
207
|
+
for _, x in ipairs(frontier) do
|
|
208
|
+
for _, e in ipairs(G.out[x] or {}) do
|
|
209
|
+
if not prev[e.id] then
|
|
210
|
+
prev[e.id] = x
|
|
211
|
+
if e.id == b then
|
|
212
|
+
local path, y = { b }, b
|
|
213
|
+
while y ~= a do y = prev[y]; table.insert(path, 1, y) end
|
|
214
|
+
return path
|
|
215
|
+
end
|
|
216
|
+
next_frontier[#next_frontier + 1] = e.id
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
frontier = next_frontier
|
|
221
|
+
if #frontier == 0 or #frontier > 20000 then break end
|
|
222
|
+
end
|
|
223
|
+
return nil
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
local file_cache = {}
|
|
227
|
+
local function file_lines(idx, rel)
|
|
228
|
+
local key = idx.root .. '/' .. rel
|
|
229
|
+
local lines = file_cache[key]
|
|
230
|
+
if lines then return lines end
|
|
231
|
+
lines = {}
|
|
232
|
+
local src = idx:query_source(rel)
|
|
233
|
+
if src then
|
|
234
|
+
for line in (src .. '\n'):gmatch('([^\n]*)\n') do lines[#lines + 1] = (line:gsub('\r$', '')) end
|
|
235
|
+
end
|
|
236
|
+
file_cache[key] = lines
|
|
237
|
+
return lines
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
local function render_range(lines, a, b, out)
|
|
241
|
+
for i = a, math.min(b, #lines) do
|
|
242
|
+
out[#out + 1] = string.format('%d\t%s', i, lines[i])
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
-- Source block for one node, cut to `full` lines (head + tail beyond).
|
|
247
|
+
-- Classes/structs over the limit collapse to their member outline.
|
|
248
|
+
local function node_block(G, idx, id, full)
|
|
249
|
+
local n = G.nodes[id]
|
|
250
|
+
local node = n.node
|
|
251
|
+
local lines = file_lines(idx, G.files[n.f].path)
|
|
252
|
+
local a, b = node.line, math.max(node.end_line, node.line)
|
|
253
|
+
local out = {}
|
|
254
|
+
local span = b - a + 1
|
|
255
|
+
if span <= full then
|
|
256
|
+
render_range(lines, a, b, out)
|
|
257
|
+
else
|
|
258
|
+
local kids = G.children[id]
|
|
259
|
+
local container = node.kind == 'class' or node.kind == 'interface' or node.kind == 'record'
|
|
260
|
+
or node.kind == 'struct' or node.kind == 'enum' or node.kind == 'union'
|
|
261
|
+
if container and kids and #kids > 0 then
|
|
262
|
+
render_range(lines, a, a, out)
|
|
263
|
+
-- one outline row per line: `int a, b;` declares two fields on
|
|
264
|
+
-- one line and the later signature covers both
|
|
265
|
+
local rows, row_lines = {}, {}
|
|
266
|
+
for _, k in ipairs(kids) do
|
|
267
|
+
local kn = G.nodes[k].node
|
|
268
|
+
if not rows[kn.line] then row_lines[#row_lines + 1] = kn.line end
|
|
269
|
+
rows[kn.line] = kn.sig or kn.name
|
|
270
|
+
end
|
|
271
|
+
for _, ln in ipairs(row_lines) do
|
|
272
|
+
out[#out + 1] = string.format('%d\t %s', ln, rows[ln])
|
|
273
|
+
end
|
|
274
|
+
out[#out + 1] = string.format('\t... (%s body of %d lines outlined; explore a member for its source)', node.kind, span)
|
|
275
|
+
else
|
|
276
|
+
render_range(lines, a, a + M.CUT_HEAD - 1, out)
|
|
277
|
+
out[#out + 1] = string.format('\t... (%d lines omitted) ...', span - M.CUT_HEAD - M.CUT_TAIL)
|
|
278
|
+
render_range(lines, b - M.CUT_TAIL + 1, b, out)
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
return table.concat(out, '\n')
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
local function label(G, id)
|
|
285
|
+
local n = G.nodes[id]
|
|
286
|
+
return string.format('%s %s (%s)', n.node.kind, n.node.qualified, graph.where(G, id))
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
-- Run a query. Returns the text and a table with what was selected.
|
|
290
|
+
function M.run(G, idx, query, opts)
|
|
291
|
+
opts = opts or {}
|
|
292
|
+
local budget = M.validate(query, opts)
|
|
293
|
+
file_cache = {}
|
|
294
|
+
local ranked, named_files = find_seeds(G, query)
|
|
295
|
+
|
|
296
|
+
-- Seeds: top-ranked, but at most 3 definitions per name so one generic
|
|
297
|
+
-- name can't crowd out the others the query mentions.
|
|
298
|
+
-- A prototype only earns a seed slot when its name has no definition
|
|
299
|
+
-- among the matches (the definition's source is what the caller wants).
|
|
300
|
+
local defined = {}
|
|
301
|
+
for _, c in ipairs(ranked) do
|
|
302
|
+
local node = G.nodes[c.id].node
|
|
303
|
+
if node.kind ~= 'prototype' and not node.decl then defined[node.name] = true end
|
|
304
|
+
end
|
|
305
|
+
local seeds, per_name = {}, {}
|
|
306
|
+
for _, c in ipairs(ranked) do
|
|
307
|
+
local node = G.nodes[c.id].node
|
|
308
|
+
local name = node.name
|
|
309
|
+
if not ((node.kind == 'prototype' or node.decl) and defined[name]) then
|
|
310
|
+
per_name[name] = (per_name[name] or 0) + 1
|
|
311
|
+
if per_name[name] <= 3 and #seeds < M.MAX_SEEDS then seeds[#seeds + 1] = c.id end
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
local is_seed = {}
|
|
315
|
+
for _, id in ipairs(seeds) do is_seed[id] = true end
|
|
316
|
+
|
|
317
|
+
-- Spine: nodes on call paths between seeds.
|
|
318
|
+
local flows, spine, on_spine = {}, {}, {}
|
|
319
|
+
for _, a in ipairs(seeds) do
|
|
320
|
+
for _, b in ipairs(seeds) do
|
|
321
|
+
if a ~= b then
|
|
322
|
+
local p = path_between(G, a, b, M.PATH_DEPTH)
|
|
323
|
+
if p then
|
|
324
|
+
local names = {}
|
|
325
|
+
for _, id in ipairs(p) do
|
|
326
|
+
names[#names + 1] = G.nodes[id].node.name
|
|
327
|
+
if not is_seed[id] and not on_spine[id] then
|
|
328
|
+
on_spine[id] = true
|
|
329
|
+
spine[#spine + 1] = id
|
|
330
|
+
end
|
|
331
|
+
end
|
|
332
|
+
flows[#flows + 1] = table.concat(names, ' -> ')
|
|
333
|
+
end
|
|
334
|
+
end
|
|
335
|
+
end
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
local parts, used, omitted = {}, 0, false
|
|
339
|
+
local function emit(s)
|
|
340
|
+
parts[#parts + 1] = s
|
|
341
|
+
used = used + #s + 1
|
|
342
|
+
end
|
|
343
|
+
local shown, files_shown, file_order = {}, {}, {}
|
|
344
|
+
|
|
345
|
+
if #seeds == 0 and #named_files == 0 then
|
|
346
|
+
return 'No symbols matched. Name functions, classes, or files (e.g. "xtimer_poll", "Session.save", "xpoll.c").', { seeds = {} }
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
-- Section 1: summary + flows.
|
|
350
|
+
local nfiles = {}
|
|
351
|
+
for _, id in ipairs(seeds) do nfiles[G.nodes[id].f] = true end
|
|
352
|
+
for _, id in ipairs(spine) do nfiles[G.nodes[id].f] = true end
|
|
353
|
+
local nf = 0
|
|
354
|
+
for _ in pairs(nfiles) do nf = nf + 1 end
|
|
355
|
+
emit(string.format('Matched %d symbols in %d files, %d more on call paths between them; short ones bring their callees. Source below is verbatim with line numbers; treat it as already Read.',
|
|
356
|
+
#seeds, nf, #spine))
|
|
357
|
+
if #flows > 0 then
|
|
358
|
+
emit('\n## Call paths')
|
|
359
|
+
for i = 1, math.min(#flows, M.LIST_CAP) do emit(' ' .. flows[i]) end
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
-- Section 2: source, grouped by file in first-appearance order.
|
|
363
|
+
local order = {}
|
|
364
|
+
for _, id in ipairs(seeds) do order[#order + 1] = { id = id, full = M.SEED_FULL_LINES } end
|
|
365
|
+
for _, id in ipairs(spine) do order[#order + 1] = { id = id, full = M.SPINE_FULL_LINES } end
|
|
366
|
+
-- One hop of context: a short named function is usually a wrapper, and
|
|
367
|
+
-- the code the caller actually wants is what it calls. Same-file callees
|
|
368
|
+
-- of short seeds ride along (budget permitting) so no follow-up is needed.
|
|
369
|
+
local in_order = {}
|
|
370
|
+
for _, item in ipairs(order) do in_order[item.id] = true end
|
|
371
|
+
for _, id in ipairs(seeds) do
|
|
372
|
+
local node = G.nodes[id].node
|
|
373
|
+
if node.end_line - node.line < M.CONTEXT_SEED_LINES then
|
|
374
|
+
local added = 0
|
|
375
|
+
for _, e in ipairs(G.out[id] or {}) do
|
|
376
|
+
if added >= M.CONTEXT_PER_SEED then break end
|
|
377
|
+
local callee = G.nodes[e.id]
|
|
378
|
+
local k = callee.node.kind
|
|
379
|
+
if not in_order[e.id] and callee.f == G.nodes[id].f and (k == 'function' or k == 'method') then
|
|
380
|
+
in_order[e.id] = true
|
|
381
|
+
order[#order + 1] = { id = e.id, full = M.SPINE_FULL_LINES }
|
|
382
|
+
added = added + 1
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
local by_file, pointers = {}, {}
|
|
388
|
+
for _, item in ipairs(order) do
|
|
389
|
+
local f = G.nodes[item.id].f
|
|
390
|
+
if not by_file[f] then by_file[f] = {}; file_order[#file_order + 1] = f end
|
|
391
|
+
table.insert(by_file[f], item)
|
|
392
|
+
end
|
|
393
|
+
-- Files named directly: an outline of their top-level symbols.
|
|
394
|
+
for _, f in ipairs(named_files) do
|
|
395
|
+
if not by_file[f] then by_file[f] = {}; file_order[#file_order + 1] = f end
|
|
396
|
+
by_file[f].outline = true
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
-- Confirm every file whose source or symbol labels this query can emit.
|
|
400
|
+
-- GBK reparsing invalidates node IDs, so rebuild and select again before
|
|
401
|
+
-- producing any output. Unrelated project files remain speculative.
|
|
402
|
+
local selected, changed = {}, false
|
|
403
|
+
for _, f in ipairs(file_order) do selected[f] = true end
|
|
404
|
+
for _, c in ipairs(ranked) do selected[G.nodes[c.id].f] = true end
|
|
405
|
+
for _, id in ipairs(seeds) do
|
|
406
|
+
for _, edges in ipairs({ G.inn[id] or {}, G.out[id] or {} }) do
|
|
407
|
+
for _, e in ipairs(edges) do selected[G.nodes[e.id].f] = true end
|
|
408
|
+
end
|
|
409
|
+
end
|
|
410
|
+
for f in pairs(selected) do
|
|
411
|
+
local _, repaired = idx:query_source(G.files[f].path)
|
|
412
|
+
changed = repaired or changed
|
|
413
|
+
end
|
|
414
|
+
if changed then return nil, { encoding_retry = true } end
|
|
415
|
+
|
|
416
|
+
local reserve = math.floor(budget * 0.15) -- keep room for sections 3-4
|
|
417
|
+
for _, f in ipairs(file_order) do
|
|
418
|
+
local rec = G.files[f]
|
|
419
|
+
local header = '\n## ' .. rec.path
|
|
420
|
+
local items = by_file[f]
|
|
421
|
+
table.sort(items, function(a, b) return G.nodes[a.id].node.line < G.nodes[b.id].node.line end)
|
|
422
|
+
local wrote_header = false
|
|
423
|
+
if items.outline then
|
|
424
|
+
local ol = {}
|
|
425
|
+
for _, node in ipairs(rec.nodes) do
|
|
426
|
+
if not node.parent and node.kind ~= 'macro' then
|
|
427
|
+
ol[#ol + 1] = string.format('%d\t%s %s', node.line, node.kind, node.sig or node.name)
|
|
428
|
+
end
|
|
429
|
+
if #ol >= 60 then ol[#ol + 1] = '\t...'; break end
|
|
430
|
+
end
|
|
431
|
+
local block = header .. ' (outline)\n' .. table.concat(ol, '\n')
|
|
432
|
+
if used + #block < budget - reserve then emit(block); wrote_header = true else omitted = true end
|
|
433
|
+
end
|
|
434
|
+
for _, item in ipairs(items) do
|
|
435
|
+
if not shown[item.id] then
|
|
436
|
+
local block = node_block(G, idx, item.id, item.full)
|
|
437
|
+
local need = #block + (wrote_header and 0 or #header + 1) + 1
|
|
438
|
+
if used + need < budget - reserve then
|
|
439
|
+
if not wrote_header then emit(header); wrote_header = true end
|
|
440
|
+
emit(block)
|
|
441
|
+
shown[item.id] = true
|
|
442
|
+
files_shown[f] = true
|
|
443
|
+
else
|
|
444
|
+
pointers[#pointers + 1] = item.id
|
|
445
|
+
end
|
|
446
|
+
end
|
|
447
|
+
end
|
|
448
|
+
end
|
|
449
|
+
|
|
450
|
+
-- Section 3: neighbors of the named symbols.
|
|
451
|
+
local rel = {}
|
|
452
|
+
for _, id in ipairs(seeds) do
|
|
453
|
+
local callers, callees = {}, {}
|
|
454
|
+
local seen_in, seen_out = {}, {}
|
|
455
|
+
for _, e in ipairs(G.inn[id] or {}) do
|
|
456
|
+
if not seen_in[e.id] and #callers < M.LIST_CAP then
|
|
457
|
+
seen_in[e.id] = true
|
|
458
|
+
callers[#callers + 1] = string.format('%s (%s:%d)', G.nodes[e.id].node.name, G.files[G.nodes[e.id].f].path, e.line)
|
|
459
|
+
end
|
|
460
|
+
end
|
|
461
|
+
for _, e in ipairs(G.out[id] or {}) do
|
|
462
|
+
if not seen_out[e.id] and #callees < M.LIST_CAP then
|
|
463
|
+
seen_out[e.id] = true
|
|
464
|
+
callees[#callees + 1] = G.nodes[e.id].node.name .. ' (' .. graph.where(G, e.id) .. ')'
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
if #callers > 0 or #callees > 0 then
|
|
468
|
+
local nsites = G.inn[id] and #G.inn[id] or 0
|
|
469
|
+
rel[#rel + 1] = string.format('%s:\n called by%s: %s\n calls: %s', G.nodes[id].node.qualified,
|
|
470
|
+
nsites > #callers and string.format(' (%d call sites)', nsites) or '',
|
|
471
|
+
#callers > 0 and table.concat(callers, ', ') or '-',
|
|
472
|
+
#callees > 0 and table.concat(callees, ', ') or '-')
|
|
473
|
+
end
|
|
474
|
+
end
|
|
475
|
+
if #rel > 0 then
|
|
476
|
+
local block = '\n## Callers / callees\n' .. table.concat(rel, '\n')
|
|
477
|
+
emit(block)
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
-- Section 4: pointers for everything that matched but was not shown.
|
|
481
|
+
local more = {}
|
|
482
|
+
for _, id in ipairs(pointers) do more[#more + 1] = ' ' .. label(G, id) end
|
|
483
|
+
local extra = 0
|
|
484
|
+
for _, c in ipairs(ranked) do
|
|
485
|
+
if not shown[c.id] and not is_seed[c.id] then
|
|
486
|
+
extra = extra + 1
|
|
487
|
+
if #more < 15 then more[#more + 1] = ' ' .. label(G, c.id) end
|
|
488
|
+
end
|
|
489
|
+
end
|
|
490
|
+
if #more > 0 then
|
|
491
|
+
local block = '\n## Not shown (explore these by name for their source)\n' .. table.concat(more, '\n')
|
|
492
|
+
if extra + #pointers > #more then block = block .. string.format('\n ... %d more', extra + #pointers - #more) end
|
|
493
|
+
if used + #block <= budget + 400 then emit(block) else omitted = true end
|
|
494
|
+
end
|
|
495
|
+
|
|
496
|
+
local result, truncated = text.limit(table.concat(parts, '\n'), budget, omitted or #pointers > 0)
|
|
497
|
+
return result, { seeds = seeds, spine = spine, shown = shown, flows = flows, truncated = truncated }
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
return M
|