@codua/codeoutline-win32-x64 0.2.4 → 0.2.6

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.
@@ -0,0 +1,474 @@
1
+ -- Query-owned, bounded lexical aliases for explicit dispatch contracts.
2
+ -- No execution, basename guesses or persistent binding graph.
3
+ local M = {}
4
+ local control = require('codeoutline.control')
5
+ local member = { ['.'] = true, [':'] = true, ['::'] = true, ['->'] = true }
6
+ local supported = { lua = true, javascript = true, typescript = true, python = true, c = true, cpp = true }
7
+ local callable = { ['function'] = true, method = true, constructor = true }
8
+ local statements = { ['local'] = true, ['function'] = true, ['end'] = true, ['return'] = true,
9
+ ['if'] = true, ['else'] = true, ['elseif'] = true, ['until'] = true, ['for'] = true,
10
+ ['while'] = true, ['do'] = true, ['repeat'] = true, ['break'] = true }
11
+
12
+ local function quoted(T, i, language)
13
+ local s = T:text(i)
14
+ if T.k[i] == 'str' and language == 'lua' then return require('codeoutline.lua_literal').decode(s) end
15
+ if T.k[i] == 'str' and (s:sub(1, 1) == '"' or s:sub(1, 1) == "'")
16
+ and s:sub(-1) == s:sub(1, 1) and not s:find('[\\$]') then return s:sub(2, -2) end
17
+ end
18
+
19
+ local function name(T, i)
20
+ if T.k[i] ~= 'id' then return nil, i end
21
+ local result = T:text(i)
22
+ i = i + 1
23
+ while member[T:text(i)] and T.k[i + 1] == 'id' do
24
+ result = result .. T:text(i) .. T:text(i + 1); i = i + 2
25
+ end
26
+ return result, i
27
+ end
28
+
29
+ local function simple(T, i, language, symbols_only)
30
+ local start = i
31
+ if T:text(i) == '&' then i = i + 1 end
32
+ local symbol, ending = name(T, i)
33
+ local literal = not symbols_only and quoted(T, i, language)
34
+ if literal then ending = i + 1 end
35
+ if not symbol and not literal then return false end
36
+ local next = T:text(ending)
37
+ local boundary = next == '' or next == ';' or next == '}' or T.k[ending] == 'nl'
38
+ or language == 'lua' and statements[next]
39
+ or T.l[ending] and T.l[ending] > T.l[ending - 1]
40
+ and (T.k[ending] == 'id' or T.k[ending] == 'kw' and next ~= 'and' and next ~= 'or')
41
+ if not boundary then return false end
42
+ return { symbol = symbol, literal = literal, token = start }, ending
43
+ end
44
+ M.simple = simple
45
+
46
+ -- Only remember literal service constructors; validate the factory's lexical
47
+ -- identity during expansion, after imports, shadowing and writes are known.
48
+ local function service_initializer(T, i)
49
+ local factory, open = name(T, i)
50
+ if not factory or T:text(open) ~= '(' or not T.m[open] then return nil end
51
+ local service = quoted(T, open + 1, 'lua')
52
+ if not service or (T:text(open + 2) ~= ',' and open + 2 ~= T.m[open]) then return nil end
53
+ local ending = T.m[open] + 1
54
+ local next = T:text(ending)
55
+ if next ~= '' and next ~= ';' and not statements[next]
56
+ and not (T.l[ending] and T.l[ending] > T.l[ending - 1]
57
+ and (T.k[ending] == 'id' or T.k[ending] == 'kw' and next ~= 'and' and next ~= 'or')) then return nil end
58
+ return { service = service, factory = { symbol = factory }, token = i }
59
+ end
60
+
61
+ local function is_service_factory(value)
62
+ return value and not value.node and (not value.import or value.import.path == 'skynet')
63
+ and (value.symbol == 'skynet.uniqueservice' or value.symbol == 'skynet.newservice')
64
+ end
65
+
66
+ local function initializer(T, i, typed)
67
+ if T:text(i + 1) == '=' then return i + 1 end
68
+ -- C/C++ inline function-pointer declarators, not a call-result expression.
69
+ if T:text(i - 2) == '(' and T:text(i - 1) == '*' and T:text(i + 1) == ')'
70
+ and T:text(i + 2) == '(' and T.m[i + 2] and T:text(T.m[i + 2] + 1) == '=' then
71
+ return T.m[i + 2] + 1
72
+ end
73
+ if typed and T:text(i + 1) == ':' then
74
+ local _, j = name(T, i + 2)
75
+ if T:text(j) == '=' then return j end
76
+ end
77
+ end
78
+
79
+ local function lvalue_end(T, i)
80
+ if T.k[i] ~= 'id' then return nil end
81
+ i = i + 1
82
+ while true do
83
+ if member[T:text(i)] and T.k[i + 1] == 'id' then i = i + 2
84
+ elseif T:text(i) == '[' and T.m[i] then i = T.m[i] + 1
85
+ else return i end
86
+ end
87
+ end
88
+
89
+ -- Import descriptors are syntax only. Lexical declarations and writes below
90
+ -- decide whether an import can actually be used at a query site.
91
+ local function imports(r, T)
92
+ local result = {}
93
+ if r.language ~= 'lua' and r.language ~= 'javascript' and r.language ~= 'typescript'
94
+ and r.language ~= 'python' then return result end
95
+ local function add(local_name, path, imported, token)
96
+ result[#result + 1] = { name = local_name, path = path, imported = imported, token = token }
97
+ end
98
+ for i = 1, T.n do
99
+ if i % 4096 == 0 then control.check() end
100
+ local s = T.k[i] == 'kw' and T:text(i)
101
+ if r.language == 'lua' and s == 'local' and T.k[i + 1] == 'id' and T:text(i + 2) == '='
102
+ and (T:text(i + 3) == 'require' or T:text(i + 3) == 'dofile')
103
+ and (T:text(i + 4) == '(' and T.m[i + 4] == i + 6 and quoted(T, i + 5, 'lua')
104
+ or quoted(T, i + 4, 'lua')) then
105
+ local path = quoted(T, i + 4, 'lua') or quoted(T, i + 5, 'lua')
106
+ add(T:text(i + 1), path, '*', i + 1)
107
+ result[#result].loader = T:text(i + 3)
108
+ elseif (r.language == 'javascript' or r.language == 'typescript') and s == 'import'
109
+ and T:text(i + 1) ~= '(' then
110
+ local j, entries = i + 1, {}
111
+ if T.k[j] == 'id' then entries[#entries + 1] = { T:text(j), 'default', j }; j = j + 1 end
112
+ if T:text(j) == ',' then j = j + 1 end
113
+ if T:text(j) == '*' and T:text(j + 1) == 'as' and T.k[j + 2] == 'id' then
114
+ entries[#entries + 1] = { T:text(j + 2), '*', j + 2 }; j = j + 3
115
+ elseif T:text(j) == '{' and T.m[j] then
116
+ local close = T.m[j]; j = j + 1
117
+ while j < close do
118
+ if T.k[j] == 'id' then
119
+ local original, alias, tok = T:text(j), T:text(j), j; j = j + 1
120
+ if T:text(j) == 'as' and T.k[j + 1] == 'id' then alias, tok, j = T:text(j + 1), j + 1, j + 2 end
121
+ entries[#entries + 1] = { alias, original, tok }
122
+ else j = j + 1 end
123
+ end
124
+ j = close + 1
125
+ end
126
+ if T:text(j) == 'from' and quoted(T, j + 1) then
127
+ for _, e in ipairs(entries) do add(e[1], quoted(T, j + 1), e[2], e[3]) end
128
+ end
129
+ elseif r.language == 'python' and s == 'import' and T:text(i - 1) ~= '.' then
130
+ -- A from-import is handled at its `from` token.
131
+ local first = i
132
+ while first > 1 and T.k[first - 1] ~= 'nl' do first = first - 1 end
133
+ if T:text(first) ~= 'from' then
134
+ local j = i + 1
135
+ while T.k[j] == 'id' do
136
+ local path
137
+ path, j = name(T, j)
138
+ if T:text(j) == 'as' and T.k[j + 1] == 'id' then add(T:text(j + 1), path, '*', j + 1)
139
+ j = j + 2
140
+ else add(path:match('^[^.]+'), path:match('^[^.]+'), '*', j - 1) end
141
+ if T:text(j) ~= ',' then break end
142
+ j = j + 1
143
+ end
144
+ end
145
+ elseif r.language == 'python' and s == 'from' then
146
+ local prefix, j = '', i + 1
147
+ while T:text(j) == '.' or T:text(j) == '...' do prefix = prefix .. T:text(j); j = j + 1 end
148
+ local path
149
+ path, j = name(T, j)
150
+ path = prefix .. (path or '')
151
+ if path == '' then path = nil end
152
+ if path and T:text(j) == 'import' then
153
+ j = j + 1
154
+ if T:text(j) == '(' then j = j + 1 end
155
+ while T.k[j] == 'id' do
156
+ local original, alias, tok = T:text(j), T:text(j), j; j = j + 1
157
+ if T:text(j) == 'as' and T.k[j + 1] == 'id' then alias, tok, j = T:text(j + 1), j + 1, j + 2 end
158
+ add(alias, path, original, tok)
159
+ if T:text(j) ~= ',' then break end
160
+ j = j + 1
161
+ end
162
+ end
163
+ end
164
+ end
165
+ return result
166
+ end
167
+ M.imports = imports
168
+
169
+ -- Cheap, conservative roots let aliases reach the query parser without
170
+ -- performing scope analysis at index time. Existing dispatch_calls stores them.
171
+ function M.hints(r, T, calls)
172
+ if not supported[r.language] then return end
173
+ local roots, values = {}, {}
174
+ for call in pairs(calls) do roots[call:match('^([%w_$]+)')] = true end
175
+ local function add(n, v)
176
+ values[n] = values[n] or {}; values[n][#values[n] + 1] = v
177
+ end
178
+ for i = 1, T.n do
179
+ if i % 4096 == 0 then control.check() end
180
+ if T.k[i] == 'op' and T:text(i) == '=' then
181
+ local lhs = T.k[i - 1] == 'id' and i - 1
182
+ -- A type annotation may separate the local name from `=`. All
183
+ -- identifier components are conservative roots, not proven bindings.
184
+ if lhs then
185
+ local v = simple(T, i + 1, r.language, true)
186
+ if v and v.symbol then
187
+ add(T:text(lhs), v.symbol)
188
+ local j = lhs
189
+ while j > 2 and T:text(j - 1) == '.' and T.k[j - 2] == 'id' do j = j - 2 end
190
+ if T:text(j - 1) == ':' and T.k[j - 2] == 'id' then add(T:text(j - 2), v.symbol) end
191
+ end
192
+ elseif T:text(i - 1) == ')' and T.m[i - 1] then
193
+ local p = T.m[i - 1]
194
+ if T:text(p - 1) == ')' and T.k[p - 2] == 'id' and T:text(p - 3) == '*'
195
+ and T:text(p - 4) == '(' then
196
+ local v = simple(T, i + 1, r.language, true)
197
+ if v and v.symbol then add(T:text(p - 2), v.symbol) end
198
+ end
199
+ end
200
+ end
201
+ end
202
+ for _, imp in ipairs(imports(r, T)) do
203
+ if imp.path:match('^[%a_][%w_%.]*$') and not (imp.imported == '*' and imp.name == imp.path) then
204
+ add(imp.name, imp.path .. (imp.imported ~= '*' and '.' .. imp.imported or ''))
205
+ end
206
+ end
207
+ local seen = {}
208
+ local function visit(n, depth)
209
+ if seen[n] and seen[n] <= depth or depth > 8 then return end
210
+ seen[n] = depth
211
+ for _, v in ipairs(values[n] or {}) do
212
+ calls['@' .. v] = true
213
+ visit(v:match('^([%w_$]+)'), depth + 1)
214
+ end
215
+ end
216
+ for n in pairs(roots) do visit(n, 0) end
217
+ end
218
+
219
+ function M.prepare(r, T)
220
+ if not supported[r.language] then return function(v) return v end end
221
+ local lua, python = r.language == 'lua', r.language == 'python'
222
+ local js = r.language == 'javascript' or r.language == 'typescript'
223
+ local scopes, ends, stack = {}, { [0] = T.n }, {}
224
+ local opens = { ['function'] = true, ['if'] = true, ['do'] = true, ['repeat'] = true }
225
+ for i = 1, T.n do
226
+ local s = T:text(i)
227
+ if lua and T.k[i] == 'kw' and (s == 'end' or s == 'until') then
228
+ local top = table.remove(stack); if top then ends[top] = i end
229
+ elseif not lua and (python and T.k[i] == 'dedent' or not python and s == '}') then table.remove(stack) end
230
+ scopes[i] = stack[#stack] or 0
231
+ if lua and T.k[i] == 'kw' and opens[s] or python and T.k[i] == 'indent'
232
+ or not lua and not python and s == '{' then
233
+ stack[#stack + 1] = i
234
+ if not lua then ends[i] = T.m[i] or T.n end
235
+ end
236
+ end
237
+ local bindings, imported, callable_declarations = {}, {}, {}
238
+ local function function_scope(at)
239
+ local owner
240
+ for _, n in ipairs(r.nodes) do
241
+ if callable[n.kind] and n.ti <= at and at <= n.tj and (not owner or n.ti > owner.ti) then owner = n end
242
+ end
243
+ return owner and owner.ti or 0, owner and owner.tj or T.n
244
+ end
245
+ local function add(n, token, scope, v, finish)
246
+ local b = { name = n, token = token, scope = scope, finish = finish or ends[scope] or T.n, value = v }
247
+ bindings[n] = bindings[n] or {}; bindings[n][#bindings[n] + 1] = b
248
+ return b
249
+ end
250
+ local function binding(n, at)
251
+ local best
252
+ for _, b in ipairs(bindings[n] or {}) do
253
+ if b.scope <= at and at <= b.finish and (b.token <= at or js or python)
254
+ and (not best or b.scope > best.scope or b.scope == best.scope and b.token > best.token) then best = b end
255
+ end
256
+ return best
257
+ end
258
+ for _, imp in ipairs(imports(r, T)) do
259
+ imported[imp.token] = true
260
+ local scope, finish = scopes[imp.token], nil
261
+ if js then scope = 0 elseif python then scope, finish = function_scope(imp.token) end
262
+ add(imp.name, imp.token, scope, { import = imp, symbol = imp.name }, finish)
263
+ end
264
+ -- Function declarations and all header identifiers provide lexical barriers
265
+ -- for parameters, including unsupported typed/destructured parameter forms.
266
+ for id, n in ipairs(r.nodes) do
267
+ if callable[n.kind] then
268
+ if js and n.kind == 'function' then
269
+ for j = n.ti, math.min(n.tj, n.ti + 4) do
270
+ local prev = T:text(j - 1)
271
+ if T:text(j) == n.name and (prev == 'const' or prev == 'let' or prev == 'var') then
272
+ callable_declarations[j] = id; break
273
+ end
274
+ end
275
+ end
276
+ local p = n.ti
277
+ while p <= n.tj and T:text(p) ~= '(' and T:text(p) ~= '{' and T:text(p) ~= '=>' do p = p + 1 end
278
+ if T:text(p) == '(' and T.m[p] then
279
+ for j = p + 1, T.m[p] - 1 do if T.k[j] == 'id' then add(T:text(j), j, n.ti, false, n.tj) end end
280
+ if lua then
281
+ for j = n.ti, p - 1 do
282
+ if T:text(j) == ':' then add('self', p, n.ti, false, n.tj); break end
283
+ end
284
+ end
285
+ elseif T:text(p) == '=>' and T.k[p - 1] == 'id' then add(T:text(p - 1), p - 1, n.ti, false, n.tj) end
286
+ -- Locally declared named functions are exact targets, not a global
287
+ -- name-only guess. Lua local functions start at the `local` token.
288
+ if n.qualified == n.name or n.parent and (lua or python) then
289
+ add(n.name, n.ti, scopes[n.ti], { node = id, symbol = n.name })
290
+ end
291
+ end
292
+ end
293
+ local declaration = {}
294
+ for i = 1, T.n do
295
+ if i % 4096 == 0 then control.check() end
296
+ local word = T:text(i)
297
+ if js and (word == 'const' or word == 'let' or word == 'var')
298
+ and (T:text(i + 1) == '{' or T:text(i + 1) == '[') and T.m[i + 1] then
299
+ for j = i + 2, T.m[i + 1] - 1 do
300
+ if T.k[j] == 'id' then add(T:text(j), j, scopes[i], false) end
301
+ end
302
+ elseif lua and word == 'local' and T.k[i + 1] == 'id' and T:text(i + 2) == ',' then
303
+ local j = i + 1
304
+ while T.k[j] == 'id' do
305
+ add(T:text(j), j, scopes[i], false)
306
+ if T:text(j + 1) ~= ',' then break end
307
+ j = j + 2
308
+ end
309
+ elseif (lua or python) and word == 'for' then
310
+ local scope, finish
311
+ if lua then
312
+ scope = i + 1
313
+ while scope <= T.n and T:text(scope) ~= 'do' do
314
+ if T.m[scope] and T.m[scope] > scope then scope = T.m[scope] end
315
+ scope = scope + 1
316
+ end
317
+ finish = ends[scope]
318
+ else scope, finish = function_scope(i) end
319
+ local j = i + 1
320
+ while T.k[j] == 'id' do
321
+ add(T:text(j), j, scope, false, finish)
322
+ if T:text(j + 1) ~= ',' then break end
323
+ j = j + 2
324
+ end
325
+ elseif python and word == 'as' and T.k[i + 1] == 'id' and not imported[i + 1] then
326
+ local scope, finish = function_scope(i)
327
+ add(T:text(i + 1), i + 1, scope, false, finish)
328
+ elseif not lua and not python and word == 'catch' and T:text(i + 1) == '(' and T.m[i + 1] then
329
+ local close = T.m[i + 1]
330
+ local scope = T:text(close + 1) == '{' and close + 1 or scopes[i]
331
+ for j = i + 2, close - 1 do
332
+ if T.k[j] == 'id' then add(T:text(j), j, scope, false) end
333
+ end
334
+ elseif python and (word == 'global' or word == 'nonlocal' or word == 'del') then
335
+ local j, scope, finish = i + 1, function_scope(i)
336
+ while T.k[j] == 'id' do
337
+ add(T:text(j), j, scope, false, finish)
338
+ if T:text(j + 1) ~= ',' then break end
339
+ j = j + 2
340
+ end
341
+ end
342
+ if T.k[i] == 'id' and not imported[i] then
343
+ local prev, next = T:text(i - 1), T:text(i + 1)
344
+ local eq = initializer(T, i, js)
345
+ local local_decl = lua and prev == 'local' or js and (prev == 'const' or prev == 'let' or prev == 'var')
346
+ if python and (next == '=' or next == ':') and not member[prev] then local_decl = true end
347
+ if (r.language == 'c' or r.language == 'cpp') and eq
348
+ and (T.k[i - 1] == 'id' or T.k[i - 1] == 'kw' or prev == '*') then local_decl = true end
349
+ if (r.language == 'c' or r.language == 'cpp') and (next == ';' or next == ',')
350
+ and (T.k[i - 1] == 'id' or prev == '*') then local_decl = true end
351
+ if local_decl then
352
+ declaration[i] = true
353
+ local scope, finish = scopes[i], nil
354
+ if python or js and prev == 'var' then scope, finish = function_scope(i) end
355
+ local v = eq and simple(T, eq + 1, r.language) or false
356
+ if callable_declarations[i] then v = { node = callable_declarations[i], symbol = T:text(i) } end
357
+ if lua and eq and not v then v = service_initializer(T, eq + 1) or false end
358
+ if lua and eq and T:text(eq + 1) == '{' then v = { namespace = true } end
359
+ local b = add(T:text(i), i, scope, v, finish)
360
+ if python or js and prev == 'var' then
361
+ for _, other in ipairs(bindings[b.name]) do
362
+ if other ~= b and other.scope == b.scope then other.invalid, b.invalid = true, true end
363
+ end
364
+ end
365
+ end
366
+ end
367
+ end
368
+ local service_writes = {}
369
+ -- Any later write poisons that lexical binding for this whole query. This
370
+ -- deliberately avoids inventing execution order across functions/branches.
371
+ for i = 1, T.n do
372
+ if js and (T:text(i) == '{' or T:text(i) == '[') and T.m[i] and T:text(T.m[i] + 1) == '=' then
373
+ for j = i + 1, T.m[i] - 1 do
374
+ local b = T.k[j] == 'id' and binding(T:text(j), i)
375
+ if b then b.invalid = true end
376
+ end
377
+ end
378
+ if T.k[i] == 'id' and not declaration[i] and not imported[i] and not member[T:text(i - 1)] then
379
+ local tail = lvalue_end(T, i)
380
+ local next = T:text(tail)
381
+ local last = tail
382
+ if lua or python then
383
+ while last and T:text(last) == ',' do last = lvalue_end(T, last + 1) end
384
+ end
385
+ if next == '=' or next:match('^[+%-%*/%%&|%^]=') or next == '++' or next == '--'
386
+ or last and T:text(last) == '=' or T:text(i - 1) == '++' or T:text(i - 1) == '--' then
387
+ local b = binding(T:text(i), i)
388
+ if b then b.invalid = true end
389
+ if lua then
390
+ local name = T:text(i)
391
+ local value = next == '=' and tail == i + 1 and T:text(i - 1) ~= ','
392
+ and (service_initializer(T, i + 2) or simple(T, i + 2, 'lua')) or false
393
+ service_writes[name] = service_writes[name] or {}
394
+ service_writes[name][#service_writes[name] + 1] = {
395
+ binding = b, token = i, scope = scopes[i], value = value,
396
+ }
397
+ end
398
+ end
399
+ end
400
+ end
401
+ local expand
402
+ expand = function(v, at, depth)
403
+ if not v or not v.symbol then return v end
404
+ depth = depth or 0
405
+ if depth > 8 then return false end
406
+ local head, suffix = v.symbol:match('^([%w_$]+)(.*)$')
407
+ local b = binding(head, at)
408
+ -- A single later initialization of a nil/uninitialized handle is useful
409
+ -- for navigation. Keep ordinary aliases poisoned, and require assignment
410
+ -- and use in the same block; no branch or cross-function ordering guesses.
411
+ if suffix == '' and (not b or not b.value) then
412
+ local write
413
+ for _, candidate in ipairs(service_writes[head] or {}) do
414
+ if candidate.binding == b then
415
+ if write then write = false; break end
416
+ write = candidate
417
+ end
418
+ end
419
+ if write and write.token < at and write.scope == scopes[at] and write.value then
420
+ local value = write.value
421
+ if value.factory then
422
+ local factory = expand(value.factory, value.token, depth + 1)
423
+ if is_service_factory(factory) then
424
+ return { service = value.service, token = write.token }
425
+ end
426
+ else
427
+ local resolved = expand(value, write.token, depth + 1)
428
+ if resolved and resolved.service then return resolved end
429
+ end
430
+ end
431
+ end
432
+ if not b then return v end
433
+ if b.invalid or not b.value or b.token > at and not b.value.node then return false end
434
+ local target = b.value
435
+ if target.factory then
436
+ if suffix ~= '' then return false end
437
+ local factory = expand(target.factory, target.token, depth + 1)
438
+ if is_service_factory(factory) then
439
+ return { service = target.service, token = b.token }
440
+ end
441
+ return false
442
+ end
443
+ if target.namespace then
444
+ local short = suffix:match('^[%.:]([%w_]+)$')
445
+ if not short then return false end
446
+ b.members = b.members or require('codeoutline.lua_bindings').local_members(r.nodes, scopes,
447
+ b.name, b.token, b.scope, b.finish)
448
+ local id = b.members[short]
449
+ return id and { node = id, symbol = r.nodes[id].qualified } or false
450
+ end
451
+ if target.import then
452
+ local imp = target.import
453
+ if imp.loader and binding(imp.loader, imp.token) then return false end
454
+ if imp.imported == 'default' and suffix ~= '' then return false end
455
+ local part = imp.imported ~= '*' and imp.imported ~= 'default' and imp.imported or ''
456
+ if imp.imported == 'default' then part = 'default' end
457
+ if suffix ~= '' then part = part .. (part == '' and suffix:sub(2) or suffix) end
458
+ local canonical = imp.path:match('^[%a_][%w_%.]*$') and imp.path .. (part ~= '' and '.' .. part or '')
459
+ return { symbol = canonical or v.symbol, import = imp, member = part, token = imp.token }
460
+ end
461
+ if target.node then
462
+ if suffix ~= '' then return false end
463
+ return { symbol = target.symbol, node = target.node, token = b.token }
464
+ end
465
+ if suffix ~= '' then
466
+ if not target.symbol or target.literal then return false end
467
+ return expand({ symbol = target.symbol .. suffix }, b.token - 1, depth + 1)
468
+ end
469
+ return expand(target, b.token - 1, depth + 1)
470
+ end
471
+ return function(v, at) return expand(v, at, 0) end
472
+ end
473
+
474
+ return M
@@ -141,11 +141,21 @@ function Doc:tokens()
141
141
  return tokens
142
142
  end
143
143
 
144
- function M.read(path)
144
+ -- Transient sites use the same alias/literal resolver as relationship queries.
145
+ function Doc:dispatch(names)
146
+ return parse.parse(parser_name(self), self.source, { dispatch = names, dispatch_only = true })
147
+ end
148
+
149
+ -- An indexed target must still match its raw on-disk snapshot before decoding.
150
+ function M.read(path, indexed)
145
151
  local f = io.open(path, 'rb')
146
152
  if not f then return nil end
147
153
  local raw = f:read(M.MAX_BYTES + 1); f:close()
148
154
  if not raw or #raw > M.MAX_BYTES then return nil end
155
+ if indexed then
156
+ local crc = xcompress and xcompress.crc32 and xcompress.crc32(raw) or xutils.sha256_hex(raw)
157
+ assert(#raw == indexed.size and crc == indexed.crc, 'source changed during query; retry: ' .. indexed.path)
158
+ end
149
159
  local source = xutils.to_utf8(raw)
150
160
  if not source then return nil end
151
161
  return M.new(M.uri(path), source)
@@ -214,11 +214,14 @@ end
214
214
  -- Shortest call path a -> b (out edges), up to depth; returns id list.
215
215
  local function path_between(G, a, b, depth)
216
216
  local prev, frontier = { [a] = a }, { a }
217
+ local expansion_limited = false
217
218
  for _ = 1, depth do
218
219
  local next_frontier = {}
219
220
  for _, x in ipairs(frontier) do
220
221
  control.check()
221
- local out = G.out[x] or {}
222
+ local out = G.out[x]
223
+ if out == nil and G.incomplete then expansion_limited = true end
224
+ out = out or {}
222
225
  for i = 1, #out, graph.EDGE do
223
226
  if i % 128 == 1 then control.check() end
224
227
  local dst = out[i]
@@ -234,10 +237,10 @@ local function path_between(G, a, b, depth)
234
237
  end
235
238
  end
236
239
  frontier = next_frontier
237
- if #frontier == 0 then return nil end
238
- if #frontier > 20000 then return nil, true end
240
+ if #frontier == 0 then return nil, false, prev, expansion_limited end
241
+ if #frontier > 20000 then return nil, true, prev, expansion_limited end
239
242
  end
240
- return nil, #frontier > 0
243
+ return nil, #frontier > 0, prev, expansion_limited
241
244
  end
242
245
 
243
246
  local file_cache = {}
@@ -282,7 +285,7 @@ local function node_block(G, idx, id, full)
282
285
  for _, k in ipairs(kids) do
283
286
  local kn = G.nodes[k]
284
287
  if not rows[kn.line] then row_lines[#row_lines + 1] = kn.line end
285
- rows[kn.line] = kn.sig or kn.name
288
+ rows[kn.line] = require('codeoutline.navigation').detail(G, G.node_file[k], kn).sig or kn.name
286
289
  end
287
290
  for _, ln in ipairs(row_lines) do
288
291
  out[#out + 1] = string.format('%d\t %s', ln, rows[ln])
@@ -338,13 +341,17 @@ function M.run(G, idx, query, opts)
338
341
 
339
342
  -- Spine: nodes on call paths between seeds.
340
343
  local flows, spine, on_spine = {}, {}, {}
344
+ local reports = require('codeoutline.query_report')
345
+ local searches, missed, connected = {}, {}, {}
341
346
  local path_limited = false
342
347
  for _, a in ipairs(seeds) do
343
348
  for _, b in ipairs(seeds) do
344
349
  if a ~= b then
345
- local p, limited = path_between(G, a, b, M.PATH_DEPTH)
350
+ local p, limited, prev, expansion_limited = path_between(G, a, b, M.PATH_DEPTH)
346
351
  path_limited = path_limited or limited or false
347
352
  if p then
353
+ connected[a] = connected[a] or {}
354
+ connected[a][b] = true
348
355
  local names = {}
349
356
  for _, id in ipairs(p) do
350
357
  names[#names + 1] = G.nodes[id].name
@@ -354,6 +361,13 @@ function M.run(G, idx, query, opts)
354
361
  end
355
362
  end
356
363
  flows[#flows + 1] = table.concat(names, ' -> ')
364
+ else
365
+ if not missed[a] then
366
+ missed[a] = { root = a, targets = {}, limited = limited, incomplete = expansion_limited,
367
+ sites = reports.capture_paths(G.report, prev, a) }
368
+ searches[#searches + 1] = missed[a]
369
+ end
370
+ missed[a].targets[#missed[a].targets + 1] = b
357
371
  end
358
372
  end
359
373
  end
@@ -422,6 +436,11 @@ function M.run(G, idx, query, opts)
422
436
  end
423
437
 
424
438
  if G.load_incoming then G:load_incoming(seeds) end
439
+ if #seeds == 1 then
440
+ searches[1] = { root = seeds[1], targets = {}, single = true,
441
+ sites = reports.capture_paths(G.report, { [seeds[1]] = seeds[1] }, seeds[1]) }
442
+ end
443
+ local breakpoints = reports.path_breakpoints(G.report, G, searches, connected)
425
444
 
426
445
  -- Confirm every file whose source or symbol labels this query can emit.
427
446
  -- GBK reparsing invalidates node IDs, so rebuild and select again before
@@ -434,6 +453,10 @@ function M.run(G, idx, query, opts)
434
453
  for i = 1, #edges, graph.EDGE do selected[G.node_file[edges[i]]] = true end
435
454
  end
436
455
  end
456
+ for _, entry in ipairs(breakpoints.entries) do
457
+ for _, id in ipairs(entry.path) do selected[G.node_file[id]] = true end
458
+ if entry.goal then selected[G.node_file[entry.goal]] = true end
459
+ end
437
460
  for f in pairs(selected) do
438
461
  local path = G.files[f].path -- a decoding repair replaces this file ID
439
462
  local source, repaired = idx:query_source(path)
@@ -442,8 +465,15 @@ function M.run(G, idx, query, opts)
442
465
  end
443
466
  if changed then return nil, { encoding_retry = true } end
444
467
 
445
- local evidence_text = require('codeoutline.query_report').render(G.report, G)
468
+ local evidence_text = reports.render(G.report, G)
446
469
  local evidence_budget = math.min(4096, math.floor(budget / 4))
470
+ local breakpoint_text = reports.render_breakpoints(breakpoints, G)
471
+ local breakpoint_budget = breakpoint_text and math.floor(evidence_budget / 2) or 0
472
+ evidence_budget = evidence_budget - breakpoint_budget
473
+ local breakpoint_cut = breakpoint_text and (#breakpoint_text > breakpoint_budget or breakpoints.truncated) or false
474
+ if breakpoint_budget >= 256 then emit(text.limit(breakpoint_text, breakpoint_budget, breakpoints.truncated))
475
+ elseif breakpoint_text then breakpoint_cut = true end
476
+ omitted = omitted or breakpoint_cut
447
477
  local evidence_cut = #evidence_text > evidence_budget
448
478
  if evidence_budget >= 256 then
449
479
  emit(text.limit(evidence_text, evidence_budget, G.report.truncated))
@@ -461,7 +491,7 @@ function M.run(G, idx, query, opts)
461
491
  local ol = {}
462
492
  for _, node in ipairs(rec.nodes) do
463
493
  if not node.parent and node.kind ~= 'macro' then
464
- ol[#ol + 1] = string.format('%d\t%s %s', node.line, node.kind, node.sig or node.name)
494
+ ol[#ol + 1] = string.format('%d\t%s %s', node.line, node.kind, require('codeoutline.navigation').detail(G, f, node).sig or node.name)
465
495
  end
466
496
  if #ol >= 60 then ol[#ol + 1] = '\t...'; break end
467
497
  end
@@ -548,11 +578,15 @@ function M.run(G, idx, query, opts)
548
578
  if evidence_cut or G.report.truncated then
549
579
  table.insert(parts, 1, 'Relationship explanations omitted due to evidence/output limits.')
550
580
  end
581
+ if breakpoint_cut then
582
+ table.insert(parts, 1, 'Call-path breakpoint explanations omitted due to site/evidence/output limits.')
583
+ end
551
584
  local result, truncated = text.limit(table.concat(parts, '\n'), budget, omitted or #pointers > 0)
552
585
  return result, { seeds = seeds, spine = spine, shown = shown, flows = flows, truncated = truncated,
553
586
  relationships_incomplete = G.incomplete or false, query_stats = G.stats,
554
587
  evidence = G.report.entries, evidence_truncated = evidence_cut or G.report.truncated,
555
- path_search_limited = path_limited, evidence_scope = G.report.scope }
588
+ path_search_limited = path_limited, evidence_scope = G.report.scope,
589
+ path_breakpoints = breakpoints.entries, path_breakpoints_truncated = breakpoint_cut }
556
590
  end
557
591
 
558
592
  return M