@codua/codeoutline-win32-x64 0.2.6 → 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 (72) 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 +48 -7
  13. package/scripts/codeoutline/completion.lua +126 -22
  14. package/scripts/codeoutline/coverage.lua +63 -0
  15. package/scripts/codeoutline/cpp_bindings.lua +46 -11
  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 +275 -28
  22. package/scripts/codeoutline/definition_candidates.lua +180 -0
  23. package/scripts/codeoutline/dispatch_aliases.lua +10 -5
  24. package/scripts/codeoutline/documents.lua +22 -9
  25. package/scripts/codeoutline/explore.lua +243 -122
  26. package/scripts/codeoutline/external_headers.lua +171 -0
  27. package/scripts/codeoutline/function_values.lua +69 -0
  28. package/scripts/codeoutline/go_methods.lua +931 -0
  29. package/scripts/codeoutline/go_modules.lua +329 -0
  30. package/scripts/codeoutline/graph.lua +322 -67
  31. package/scripts/codeoutline/implementations.lua +403 -0
  32. package/scripts/codeoutline/index.lua +161 -28
  33. package/scripts/codeoutline/index_worker.lua +11 -4
  34. package/scripts/codeoutline/indirect.lua +4 -2
  35. package/scripts/codeoutline/js_evidence.lua +2 -1
  36. package/scripts/codeoutline/js_resolution.lua +40 -12
  37. package/scripts/codeoutline/js_workspace.lua +325 -0
  38. package/scripts/codeoutline/lang/c.lua +15 -2
  39. package/scripts/codeoutline/lang/cpp.lua +7 -4
  40. package/scripts/codeoutline/lang/csharp.lua +1 -1
  41. package/scripts/codeoutline/lang/js.lua +56 -20
  42. package/scripts/codeoutline/lang/rust.lua +9 -1
  43. package/scripts/codeoutline/language_detect.lua +100 -0
  44. package/scripts/codeoutline/lsp.lua +173 -23
  45. package/scripts/codeoutline/lua_bindings.lua +151 -47
  46. package/scripts/codeoutline/lua_callback_loops.lua +210 -0
  47. package/scripts/codeoutline/main.lua +32 -7
  48. package/scripts/codeoutline/mcp.lua +52 -9
  49. package/scripts/codeoutline/navigation.lua +284 -64
  50. package/scripts/codeoutline/on_demand.lua +93 -0
  51. package/scripts/codeoutline/ordinary_bindings.lua +210 -34
  52. package/scripts/codeoutline/parse.lua +18 -2
  53. package/scripts/codeoutline/parse_pool.lua +1 -1
  54. package/scripts/codeoutline/parse_worker.lua +1 -1
  55. package/scripts/codeoutline/python_bindings.lua +2 -2
  56. package/scripts/codeoutline/query_filters.lua +65 -0
  57. package/scripts/codeoutline/query_report.lua +56 -19
  58. package/scripts/codeoutline/resolution.lua +23 -0
  59. package/scripts/codeoutline/rust_packages.lua +203 -0
  60. package/scripts/codeoutline/rust_resolution.lua +301 -0
  61. package/scripts/codeoutline/search_ranking.lua +70 -0
  62. package/scripts/codeoutline/service.lua +47 -7
  63. package/scripts/codeoutline/shared_events.lua +369 -0
  64. package/scripts/codeoutline/static_dispatch.lua +46 -11
  65. package/scripts/codeoutline/text.lua +3 -2
  66. package/scripts/codeoutline/ts_implementations.lua +81 -0
  67. package/scripts/codeoutline/type_bases.lua +61 -0
  68. package/scripts/codeoutline/type_hierarchy.lua +324 -0
  69. package/scripts/codeoutline/type_references.lua +290 -0
  70. package/scripts/codeoutline/type_uses.lua +260 -0
  71. package/scripts/codeoutline/update_sequence.lua +1 -1
  72. package/scripts/codeoutline/version.lua +1 -1
@@ -27,6 +27,11 @@ local function literal(T, i)
27
27
  return s:sub(2, -2)
28
28
  end
29
29
 
30
+ local function value_end(T, i)
31
+ local tail = T:text(i)
32
+ return (T.k[i] ~= 'op' or tail == ';' or tail == ',') and tail ~= 'and' and tail ~= 'or'
33
+ end
34
+
30
35
  -- Shared lexical table-member lookup for normal references and query-only
31
36
  -- configured callback aliases. Results are local parser node indices.
32
37
  function M.local_members(nodes, scopes, name, token, scope, finish)
@@ -42,11 +47,15 @@ end
42
47
 
43
48
  function M.collect(r, T, bm)
44
49
  r.lua_imports, r.lua_exports, r.lua_locals = {}, {}, {}
45
- local scopes, stack, params, bindings = {}, {}, {}, {}
50
+ local details = not r.defer_analysis and (r.ranges or r.ordinary_bindings)
51
+ local scopes, stack, params, bindings, function_scopes = {}, {}, {}, {}, {}
46
52
  for i = 1, T.n do
47
53
  if stack[#stack] and bm[stack[#stack]] == i then stack[#stack] = nil end
48
54
  scopes[i] = stack[#stack] or 0
49
- if bm[i] and bm[i] > i then stack[#stack + 1] = i end
55
+ if bm[i] and bm[i] > i then
56
+ function_scopes[i] = T:text(i) == 'function' or function_scopes[scopes[i]]
57
+ stack[#stack + 1] = i
58
+ end
50
59
  if T:text(i) == 'function' and T.k[i] == 'kw' then
51
60
  local p = i + 1
52
61
  while p < (bm[i] or i) and T:text(p) ~= '(' do p = p + 1 end
@@ -55,6 +64,13 @@ function M.collect(r, T, bm)
55
64
  end
56
65
  local function add(name, token, scope, hint, imported)
57
66
  bindings[name] = bindings[name] or {}
67
+ -- Reassignment/parameter shadowing must not revive a global name
68
+ -- after a loader value was bound under this lexical name.
69
+ if hint == '?' and not imported and T:text(token + 2) ~= 'function' then
70
+ for _, b in ipairs(bindings[name]) do
71
+ if b.imported and b.token < token and b.a <= token and b.b >= token then imported = true end
72
+ end
73
+ end
58
74
  bindings[name][#bindings[name] + 1] = { token = token, a = scope, b = bm[scope] or T.n, hint = hint, imported = imported }
59
75
  end
60
76
  for i = 1, T.n do
@@ -75,18 +91,23 @@ function M.collect(r, T, bm)
75
91
  local close = T:text(call + 1) == '(' and T.m[call + 1] or arg
76
92
  if close and ((close == arg) or (close == arg + 1)
77
93
  or (close > arg and T:text(arg + 1) == ',')) and path then
78
- -- A concatenation/operation after the loader is not its return value.
79
- local tail = T:text(close + 1)
80
- if tail ~= '..' and tail ~= '.' and tail ~= ':' and tail ~= 'or' and tail ~= 'and'
81
- and tail ~= '(' and tail ~= '[' and tail ~= '+' and tail ~= '-' and tail ~= '*' and tail ~= '/' then
94
+ local member = T:text(close + 1) == '.' and T.k[close + 2] == 'id' and close + 2
95
+ -- One selected member is a value alias. Calls, nested
96
+ -- selectors and operations produce a different value.
97
+ if value_end(T, (member or close) + 1) then
82
98
  local shadowed = false
83
99
  for _, b in ipairs(bindings[base] or {}) do
84
100
  if b.token < call and b.a <= call and b.b >= call then shadowed = true end
85
101
  end
86
102
  if not shadowed then
87
- r.lua_imports[#r.lua_imports + 1] = { path = path, kind = kind }
88
- hint = 'lua:' .. #r.lua_imports
103
+ r.lua_imports[#r.lua_imports + 1] = { path = path, kind = kind,
104
+ member = member and T:text(member) or nil }
105
+ hint = (member and 'lua-value:' or 'lua:') .. #r.lua_imports
89
106
  if kind == 'loadfile' then hint = '?' end
107
+ if member and kind ~= 'loadfile' then
108
+ r.refs[#r.refs + 1] = { tok = member, name = T:text(member), kind = 'ref',
109
+ line = T:line(member), lua_binding = hint }
110
+ end
90
111
  end
91
112
  end
92
113
  end
@@ -98,30 +119,57 @@ function M.collect(r, T, bm)
98
119
  end
99
120
  -- Query-only lexical bindings travel with the cached tokens, not the index.
100
121
  T.lua_bindings = bindings
101
- for name, list in pairs(bindings) do
102
- for _, b in ipairs(list) do
103
- if b.hint:match('^lua%-local:') then
104
- local members = M.local_members(r.nodes, scopes, name, b.token, b.a, b.b)
105
- r.lua_locals[#r.lua_locals + 1] = members
106
- b.hint = 'lua-local:' .. #r.lua_locals
122
+ if details then
123
+ for name, list in pairs(bindings) do
124
+ for _, b in ipairs(list) do
125
+ if b.hint:match('^lua%-local:') then
126
+ local members = M.local_members(r.nodes, scopes, name, b.token, b.a, b.b)
127
+ r.lua_locals[#r.lua_locals + 1] = members
128
+ b.hint = 'lua-local:' .. #r.lua_locals
129
+ end
107
130
  end
108
131
  end
109
- end
110
- for _, ref in ipairs(r.refs) do
111
- do
112
- local best
113
- for _, b in ipairs(bindings[ref.recv or ref.name] or {}) do
114
- if b.token <= ref.tok and b.a <= ref.tok and b.b >= ref.tok
115
- and (not best or b.a > best.a or (b.a == best.a and b.token > best.token)) then best = b end
132
+ for _, ref in ipairs(r.refs) do
133
+ if not ref.lua_binding then
134
+ local best
135
+ for _, b in ipairs(bindings[ref.recv or ref.name] or {}) do
136
+ if b.token <= ref.tok and b.a <= ref.tok and b.b >= ref.tok
137
+ and (not best or b.a > best.a or (b.a == best.a and b.token > best.token)) then best = b end
138
+ end
139
+ if best and (ref.recv or best.imported) then
140
+ local value = best.hint:match('^lua%-value:')
141
+ ref.lua_binding = value and (not ref.recv and best.hint or '?') or (ref.recv and best.hint or '?')
142
+ end
143
+ if ref.recv and (T:text(ref.tok - 3) == '.' or T:text(ref.tok - 3) == ':') then ref.lua_binding = '?' end
116
144
  end
117
- if best and (ref.recv or best.imported) then ref.lua_binding = ref.recv and best.hint or '?' end
118
- if ref.recv and (T:text(ref.tok - 3) == '.' or T:text(ref.tok - 3) == ':') then ref.lua_binding = '?' end
145
+ end
146
+ else
147
+ r.lua_locals = nil
148
+ local wanted = {}
149
+ for _, ref in ipairs(r.refs) do
150
+ local name = ref.recv or ref.name
151
+ if bindings[name] then wanted[name] = true end
152
+ end
153
+ if next(wanted) then
154
+ r.lua_binding_names = {}; for name in pairs(wanted) do r.lua_binding_names[#r.lua_binding_names + 1] = name end
155
+ table.sort(r.lua_binding_names)
119
156
  end
120
157
  end
121
158
 
122
- local function alias(into, key, target)
159
+ local function alias(into, key, target, token)
160
+ local bound
161
+ if token then
162
+ for _, b in ipairs(bindings[target] or {}) do
163
+ if b.token <= token and b.a <= token and b.b >= token
164
+ and (not bound or b.a > bound.a or b.a == bound.a and b.token > bound.token) then bound = b end
165
+ end
166
+ end
123
167
  for index, n in ipairs(r.nodes) do
124
- if not n.parent and n.name == target and n.qualified == target and (n.kind == 'function' or n.kind == 'method') then
168
+ local scope = scopes[n.ti]
169
+ local visible = not token or n.ti < token and (bound
170
+ and bound.token >= n.ti and bound.token <= n.tj and bound.a == scope
171
+ or not bound and (scope == 0 or scope <= token and bm[scope] >= token))
172
+ if visible and not n.parent and n.name == target and n.qualified == target and (n.kind == 'function' or n.kind == 'method') then
125
173
  into[key] = index
126
174
  end
127
175
  end
@@ -129,7 +177,13 @@ function M.collect(r, T, bm)
129
177
  -- A global alias is a statement of its own: the right-hand side is a bare
130
178
  -- identifier and nothing but a separator follows it on that line. The key
131
179
  -- is dotted (`g_exports.name`) or a literal (`g_exports['name']`).
132
- local globals = {}
180
+ local globals, definitions = {}, {}
181
+ for index, n in ipairs(r.nodes) do
182
+ local owner, key = n.qualified:match('^([%a_][%w_]*)[.:]([%a_][%w_]*)$')
183
+ if owner and not bindings[owner] and not n.parent and (n.kind == 'function' or n.kind == 'method') then
184
+ definitions[n.ti] = { owner = owner, key = key, index = index }
185
+ end
186
+ end
133
187
  for i = 1, T.n - 4 do
134
188
  if i % 4096 == 0 then control.check() end
135
189
  local owner = T:text(i)
@@ -142,10 +196,17 @@ function M.collect(r, T, bm)
142
196
  globals[owner] = globals[owner] or {}
143
197
  globals[owner][key] = nil
144
198
  end
145
- if target and target ~= key and (T.l[eq + 2] ~= T.l[eq + 1] or T:text(eq + 2) == ';') then
146
- alias(globals[owner], key, target)
199
+ if target and value_end(T, eq + 2) and (T.l[eq + 2] ~= T.l[eq + 1] or T:text(eq + 2) == ';') then
200
+ alias(globals[owner], key, target, eq + 1)
147
201
  end
148
202
  end
203
+ -- Direct definitions participate in the same ordered overwrite rules
204
+ -- as named aliases, without requiring another name-index entry.
205
+ local definition = definitions[i]
206
+ if definition then
207
+ globals[definition.owner] = globals[definition.owner] or {}
208
+ globals[definition.owner][definition.key] = definition.index
209
+ end
149
210
  end
150
211
  -- Keep candidates independent of project configuration, including in caches.
151
212
  for owner, exports in pairs(globals) do
@@ -201,42 +262,72 @@ function M.collect(r, T, bm)
201
262
  end
202
263
  return
203
264
  end
204
- local owner, table_start
265
+ local owner, table_start, first = nil, nil, 1
266
+ local function shadowed_owner(token)
267
+ for _, b in ipairs(bindings[owner] or {}) do
268
+ if b.a ~= 0 and b.token <= token and b.b >= token
269
+ and (params[b.token] or T:text(b.token - 1) == 'local'
270
+ or T:text(b.token - 1) == 'function' and T:text(b.token - 2) == 'local') then return true end
271
+ end
272
+ return false
273
+ end
205
274
  if T.k[ret + 1] == 'id' and (ret + 1 == T.n or T:text(ret + 2) == ';') then
206
275
  owner = T:text(ret + 1)
207
- for i = 1, ret - 1 do
208
- if T:text(i) == owner and T:text(i + 1) == '=' and T:text(i + 2) == '{' and scopes[i] == 0 then table_start = i + 2 end
276
+ -- A returned object need not originate in a literal table: cached or
277
+ -- factory-provided objects can receive definite members afterwards.
278
+ -- Any possible replacement outside a function invalidates older fields.
279
+ for _, b in ipairs(bindings[owner] or {}) do
280
+ if b.token < ret and not function_scopes[b.a] and not shadowed_owner(b.token) then
281
+ first = b.token + 1
282
+ table_start = b.a == 0 and T:text(b.token + 1) == '=' and T:text(b.token + 2) == '{'
283
+ and b.token + 2 or nil
284
+ end
209
285
  end
210
286
  elseif T:text(ret + 1) == '{' and T.m[ret + 1] then
211
287
  table_start = ret + 1
288
+ first = table_start
212
289
  owner = r.path:match('([^/\\]+)%.lua$') or r.path
213
290
  else return end
291
+ local writes = {}
292
+ local finish = table_start == ret + 1 and T.m[table_start] or ret
214
293
  if owner then
215
- if not table_start or not T.m[table_start] then return end
216
- for i = table_start + 1, ret - 1 do
217
- if scopes[i] == 0 and T:text(i) == owner and T:text(i + 1) == '=' then r.lua_exports = {}; return end
218
- end
294
+ if table_start and not T.m[table_start] then return end
219
295
  r.lua_owner = owner
220
296
  for index, n in ipairs(r.nodes) do
221
- if not n.parent and n.ti >= table_start and (n.qualified == owner .. '.' .. n.name or n.qualified == owner .. ':' .. n.name)
222
- and (n.kind == 'function' or n.kind == 'method') then r.lua_exports[n.name] = index end
297
+ if not n.parent and n.ti >= first and n.ti < finish and not function_scopes[scopes[n.ti]]
298
+ and (n.qualified == owner .. '.' .. n.name or n.qualified == owner .. ':' .. n.name)
299
+ and (n.kind == 'function' or n.kind == 'method') and not shadowed_owner(n.ti) then
300
+ writes[n.name] = n.ti
301
+ r.lua_exports[n.name] = scopes[n.ti] == 0 and index or nil
302
+ end
223
303
  end
224
304
  end
225
305
  if table_start then
226
306
  local i, close = table_start + 1, T.m[table_start]
227
307
  while i < close do
228
308
  if T.k[i] == 'id' and T:text(i + 1) == '=' and T.k[i + 2] == 'id'
229
- and (T:text(i + 3) == ',' or T:text(i + 3) == ';' or i + 3 == close) then alias(r.lua_exports, T:text(i), T:text(i + 2)) end
309
+ and (T:text(i + 3) == ',' or T:text(i + 3) == ';' or i + 3 == close) then
310
+ local key = T:text(i)
311
+ if not writes[key] or writes[key] < i then
312
+ alias(r.lua_exports, key, T:text(i + 2)); writes[key] = i
313
+ end
314
+ end
230
315
  if T.m[i] and T.m[i] > i then i = T.m[i]
231
316
  elseif bm[i] and bm[i] > i then i = bm[i] end
232
317
  i = i + 1
233
318
  end
234
319
  end
235
- for i = 1, ret - 1 do
236
- if scopes[i] == 0 and T:text(i) == owner and T:text(i + 1) == '.' and T.k[i + 2] == 'id'
237
- and T:text(i + 3) == '=' and T:text(i + 4) ~= 'function' then
238
- r.lua_exports[T:text(i + 2)] = nil
239
- if T.k[i + 4] == 'id' and (T.l[i + 5] ~= T.l[i + 4] or T:text(i + 5) == ';') then alias(r.lua_exports, T:text(i + 2), T:text(i + 4)) end
320
+ for i = first, ret - 1 do
321
+ if not function_scopes[scopes[i]] and T:text(i) == owner and T:text(i + 1) == '.' and T.k[i + 2] == 'id'
322
+ and T:text(i + 3) == '=' and not shadowed_owner(i) then
323
+ local key = T:text(i + 2)
324
+ if not writes[key] or writes[key] < i then
325
+ r.lua_exports[key], writes[key] = nil, i
326
+ if scopes[i] == 0 and T.k[i + 4] == 'id'
327
+ and (T.l[i + 5] ~= T.l[i + 4] or T:text(i + 5) == ';') then
328
+ alias(r.lua_exports, key, T:text(i + 4))
329
+ end
330
+ end
240
331
  end
241
332
  end
242
333
  end
@@ -260,7 +351,7 @@ function M.context(G)
260
351
  local owners = config.luaGlobalExportTables
261
352
  if owners ~= nil then
262
353
  assert(type(owners) == 'table', 'luaGlobalExportTables must be an array of Lua identifiers')
263
- local seen, names = {}, {}
354
+ local seen, names = { g_exports = true }, { 'g_exports' }
264
355
  for key, name in pairs(owners) do
265
356
  assert(type(key) == 'number' and key >= 1 and key <= #owners and key % 1 == 0
266
357
  and type(name) == 'string' and name:match('^[%a_][%w_]*$'),
@@ -286,11 +377,20 @@ function M.aliases(node, owners)
286
377
  local names, seen = {}, {}
287
378
  for _, owner in ipairs(owners) do
288
379
  for _, name in ipairs((node.lua_aliases or {})[owner] or {}) do
289
- if not seen[name] then names[#names + 1], seen[name] = name, true end
380
+ -- The ordinary name already has one entry. Retain same-name
381
+ -- publication metadata without duplicating resident/draft IDs.
382
+ if name ~= node.name and not seen[name] then names[#names + 1], seen[name] = name, true end
290
383
  end
291
384
  end
292
385
  return names
293
386
  end
387
+ function M.exported(node, name, owners)
388
+ if not node.lua_aliases then return false end
389
+ for _, owner in ipairs(owners) do
390
+ for _, alias in ipairs(node.lua_aliases[owner] or EMPTY) do if alias == name then return true end end
391
+ end
392
+ return false
393
+ end
294
394
  function M.modules(G, imp, ctx)
295
395
  ctx = ctx or M.context(G)
296
396
  local key = imp.kind .. '\0' .. imp.path
@@ -412,12 +512,16 @@ function M.resolve(G, f, name, hint, ctx, recv)
412
512
  if hint == '?' then return {} end
413
513
  local local_index = tonumber(hint:match('^lua%-local:(%d+)$'))
414
514
  if local_index then
415
- local n = ((G.files[f].lua_locals or {})[local_index] or {})[name]
515
+ local rec = G.files[f]
516
+ local locals = rec.lua_locals or ctx and ctx.ordinary and ctx.ordinary.data(f).lua_locals or {}
517
+ local n = (locals[local_index] or {})[name]
416
518
  return n and { G.id_of[f][n] } or {}
417
519
  end
418
- local index = tonumber(hint:match('^lua:(%d+)$'))
520
+ local value = tonumber(hint:match('^lua%-value:(%d+)$'))
521
+ local index = value or tonumber(hint:match('^lua:(%d+)$'))
419
522
  local imp = index and (G.files[f].lua_imports or {})[index]
420
523
  if not imp then return nil end
524
+ if value then name = imp.member end
421
525
  local result = {}
422
526
  for _, target in ipairs(M.modules(G, imp, ctx)) do
423
527
  local rec = G.files[target]
@@ -0,0 +1,210 @@
1
+ -- Query-only traversal of a closed, same-file local callback collection.
2
+ local M = { MAX_FILES = 128, MAX_LOOPS = 128, MAX_TARGETS = 64, MAX_LINKS = 512 }
3
+ local control = require('codeoutline.control')
4
+ local iterators = { pairs = true, ipairs = true }
5
+ local function loop_header(T, i)
6
+ return T:text(i) == 'for' and T.k[i + 1] == 'id' and T:text(i + 2) == ',' and T.k[i + 3] == 'id'
7
+ and T:text(i + 4) == 'in' and iterators[T:text(i + 5)] and T:text(i + 6) == '('
8
+ and T.k[i + 7] == 'id' and T:text(i + 8) == ')' and T:text(i + 9) == 'do'
9
+ end
10
+
11
+ function M.candidate(rec)
12
+ if rec.language ~= 'lua' then return false end
13
+ for _, name in ipairs(rec.dispatch_calls or {}) do if iterators[name] then return true end end
14
+ return false
15
+ end
16
+
17
+ function M.tokens_candidate(T)
18
+ for i = 1, T.n do
19
+ if i % 4096 == 0 then control.check() end
20
+ if loop_header(T, i) then return true end
21
+ end
22
+ return false
23
+ end
24
+
25
+ function M.collect(r, T, E)
26
+ local result = { sites = {}, handled = {}, weight = 0 }
27
+ r.lua_callback_loops = result
28
+ local functions = {}
29
+ for id, n in ipairs(r.nodes) do
30
+ if n.kind == 'function' or n.kind == 'method' then
31
+ for i = n.ti, n.tj do
32
+ if T:text(i) == 'function' then functions[i] = id; break end
33
+ if T:text(i) == '(' then break end
34
+ end
35
+ end
36
+ end
37
+ local function function_owner(scope)
38
+ while scope and scope > 0 do
39
+ if T:text(scope) == 'function' then return functions[scope] end
40
+ scope = E.scopes[scope]
41
+ end
42
+ end
43
+ local function root(b)
44
+ for _ = 1, 9 do
45
+ if not b then return nil end
46
+ if b.slots then return b end
47
+ b = b.alias and E.binding(b.alias, b.token - 1) or nil
48
+ end
49
+ end
50
+ local collections, uses, wanted, count = {}, {}, {}, 0
51
+ for i = 1, T.n do
52
+ if i % 4096 == 0 then control.check() end
53
+ if loop_header(T, i) then
54
+ if count >= M.MAX_LOOPS then result.incomplete = true; break end
55
+ count = count + 1
56
+ local b = root(E.binding(T:text(i + 7), i + 7))
57
+ if b then wanted[b] = true end
58
+ end
59
+ end
60
+ local function target(at)
61
+ local id = T.k[at] == 'id' and E.binding(T:text(at), at) and E.function_target(T:text(at), at)
62
+ local n = id and r.nodes[id]
63
+ return n and n['local'] and n.kind == 'function' and id or nil
64
+ end
65
+ local function entry(c, at, line)
66
+ if c.invalid then return end
67
+ if #c.entries >= M.MAX_TARGETS then c.invalid = true; result.incomplete = true; return end
68
+ local id = target(at)
69
+ if not id then c.invalid = true; return end
70
+ c.entries[#c.entries + 1] = { index = id, token = at, line = T:line(line) }
71
+ end
72
+ for b in pairs(wanted) do
73
+ local c = { binding = b, entries = {}, invalid = b.invalid }
74
+ collections[b] = c
75
+ for _, span in ipairs(E.parts(b.open + 1, T.m[b.open] - 1, ',')) do
76
+ if span[1] ~= span[2] then c.invalid = true else entry(c, span[1], span[1]) end
77
+ end
78
+ end
79
+ local poisoned = {}
80
+ for i = 1, T.n do
81
+ if i % 4096 == 0 then control.check() end
82
+ local word = T:text(i)
83
+ if word == '_ENV' or word == '_G' then poisoned.environment = true end
84
+ -- Passing/aliasing the mutable library table can hide insert overrides.
85
+ if word == 'table' and not E.binding(word, i) and T:text(i + 1) ~= '.' then poisoned.table = true end
86
+ if (iterators[word] or word == 'table') and not E.binding(word, i)
87
+ and (E.assignment_end(i) or T:text(i - 1) == 'function') then poisoned[word] = true end
88
+ end
89
+ -- Only direct two-argument table.insert calls in the collection's block.
90
+ for i = 1, T.n do
91
+ if i % 4096 == 0 then control.check() end
92
+ if T:text(i) == 'table' and T:text(i + 1) == '.' and T:text(i + 2) == 'insert' and T:text(i + 3) == '('
93
+ and T.k[i + 4] == 'id' then
94
+ local b = E.binding(T:text(i + 4), i + 4)
95
+ local c = collections[root(b)]
96
+ if c then
97
+ uses[i + 4] = true
98
+ if E.binding('table', i) or poisoned.table or T:text(i + 5) ~= ',' or T.m[i + 3] ~= i + 7
99
+ or E.scopes[i] ~= c.binding.scope then c.invalid = true
100
+ else entry(c, i + 6, i) end
101
+ end
102
+ end
103
+ end
104
+ local loops = {}
105
+ for i = 1, T.n do
106
+ if i % 4096 == 0 then control.check() end
107
+ if loop_header(T, i) then
108
+ local b = E.binding(T:text(i + 7), i + 7)
109
+ local c = collections[root(b)]
110
+ if c then uses[i + 7] = true end
111
+ if #loops >= M.MAX_LOOPS then result.incomplete = true; break end
112
+ loops[#loops + 1] = { collection = c, token = i, body = i + 9, value = i + 3,
113
+ iterator = T:text(i + 5), valid = not E.binding(T:text(i + 5), i + 5) and not poisoned[T:text(i + 5)] }
114
+ end
115
+ end
116
+ -- Reject escapes, unsupported writes/reads and invalid aliases of a collection.
117
+ for i = 1, T.n do
118
+ if i % 4096 == 0 then control.check() end
119
+ if T.k[i] == 'id' then
120
+ local b = E.binding(T:text(i), i)
121
+ local c = collections[root(b)]
122
+ if c then
123
+ local alias = T:text(i - 3) == 'local' and T:text(i - 1) == '='
124
+ and T.k[i - 2] == 'id' and E.binding(T:text(i - 2), i - 2)
125
+ local alias_use = alias and alias.alias == T:text(i) and root(alias) == c.binding
126
+ if i ~= b.token and not uses[i] and not alias_use then c.invalid = true end
127
+ local current, depth = b, 0
128
+ while current do
129
+ if current.invalid or depth > 8 then c.invalid = true; break end
130
+ depth = depth + 1
131
+ current = current.alias and E.binding(current.alias, current.token - 1) or nil
132
+ end
133
+ end
134
+ end
135
+ end
136
+ for _, loop in ipairs(loops) do
137
+ local c, name = loop.collection, T:text(loop.value)
138
+ local original = E.binding(name, loop.body)
139
+ local last = E.ends[loop.body] or loop.body
140
+ for ri, ref in ipairs(r.refs) do
141
+ if ri % 128 == 0 then control.check() end
142
+ if ref.kind == 'call' and ref.name == name and ref.tok > loop.body and ref.tok < last then
143
+ local current = E.binding(name, ref.tok)
144
+ if current == original or original and original.invalid then
145
+ result.handled[ri] = true; result.weight = result.weight + 32
146
+ if E.scopes[ref.tok] ~= loop.body or ref.from == 0
147
+ or function_owner(E.scopes[loop.body]) ~= ref.from then goto next_ref end
148
+ local site = { from = ref.from, line = ref.line, recv = name, iterator = loop.iterator, registrations = {} }
149
+ result.sites[#result.sites + 1] = site
150
+ if loop.valid and not poisoned.environment and c and not c.invalid and original and not original.invalid then
151
+ for _, registration in ipairs(c.entries) do
152
+ if registration.token < loop.token then
153
+ if #site.registrations >= M.MAX_TARGETS or result.weight >= M.MAX_LINKS * 512 then result.incomplete = true; break end
154
+ site.registrations[#site.registrations + 1] = registration; result.weight = result.weight + 512
155
+ end
156
+ end
157
+ end
158
+ if #result.sites >= M.MAX_LINKS then result.incomplete = true; return end
159
+ end
160
+ end
161
+ ::next_ref::
162
+ end
163
+ end
164
+ end
165
+
166
+ function M.context(G, ctx, report)
167
+ local C = { incomplete = false, stats = { files = 0, sites = 0, links = 0 } }
168
+ local cache, empty = {}, { sites = {}, handled = {} }
169
+ local function data(f)
170
+ if cache[f] then return cache[f] end
171
+ if not M.candidate(G.files[f]) then cache[f] = empty; return empty end
172
+ if C.stats.files >= M.MAX_FILES then C.incomplete = true; cache[f] = empty; return empty end
173
+ C.stats.files = C.stats.files + 1
174
+ local found = ctx.ordinary.data(f).lua_callback_loops or { sites = {}, handled = {} }
175
+ if found.incomplete then C.incomplete = true end
176
+ C.stats.sites = C.stats.sites + #found.sites; cache[f] = found; return found
177
+ end
178
+ local function scan(f, only, wanted, out)
179
+ for _, site in ipairs(data(f).sites) do
180
+ local src, seen = G.id_of[f][site.from], {}
181
+ if not only or src == only then
182
+ for _, registration in ipairs(site.registrations) do
183
+ local id = G.id_of[f][registration.index]
184
+ if not seen[id] and (not wanted or wanted[id]) then
185
+ seen[id] = true; C.stats.links = C.stats.links + 1
186
+ out[src] = out[src] or {}; out[src][#out[src] + 1] = { target = id, line = site.line, kind = 'automatic:callback-loop' }
187
+ if report then report:add({ source = src, target = id, line = site.line, call = site.recv,
188
+ kind = 'possible callback traversal (same local collection and builtin ' .. site.iterator .. ')',
189
+ registered_at = G.files[f].path .. ':' .. registration.line }) end
190
+ end
191
+ end
192
+ if report and only and not next(seen) then report:add({ source = src, line = site.line, call = site.recv,
193
+ reason = 'callback collection is empty, mutated, escaped, shadowed or unsupported' }) end
194
+ end
195
+ end
196
+ end
197
+ function C.outgoing(id)
198
+ local out = {}; scan(G.node_file[id], id, nil, out); return out[id] or {}
199
+ end
200
+ function C.incoming(wanted)
201
+ local out, files = {}, {}
202
+ for id in pairs(wanted) do local f = G.node_file[id]; if G.files[f].language == 'lua' then files[f] = true end end
203
+ for f in pairs(files) do control.check(); scan(f, nil, wanted, out) end
204
+ return out
205
+ end
206
+ function C.handles(f, i) return data(f).handled[i] == true end
207
+ return C
208
+ end
209
+
210
+ return M
@@ -50,6 +50,7 @@ if #config.roots == 0 then
50
50
  end
51
51
  local shared = assert(xshared.create('codeoutline_control', 65536, 256))
52
52
  local jobs, next_id, job_count, worker_ready = {}, 0, 0, false
53
+ local stopping, indexing = false, false
53
54
  local endpoint, stdio, input = nil, nil, ''
54
55
  local updating = false
55
56
  local startup = xtimer.now_ms()
@@ -63,13 +64,16 @@ else exit_on_update = exit_on_update == '1' end
63
64
  local installed_update
64
65
  -- Background service: listening starts once the port proved free.
65
66
  local codec, next_stop_check = nil, 0
67
+ local query_methods = { explore = true, callers = true, affected = true, type_hierarchy = true, type_references = true }
68
+ local QUERY_QUEUE_WAIT_MS = 750
66
69
 
67
70
  local function submit(req, done)
68
71
  if job_count >= 64 then return nil, 'Request queue is full; retry later' end
69
72
  next_id = next_id + 1
70
73
  local id = tostring(next_id)
71
74
  req.deadline = xtimer.now_ms() + 120000
72
- jobs[id], job_count = { done = done, request = req }, job_count + 1
75
+ req.queue_until = query_methods[req.method] and xtimer.now_ms() + QUERY_QUEUE_WAIT_MS or nil
76
+ jobs[id], job_count = { done = done, request = req, queue_until = req.queue_until }, job_count + 1
73
77
  if worker_ready then
74
78
  local ok, err = xthread.post(2, 'run', id, req)
75
79
  if not ok then jobs[id], job_count = nil, job_count - 1; return nil, tostring(err) end
@@ -93,7 +97,7 @@ local function await_worker(req, started)
93
97
  return ok, result
94
98
  end
95
99
  local function send(response)
96
- if response then
100
+ if response and not stopping and not (lsp_mode and stdio and stdio.state == 'closed') then
97
101
  io.stdout:write(lsp_mode and lsp.frame(response) or (xutils.json_pack(response) .. '\n'))
98
102
  io.stdout:flush()
99
103
  end
@@ -105,8 +109,11 @@ local function listen()
105
109
  io.stdout:write(string.format('[codeoutline] listening http://%s:%d/mcp\n', config.host, config.port))
106
110
  io.stdout:flush()
107
111
  end
108
- local stopping, indexing = false, false
109
- local function stop(code) stopping = true; xthread.stop(code) end
112
+ local function stop(code)
113
+ stopping = true
114
+ shared:set('shutdown', true)
115
+ xthread.stop(code)
116
+ end
110
117
 
111
118
  return {
112
119
  __tick_ms = 5,
@@ -171,12 +178,20 @@ return {
171
178
  if not resumed then error(err) end
172
179
  else
173
180
  local posted, err = xthread.post(2, 'run', job_id, job.request)
174
- if not posted then jobs[job_id], job_count = nil, job_count - 1; job.done(false, tostring(err)) end
181
+ if not posted then
182
+ jobs[job_id], job_count = nil, job_count - 1
183
+ if job.done then job.done(false, tostring(err)) end
184
+ end
175
185
  end
176
186
  end
187
+ elseif op == 'worker_started' then
188
+ if jobs[id] then jobs[id].started = true end
177
189
  elseif op == 'worker_result' then
178
190
  local job = jobs[id]
179
- if job then jobs[id], job_count = nil, job_count - 1; job.done(ok, result) end
191
+ if job then
192
+ jobs[id], job_count = nil, job_count - 1
193
+ if job.done then job.done(ok, result) end
194
+ end
180
195
  elseif op == 'update_result' then
181
196
  -- Arguments: error, installed version (nil when already current).
182
197
  if id then notice('update unavailable: ' .. tostring(id))
@@ -188,6 +203,16 @@ return {
188
203
  end,
189
204
  __update = function()
190
205
  if stopping then return end
206
+ local now = xtimer.now_ms()
207
+ for id, job in pairs(jobs) do
208
+ if job.done and job.queue_until and not job.started and now >= job.queue_until then
209
+ -- Keep the occupied slot until the worker acknowledges cancellation:
210
+ -- abandoned requests must not grow an unbounded worker queue.
211
+ local done = job.done; job.done = nil
212
+ shared:set('cancel:' .. id, true)
213
+ done(false, 'Request queue wait exceeded 750ms; retry later')
214
+ end
215
+ end
191
216
  if not worker_ready and xtimer.now_ms() - startup > 10000 then error('Index worker failed to start') end
192
217
  if installed_update and exit_on_update and job_count == 0
193
218
  and not (lsp_mode and stdio and stdio.state ~= 'closed')
@@ -211,7 +236,7 @@ return {
211
236
  if incomplete then send(mcp.error(nil, -32700, lsp_mode and 'EOF before complete LSP frame' or 'EOF before message newline')) end
212
237
  if err == 'stdio requires redirected stdin' then err = 'run by an MCP client; see codeoutline --help' end
213
238
  if err ~= 'eof' then io.stderr:write(tostring(err) .. '\n') end
214
- stdio:close(); shared:set('shutdown', true); xthread.stop(err == 'eof' and not incomplete and 0 or 1); return
239
+ stdio:close(); stop(err == 'eof' and not incomplete and 0 or 1); return
215
240
  end
216
241
  if lsp_mode then
217
242
  local ok, why = pcall(lsp_reader.feed, lsp_reader, data, function(body) stdio:accept(body) end)