@codua/codeoutline-win32-x64 0.2.1 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +1 -0
  2. package/bin/xnet.exe +0 -0
  3. package/build-info.json +1 -1
  4. package/package.json +1 -1
  5. package/scripts/codeoutline/callers.lua +116 -0
  6. package/scripts/codeoutline/command.lua +7 -3
  7. package/scripts/codeoutline/common.lua +29 -3
  8. package/scripts/codeoutline/completion.lua +162 -62
  9. package/scripts/codeoutline/cpp_bindings.lua +116 -0
  10. package/scripts/codeoutline/cpp_types.lua +209 -0
  11. package/scripts/codeoutline/csharp_bindings.lua +498 -0
  12. package/scripts/codeoutline/dispatch_aliases.lua +474 -0
  13. package/scripts/codeoutline/documents.lua +11 -1
  14. package/scripts/codeoutline/explore.lua +73 -12
  15. package/scripts/codeoutline/foreign.lua +125 -0
  16. package/scripts/codeoutline/foreign_lua.lua +273 -0
  17. package/scripts/codeoutline/graph.lua +80 -22
  18. package/scripts/codeoutline/index.lua +15 -6
  19. package/scripts/codeoutline/index_worker.lua +15 -7
  20. package/scripts/codeoutline/indirect.lua +148 -50
  21. package/scripts/codeoutline/js_evidence.lua +30 -2
  22. package/scripts/codeoutline/js_resolution.lua +34 -0
  23. package/scripts/codeoutline/lang/csharp.lua +15 -3
  24. package/scripts/codeoutline/lang/lua.lua +17 -2
  25. package/scripts/codeoutline/lsp.lua +58 -34
  26. package/scripts/codeoutline/lua_bindings.lua +242 -34
  27. package/scripts/codeoutline/lua_literal.lua +38 -0
  28. package/scripts/codeoutline/main.lua +24 -3
  29. package/scripts/codeoutline/mcp.lua +13 -4
  30. package/scripts/codeoutline/navigation.lua +337 -96
  31. package/scripts/codeoutline/ordinary_bindings.lua +220 -0
  32. package/scripts/codeoutline/parse.lua +2 -0
  33. package/scripts/codeoutline/python_bindings.lua +112 -0
  34. package/scripts/codeoutline/query_report.lua +126 -0
  35. package/scripts/codeoutline/rules.lua +127 -0
  36. package/scripts/codeoutline/service.lua +11 -3
  37. package/scripts/codeoutline/static_dispatch.lua +534 -0
  38. package/scripts/codeoutline/update_sequence.lua +1 -1
  39. package/scripts/codeoutline/version.lua +1 -1
@@ -285,14 +285,31 @@ local scored_ids, scored_vals = {}, {}
285
285
  -- accept overrides the node kinds the ref kind may bind to (navigation only).
286
286
  local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept, evidence, js_context)
287
287
  if kind == 'annotation' then return nil end
288
+ if evidence and evidence:sub(1, 7) == 'cs:new:' then kind = 'new' end
288
289
  accept = accept or (kind == 'new' and NEWABLE) or (kind == 'ref' and VALUE) or CALLABLE
289
290
  if evidence == '' then evidence = nil end
291
+ local strategy = 'js/ts binding'
290
292
  local exact = js_resolution.resolve(G, f, name, kind, recv, evidence, js_context)
291
- if exact == nil then exact = lua_bindings.resolve(G, f, name, evidence, js_context and js_context.lua) end
293
+ if exact == nil and G.files[f].language == 'python' then
294
+ strategy = 'python binding'
295
+ exact = require('codeoutline.python_bindings').resolve(G, f, evidence, js_context)
296
+ end
297
+ if exact == nil and G.files[f].language == 'cpp' then
298
+ strategy = 'c++ declared receiver'
299
+ exact = require('codeoutline.cpp_bindings').resolve(G, f, src_id, name, evidence, deps, js_context)
300
+ end
301
+ if exact == nil and G.files[f].language == 'csharp' then
302
+ strategy = 'c# binding'
303
+ exact = require('codeoutline.csharp_bindings').resolve(G, f, src_id, name, kind, recv, evidence, js_context)
304
+ end
305
+ if exact == nil then
306
+ strategy = 'lua binding'
307
+ exact = lua_bindings.resolve(G, f, name, evidence, js_context and js_context.lua, recv)
308
+ end
292
309
  if exact ~= nil then
293
310
  local result = {}
294
311
  for _, id in ipairs(exact) do if accept[G.nodes[id].kind] then result[#result + 1] = id end end
295
- return result
312
+ return result, #result == 0 and 'binding target unresolved' or strategy
296
313
  end
297
314
  local cands = G.by_name[name]
298
315
  if G.files[f].language == 'kotlin' and not recv then
@@ -308,7 +325,7 @@ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept,
308
325
  end
309
326
  end
310
327
  end
311
- if not cands then return nil end
328
+ if not cands then return nil, 'no indexed target' end
312
329
  -- Score first: score() rejects other languages and statics of other
313
330
  -- files, so "is there a real definition?" is only asked among the
314
331
  -- candidates this call could actually reach. (Asking it over all
@@ -320,7 +337,7 @@ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept,
320
337
  -- score() rejects an unknown-receiver member call to an unrelated file;
321
338
  -- checking placement first skips most candidates of common method names.
322
339
  local local_only = kind == 'member_call' and not self_call
323
- local n, has_def = 0, false
340
+ local n, has_def, has_name = 0, false, false
324
341
  for i = 1, math.min(#cands, M.MAX_CANDIDATES) do
325
342
  local cid = cands[i]
326
343
  local node = G.nodes[cid]
@@ -331,26 +348,33 @@ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept,
331
348
  n = n + 1
332
349
  scored_ids[n], scored_vals[n] = cid, s
333
350
  if not is_decl(node) then has_def = true end
351
+ if node.name == name then has_name = true end
334
352
  end
335
353
  end
336
354
  end
337
- -- Prefer definitions: drop prototypes / in-class declarations when a
338
- -- reachable definition exists.
339
- local best, ties = nil, {}
355
+ -- Prefer real names over export aliases (which may be platform stubs),
356
+ -- then definitions over prototypes / in-class declarations.
357
+ local best, ties, tie_count = nil, {}, 0
340
358
  for i = 1, n do
341
359
  local cid, s = scored_ids[i], scored_vals[i]
342
- if not (has_def and is_decl(G.nodes[cid])) then
360
+ if not (has_def and is_decl(G.nodes[cid])) and (not has_name or G.nodes[cid].name == name) then
343
361
  if not best or s > best then
344
- best, ties = s, { cid }
345
- elseif s == best and #ties < M.MAX_TIES then
346
- ties[#ties + 1] = cid
362
+ best, ties, tie_count = s, { cid }, 1
363
+ elseif s == best then
364
+ tie_count = tie_count + 1
365
+ if #ties < M.MAX_TIES then ties[#ties + 1] = cid end
347
366
  end
348
367
  end
349
368
  end
350
369
  -- A lone candidate far away (score 0) is still a match for a unique
351
370
  -- name; many far candidates with nothing to separate them is a guess.
352
- if best and best <= 0 and #ties > 1 then return nil end
353
- return ties
371
+ if best and best <= 0 and #ties > 1 then
372
+ return nil, 'ambiguous name candidates' .. (#cands > M.MAX_CANDIDATES and '; candidate scan limit reached' or '')
373
+ end
374
+ local reason = #ties == 0 and 'no eligible target' or #ties > 1 and 'multiple name/locality candidates' or 'name/locality match'
375
+ if #cands > M.MAX_CANDIDATES then reason = reason .. '; candidate scan limit reached' end
376
+ if tie_count > M.MAX_TIES then reason = reason .. '; tied candidate limit reached' end
377
+ return ties, reason
354
378
  end
355
379
 
356
380
  -- Files compare by path; nodes by file path, then node order (ids ascend
@@ -395,6 +419,7 @@ end
395
419
  function M.new()
396
420
  return { files = {}, file_index = {}, file_order = {}, file_dir = {}, dir_ids = {}, nodes = {}, node_file = {},
397
421
  id_of = {}, by_name = {}, by_lname = {}, by_qualified = {}, by_owner = {}, by_base = {}, by_stem = {}, pkg_files = {},
422
+ lua_global_export_tables = { 'g_exports' },
398
423
  children = {}, dir_files = {}, dir_by_base = {}, ns_files = {},
399
424
  next_file = 0, next_node = 0, next_dir = 0, file_count = 0, node_count = 0 }
400
425
  end
@@ -446,6 +471,7 @@ function M.add_file(G, rel, rec)
446
471
  G.nodes[id], G.node_file[id] = node, f
447
472
  ids[n] = id
448
473
  insert(G, G.by_name, node.name, id, node_before)
474
+ for _, alias in ipairs(lua_bindings.aliases(node, G.lua_global_export_tables)) do insert(G, G.by_name, alias, id, node_before) end
449
475
  insert(G, G.by_lname, node.name:lower(), id, node_before)
450
476
  insert(G, G.by_qualified, node.qualified, id, node_before)
451
477
  local owner = M.owner(node.qualified)
@@ -463,6 +489,7 @@ function M.remove_file(G, rel)
463
489
  for _, id in ipairs(ids) do
464
490
  local node = G.nodes[id]
465
491
  remove(G, G.by_name, node.name, id, node_before)
492
+ for _, alias in ipairs(lua_bindings.aliases(node, G.lua_global_export_tables)) do remove(G, G.by_name, alias, id, node_before) end
466
493
  remove(G, G.by_lname, node.name:lower(), id, node_before)
467
494
  remove(G, G.by_qualified, node.qualified, id, node_before)
468
495
  local owner = M.owner(node.qualified)
@@ -485,9 +512,10 @@ end
485
512
 
486
513
  -- Symbol tables for every record of idx. Checkpoints per file: a cancelled
487
514
  -- build is simply discarded.
488
- function M.build(idx)
515
+ function M.build(idx, lua_context)
489
516
  local G = M.new()
490
517
  G.root = idx.root
518
+ G.lua_global_export_tables = (lua_context or lua_bindings.context(G)).global_export_tables
491
519
  local paths = {}
492
520
  for rel in pairs(idx.files) do paths[#paths + 1] = rel end
493
521
  table.sort(paths)
@@ -511,7 +539,12 @@ function M.query(symbols, opts)
511
539
  local dependencies, references = {}, {}
512
540
  local js_context = js_resolution.context(symbols)
513
541
  js_context.lua = lua_bindings.context(symbols)
514
- local indirect = require('codeoutline.indirect').query(symbols, opts.dispatch_rules)
542
+ js_context.ordinary = require('codeoutline.ordinary_bindings').context(symbols, js_context)
543
+ Q.stats.ordinary = js_context.ordinary.stats
544
+ local report = opts.explain and require('codeoutline.query_report').new() or nil
545
+ Q.report = report
546
+ local indirect = require('codeoutline.indirect').query(symbols, opts.dispatch_rules, report)
547
+ local static_dispatch = require('codeoutline.static_dispatch').query(symbols, report)
515
548
  local function deps(f)
516
549
  local entry = dependencies[f]
517
550
  if not entry then
@@ -520,13 +553,26 @@ function M.query(symbols, opts)
520
553
  end
521
554
  return entry[1], entry[2]
522
555
  end
523
- local function targets(f, i)
556
+ local function targets(f, i, explain)
557
+ if static_dispatch.handles(f, i) then return {} end
524
558
  local rec, ids = symbols.files[f], symbols.id_of[f]
525
559
  local refs = rec.refs
526
560
  local direct, paired = deps(f)
527
561
  Q.stats.resolved_refs = Q.stats.resolved_refs + 1
528
- return resolve_ref(symbols, f, ids[refs.from[i]], refs.name[i], refs.kind[i],
529
- refs.recv[i] or nil, direct, paired, nil, refs.lua_binding and refs.lua_binding[i] or refs.js_receiver and refs.js_receiver[i], js_context)
562
+ local found, reason = resolve_ref(symbols, f, ids[refs.from[i]], refs.name[i], refs.kind[i],
563
+ refs.recv[i] or nil, direct, paired, nil, js_context.ordinary.evidence(f, i), js_context)
564
+ if report and explain and (refs.kind[i] == 'call' or refs.kind[i] == 'member_call') then
565
+ if (not found or #found == 0) and explain == true then
566
+ report:add({ source = ids[refs.from[i]], line = refs.line[i], call = refs.name[i], reason = reason or 'unresolved reference' })
567
+ else
568
+ for _, dst in ipairs(found or {}) do
569
+ if explain == true or explain[dst] then
570
+ report:add({ source = ids[refs.from[i]], target = dst, line = refs.line[i], kind = reason })
571
+ end
572
+ end
573
+ end
574
+ end
575
+ return found
530
576
  end
531
577
  setmetatable(Q.out, { __index = function(cache, id)
532
578
  control.check()
@@ -549,9 +595,14 @@ function M.query(symbols, opts)
549
595
  edges[#edges + 1] = edge.line
550
596
  edges[#edges + 1] = edge.kind
551
597
  end
598
+ for _, edge in ipairs(static_dispatch.outgoing(id)) do
599
+ edges[#edges + 1] = edge.target
600
+ edges[#edges + 1] = edge.line
601
+ edges[#edges + 1] = edge.kind
602
+ end
552
603
  for _, i in ipairs(grouped[id] or {}) do
553
604
  control.check()
554
- for _, dst in ipairs(targets(f, i) or {}) do
605
+ for _, dst in ipairs(targets(f, i, true) or {}) do
555
606
  edges[#edges + 1] = dst
556
607
  edges[#edges + 1] = rec.refs.line[i]
557
608
  edges[#edges + 1] = rec.refs.kind[i]
@@ -566,12 +617,16 @@ function M.query(symbols, opts)
566
617
  for _, id in ipairs(ids) do
567
618
  if rawget(self.inn, id) == nil then
568
619
  wanted[id], names[symbols.nodes[id].name], found = true, true, true
620
+ for _, alias in ipairs(lua_bindings.aliases(symbols.nodes[id], symbols.lua_global_export_tables)) do names[alias] = true end
569
621
  end
570
622
  end
571
623
  if not found then return end
572
624
  local incoming = {}
573
625
  for id in pairs(wanted) do incoming[id] = {} end
574
626
  local indirect_incoming = indirect.incoming(wanted)
627
+ for src, edges in pairs(static_dispatch.incoming(wanted)) do
628
+ for _, edge in ipairs(edges) do push3(incoming, edge.target, src, edge.line, edge.kind) end
629
+ end
575
630
  for _, f in ipairs(symbols.file_order) do
576
631
  for _, src in ipairs(symbols.id_of[f]) do
577
632
  for _, edge in ipairs(indirect_incoming[src] or {}) do
@@ -583,10 +638,11 @@ function M.query(symbols, opts)
583
638
  -- Hot loop over every reference: numeric loops, hoisted fields, and the
584
639
  -- selective name test first. Checkpoints run per file and per chunk.
585
640
  local files = symbols.files
641
+ local candidates = js_context.ordinary.candidates(wanted)
586
642
  for _, f in ipairs(symbols.file_order) do
587
643
  control.check()
644
+ local alias_refs = js_context.ordinary.aliases(f, wanted, candidates)
588
645
  local aliases = {}
589
- for _, binding in ipairs(files[f].js_imports or {}) do aliases[binding.local_name] = true end
590
646
  if files[f].language == 'kotlin' then
591
647
  for _, imp in ipairs(files[f].imports) do
592
648
  if imp.alias and names[imp.path:match('([^.]+)$')] then aliases[imp.alias] = true end
@@ -598,8 +654,10 @@ function M.query(symbols, opts)
598
654
  for first = 1, count, 4096 do
599
655
  if first > 1 then control.check() end
600
656
  for i = first, math.min(count, first + 4095) do
601
- if (names[ref_names[i]] or aliases[ref_names[i]] or aliases[refs.recv[i]] or (refs.lua_binding and refs.lua_binding[i] ~= '')) and froms[i] > 0 then
602
- for _, dst in ipairs(targets(f, i) or {}) do
657
+ if (names[ref_names[i]] or aliases[ref_names[i]] or aliases[refs.recv[i]]
658
+ or alias_refs[i]
659
+ or (refs.lua_binding and refs.lua_binding[i] ~= '')) and froms[i] > 0 then
660
+ for _, dst in ipairs(targets(f, i, wanted) or {}) do
603
661
  if wanted[dst] then
604
662
  push3(incoming, dst, symbols.id_of[f][froms[i]], refs.line[i], refs.kind[i])
605
663
  end
@@ -21,13 +21,18 @@ local graph = require('codeoutline.graph')
21
21
  local pool = require('codeoutline.parse_pool')
22
22
 
23
23
  local M = {}
24
- local INDEX_PARSE = { ranges = false }
24
+ local INDEX_PARSE = { ranges = false, omit_signatures = true }
25
+ local LARGE_PARSE = { ranges = false }
26
+ -- Keep signatures for files beyond the shared query parser's 1 MiB limit.
27
+ local function parse_options(source)
28
+ return #source <= 1048576 and INDEX_PARSE or LARGE_PARSE
29
+ end
25
30
 
26
31
  -- Cache version: a checksum of the parser sources, so any parser change
27
32
  -- rebuilds stale caches instead of silently reusing old parse results.
28
33
  M.VERSION = (function()
29
34
  local parts = {}
30
- for _, m in ipairs({ 'xscan', 'text', 'common', 'indirect', 'js_evidence', 'lua_bindings', 'parse', 'lang.c', 'lang.cpp', 'lang.lua', 'lang.python',
35
+ for _, m in ipairs({ 'xscan', 'text', 'common', 'indirect', 'dispatch_aliases', 'static_dispatch', 'js_evidence', 'python_bindings', 'cpp_bindings', 'cpp_types', 'csharp_bindings', 'lua_bindings', 'foreign', 'foreign_lua', 'parse', 'lang.c', 'lang.cpp', 'lang.lua', 'lang.python',
31
36
  'lang.java', 'lang.kotlin', 'lang.go', 'lang.js', 'lang.csharp', 'lang.rust' }) do
32
37
  local file = package.searchpath and package.searchpath('codeoutline.' .. m, package.path)
33
38
  local h = file and io.open(file, 'rb')
@@ -229,7 +234,11 @@ end
229
234
  -- Symbol tables over the records, built on first use and then kept current
230
235
  -- by commit(). Hosts that never resolve names do not pay for them.
231
236
  function Index:symbols()
232
- if not self.symbol_tables then self.symbol_tables = graph.build(self) end
237
+ local ctx = require('codeoutline.lua_bindings').context(self)
238
+ if not self.symbol_tables or table.concat(self.symbol_tables.lua_global_export_tables, '\0')
239
+ ~= table.concat(ctx.global_export_tables, '\0') then
240
+ self.symbol_tables = graph.build(self, ctx)
241
+ end
233
242
  return self.symbol_tables
234
243
  end
235
244
 
@@ -262,7 +271,7 @@ function Index:query_source(rel)
262
271
  local changed = false
263
272
  if not rec.encoding then
264
273
  if encoding == 'gbk' then
265
- local parsed = assert(ci.parse(rel, src, INDEX_PARSE), 'cannot parse decoded source: ' .. rel)
274
+ local parsed = assert(ci.parse(rel, src, parse_options(src)), 'cannot parse decoded source: ' .. rel)
266
275
  parsed.size, parsed.crc, parsed.mtime, parsed.checked = rec.size, rec.crc, rec.mtime, rec.checked
267
276
  parsed.encoding = encoding
268
277
  self:commit(rel, parsed)
@@ -443,7 +452,7 @@ function M.load_file(abs, rel, max_bytes, old_size, old_crc)
443
452
  if old_size == #src and old_crc == crc then
444
453
  return { status = 'unchanged', checked = checked }
445
454
  end
446
- local ok, r = pcall(ci.parse, rel, src, INDEX_PARSE)
455
+ local ok, r = pcall(ci.parse, rel, src, parse_options(src))
447
456
  if not ok and control.is_interrupted(r) then error(r, 0) end
448
457
  -- A parser exception must not permanently hide a GBK file.
449
458
  -- Successful speculative parsing does not scan encoding here.
@@ -451,7 +460,7 @@ function M.load_file(abs, rel, max_bytes, old_size, old_crc)
451
460
  if xutils.to_utf8 then
452
461
  local decoded, encoding = xutils.to_utf8(src)
453
462
  if decoded then
454
- ok, r = pcall(ci.parse, rel, decoded, INDEX_PARSE)
463
+ ok, r = pcall(ci.parse, rel, decoded, parse_options(src))
455
464
  if not ok and control.is_interrupted(r) then error(r, 0) end
456
465
  if ok and r then r.encoding = encoding end
457
466
  end
@@ -57,8 +57,8 @@ local function execute(req)
57
57
  service.configure({ max_projects = lsp.MAX_ROOTS })
58
58
  service.refresh(root, { full_interval = 45, paths = req.paths })
59
59
  return true
60
- elseif req.method == 'explore' then
61
- local text, info = service.explore(root, req.query, { budget = req.budget })
60
+ elseif req.method == 'explore' or req.method == 'callers' then
61
+ local text, info = service[req.method](root, req.query, { budget = req.budget, depth = req.depth, max_nodes = req.max_nodes })
62
62
  return { text = text, truncated = info.truncated or false, refresh = info.refresh }
63
63
  elseif req.method == 'status' then return service.status(root)
64
64
  elseif req.method == 'rebuild' then return service.rebuild(root)
@@ -70,7 +70,7 @@ end
70
70
  -- threads; jobs arriving meanwhile queue behind it.
71
71
  local queue, active = {}, nil
72
72
 
73
- local function run(id, req)
73
+ local function run(id, req, reply)
74
74
  -- Draft text applies even to a cancelled request: the session counts it as sent.
75
75
  if req.session and req.open then
76
76
  local synced, err = pcall(navigation.sync, req)
@@ -84,7 +84,11 @@ local function run(id, req)
84
84
  control.callback = nil
85
85
  if not ok then result = tostring(result) end
86
86
  shared:delete('cancel:' .. id)
87
- assert(xthread.post(1, 'worker_result', id, ok, result))
87
+ if reply then
88
+ assert(_thread_replys[reply.router](reply.co, reply.sk, 'run_rpc', ok, result))
89
+ else
90
+ assert(xthread.post(1, 'worker_result', id, ok, result))
91
+ end
88
92
  end
89
93
 
90
94
  local function resume(...)
@@ -98,7 +102,7 @@ local function pump()
98
102
  local item = table.remove(queue, 1)
99
103
  active = coroutine.create(run)
100
104
  pool.bind(active)
101
- resume(item[1], item[2])
105
+ resume(item[1], item[2], item[3])
102
106
  end
103
107
  end
104
108
 
@@ -115,8 +119,12 @@ return {
115
119
  pool.sweep()
116
120
  if not active then service.sweep(); service.collect() end
117
121
  end,
118
- __thread_handle = function(_, op, id, req, failure)
119
- if op == 'run' then
122
+ __thread_handle = function(router, op, id, req, failure, rpc_request)
123
+ if type(router) == 'string' and req == 'run_rpc' then
124
+ -- RPC wire: router, coroutine id, session key, method, job id, request.
125
+ queue[#queue + 1] = { failure, rpc_request, { router = router, co = op, sk = id } }
126
+ pump()
127
+ elseif op == 'run' then
120
128
  queue[#queue + 1] = { id, req }
121
129
  pump()
122
130
  elseif op == 'parsed' then
@@ -1,11 +1,16 @@
1
1
  -- Explicit dispatch contracts. No wildcard API guesses or source evaluation.
2
2
  local control = require('codeoutline.control')
3
3
  local M = {}
4
+ local aliases = require('codeoutline.dispatch_aliases')
4
5
  local member = { ['.'] = true, [':'] = true, ['::'] = true, ['->'] = true }
5
6
 
6
- local function value(T, a, b)
7
+ local function value(T, a, b, language)
7
8
  if a == b and T.k[a] == 'str' then
8
9
  local raw = T:text(a)
10
+ if language == 'lua' then
11
+ local literal = require('codeoutline.lua_literal').decode(raw)
12
+ return literal and { literal = literal } or false
13
+ end
9
14
  local q = raw:sub(1, 1)
10
15
  if (q == '"' or q == "'") and raw:sub(-1) == q then
11
16
  local body = raw:sub(2, -2)
@@ -25,6 +30,7 @@ end
25
30
 
26
31
  function M.collect(r, T)
27
32
  local sites, calls = {}, {}
33
+ local expand = r.collect_dispatch and aliases.prepare(r, T)
28
34
  for index, ref in ipairs(r.refs) do
29
35
  if index % 128 == 0 then control.check() end
30
36
  local x = ref.tok
@@ -33,67 +39,74 @@ function M.collect(r, T)
33
39
  if close then
34
40
  local first = x
35
41
  while first > 2 and member[T:text(first - 1)] and T.k[first - 2] == 'id' do first = first - 2 end
36
- local call = value(T, first, x)
42
+ local call = value(T, first, x, r.language)
37
43
  if call then calls[call.symbol] = true end
38
- if not r.collect_dispatch or (type(r.collect_dispatch) == 'table' and not r.collect_dispatch[call and call.symbol]) then goto next_ref end
39
- local args, a, i = {}, x + 2, x + 2
44
+ if not r.collect_dispatch then goto next_ref end
45
+ local api = call and expand(call, first)
46
+ if not api or not api.symbol or type(r.collect_dispatch) == 'table'
47
+ and not r.collect_dispatch[call.symbol] and not r.collect_dispatch[api.symbol] then goto next_ref end
48
+ local args, ranges, a, i = {}, r.ranges and {} or nil, x + 2, x + 2
40
49
  while i <= close do
41
50
  if i == close or T:text(i) == ',' then
42
- args[#args + 1] = value(T, a, i - 1)
51
+ args[#args + 1] = expand(value(T, a, i - 1, r.language), a)
52
+ if ranges then ranges[#args] = a < i and { T.s[a] - 1, T.e[i - 1] } or false end
43
53
  a = i + 1
44
- elseif T:match(i) and T:match(i) > i then i = T:match(i) end
54
+ elseif T:match(i) and T:match(i) > i then i = T:match(i)
55
+ elseif r.language == 'lua' and T.k[i] == 'kw' and T:text(i) == 'function' then
56
+ local depth = 1
57
+ while depth > 0 and i < close do
58
+ i = i + 1
59
+ local kw = T.k[i] == 'kw' and T:text(i)
60
+ if kw == 'function' or kw == 'if' or kw == 'do' or kw == 'repeat' then depth = depth + 1
61
+ elseif kw == 'end' or kw == 'until' then depth = depth - 1 end
62
+ end
63
+ if depth > 0 then break end
64
+ end
45
65
  i = i + 1
46
66
  end
47
- if call then sites[#sites + 1] = { call = call.symbol, args = args, from = ref.from, line = ref.line } end
67
+ if call then sites[#sites + 1] = { call = call.symbol, api = api.symbol,
68
+ args = args, ranges = ranges, from = ref.from, line = ref.line } end
48
69
  end
49
70
  end
50
71
  ::next_ref::
51
72
  end
73
+ if not r.collect_dispatch and next(calls) then aliases.hints(r, T, calls) end
52
74
  local names = {}
53
75
  for name in pairs(calls) do names[#names + 1] = name end
54
76
  table.sort(names)
55
77
  if #names > 0 then r.dispatch_calls = names end
56
- if #sites > 0 then r.dispatch_sites = sites end
78
+ if r.collect_dispatch then r.dispatch_sites = sites end
57
79
  end
58
80
 
59
81
  local function rules_for(G, rules)
60
- if rules == nil and G.root then
61
- local f = io.open(G.root .. '/.codeoutline.json', 'rb')
62
- if f then
63
- local raw = f:read(65537); f:close()
64
- assert(#raw <= 65536, '.codeoutline.json exceeds 64 KiB')
65
- local ok, config = pcall(xutils.json_unpack, raw)
66
- assert(ok and type(config) == 'table', 'invalid .codeoutline.json')
67
- rules = config.dispatchRules
82
+ local config = require('codeoutline.rules')
83
+ if rules ~= nil then return config.dispatch(rules) end
84
+ return config.load(G.root).dispatch
85
+ end
86
+
87
+ function M.candidate(rec, call)
88
+ if rec.dispatch_sites then return true end -- explicit in-memory parse option
89
+ for _, name in ipairs(rec.dispatch_calls or {}) do
90
+ if name == call then return true end
91
+ if name:sub(1, 1) == '@' then
92
+ local root = name:sub(2)
93
+ if root == call or call:sub(1, #root + 1) == root .. '.'
94
+ or call:sub(1, #root + 1) == root .. ':'
95
+ or call:sub(1, #root + 2) == root .. '->' then return true end
68
96
  end
69
97
  end
70
- rules = rules or {}
71
- assert(type(rules) == 'table' and #rules <= 32, 'dispatchRules must contain at most 32 rules')
72
- for _, r in ipairs(rules) do
73
- assert(type(r) == 'table' and type(r.call) == 'string' and r.call:match('^[%a_][%w_%.:>%-]*$'), 'invalid dispatchRules.call')
74
- assert(r.kind == 'target' or r.kind == 'callback' or r.kind == 'event', 'invalid dispatchRules.kind')
75
- assert(r.language == nil or type(r.language) == 'string', 'invalid dispatchRules.language')
76
- for _, key in ipairs(r.kind == 'event' and { 'argument', 'keyArgument', 'handlerArgument' } or { 'argument' }) do
77
- local n = r[key]
78
- assert(type(n) == 'number' and n >= 1 and n <= 32 and n % 1 == 0, 'invalid dispatchRules.' .. key)
79
- end
80
- if r.kind == 'event' then assert(type(r.register) == 'string' and r.register:match('^[%a_][%w_%.:>%-]*$'), 'invalid dispatchRules.register') end
81
- end
82
- return rules
98
+ return false
83
99
  end
84
100
 
85
- function M.query(G, rules)
101
+ function M.query(G, rules, report)
86
102
  rules = rules_for(G, rules)
87
103
  if #rules == 0 then return { outgoing = function() return {} end, incoming = function() return {} end } end
88
104
  local graph = require('codeoutline.graph')
89
105
  local names, cache, order, bytes, registrations_by_rule = {}, {}, {}, 0, {}
106
+ local dependencies = {}
90
107
  for _, rule in ipairs(rules) do names[rule.call] = true; if rule.register then names[rule.register] = true end end
91
- local function candidate(rec, call)
92
- if rec.dispatch_sites then return true end -- explicit in-memory parse option
93
- for _, name in ipairs(rec.dispatch_calls or {}) do if name == call then return true end end
94
- return false
95
- end
96
- local function sites(f)
108
+ local candidate = M.candidate
109
+ local function sites(f, literals)
97
110
  local rec = G.files[f]
98
111
  if rec.dispatch_sites then return rec.dispatch_sites end
99
112
  if cache[f] then return cache[f].sites end
@@ -106,7 +119,26 @@ function M.query(G, rules)
106
119
  if rec.encoding ~= 'utf-8' and rec.encoding ~= 'utf-8-bom' then
107
120
  source = assert(xutils.to_utf8(raw, rec.encoding == 'gbk' and 'gbk' or 'auto'))
108
121
  end
109
- local parsed = assert(require('codeoutline.parse').parse(rec.path, source, { ranges = false, dispatch = names }))
122
+ -- Target rules can only obtain a literal command from this file. Reject
123
+ -- broad module hints before scope/registry analysis, but decode escaped
124
+ -- Lua literals so aliases and alternate spellings remain candidates.
125
+ if literals and rec.language == 'lua' then
126
+ local relevant = false
127
+ for name in pairs(literals) do if source:find(name, 1, true) then relevant = true; break end end
128
+ if not relevant and source:find('\\', 1, true) then
129
+ local T = require('codeoutline.lang.lua').lang:tokenize(source)
130
+ local decode = require('codeoutline.lua_literal').decode
131
+ for i = 1, T.n do
132
+ if i % 4096 == 0 then control.check() end
133
+ if T.k[i] == 'str' and literals[decode(T:text(i))] then relevant = true; break end
134
+ end
135
+ end
136
+ -- Do not cache negatives: later incoming calls can request other
137
+ -- targets within this same query, and outgoing needs all sites.
138
+ if not relevant then return {} end
139
+ end
140
+ local parsed = assert(require('codeoutline.parse').parse(rec.path, source,
141
+ { ranges = false, dispatch = names, dispatch_only = true }))
110
142
  assert(#parsed.nodes == #rec.nodes, 'source encoding changed; refresh index before indirect query: ' .. rec.path)
111
143
  for i, n in ipairs(parsed.nodes) do
112
144
  assert(n.qualified == rec.nodes[i].qualified and n.line == rec.nodes[i].line,
@@ -124,13 +156,49 @@ function M.query(G, rules)
124
156
  return result
125
157
  end
126
158
  local function targets(f, site, arg, literal)
159
+ if arg and not literal and arg.node then return { G.id_of[f][arg.node] } end
160
+ if arg and not literal and arg.import then
161
+ local imp = arg.import
162
+ local rec = G.files[f]
163
+ if rec.language == 'javascript' or rec.language == 'typescript' then
164
+ local js = require('codeoutline.js_resolution')
165
+ dependencies.js = dependencies.js or js.context(G)
166
+ return js.imported(G, f, imp.path, arg.member, dependencies.js) or {}
167
+ elseif rec.language == 'lua' then
168
+ local bindings = require('codeoutline.lua_bindings')
169
+ dependencies.lua = dependencies.lua or bindings.context(G)
170
+ local result = {}
171
+ for _, file in ipairs(bindings.modules(G, { path = imp.path, kind = imp.loader }, dependencies.lua)) do
172
+ local record = G.files[file]
173
+ local exports = require('codeoutline.foreign').exports(record, 'lua', imp.path) or record.lua_exports
174
+ local id = exports and exports[arg.member]
175
+ if id then result[#result + 1] = G.id_of[file][id] end
176
+ end
177
+ return result
178
+ elseif rec.language == 'python' then
179
+ local base = imp.path:gsub('%.', '/')
180
+ local file = G.file_index[base .. '.py'] or G.file_index[base .. '/__init__.py']
181
+ local result = {}
182
+ for _, id in ipairs(file and G.id_of[file] or {}) do
183
+ local n = G.nodes[id]
184
+ if not n.parent and n.name == arg.member then result[#result + 1] = id end
185
+ end
186
+ return result
187
+ end
188
+ return {}
189
+ end
127
190
  local name
128
191
  if arg then
129
192
  if literal then name = arg.literal else name = arg.symbol end
130
193
  end
131
194
  if not name or not name:match('^[%a_][%w_%.:>%-]*$') then return {} end
132
195
  local short = name:match('([%w_]+)$')
133
- local deps, paired = graph.dependencies(G, f)
196
+ local dependency = dependencies[f]
197
+ if not dependency then
198
+ local deps, paired = graph.dependencies(G, f)
199
+ dependency = { deps, paired }; dependencies[f] = dependency
200
+ end
201
+ local deps, paired = dependency[1], dependency[2]
134
202
  local view = G
135
203
  if short ~= name then
136
204
  local filtered = {}
@@ -151,11 +219,12 @@ function M.query(G, rules)
151
219
  if (not rule.language or rule.language == rec.language) and candidate(rec, rule.register) then
152
220
  for _, site in ipairs(sites(f)) do
153
221
  local key = site.args[rule.keyArgument]
154
- if site.call == rule.register and key and key.literal then
222
+ if (site.call == rule.register or site.api == rule.register) and key and key.literal then
155
223
  local bucket = rec.language .. '\0' .. key.literal
156
224
  registrations[bucket] = registrations[bucket] or {}
157
225
  for _, id in ipairs(targets(f, site, site.args[rule.handlerArgument])) do
158
- registrations[bucket][id] = true
226
+ registrations[bucket][id] = registrations[bucket][id]
227
+ or (report and (rec.path .. ':' .. site.line) or true)
159
228
  end
160
229
  end
161
230
  end
@@ -167,23 +236,51 @@ function M.query(G, rules)
167
236
  end
168
237
  local function scan(only, wanted)
169
238
  local out = {}
170
- for ri, rule in ipairs(rules) do
171
- local files = only and { G.node_file[only] } or G.file_order
172
- for _, f in ipairs(files) do
173
- control.check()
174
- local rec = G.files[f]
239
+ local literals
240
+ if wanted then
241
+ local targets_only = true
242
+ for _, rule in ipairs(rules) do if rule.kind ~= 'target' then targets_only = false; break end end
243
+ if targets_only then
244
+ literals = {}
245
+ for id in pairs(wanted) do
246
+ local node = G.nodes[id]
247
+ literals[node.name], literals[node.qualified or node.name] = true, true
248
+ end
249
+ end
250
+ end
251
+ local files = only and { G.node_file[only] } or G.file_order
252
+ for _, f in ipairs(files) do
253
+ control.check()
254
+ local rec, file_sites = G.files[f], nil
255
+ for ri, rule in ipairs(rules) do
175
256
  if (not rule.language or rule.language == rec.language) and candidate(rec, rule.call) then
176
- for _, site in ipairs(sites(f)) do
177
- if site.call == rule.call and site.from > 0 and (not only or G.id_of[f][site.from] == only) then
178
- local ids, arg = {}, site.args[rule.argument]
257
+ -- Process every applicable rule while this file is available,
258
+ -- even if event registration scans evict it from the FIFO.
259
+ file_sites = file_sites or sites(f, literals)
260
+ for _, site in ipairs(file_sites) do
261
+ if (site.call == rule.call or site.api == rule.call) and site.from > 0 and (not only or G.id_of[f][site.from] == only) then
262
+ local ids, arg, registered = {}, site.args[rule.argument], nil
179
263
  if rule.kind == 'event' then
180
- local registered = registrations(ri, rule)
264
+ registered = registrations(ri, rule)
181
265
  for id in pairs(arg and arg.literal and registered[rec.language .. '\0' .. arg.literal] or {}) do ids[#ids + 1] = id end
182
266
  table.sort(ids, function(a, b) return graph.before(G, a, b) end)
183
267
  else ids = targets(f, site, arg, rule.kind == 'target') end
184
268
  local src = G.id_of[f][site.from]
269
+ if report and only and #ids == 0 then
270
+ local static = arg and (rule.kind == 'callback' and arg.symbol or rule.kind ~= 'callback' and arg.literal)
271
+ report:add({ source = src, line = site.line, rule = ri, argument = rule.argument, call = rule.call,
272
+ reason = not static and 'dispatch argument is dynamic, unsupported or missing'
273
+ or rule.kind == 'event' and 'no resolved registration for event key'
274
+ or 'dispatch target unresolved or ambiguous' })
275
+ end
185
276
  for _, id in ipairs(ids) do
186
277
  if not wanted or wanted[id] then
278
+ if report then
279
+ local bucket = registered and arg and arg.literal and registered[rec.language .. '\0' .. arg.literal]
280
+ report:add({ source = src, target = id, line = site.line, rule = ri, argument = rule.argument,
281
+ call = rule.call, kind = 'configured ' .. rule.kind,
282
+ registered_at = bucket and bucket[id] or nil })
283
+ end
187
284
  out[src] = out[src] or {}
188
285
  out[src][#out[src] + 1] = { target = id, line = site.line, kind = 'indirect:' .. rule.kind .. '#' .. ri }
189
286
  end
@@ -195,7 +292,8 @@ function M.query(G, rules)
195
292
  end
196
293
  return out
197
294
  end
198
- return { outgoing = function(id) return scan(id)[id] or {} end, incoming = function(wanted) return scan(nil, wanted) end }
295
+ return { outgoing = function(id) return scan(id)[id] or {} end, incoming = function(wanted) return scan(nil, wanted) end,
296
+ resolve_targets = targets }
199
297
  end
200
298
 
201
299
  return M