@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
@@ -27,13 +27,35 @@ 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
+
35
+ -- Shared lexical table-member lookup for normal references and query-only
36
+ -- configured callback aliases. Results are local parser node indices.
37
+ function M.local_members(nodes, scopes, name, token, scope, finish)
38
+ local members = {}
39
+ for index, n in ipairs(nodes) do
40
+ if n.ti >= token and n.ti <= finish and scopes[n.ti] == scope
41
+ and (n.qualified == name .. '.' .. n.name or n.qualified == name .. ':' .. n.name) then
42
+ members[n.name] = index
43
+ end
44
+ end
45
+ return members
46
+ end
47
+
30
48
  function M.collect(r, T, bm)
31
49
  r.lua_imports, r.lua_exports, r.lua_locals = {}, {}, {}
32
- 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 = {}, {}, {}, {}, {}
33
52
  for i = 1, T.n do
34
53
  if stack[#stack] and bm[stack[#stack]] == i then stack[#stack] = nil end
35
54
  scopes[i] = stack[#stack] or 0
36
- 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
37
59
  if T:text(i) == 'function' and T.k[i] == 'kw' then
38
60
  local p = i + 1
39
61
  while p < (bm[i] or i) and T:text(p) ~= '(' do p = p + 1 end
@@ -42,6 +64,13 @@ function M.collect(r, T, bm)
42
64
  end
43
65
  local function add(name, token, scope, hint, imported)
44
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
45
74
  bindings[name][#bindings[name] + 1] = { token = token, a = scope, b = bm[scope] or T.n, hint = hint, imported = imported }
46
75
  end
47
76
  for i = 1, T.n do
@@ -62,18 +91,23 @@ function M.collect(r, T, bm)
62
91
  local close = T:text(call + 1) == '(' and T.m[call + 1] or arg
63
92
  if close and ((close == arg) or (close == arg + 1)
64
93
  or (close > arg and T:text(arg + 1) == ',')) and path then
65
- -- A concatenation/operation after the loader is not its return value.
66
- local tail = T:text(close + 1)
67
- if tail ~= '..' and tail ~= '.' and tail ~= ':' and tail ~= 'or' and tail ~= 'and'
68
- 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
69
98
  local shadowed = false
70
99
  for _, b in ipairs(bindings[base] or {}) do
71
100
  if b.token < call and b.a <= call and b.b >= call then shadowed = true end
72
101
  end
73
102
  if not shadowed then
74
- r.lua_imports[#r.lua_imports + 1] = { path = path, kind = kind }
75
- 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
76
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
77
111
  end
78
112
  end
79
113
  end
@@ -83,35 +117,59 @@ function M.collect(r, T, bm)
83
117
  end
84
118
  end
85
119
  end
86
- for name, list in pairs(bindings) do
87
- for _, b in ipairs(list) do
88
- if b.hint:match('^lua%-local:') then
89
- local members = {}
90
- for index, n in ipairs(r.nodes) do
91
- local scope = scopes[n.ti]
92
- if n.ti >= b.token and n.ti <= b.b and scope == b.a
93
- and (n.qualified == name .. '.' .. n.name or n.qualified == name .. ':' .. n.name) then members[n.name] = index end
120
+ -- Query-only lexical bindings travel with the cached tokens, not the index.
121
+ T.lua_bindings = bindings
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
94
129
  end
95
- r.lua_locals[#r.lua_locals + 1] = members
96
- b.hint = 'lua-local:' .. #r.lua_locals
97
130
  end
98
131
  end
99
- end
100
- for _, ref in ipairs(r.refs) do
101
- do
102
- local best
103
- for _, b in ipairs(bindings[ref.recv or ref.name] or {}) do
104
- if b.token <= ref.tok and b.a <= ref.tok and b.b >= ref.tok
105
- 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
106
144
  end
107
- if best and (ref.recv or best.imported) then ref.lua_binding = ref.recv and best.hint or '?' end
108
- 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)
109
156
  end
110
157
  end
111
158
 
112
- 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
113
167
  for index, n in ipairs(r.nodes) do
114
- 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
115
173
  into[key] = index
116
174
  end
117
175
  end
@@ -119,7 +177,13 @@ function M.collect(r, T, bm)
119
177
  -- A global alias is a statement of its own: the right-hand side is a bare
120
178
  -- identifier and nothing but a separator follows it on that line. The key
121
179
  -- is dotted (`g_exports.name`) or a literal (`g_exports['name']`).
122
- 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
123
187
  for i = 1, T.n - 4 do
124
188
  if i % 4096 == 0 then control.check() end
125
189
  local owner = T:text(i)
@@ -132,10 +196,17 @@ function M.collect(r, T, bm)
132
196
  globals[owner] = globals[owner] or {}
133
197
  globals[owner][key] = nil
134
198
  end
135
- if target and target ~= key and (T.l[eq + 2] ~= T.l[eq + 1] or T:text(eq + 2) == ';') then
136
- 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)
137
201
  end
138
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
139
210
  end
140
211
  -- Keep candidates independent of project configuration, including in caches.
141
212
  for owner, exports in pairs(globals) do
@@ -191,42 +262,72 @@ function M.collect(r, T, bm)
191
262
  end
192
263
  return
193
264
  end
194
- 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
195
274
  if T.k[ret + 1] == 'id' and (ret + 1 == T.n or T:text(ret + 2) == ';') then
196
275
  owner = T:text(ret + 1)
197
- for i = 1, ret - 1 do
198
- 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
199
285
  end
200
286
  elseif T:text(ret + 1) == '{' and T.m[ret + 1] then
201
287
  table_start = ret + 1
288
+ first = table_start
202
289
  owner = r.path:match('([^/\\]+)%.lua$') or r.path
203
290
  else return end
291
+ local writes = {}
292
+ local finish = table_start == ret + 1 and T.m[table_start] or ret
204
293
  if owner then
205
- if not table_start or not T.m[table_start] then return end
206
- for i = table_start + 1, ret - 1 do
207
- if scopes[i] == 0 and T:text(i) == owner and T:text(i + 1) == '=' then r.lua_exports = {}; return end
208
- end
294
+ if table_start and not T.m[table_start] then return end
209
295
  r.lua_owner = owner
210
296
  for index, n in ipairs(r.nodes) do
211
- if not n.parent and n.ti >= table_start and (n.qualified == owner .. '.' .. n.name or n.qualified == owner .. ':' .. n.name)
212
- 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
213
303
  end
214
304
  end
215
305
  if table_start then
216
306
  local i, close = table_start + 1, T.m[table_start]
217
307
  while i < close do
218
308
  if T.k[i] == 'id' and T:text(i + 1) == '=' and T.k[i + 2] == 'id'
219
- 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
220
315
  if T.m[i] and T.m[i] > i then i = T.m[i]
221
316
  elseif bm[i] and bm[i] > i then i = bm[i] end
222
317
  i = i + 1
223
318
  end
224
319
  end
225
- for i = 1, ret - 1 do
226
- if scopes[i] == 0 and T:text(i) == owner and T:text(i + 1) == '.' and T.k[i + 2] == 'id'
227
- and T:text(i + 3) == '=' and T:text(i + 4) ~= 'function' then
228
- r.lua_exports[T:text(i + 2)] = nil
229
- 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
230
331
  end
231
332
  end
232
333
  end
@@ -250,7 +351,7 @@ function M.context(G)
250
351
  local owners = config.luaGlobalExportTables
251
352
  if owners ~= nil then
252
353
  assert(type(owners) == 'table', 'luaGlobalExportTables must be an array of Lua identifiers')
253
- local seen, names = {}, {}
354
+ local seen, names = { g_exports = true }, { 'g_exports' }
254
355
  for key, name in pairs(owners) do
255
356
  assert(type(key) == 'number' and key >= 1 and key <= #owners and key % 1 == 0
256
357
  and type(name) == 'string' and name:match('^[%a_][%w_]*$'),
@@ -276,11 +377,20 @@ function M.aliases(node, owners)
276
377
  local names, seen = {}, {}
277
378
  for _, owner in ipairs(owners) do
278
379
  for _, name in ipairs((node.lua_aliases or {})[owner] or {}) do
279
- 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
280
383
  end
281
384
  end
282
385
  return names
283
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
284
394
  function M.modules(G, imp, ctx)
285
395
  ctx = ctx or M.context(G)
286
396
  local key = imp.kind .. '\0' .. imp.path
@@ -302,9 +412,10 @@ function M.modules(G, imp, ctx)
302
412
  local path = normalize(ctx.directory .. '/' .. imp.path)
303
413
  local f = G.file_index[path]
304
414
  if f and G.files[f].language == 'lua' then result[1] = f end
305
- if not result[1] and not loaders[imp.kind] then
306
- -- `*import` pattern loader: bind when any indexed Lua file's path
307
- -- ends with the quoted path, regardless of directory prefix.
415
+ if not result[1] then
416
+ -- No file at that exact path: bind any indexed Lua file whose path
417
+ -- ends with the quoted one, however deep the workspace root sits
418
+ -- above the Lua tree (`dofile('scripts/x.lua')` opened from a parent).
308
419
  local want = normalize(imp.path)
309
420
  local base = want:match('([^/]+)$')
310
421
  if base then
@@ -329,6 +440,65 @@ function M.native_modules(G, name, ctx)
329
440
  return require('codeoutline.foreign').modules(G, 'lua', name, ctx)
330
441
  end
331
442
 
443
+ -- Shared query-time receiver resolution for completion and navigation. Reuse
444
+ -- the parser's lexical bindings and loader contracts; never execute Lua.
445
+ function M.member(G, target, name)
446
+ local result = {}
447
+ for _, f in ipairs(target and target.modules or {}) do
448
+ local rec = G.files[f]
449
+ local exports = require('codeoutline.foreign').exports(rec, 'lua', target.module_name) or rec.lua_exports
450
+ local n = exports and exports[name]
451
+ if n then result[#result + 1] = { node = rec.nodes[n], file = f, index = n } end
452
+ end
453
+ return result
454
+ end
455
+
456
+ function M.return_target(G, node, ctx)
457
+ local name = require('codeoutline.foreign').returns(node, 'lua')
458
+ local files = name and M.native_modules(G, name, ctx)
459
+ if files and #files > 0 then return { modules = files, module_name = name } end
460
+ end
461
+
462
+ function M.receiver(G, rec, T, name, offset, ctx, depth)
463
+ depth = depth or 0
464
+ if depth >= 6 then return nil, true end
465
+ local best
466
+ for _, b in ipairs((T.lua_bindings or {})[name] or {}) do
467
+ if T.s[b.token] - 1 < offset and (b.a == 0 or T.s[b.a] - 1 <= offset)
468
+ and (b.a == 0 or not T.e[b.b] or offset < T.e[b.b])
469
+ and (not best or b.a > best.a or b.a == best.a and b.token > best.token) then best = b end
470
+ end
471
+ if not best then
472
+ local files = M.native_modules(G, name, ctx)
473
+ return #files > 0 and { modules = files, module_name = name } or nil, (T.lua_bindings or {})[name] ~= nil
474
+ end
475
+ local imported = tonumber(best.hint:match('^lua:(%d+)$'))
476
+ if imported then
477
+ local imp = rec.lua_imports[imported]
478
+ return { modules = M.modules(G, imp, ctx), module_name = imp.path }, true
479
+ end
480
+ local i = best.token + 2
481
+ if best.imported or T:text(best.token + 1) ~= '=' or T.k[i] ~= 'id' then return nil, true end
482
+ local target = M.receiver(G, rec, T, T:text(i), T.s[best.token] - 1, ctx, depth + 1)
483
+ i = i + 1
484
+ while target and (T:text(i) == '.' or T:text(i) == ':') and T.k[i + 1] == 'id' do
485
+ local close = T:text(i + 2) == '(' and T.m[i + 2]
486
+ if not close then return nil, true end
487
+ local next_target
488
+ for _, entry in ipairs(M.member(G, target, T:text(i + 1))) do
489
+ local value = M.return_target(G, entry.node, ctx)
490
+ if not value or next_target and next_target.module_name ~= value.module_name then return nil, true end
491
+ next_target = value
492
+ end
493
+ target, i = next_target, close + 1
494
+ end
495
+ -- Reject expressions whose value is not the simple receiver/call chain.
496
+ local tail = T:text(i)
497
+ if T.k[i] == 'op' and tail ~= ';' and tail ~= ')' and tail ~= ','
498
+ or tail == 'and' or tail == 'or' then return nil, true end
499
+ return target, true
500
+ end
501
+
332
502
  function M.resolve(G, f, name, hint, ctx, recv)
333
503
  if G.files[f].language ~= 'lua' then return nil end
334
504
  if not hint or hint == '' then
@@ -342,12 +512,16 @@ function M.resolve(G, f, name, hint, ctx, recv)
342
512
  if hint == '?' then return {} end
343
513
  local local_index = tonumber(hint:match('^lua%-local:(%d+)$'))
344
514
  if local_index then
345
- 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]
346
518
  return n and { G.id_of[f][n] } or {}
347
519
  end
348
- 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+)$'))
349
522
  local imp = index and (G.files[f].lua_imports or {})[index]
350
523
  if not imp then return nil end
524
+ if value then name = imp.member end
351
525
  local result = {}
352
526
  for _, target in ipairs(M.modules(G, imp, ctx)) do
353
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
@@ -0,0 +1,38 @@
1
+ -- Decode Lua string syntax without evaluating source code.
2
+ local M = {}
3
+ function M.decode(raw)
4
+ local eq, body = raw:match('^%[(=*)%[(.*)%]%1%]$')
5
+ if eq then return body:gsub('^\r?\n', '', 1) end
6
+ local quote = raw:sub(1, 1)
7
+ if (quote ~= '"' and quote ~= "'") or raw:sub(-1) ~= quote then return nil end
8
+ if not raw:find('\\', 1, true) then return raw:sub(2, -2) end
9
+ local escaped = { a = '\a', b = '\b', f = '\f', n = '\n', r = '\r', t = '\t', v = '\v',
10
+ ['\\'] = '\\', ['"'] = '"', ["'"] = "'", ['\n'] = '\n' }
11
+ local out, i, stop = {}, 2, #raw - 1
12
+ while i <= stop do
13
+ local c = raw:sub(i, i)
14
+ if c ~= '\\' then out[#out + 1] = c; i = i + 1
15
+ else
16
+ c = raw:sub(i + 1, i + 1); i = i + 2
17
+ if escaped[c] then out[#out + 1] = escaped[c]
18
+ elseif c == 'z' then
19
+ while i <= stop and raw:sub(i, i):match('%s') do i = i + 1 end
20
+ elseif c == '\r' then
21
+ if raw:sub(i, i) == '\n' then i = i + 1 end
22
+ out[#out + 1] = '\n'
23
+ elseif c == 'x' then
24
+ local hex = raw:sub(i, i + 1)
25
+ if not hex:match('^%x%x$') then return nil end
26
+ out[#out + 1] = string.char(tonumber(hex, 16)); i = i + 2
27
+ elseif c:match('%d') then
28
+ local digits = (c .. raw:sub(i, stop)):match('^%d%d?%d?')
29
+ local n = tonumber(digits)
30
+ if n > 255 then return nil end
31
+ out[#out + 1] = string.char(n); i = i + #digits - 1
32
+ else return nil end
33
+ end
34
+ end
35
+ return table.concat(out)
36
+ end
37
+
38
+ return M