@codua/codeoutline-win32-x64 0.2.4 → 0.2.9

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.
Files changed (77) hide show
  1. package/README.md +205 -3
  2. package/README.zh-CN.md +40 -2
  3. package/bin/xnet.exe +0 -0
  4. package/build-info.json +1 -1
  5. package/package.json +1 -1
  6. package/scripts/codeoutline/affected.lua +145 -0
  7. package/scripts/codeoutline/automatic_events.lua +202 -0
  8. package/scripts/codeoutline/cache_repairs.lua +47 -0
  9. package/scripts/codeoutline/callers.lua +56 -16
  10. package/scripts/codeoutline/cargo_toml.lua +125 -0
  11. package/scripts/codeoutline/command.lua +26 -4
  12. package/scripts/codeoutline/common.lua +65 -9
  13. package/scripts/codeoutline/completion.lua +221 -65
  14. package/scripts/codeoutline/coverage.lua +63 -0
  15. package/scripts/codeoutline/cpp_bindings.lua +151 -0
  16. package/scripts/codeoutline/cpp_fragments.lua +143 -0
  17. package/scripts/codeoutline/cpp_implementations.lua +340 -0
  18. package/scripts/codeoutline/cpp_includes.lua +156 -0
  19. package/scripts/codeoutline/cpp_macros.lua +279 -0
  20. package/scripts/codeoutline/cpp_receivers.lua +342 -0
  21. package/scripts/codeoutline/cpp_types.lua +456 -0
  22. package/scripts/codeoutline/csharp_bindings.lua +498 -0
  23. package/scripts/codeoutline/definition_candidates.lua +180 -0
  24. package/scripts/codeoutline/dispatch_aliases.lua +479 -0
  25. package/scripts/codeoutline/documents.lua +32 -9
  26. package/scripts/codeoutline/explore.lua +274 -119
  27. package/scripts/codeoutline/external_headers.lua +171 -0
  28. package/scripts/codeoutline/foreign.lua +79 -14
  29. package/scripts/codeoutline/foreign_lua.lua +146 -19
  30. package/scripts/codeoutline/function_values.lua +69 -0
  31. package/scripts/codeoutline/go_methods.lua +931 -0
  32. package/scripts/codeoutline/go_modules.lua +329 -0
  33. package/scripts/codeoutline/graph.lua +346 -63
  34. package/scripts/codeoutline/implementations.lua +403 -0
  35. package/scripts/codeoutline/index.lua +165 -27
  36. package/scripts/codeoutline/index_worker.lua +11 -4
  37. package/scripts/codeoutline/indirect.lua +116 -40
  38. package/scripts/codeoutline/js_evidence.lua +32 -3
  39. package/scripts/codeoutline/js_resolution.lua +72 -10
  40. package/scripts/codeoutline/js_workspace.lua +325 -0
  41. package/scripts/codeoutline/lang/c.lua +15 -2
  42. package/scripts/codeoutline/lang/cpp.lua +7 -4
  43. package/scripts/codeoutline/lang/csharp.lua +15 -3
  44. package/scripts/codeoutline/lang/js.lua +56 -20
  45. package/scripts/codeoutline/lang/rust.lua +9 -1
  46. package/scripts/codeoutline/language_detect.lua +100 -0
  47. package/scripts/codeoutline/lsp.lua +173 -23
  48. package/scripts/codeoutline/lua_bindings.lua +228 -54
  49. package/scripts/codeoutline/lua_callback_loops.lua +210 -0
  50. package/scripts/codeoutline/lua_literal.lua +38 -0
  51. package/scripts/codeoutline/main.lua +32 -7
  52. package/scripts/codeoutline/mcp.lua +52 -9
  53. package/scripts/codeoutline/navigation.lua +593 -147
  54. package/scripts/codeoutline/on_demand.lua +93 -0
  55. package/scripts/codeoutline/ordinary_bindings.lua +396 -0
  56. package/scripts/codeoutline/parse.lua +18 -2
  57. package/scripts/codeoutline/parse_pool.lua +1 -1
  58. package/scripts/codeoutline/parse_worker.lua +1 -1
  59. package/scripts/codeoutline/python_bindings.lua +112 -0
  60. package/scripts/codeoutline/query_filters.lua +65 -0
  61. package/scripts/codeoutline/query_report.lua +135 -8
  62. package/scripts/codeoutline/resolution.lua +23 -0
  63. package/scripts/codeoutline/rules.lua +127 -0
  64. package/scripts/codeoutline/rust_packages.lua +203 -0
  65. package/scripts/codeoutline/rust_resolution.lua +301 -0
  66. package/scripts/codeoutline/search_ranking.lua +70 -0
  67. package/scripts/codeoutline/service.lua +47 -7
  68. package/scripts/codeoutline/shared_events.lua +369 -0
  69. package/scripts/codeoutline/static_dispatch.lua +569 -0
  70. package/scripts/codeoutline/text.lua +3 -2
  71. package/scripts/codeoutline/ts_implementations.lua +81 -0
  72. package/scripts/codeoutline/type_bases.lua +61 -0
  73. package/scripts/codeoutline/type_hierarchy.lua +324 -0
  74. package/scripts/codeoutline/type_references.lua +290 -0
  75. package/scripts/codeoutline/type_uses.lua +260 -0
  76. package/scripts/codeoutline/update_sequence.lua +1 -1
  77. package/scripts/codeoutline/version.lua +1 -1
@@ -0,0 +1,569 @@
1
+ -- Bounded static registrations, derived on demand; never evaluate source.
2
+ local M = {}
3
+ local control = require('codeoutline.control')
4
+ local callable = { ['function'] = true, method = true, constructor = true }
5
+
6
+ local function site_key(from, line, recv, name)
7
+ return from .. ':' .. line .. ':' .. tostring(recv) .. ':' .. tostring(name)
8
+ end
9
+
10
+ local function literal(T, i)
11
+ local s = T:text(i)
12
+ if T.k[i] == 'str' and (s:sub(1, 1) == '"' or s:sub(1, 1) == "'")
13
+ and s:sub(-1) == s:sub(1, 1) and not s:find('\\', 1, true) then return s:sub(2, -2) end
14
+ end
15
+
16
+ -- Necessary syntax only: leave scope, shadowing and registration validation to
17
+ -- the collector. Token checks work identically with native and Lua scanners.
18
+ local function has_dispatch(T, lua)
19
+ local declared, called = {}, {}
20
+ for i = 1, T.n do
21
+ if i % 4096 == 0 then control.check() end
22
+ if lua then
23
+ if T.k[i] == 'kw' and T:text(i) == 'local' then
24
+ local j = i + 1
25
+ while T.k[j] == 'id' do
26
+ if T:text(j + 1) == '=' and T:text(j + 2) == '{' then declared[T:text(j)] = true end
27
+ -- A necessary-syntax prefilter only; lexical validity is
28
+ -- checked below. Include aliases so they cannot hide sites.
29
+ if T:text(j + 1) == '=' and T.k[j + 2] == 'id' and declared[T:text(j + 2)] then
30
+ declared[T:text(j)] = true
31
+ end
32
+ if T:text(j + 1) ~= ',' then break end
33
+ j = j + 2
34
+ end
35
+ elseif T.k[i] == 'id' then
36
+ local tail
37
+ if T:text(i + 1) == '.' and T.k[i + 2] == 'id' then tail = i + 3
38
+ elseif T:text(i + 1) == '[' and T.m[i + 1] then tail = T.m[i + 1] + 1 end
39
+ if tail and T:text(tail) == '(' then called[T:text(i)] = true end
40
+ end
41
+ else
42
+ if T.k[i] == 'kw' and (T:text(i) == 'struct' or T:text(i) == 'class')
43
+ and T.k[i + 1] == 'id' and T:text(i + 2) == '{' and T.m[i + 2] then
44
+ for j = i + 3, T.m[i + 2] - 1 do
45
+ if j % 4096 == 0 then control.check() end
46
+ if T.k[j] == 'id' and T:text(j - 1) == '*' and T:text(j - 2) == '('
47
+ and T:text(j + 1) == ')' and T:text(j + 2) == '(' then declared[T:text(j)] = true end
48
+ end
49
+ elseif T.k[i] == 'id' and (T:text(i - 1) == '.' or T:text(i - 1) == '->') then
50
+ if T:text(i + 1) == '(' or T:text(i + 1) == ')' and T:text(i + 2) == '(' then
51
+ called[T:text(i)] = true
52
+ end
53
+ end
54
+ end
55
+ end
56
+ for name in pairs(declared) do if called[name] then return true end end
57
+ return false
58
+ end
59
+
60
+ function M.collect(r, T)
61
+ local lua = r.language == 'lua'
62
+ if not lua and r.language ~= 'c' and r.language ~= 'cpp' then return end
63
+ local executable
64
+ for _, node in ipairs(r.nodes) do if callable[node.kind] then executable = true; break end end
65
+ if not executable then return end
66
+ local loops = lua and r.ordinary_bindings and require('codeoutline.lua_callback_loops').tokens_candidate(T)
67
+ -- Reject cheap negatives before collecting compact query hints.
68
+ for i = 1, T.n do
69
+ if i % 4096 == 0 then control.check() end
70
+ local registration
71
+ if T.k[i] == 'op' and lua and T:text(i) == '=' and T.k[i + 1] == 'id' then
72
+ local prev, before = T:text(i - 1), T:text(i - 2)
73
+ registration = prev == ']' or T.k[i - 1] == 'id'
74
+ and (before == '.' or before == '{' or before == ',' or before == ';')
75
+ end
76
+ if registration or (not lua and T.k[i] == 'op' and T:text(i) == '*' and T:text(i - 1) == '(') then
77
+ r.static_dispatch_candidate = true; break
78
+ end
79
+ end
80
+ if not r.static_dispatch_candidate and not loops then return end
81
+ if r.static_dispatch_candidate and not has_dispatch(T, lua) then
82
+ r.static_dispatch_candidate = nil
83
+ if not loops then return end
84
+ end
85
+ -- Full indexing retains only a syntax candidate. Scope, registry writes
86
+ -- and resolved registrations belong to an open-file/query parse.
87
+ if not r.collect_static_dispatch and (r.defer_analysis or not r.ordinary_bindings and not r.ranges) then
88
+ local targets = {}
89
+ for _, i in ipairs(T:idents(1, T.n)) do
90
+ local prev = T:text(i - 1)
91
+ if prev == '=' or prev == '&' or prev == '{' or prev == ',' then targets[T:text(i)] = true end
92
+ end
93
+ r.static_dispatch_candidate = { targets = targets, syntax = true }
94
+ return
95
+ end
96
+ local scopes, ends, stack, owners = {}, { [0] = T.n }, {}, {}
97
+ local opens = { ['function'] = true, ['if'] = true, ['do'] = true, ['repeat'] = true }
98
+ for i = 1, T.n do
99
+ local s = T:text(i)
100
+ if lua and T.k[i] == 'kw' and (s == 'end' or s == 'until') then
101
+ local top = table.remove(stack); if top then ends[top] = i end
102
+ elseif not lua and s == '}' then table.remove(stack) end
103
+ scopes[i] = stack[#stack] or 0
104
+ if (lua and T.k[i] == 'kw' and opens[s]) or (not lua and s == '{') then
105
+ stack[#stack + 1] = i
106
+ if not lua then ends[i] = T.m[i] or T.n end
107
+ end
108
+ end
109
+ -- Only executable owners: an initializer belongs to no caller function.
110
+ local ordered = {}
111
+ for id, n in ipairs(r.nodes) do if callable[n.kind] then ordered[#ordered + 1] = id end end
112
+ local functions_by_token
113
+ if loops then
114
+ functions_by_token = {}
115
+ for _, id in ipairs(ordered) do functions_by_token[r.nodes[id].ti] = id end
116
+ end
117
+ table.sort(ordered, function(a, b) return r.nodes[a].ti < r.nodes[b].ti end)
118
+ local cursor = 1; stack = {}
119
+ for i = 1, T.n do
120
+ while ordered[cursor] and r.nodes[ordered[cursor]].ti <= i do
121
+ stack[#stack + 1] = ordered[cursor]; cursor = cursor + 1
122
+ end
123
+ while stack[#stack] and r.nodes[stack[#stack]].tj < i do table.remove(stack) end
124
+ owners[i] = stack[#stack] or 0
125
+ end
126
+ local data = { sites = {} }
127
+ r.static_dispatch = data
128
+ local bindings, structs = {}, {}
129
+ local function bind(name, token, scope, value)
130
+ value = value or {}
131
+ value.token, value.scope, value.finish = token, scope, ends[scope] or T.n
132
+ bindings[name] = bindings[name] or {}
133
+ for i, previous in ipairs(bindings[name]) do
134
+ if previous.token == token and previous.scope == scope then bindings[name][i] = value; return value end
135
+ end
136
+ table.insert(bindings[name], value)
137
+ return value
138
+ end
139
+ local function binding(name, token)
140
+ local best
141
+ for _, b in ipairs(bindings[name] or {}) do
142
+ if b.token <= token and b.scope <= token and b.finish >= token
143
+ and (not best or b.scope > best.scope or b.scope == best.scope and b.token > best.token) then best = b end
144
+ end
145
+ return best
146
+ end
147
+ local function function_target(name, at, depth)
148
+ depth = depth or 0
149
+ if depth > 8 then return nil end
150
+ local b = binding(name, at)
151
+ if b then
152
+ if b.alias then
153
+ if b.invalid then return nil end
154
+ return function_target(b.alias, b.token - 1, depth + 1)
155
+ end
156
+ if depth > 0 and b.invalid then return nil end
157
+ if not b.function_name then return nil end
158
+ if functions_by_token then
159
+ local id = functions_by_token[b.token]
160
+ return id and r.nodes[id].name == name and id or nil
161
+ end
162
+ for id, n in ipairs(r.nodes) do
163
+ if callable[n.kind] and n.name == name and n.ti == b.token then return id end
164
+ end
165
+ return nil
166
+ end
167
+ -- Alias propagation is limited to lexical locals. Global functions can
168
+ -- be replaced outside this file and have no stable local binding.
169
+ if lua and depth > 0 then return nil end
170
+ local best, best_scope
171
+ for id, n in ipairs(r.nodes) do
172
+ local scope = scopes[n.ti] or 0
173
+ if callable[n.kind] and n.name == name and n.qualified == name
174
+ and scope <= at and (ends[scope] or T.n) >= at and (not lua or n.ti < at)
175
+ and (not best_scope or scope > best_scope or scope == best_scope and n.ti > r.nodes[best].ti) then best, best_scope = id, scope end
176
+ end
177
+ return best
178
+ end
179
+ local function register(bucket, key, a, b, site)
180
+ -- Anonymous/named table functions already belong to the ordinary Lua
181
+ -- binding resolver. Do not replace those edges with an unknown slot.
182
+ if lua and T:text(a) == 'function' then bucket[key] = nil; return end
183
+ if T:text(a) == '&' then a = a + 1 end
184
+ local target = a == b and T.k[a] == 'id' and T:text(a)
185
+ local index = target and function_target(target, a)
186
+ if target and binding(target, a) and not index then target = nil end
187
+ bucket[key] = bucket[key] or {}
188
+ if lua and #bucket[key] > 0 then bucket[key].unknown = true end
189
+ if not target or (lua and not index) then bucket[key].unknown = true; return end
190
+ table.insert(bucket[key], { name = index and r.nodes[index].name or target,
191
+ index = index, line = T:line(site), from = owners[a] })
192
+ end
193
+ local function parts(a, b, separator)
194
+ local result, first, i = {}, a, a
195
+ while i <= b do
196
+ if T:text(i) == separator or lua and T:text(i) == ';' then
197
+ result[#result + 1] = { first, i - 1 }; first = i + 1
198
+ elseif T.m[i] and T.m[i] > i then i = T.m[i]
199
+ elseif lua and ends[i] then i = ends[i] end
200
+ i = i + 1
201
+ end
202
+ if first <= b then result[#result + 1] = { first, b } end
203
+ return result
204
+ end
205
+ local function lvalue_end(i)
206
+ if T.k[i] ~= 'id' then return nil end
207
+ local j = i + 1
208
+ while true do
209
+ if T:text(j) == '.' and T.k[j + 1] == 'id' then j = j + 2
210
+ elseif T:text(j) == '[' and T.m[j] then j = T.m[j] + 1
211
+ else return j end
212
+ end
213
+ end
214
+ local function assignment_end(i)
215
+ local j = lvalue_end(i)
216
+ while j and T:text(j) == ',' do j = lvalue_end(j + 1) end
217
+ return j and T:text(j) == '=' and j or nil
218
+ end
219
+ if lua then
220
+ local statement = { ['local'] = true, ['function'] = true, ['return'] = true,
221
+ ['if'] = true, ['for'] = true, ['while'] = true, ['repeat'] = true,
222
+ ['do'] = true, ['end'] = true, ['else'] = true, ['elseif'] = true,
223
+ ['until'] = true, ['break'] = true, ['goto'] = true }
224
+ local function alias_end(i)
225
+ local next = T:text(i + 1)
226
+ return next == '' or next == ';' or statement[next]
227
+ or T.l[i + 1] > T.l[i] and T.k[i + 1] == 'id'
228
+ end
229
+ -- Lexical local bindings (including unknown parameters) prevent name leaks.
230
+ for i = 1, T.n do
231
+ if T:text(i) == 'function' and T.k[i] == 'kw' then
232
+ local p = i + 1
233
+ while p <= T.n and T:text(p) ~= '(' do p = p + 1 end
234
+ for j = p + 1, (T.m[p] or p) - 1 do
235
+ if T.k[j] == 'id' then bind(T:text(j), j, i) end
236
+ end
237
+ elseif T:text(i) == 'local' and T.k[i] == 'kw' then
238
+ local j = i + 1
239
+ if T:text(j) == 'function' then
240
+ bind(T:text(j + 1), i, scopes[i], { function_name = true })
241
+ else
242
+ while T.k[j] == 'id' do
243
+ local b = bind(T:text(j), j, scopes[i])
244
+ if T:text(j + 1) == '=' and T:text(j + 2) == '{' and T.m[j + 2] then
245
+ b.slots, b.open = {}, j + 2
246
+ elseif T:text(j - 1) == 'local' and T:text(j + 1) == '='
247
+ and T.k[j + 2] == 'id' and alias_end(j + 2) then
248
+ b.alias = T:text(j + 2)
249
+ end
250
+ if T:text(j + 1) ~= ',' then break end
251
+ j = j + 2
252
+ end
253
+ end
254
+ elseif T:text(i) == 'for' and T.k[i] == 'kw' then
255
+ local first, body = i + 1, i + 1
256
+ while body <= T.n and T:text(body) ~= 'do' do
257
+ if T.m[body] and T.m[body] > body then body = T.m[body] end
258
+ body = body + 1
259
+ end
260
+ while T.k[first] == 'id' do
261
+ bind(T:text(first), first, body)
262
+ if T:text(first + 1) ~= ',' then break end
263
+ first = first + 2
264
+ end
265
+ end
266
+ end
267
+ for i = 1, T.n do
268
+ local prev = T:text(i - 1)
269
+ if T.k[i] == 'id' and prev ~= '.' and prev ~= ':' and prev ~= '{' and prev ~= ',' then
270
+ local eq = assignment_end(i)
271
+ local definition = prev == 'function' and T:text(i - 2) ~= 'local' and T:text(i + 1) == '('
272
+ if eq or definition then
273
+ local j = i
274
+ while j < (eq or i + 1) do
275
+ local tail = definition and i + 1 or lvalue_end(j)
276
+ local existing = binding(T:text(j), j)
277
+ if tail == j + 1 and existing and existing.token ~= j and prev ~= 'local' then
278
+ existing.invalid = true
279
+ bind(T:text(j), j, scopes[j])
280
+ end
281
+ j = tail + 1
282
+ end
283
+ end
284
+ end
285
+ end
286
+ -- Share registry state, not copies: writes through any alias must
287
+ -- invalidate the same slots. Resolve at the initializer's lexical point.
288
+ local function registry(b, depth)
289
+ if not b or depth > 8 then return nil end
290
+ if b.slots then return b end
291
+ if b.alias then return registry(binding(b.alias, b.token - 1), depth + 1) end
292
+ end
293
+ for _, list in pairs(bindings) do
294
+ for _, b in ipairs(list) do
295
+ if b.alias then b.registry = registry(b, 0) end
296
+ end
297
+ end
298
+ for _, list in pairs(bindings) do
299
+ for _, b in ipairs(list) do
300
+ if b.slots then
301
+ for _, span in ipairs(parts(b.open + 1, T.m[b.open] - 1, ',')) do
302
+ local a, z, key, eq = span[1], span[2]
303
+ if T.k[a] == 'id' and T:text(a + 1) == '=' then key, eq = T:text(a), a + 1
304
+ elseif T:text(a) == '[' and T.m[a] == a + 2 and T:text(a + 3) == '=' then key, eq = literal(T, a + 1), a + 3 end
305
+ if key then register(b.slots, key, eq + 1, z, a) end
306
+ end
307
+ end
308
+ end
309
+ end
310
+ else
311
+ -- Inline function-pointer declarations only; typedefs/macros need separate evidence.
312
+ for i = 1, T.n - 2 do
313
+ if (T:text(i) == 'struct' or T:text(i) == 'class') and T.k[i + 1] == 'id'
314
+ and T:text(i + 2) == '{' and T.m[i + 2] then
315
+ local fields, slots = {}, {}
316
+ for _, span in ipairs(parts(i + 3, T.m[i + 2] - 1, ';')) do
317
+ local a, z, name, pointer = span[1], span[2]
318
+ for j = a, z do
319
+ if T:text(j) == '(' and T:text(j + 1) == '*' and T.k[j + 2] == 'id'
320
+ and T:text(j + 3) == ')' and T:text(j + 4) == '(' then
321
+ name, pointer = T:text(j + 2), true; break
322
+ end
323
+ if T.k[j] == 'id' then name = T:text(j) end
324
+ end
325
+ fields[#fields + 1] = { name = name, pointer = pointer }
326
+ if pointer then slots[name] = {} end
327
+ end
328
+ local name = T:text(i + 1)
329
+ if structs[name] then structs[name].ambiguous = true
330
+ else structs[name] = { fields = fields, slots = slots } end
331
+ end
332
+ end
333
+ -- Unknown parameters/locals shadow file-scope registries and handler names.
334
+ for _, id in ipairs(ordered) do
335
+ local n = r.nodes[id]
336
+ local p = n.ti
337
+ while p <= n.tj and T:text(p) ~= '(' do p = p + 1 end
338
+ ends[n.ti] = n.tj
339
+ if T.m[p] then
340
+ for _, span in ipairs(parts(p + 1, T.m[p] - 1, ',')) do
341
+ local j = span[2]
342
+ if T.k[j] == 'id' and j > span[1] then bind(T:text(j), j, n.ti) end
343
+ end
344
+ end
345
+ end
346
+ for i = 1, T.n - 1 do
347
+ if owners[i] > 0 and T.k[i] == 'id' and (T:text(i + 1) == '=' or T:text(i + 1) == ';') then
348
+ local j = i - 1
349
+ while T:text(j) == '*' or T:text(j) == '&' do j = j - 1 end
350
+ if (T.k[j] == 'id' or T.k[j] == 'kw') and T:text(j) ~= 'return'
351
+ and (T:text(j - 1) == ';' or T:text(j - 1) == '{') then
352
+ bind(T:text(i), i, scopes[i])
353
+ end
354
+ end
355
+ end
356
+ for i = 1, T.n - 1 do
357
+ local ty = structs[T:text(i)]
358
+ if ty and not ty.ambiguous and (T.k[i] == 'id') then
359
+ local j = i + 1
360
+ while T:text(j) == '*' or T:text(j) == '&' or T:text(j) == 'const' do j = j + 1 end
361
+ if T.k[j] == 'id' and (T:text(j + 1) == '=' or T:text(j + 1) == ';'
362
+ or T:text(j + 1) == ',' or T:text(j + 1) == ')' or T:text(j + 1) == '[') then
363
+ local scope = scopes[j]
364
+ local owner = owners[j]
365
+ if owner > 0 then
366
+ local n = r.nodes[owner]
367
+ -- Parameter scopes start at the function, not the enclosing file.
368
+ if scope == scopes[n.ti] then scope = n.ti; ends[scope] = n.tj end
369
+ end
370
+ local b = bind(T:text(j), j, scope, { type = ty })
371
+ local eq = j + 1
372
+ if T:text(eq) == '[' and T.m[eq] then eq = T.m[eq] + 1; b.array = true end
373
+ if T:text(eq) == '=' and T:text(eq + 1) == '{' and T.m[eq + 1] then
374
+ local function row(a, z)
375
+ for k, span in ipairs(parts(a, z, ',')) do
376
+ local x, y, field = span[1], span[2], ty.fields[k]
377
+ if T:text(x) == '.' and T.k[x + 1] == 'id' and T:text(x + 2) == '=' then
378
+ field = { name = T:text(x + 1), pointer = ty.slots[T:text(x + 1)] ~= nil }; x = x + 3
379
+ end
380
+ if field and field.pointer then register(ty.slots, field.name, x, y, span[1]) end
381
+ end
382
+ end
383
+ if b.array then
384
+ for _, span in ipairs(parts(eq + 2, T.m[eq + 1] - 1, ',')) do
385
+ if T:text(span[1]) == '{' and T.m[span[1]] == span[2] then row(span[1] + 1, span[2] - 1) end
386
+ end
387
+ else row(eq + 2, T.m[eq + 1] - 1) end
388
+ end
389
+ end
390
+ end
391
+ end
392
+ end
393
+ for i = 1, T.n do
394
+ if i % 4096 == 0 then control.check() end
395
+ if T.k[i] ~= 'id' or T:text(i - 1) == '.' or T:text(i - 1) == '->'
396
+ or lua and T:text(i - 1) == ':' then goto continue end
397
+ local b = binding(T:text(i), i)
398
+ if not b or (not b.slots and not b.type and not b.registry) then goto continue end
399
+ if lua and T:text(i + 1) == '=' and i ~= b.token then b.invalid = true end
400
+ local key, tail, dynamic
401
+ local member = i + 1
402
+ if not lua and b.array and T:text(member) == '[' and T.m[member] then member = T.m[member] + 1 end
403
+ if T:text(member) == '.' or not lua and T:text(member) == '->' then
404
+ if T.k[member + 1] == 'id' then key, tail = T:text(member + 1), member + 2 end
405
+ elseif lua and T:text(i + 1) == '[' and T.m[i + 1] then
406
+ local close = T.m[i + 1]
407
+ key = close == i + 3 and literal(T, i + 2) or nil
408
+ tail, dynamic = close + 1, not key
409
+ end
410
+ if not tail then goto continue end
411
+ local registry = b.registry or b
412
+ local slots = registry.slots or b.type.slots
413
+ if lua and T:text(tail) == ',' then
414
+ -- A member in a multiple-assignment LHS cannot retain a proven
415
+ -- registration. Do not attempt to pair its RHS expression.
416
+ if assignment_end(i) then
417
+ if dynamic then registry.invalid = true else slots[key] = { unknown = true } end
418
+ end
419
+ elseif T:text(tail) == '=' then
420
+ if dynamic then registry.invalid = true
421
+ elseif lua or slots[key] then
422
+ local last = tail + 1
423
+ if T:text(last) == '&' then last = last + 1 end
424
+ local next = T:text(last + 1)
425
+ local simple = next == ';' or next == '' or lua and (T.l[last + 1] > T.l[last] or next == 'end')
426
+ if lua and (scopes[i] ~= registry.scope or T:text(i - 1) == ',') then slots[key] = { unknown = true }
427
+ else register(slots, key, tail + 1, simple and last or last + 1, i) end
428
+ end
429
+ elseif lua and T:text(i - 1) == 'function' then slots[key] = nil
430
+ elseif (T:text(tail) == '(' or not lua and T:text(tail) == ')' and T:text(tail + 1) == '('
431
+ and T:text(i - 1) == '*' and T:text(i - 2) == '(' and T.m[i - 2] == tail)
432
+ and owners[i] > 0 and (dynamic or slots[key]) then
433
+ data.sites[#data.sites + 1] = { from = owners[i], line = T:line(i), recv = T:text(i),
434
+ ref_recv = member == i + 1 and T:text(i) or false,
435
+ name = key, binding = b, slots = slots, dynamic = dynamic,
436
+ kind = lua and 'indirect:registry' or 'indirect:fn-pointer' }
437
+ end
438
+ ::continue::
439
+ end
440
+ if loops then require('codeoutline.lua_callback_loops').collect(r, T, {
441
+ binding = binding, parts = parts, scopes = scopes, ends = ends,
442
+ owners = owners, function_target = function_target, assignment_end = assignment_end,
443
+ }) end
444
+ -- Flatten shared temporary tables; no tokens/source survive query parsing.
445
+ for _, site in ipairs(data.sites) do
446
+ local entries = site.name and site.slots[site.name]
447
+ local b, valid, depth = site.binding, true, 0
448
+ while b do
449
+ if b.invalid or depth > 8 then valid = false; break end
450
+ depth = depth + 1
451
+ b = b.alias and binding(b.alias, b.token - 1) or nil
452
+ end
453
+ site.registrations = valid and entries and not entries.unknown and entries or {}
454
+ site.slots, site.binding = nil, nil
455
+ end
456
+ if not r.collect_static_dispatch and not r.ordinary_bindings and not r.ranges then
457
+ local targets, members = {}, {}
458
+ for _, site in ipairs(data.sites) do
459
+ if site.name then members[site_key(site.from, site.line, site.ref_recv, site.name)] = true end
460
+ for _, registration in ipairs(site.registrations) do targets[registration.name] = true end
461
+ end
462
+ r.static_dispatch_candidate = #data.sites > 0 and { targets = targets, members = members } or nil
463
+ r.static_dispatch = nil
464
+ end
465
+ end
466
+
467
+ function M.query(G, report, ctx)
468
+ local graph = require('codeoutline.graph')
469
+ local cache, entries = {}, 0
470
+ local empty = { sites = {} }
471
+ local function remember(f, result)
472
+ entries = entries + 1 + #result.sites
473
+ for _, site in ipairs(result.sites) do entries = entries + #site.registrations end
474
+ assert(entries <= (M.max_query_entries or 100000), 'static dispatch query limit exceeded; narrow the project scope')
475
+ cache[f] = result
476
+ return result
477
+ end
478
+ local function data(f)
479
+ local rec = G.files[f]
480
+ if rec.static_dispatch then return rec.static_dispatch end
481
+ if not rec.static_dispatch_candidate or not G.root then return empty end
482
+ if cache[f] then return cache[f] end
483
+ if ctx and ctx.ordinary then
484
+ local result = ctx.ordinary.data(f).static_dispatch or empty
485
+ return remember(f, result)
486
+ end
487
+ local h = assert(io.open(G.root .. '/' .. rec.path, 'rb'), 'cannot read static dispatch source: ' .. rec.path)
488
+ local raw = h:read('a'); h:close()
489
+ local crc = xcompress and xcompress.crc32 and xcompress.crc32(raw) or xutils.sha256_hex(raw)
490
+ assert(#raw == rec.size and crc == rec.crc, 'source changed during query; retry: ' .. rec.path)
491
+ local source = raw
492
+ if rec.encoding ~= 'utf-8' and rec.encoding ~= 'utf-8-bom' then
493
+ source = assert(xutils.to_utf8(raw, rec.encoding == 'gbk' and 'gbk' or 'auto'))
494
+ end
495
+ local parsed = require('codeoutline.parse').parse(rec.path, source,
496
+ { ranges = false, dispatch_only = true, static_dispatch = true, language = rec.language })
497
+ assert(#parsed.nodes == #rec.nodes, 'source encoding changed; refresh index: ' .. rec.path)
498
+ for i, n in ipairs(parsed.nodes) do
499
+ assert(n.qualified == rec.nodes[i].qualified and n.line == rec.nodes[i].line, 'source encoding changed; refresh index: ' .. rec.path)
500
+ end
501
+ local result = parsed.static_dispatch or { sites = {} }
502
+ return remember(f, result)
503
+ end
504
+ local function scan(f, only, wanted)
505
+ local result, deps, paired = {}
506
+ for _, site in ipairs(data(f).sites) do
507
+ local src = G.id_of[f][site.from]
508
+ if not only or only == src then
509
+ local seen = {}
510
+ for _, registration in ipairs(site.registrations) do
511
+ local ids
512
+ if registration.index then ids = { G.id_of[f][registration.index] }
513
+ else
514
+ if not deps then deps, paired = graph.dependencies(G, f) end
515
+ ids = graph.resolve(G, f, G.id_of[f][registration.from], registration.name, 'call', nil, deps, paired) or {}
516
+ end
517
+ for _, id in ipairs(ids) do
518
+ if not seen[id] and (not wanted or wanted[id]) then
519
+ seen[id] = true
520
+ result[src] = result[src] or {}
521
+ result[src][#result[src] + 1] = { target = id, line = site.line, kind = site.kind }
522
+ if report then report:add({ source = src, target = id, line = site.line, kind = site.kind,
523
+ call = site.recv .. '.' .. (site.name or '?'), registered_at = G.files[f].path .. ':' .. registration.line }) end
524
+ end
525
+ end
526
+ end
527
+ if report and only and not next(seen) then report:add({ source = src, line = site.line,
528
+ call = site.recv .. '.' .. (site.name or '?'), reason = site.dynamic and 'dynamic registry key'
529
+ or 'static registration missing, overwritten or unsupported' }) end
530
+ end
531
+ end
532
+ return result
533
+ end
534
+ return {
535
+ outgoing = function(id) return scan(G.node_file[id], id)[id] or {} end,
536
+ incoming = function(wanted)
537
+ local result = {}
538
+ local names = {}
539
+ for id in pairs(wanted) do names[G.nodes[id].name] = true end
540
+ for _, f in ipairs(G.file_order) do
541
+ control.check()
542
+ local hint = G.files[f].static_dispatch_candidate
543
+ local relevant = G.files[f].static_dispatch ~= nil or hint == true
544
+ if type(hint) == 'table' then
545
+ for name in pairs(names) do if hint.targets[name] then relevant = true; break end end
546
+ end
547
+ if relevant then
548
+ for src, edges in pairs(scan(f, nil, wanted)) do result[src] = edges end
549
+ end
550
+ end
551
+ return result
552
+ end,
553
+ handles = function(f, i)
554
+ local refs = G.files[f].refs
555
+ if refs.kind[i] ~= 'member_call' then return false end
556
+ local hint = G.files[f].static_dispatch_candidate
557
+ if type(hint) == 'table' and hint.members then
558
+ return hint.members[site_key(refs.from[i], refs.line[i], refs.recv[i], refs.name[i])] == true
559
+ end
560
+ for _, site in ipairs(data(f).sites) do
561
+ if site.from == refs.from[i] and site.line == refs.line[i] and site.ref_recv == refs.recv[i]
562
+ and site.name == refs.name[i] then return true end
563
+ end
564
+ return false
565
+ end,
566
+ }
567
+ end
568
+
569
+ return M
@@ -33,11 +33,12 @@ end
33
33
  function M.limit(s, budget, omitted)
34
34
  s = M.valid(s)
35
35
  local truncated = #s > budget or omitted
36
+ local kept = #s
36
37
  if truncated then
37
38
  local marker = '\n[Results truncated; narrow the query or increase budget.]'
38
- s = M.prefix(s, budget - #marker) .. marker
39
+ s = M.prefix(s, budget - #marker); kept = #s; s = s .. marker
39
40
  end
40
- return s, truncated == true
41
+ return s, truncated == true, kept
41
42
  end
42
43
 
43
44
  return M
@@ -0,0 +1,81 @@
1
+ -- Conservative named TS contracts. All token evidence belongs to one query.
2
+ local M = {}
3
+ local control = require('codeoutline.control')
4
+
5
+ function M.collect(r, T)
6
+ local evidence = {}
7
+ local function annotation(a, b)
8
+ if a > b then return nil end
9
+ local text = T.src:sub(T.s[a], T.e[b]):gsub('%s+', '')
10
+ if text:match('^[%a_$][%w_$.]*$') or text:match('^[%a_$][%w_$.]*%[%]$') then return text end
11
+ end
12
+ for index, n in ipairs(r.nodes) do
13
+ if index % 128 == 0 then control.check() end
14
+ if n.kind == 'class' or n.kind == 'interface' or n.kind == 'method' then
15
+ local d = { at = T.s[n.ti] - 1 }
16
+ if n.kind ~= 'method' then d.hierarchy_bases = require('codeoutline.type_bases').collect(T, n, r.language) end
17
+ local opening, ni
18
+ for i = n.ti, n.tj do
19
+ local s = T:text(i)
20
+ if s == '{' then break end
21
+ if s == '<' then d.generic = true end
22
+ if s == 'static' or s == 'private' or s == 'protected' or s == 'abstract' or s == 'declare' then d[s] = true end
23
+ if s == 'get' or s == 'set' then d.accessor = true end
24
+ if n.kind == 'method' and s == n.name and T:text(i + 1) == '(' then ni, opening = i, i + 1; break end
25
+ end
26
+ if opening and T.m[opening] and not n.name:find('^#') then
27
+ local close, params, valid = T.m[opening], {}, true
28
+ local a = opening + 1
29
+ for i = a, close do
30
+ if i == close or T:text(i) == ',' then
31
+ if a < i then
32
+ local ty = T.k[a] == 'id' and T:text(a + 1) == ':' and annotation(a + 2, i - 1)
33
+ if not ty then valid = false end
34
+ params[#params + 1] = ty or '?'; a = i + 1
35
+ elseif i ~= close or i > opening + 1 then valid = false end
36
+ end
37
+ end
38
+ if valid then d.params = params end
39
+ local last = close + 1
40
+ if T:text(last) == ':' then
41
+ local first = last + 1; last = first
42
+ while last <= n.tj and T:text(last) ~= '{' and T:text(last) ~= ';' do last = last + 1 end
43
+ d.returns = annotation(first, last - 1)
44
+ end
45
+ d.body = T:text(last) == '{'
46
+ -- Optional names, accessors and arrow fields are not ordinary methods.
47
+ if T:text(ni - 1) == '#' or d.accessor then d.params = nil end
48
+ end
49
+ evidence[index] = d
50
+ end
51
+ end
52
+ r.implementation_evidence = evidence
53
+ end
54
+
55
+ function M.types(G, f, written, ctx)
56
+ if not written:match('^[%a_$][%w_$.]*$') then return {} end
57
+ local head, tail = written:match('^([%w_$]+)%.([%w_$]+)$')
58
+ local ids
59
+ for _, imp in ipairs(G.files[f].js_imports or {}) do
60
+ if imp.local_name == (head or written) then
61
+ if tail and imp.imported ~= '*' or not tail and imp.imported == '*' then return {} end
62
+ ids = require('codeoutline.js_resolution').imported(G, f, imp.path, tail or imp.imported, ctx)
63
+ break
64
+ end
65
+ end
66
+ if not ids then
67
+ ids = {}
68
+ for _, id in ipairs(G.by_name[written] or {}) do
69
+ local n = G.nodes[id]
70
+ if G.node_file[id] == f and not n.parent then ids[#ids + 1] = id end
71
+ end
72
+ end
73
+ -- A same-name alias/value is a barrier rather than a global-name fallback.
74
+ if #ids ~= 1 then return {} end
75
+ local n = G.nodes[ids[1]]
76
+ if n.parent or (n.kind ~= 'class' and n.kind ~= 'interface')
77
+ or G.files[G.node_file[ids[1]]].language ~= 'typescript' then return {} end
78
+ return ids
79
+ end
80
+
81
+ return M