@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
@@ -11,8 +11,10 @@ local documents = require('codeoutline.documents')
11
11
  local paths = require('codeoutline.path')
12
12
  local control = require('codeoutline.control')
13
13
  local lsp = require('codeoutline.lsp')
14
+ local parse = require('codeoutline.parse')
14
15
  -- Reference scans stop at a result count or time budget and report truncation.
15
- local M = { MAX_RESULTS = 1000, MAX_HOVER = 3, SCAN_SECONDS = 3, POSITION_SECONDS = 2, MAX_INCLUDES = 2000 }
16
+ local M = { MAX_RESULTS = 1000, MAX_HOVER = 3, SCAN_SECONDS = 3, POSITION_SECONDS = 2, MAX_INCLUDES = 2000,
17
+ MAX_FILE_BYTES = 1048576, CACHE_FILES = 64 }
16
18
 
17
19
  local sessions = {}
18
20
  local ANY = setmetatable({}, { __index = function() return true end })
@@ -75,6 +77,9 @@ local function view(session, root)
75
77
  next_file = G.next_file, next_node = G.next_node, next_dir = G.next_dir }, { __index = G })
76
78
  V.js_context = require('codeoutline.js_resolution').context(V)
77
79
  V.js_context.lua = require('codeoutline.lua_bindings').context(V)
80
+ V.js_context.ordinary = require('codeoutline.ordinary_bindings').context(V, V.js_context, function(f)
81
+ return V.drafts[f] and V.drafts[f].source
82
+ end)
78
83
  for _, name in ipairs({ 'files', 'nodes', 'node_file', 'id_of', 'file_index', 'file_dir' }) do
79
84
  V[name] = setmetatable({}, { __index = G[name] })
80
85
  end
@@ -109,6 +114,10 @@ local function view(session, root)
109
114
  local list = added[node.name]
110
115
  if not list then list = {}; added[node.name] = list end
111
116
  list[#list + 1] = id
117
+ for _, alias in ipairs(require('codeoutline.lua_bindings').aliases(node, V.lua_global_export_tables)) do
118
+ added[alias] = added[alias] or {}
119
+ added[alias][#added[alias] + 1] = id
120
+ end
112
121
  end
113
122
  return f
114
123
  end
@@ -182,7 +191,8 @@ local function recursive(node, recv)
182
191
  end
183
192
 
184
193
  -- Targets of one occurrence of name in file f, enclosed by node index from.
185
- local function resolve_site(V, f, from, name, kind, recv, evidence)
194
+ local function resolve_site(V, f, from, name, kind, recv, evidence, offset, index)
195
+ if index then evidence = V.js_context.ordinary.evidence(f, index) end
186
196
  local src = from > 0 and V.id_of[f][from] or nil
187
197
  local direct, paired = deps(V, f)
188
198
  local rkind, accept = kind, nil
@@ -208,6 +218,22 @@ local function resolve_site(V, f, from, name, kind, recv, evidence)
208
218
  if #targets == 0 and (kind == 'call' or kind == 'ref') then
209
219
  targets = graph.resolve(V, f, src, name, kind, recv or nil, direct, paired, PROTOTYPE, evidence, V.js_context) or {}
210
220
  end
221
+ -- A native object: the local's initializer chain names the metatable whose
222
+ -- methods the C side registers (`local pool = mod.open()`, then `pool:read()`).
223
+ if #targets == 0 and recv and (kind == 'member_call' or kind == 'member_symbol')
224
+ and V.files[f].language == 'lua' then
225
+ local bindings = require('codeoutline.lua_bindings')
226
+ local ctx = V.js_context.lua
227
+ local rec, T
228
+ if offset and require('codeoutline.foreign').has_candidates(V, ctx) then rec, T = M.full(V, f) end
229
+ if T then
230
+ local target = bindings.receiver(V, rec, T, recv, offset, V.js_context.lua)
231
+ for _, entry in ipairs(bindings.member(V, target, name)) do
232
+ local id = V.id_of[entry.file] and V.id_of[entry.file][entry.index]
233
+ if id then targets[#targets + 1] = id end
234
+ end
235
+ end
236
+ end
211
237
  if C_FAMILY[V.files[f].language] then
212
238
  -- C binds only what its includes make visible; a lone far definition
213
239
  -- (another library's macro) is not the one a system header declares.
@@ -237,8 +263,8 @@ local function find_cursor(doc, offset, strict)
237
263
  local a = refs.start[i]
238
264
  if a and inside(a, refs.stop[i]) then
239
265
  return { from = refs.from[i], name = refs.name[i], kind = refs.kind[i], recv = refs.recv[i],
240
- evidence = refs.lua_binding and refs.lua_binding[i] or refs.js_receiver and refs.js_receiver[i],
241
- first = a, last = refs.stop[i] }
266
+ evidence = refs.binding and refs.binding[i] or refs.lua_binding and refs.lua_binding[i] or refs.js_receiver and refs.js_receiver[i],
267
+ first = a, last = refs.stop[i], index = i }
242
268
  end
243
269
  end
244
270
  end
@@ -273,20 +299,80 @@ local function cursor(doc, offset)
273
299
  from, span = n, node.end_byte - node.start_byte
274
300
  end
275
301
  end
276
- return { from = from, name = T:text(t), kind = kind, recv = recv, first = T.s[t] - 1, last = T.e[t] }
302
+ local evidence
303
+ if rec.language == 'csharp' then
304
+ local first = t
305
+ while (T:text(first - 1) == '.' or T:text(first - 1) == '::') and T.k[first - 2] == 'id' do first = first - 2 end
306
+ local name = require('codeoutline.csharp_bindings').name(T.src:sub(T.s[first], T.e[t]))
307
+ evidence = name and 'cs:name:' .. name .. '\0' .. (T.s[first] - 1)
308
+ end
309
+ return { from = from, name = T:text(t), kind = kind, recv = recv, first = T.s[t] - 1, last = T.e[t], evidence = evidence }
277
310
  end
278
311
 
279
312
  -- Source document for file f: its overlay, else the disk file read once per request.
280
313
  local function source_doc(V, f)
281
- if V.drafts[f] then return V.drafts[f] end
314
+ if V.drafts and V.drafts[f] then return V.drafts[f] end
315
+ local rec = V.files[f]
316
+ local indexed = rec.crc ~= nil and rec or nil
317
+ if not V.disk then return V.root and documents.read(V.root .. '/' .. rec.path, indexed) end
282
318
  local doc = V.disk[f]
283
319
  if doc == nil then
284
- doc = documents.read(V.idx:abs(V.files[f].path)) or false
320
+ doc = documents.read(V.idx:abs(rec.path), indexed) or false
285
321
  V.disk[f] = doc
286
322
  end
287
323
  return doc or nil
288
324
  end
289
325
 
326
+ -- Full parse (record plus tokens) of one file, cached per record. Shared with
327
+ -- completion, which needs tokens for types this index does not store.
328
+ local full_cache, full_order = setmetatable({}, { __mode = 'k' }), {}
329
+ function M.full(V, f, native)
330
+ local draft = V.drafts and V.drafts[f]
331
+ if draft then
332
+ draft.full = draft.full or {}
333
+ local entry = draft.full
334
+ if not entry.rec or native and not entry.foreign then
335
+ entry.rec, entry.tokens = parse.parse_tokens(draft.path or V.files[f].path, draft.source, { foreign = native })
336
+ entry.foreign = native
337
+ end
338
+ return entry.rec, entry.tokens, draft
339
+ end
340
+ local key = V.files[f]
341
+ local entry = full_cache[key]
342
+ if entry == nil or native and not entry.foreign then
343
+ local fresh = entry == nil
344
+ if (key.size or 0) > M.MAX_FILE_BYTES then return nil end
345
+ local doc = source_doc(V, f)
346
+ -- Read/parse failures are transient, unlike a successful empty result.
347
+ if not doc then return nil end
348
+ local rec, tokens = parse.parse_tokens(key.path, doc.source, { foreign = native })
349
+ if not rec then return nil end
350
+ entry = { rec = rec, tokens = tokens, foreign = native }
351
+ full_cache[key] = entry
352
+ if fresh then full_order[#full_order + 1] = key end
353
+ if #full_order > M.CACHE_FILES then full_cache[table.remove(full_order, 1)] = nil end
354
+ end
355
+ if not entry then return nil end
356
+ return entry.rec, entry.tokens
357
+ end
358
+
359
+ -- Restore display/type details without attaching them to resident index nodes.
360
+ function M.detail(V, f, node)
361
+ if node.sig then return node end
362
+ local rec = M.full(V, f)
363
+ if not rec then return node end
364
+ if not rec.detail_nodes then
365
+ local map = {}
366
+ for n, original in ipairs(V.files[f].nodes) do
367
+ local current = rec.nodes[n]
368
+ if current and current.name == original.name and current.qualified == original.qualified
369
+ and current.line == original.line and current.kind == original.kind then map[original] = current end
370
+ end
371
+ rec.detail_nodes = map
372
+ end
373
+ return rec.detail_nodes[node] or node
374
+ end
375
+
290
376
  local function uri_of(V, f)
291
377
  local doc = source_doc(V, f)
292
378
  return doc and doc.uri or documents.uri(V.idx:abs(V.files[f].path))
@@ -324,7 +410,7 @@ local function item(V, id)
324
410
  local loc, doc, hit = location(V, id)
325
411
  local node = V.nodes[id]
326
412
  local n, f = index_of(V, id)
327
- return { name = node.name, kind = lsp.symbol_kind(node), detail = node.sig or node.qualified, uri = loc.uri,
413
+ return { name = node.name, kind = lsp.symbol_kind(node), detail = (hit and hit.sig) or node.sig or node.qualified, uri = loc.uri,
328
414
  range = hit and doc:range(hit.start_byte, hit.end_byte) or loc.range, selectionRange = loc.range,
329
415
  data = { root = V.root, path = V.files[f].path, index = n, name = node.name, qualified = node.qualified } }
330
416
  end
@@ -408,15 +494,20 @@ end
408
494
  -- Hot names can match thousands of files; past the budget the scan stops.
409
495
  local function scan(V, targets, name)
410
496
  local hits, deadline = {}, os.clock() + M.SCAN_SECONDS
497
+ local wanted = { [name] = true }
498
+ for id in pairs(targets) do
499
+ for _, alias in ipairs(require('codeoutline.lua_bindings').aliases(V.nodes[id], V.lua_global_export_tables)) do wanted[alias] = true end
500
+ end
411
501
  local files = {}
412
502
  for i, f in ipairs(V.file_order) do files[i] = f end
413
503
  for _, f in ipairs(V.new_files) do files[#files + 1] = f end
504
+ local candidates = V.js_context.ordinary.candidates(targets)
414
505
  for _, f in ipairs(files) do
415
506
  control.check()
507
+ local alias_refs = V.js_context.ordinary.aliases(f, targets, candidates)
416
508
  local refs = V.files[f].refs
417
509
  local names = refs.name
418
510
  local aliases = {}
419
- for _, binding in ipairs(V.files[f].js_imports or {}) do aliases[binding.local_name] = true end
420
511
  if V.files[f].language == 'kotlin' then
421
512
  for _, imp in ipairs(V.files[f].imports) do
422
513
  if imp.alias and imp.path:match('([^.]+)$') == name then aliases[imp.alias] = true end
@@ -424,9 +515,11 @@ local function scan(V, targets, name)
424
515
  end
425
516
  for i = 1, #names do
426
517
  if i % 4096 == 0 then control.check() end
427
- if names[i] == name or aliases[names[i]] or aliases[refs.recv[i]] or (refs.lua_binding and refs.lua_binding[i] ~= '') then
518
+ if wanted[names[i]] or aliases[names[i]] or aliases[refs.recv[i]]
519
+ or alias_refs[i]
520
+ or (refs.lua_binding and refs.lua_binding[i] ~= '') then
428
521
  if os.clock() > deadline then return hits, true end
429
- for _, dst in ipairs(resolve_site(V, f, refs.from[i], names[i], refs.kind[i], refs.recv[i], refs.lua_binding and refs.lua_binding[i] or refs.js_receiver and refs.js_receiver[i])) do
522
+ for _, dst in ipairs(resolve_site(V, f, refs.from[i], names[i], refs.kind[i], refs.recv[i], nil, refs.start and refs.start[i] or nil, i)) do
430
523
  if targets[dst] then hits[#hits + 1] = { f, i }; break end
431
524
  end
432
525
  if #hits >= M.MAX_RESULTS then return hits, true end
@@ -540,83 +633,11 @@ end
540
633
 
541
634
  -- Project-local declarative rules; read per request so configuration edits are live.
542
635
  local function definition_rules(root)
543
- local file = io.open(root .. '/.codeoutline.json', 'rb')
544
- if not file then return {} end
545
- local raw = file:read(65537); file:close()
546
- assert(#raw <= 65536, '.codeoutline.json exceeds 64 KiB')
547
- local ok, config = pcall(xutils.json_unpack, raw)
548
- assert(ok and type(config) == 'table', 'invalid .codeoutline.json')
549
- local rules = config.definitionRules or {}
550
- assert(type(rules) == 'table' and #rules <= 32, 'definitionRules must contain at most 32 rules')
551
- for _, rule in ipairs(rules) do
552
- assert(type(rule) == 'table' and (rule.language == nil or rule.language == 'lua'),
553
- 'definitionRules currently support language lua')
554
- assert(type(rule.call) == 'string' and rule.call:match('^[%a_][%w_%.:]*$'),
555
- 'definitionRules.call must be a qualified function name')
556
- assert(type(rule.argument) == 'number' and rule.argument >= 1 and rule.argument <= 32
557
- and rule.argument % 1 == 0,
558
- 'definitionRules.argument must be an integer from 1 to 32')
559
- local kind = rule.targetKind or 'call'
560
- assert(kind == 'call' or kind == 'declaration' or kind == 'key',
561
- "definitionRules.targetKind must be 'call', 'declaration' or 'key'")
562
- if kind ~= 'call' then
563
- assert(rule.target == nil and rule.targetArgument == nil,
564
- 'declaration and key rules must not set target or targetArgument')
565
- if rule.qualifiers ~= nil then
566
- assert(type(rule.qualifiers) == 'table', 'definitionRules.qualifiers must be an array of names')
567
- local qualifiers = {}
568
- for _, q in ipairs(rule.qualifiers) do
569
- assert(type(q) == 'string' and q:match('^[%a_][%w_]*$'),
570
- 'definitionRules.qualifiers must contain plain names')
571
- qualifiers[q] = true
572
- end
573
- rule.qualifiers = qualifiers
574
- end
575
- else
576
- assert(type(rule.target) == 'string' and rule.target:match('^[%a_][%w_%.:]*$'),
577
- 'definitionRules.target must be a qualified function name')
578
- assert(type(rule.targetArgument) == 'number' and rule.targetArgument >= 1
579
- and rule.targetArgument <= 32 and rule.targetArgument % 1 == 0,
580
- 'definitionRules.targetArgument must be an integer from 1 to 32')
581
- end
582
- end
583
- return rules
636
+ return require('codeoutline.rules').load(root).definitions
584
637
  end
585
638
 
586
639
  -- Decode literals without running source code. Lua long strings and quoted escapes.
587
- local function string_value(raw)
588
- local eq, body = raw:match('^%[(=*)%[(.*)%]%1%]$')
589
- if eq then return body:gsub('^\r?\n', '', 1) end
590
- local quote = raw:sub(1, 1)
591
- if (quote ~= '"' and quote ~= "'") or raw:sub(-1) ~= quote then return nil end
592
- local escaped = { a = '\a', b = '\b', f = '\f', n = '\n', r = '\r', t = '\t', v = '\v',
593
- ['\\'] = '\\', ['"'] = '"', ["'"] = "'", ['\n'] = '\n' }
594
- local out, i, stop = {}, 2, #raw - 1
595
- while i <= stop do
596
- local c = raw:sub(i, i)
597
- if c ~= '\\' then out[#out + 1] = c; i = i + 1
598
- else
599
- c = raw:sub(i + 1, i + 1); i = i + 2
600
- if escaped[c] then out[#out + 1] = escaped[c]
601
- elseif c == 'z' then
602
- while i <= stop and raw:sub(i, i):match('%s') do i = i + 1 end
603
- elseif c == '\r' then
604
- if raw:sub(i, i) == '\n' then i = i + 1 end
605
- out[#out + 1] = '\n'
606
- elseif c == 'x' then
607
- local hex = raw:sub(i, i + 1)
608
- if not hex:match('^%x%x$') then return nil end
609
- out[#out + 1] = string.char(tonumber(hex, 16)); i = i + 2
610
- elseif c:match('%d') then
611
- local digits = (c .. raw:sub(i, stop)):match('^%d%d?%d?')
612
- local n = tonumber(digits)
613
- if n > 255 then return nil end
614
- out[#out + 1] = string.char(n); i = i + #digits - 1
615
- else return nil end
616
- end
617
- end
618
- return table.concat(out)
619
- end
640
+ local string_value = require('codeoutline.lua_literal').decode
620
641
 
621
642
  local function call_name(T, i)
622
643
  if T.k[i] ~= 'id' or T:text(i + 1) ~= '(' then return nil end
@@ -689,9 +710,183 @@ local function call_matches(name, rule_name)
689
710
  return name:find(':') ~= nil and name:match('([%w_]+)$') == rule_name
690
711
  end
691
712
 
692
- local function configured_definition(V, doc, offset)
713
+ local function service_argument(rule)
714
+ if rule.serviceArgument then return rule.serviceArgument end
715
+ if rule.call == 'skynet.call' or rule.call == 'skynet.send' then return 1 end
716
+ return rule.argument - 1
717
+ end
718
+
719
+ -- Service filenames are candidate locations, not proof of a runtime RPC edge.
720
+ -- Keep every exact suffix match if several indexed service roots use the name.
721
+ local function service_files(V, site, rule)
722
+ local arg = site.args[service_argument(rule)]
723
+ if not arg then return nil end
724
+ local name = arg.service
725
+ if not name and (rule.serviceArgument or rule.call:match('^dservice%.')
726
+ or rule.call == 'skynet.call' or rule.call == 'skynet.send'
727
+ or rule.call == 'cluster.call' or rule.call == 'cluster.send') then name = arg.literal end
728
+ if not name or not name:match('^[%w_][%w_/-]*$') then return nil end
729
+ local suffix, result = name .. '.lua', {}
730
+ local function add(file)
731
+ local rec = V.files[file]
732
+ if rec.language == 'lua' and (rec.path == suffix or rec.path:sub(-#suffix - 1) == '/' .. suffix) then result[file] = true end
733
+ end
734
+ for _, file in ipairs(V.file_order) do control.check(); add(file) end
735
+ for _, file in ipairs(V.new_files) do control.check(); add(file) end
736
+ return next(result) and result or nil
737
+ end
738
+
739
+ -- Shared rules reuse graph sites and aliases. String targets navigate to project
740
+ -- declarations: remote services need not be lexical dependencies of the caller.
741
+ local function shared_definition(V, doc, offset, rules)
742
+ local record = doc:parse()
743
+ local language = record.language
744
+ local names, applicable, literals = {}, {}, {}
745
+ local function literal_position(name, argument)
746
+ literals[name] = literals[name] or {}
747
+ literals[name][argument] = true
748
+ end
749
+ for _, rule in ipairs(rules) do
750
+ if rule.shared and (not rule.language or rule.language == language)
751
+ and require('codeoutline.indirect').candidate(record, rule.call) then
752
+ applicable[#applicable + 1] = rule
753
+ names[rule.call] = true
754
+ if rule.register then names[rule.register] = true end
755
+ if rule.kind == 'target' or rule.kind == 'event' then literal_position(rule.call, rule.argument) end
756
+ if rule.kind == 'target' and service_argument(rule) > 0 then literal_position(rule.call, service_argument(rule)) end
757
+ if rule.kind == 'event' then literal_position(rule.register, rule.keyArgument) end
758
+ end
759
+ end
760
+ if #applicable == 0 then return nil end
761
+ local parsed = {}
762
+ local function sites(source)
763
+ if not parsed[source] then
764
+ local record = source:dispatch(names)
765
+ parsed[source] = record and record.dispatch_sites or {}
766
+ -- Preserve the explicit Lua literal contract even when conservative
767
+ -- alias analysis cannot prove a receiver's identity. These sites
768
+ -- belong only to navigation, never to the relationship resolver.
769
+ if language == 'lua' then
770
+ local T = source:tokens()
771
+ local known = {}
772
+ for _, site in ipairs(parsed[source]) do
773
+ for argument in pairs(literals[site.call] or {}) do
774
+ local arg, range = site.args[argument], site.ranges[argument]
775
+ if arg and arg.literal and range then
776
+ known[site.call .. ':' .. argument .. ':' .. range[1]] = true
777
+ end
778
+ end
779
+ end
780
+ for i = 1, T.n do
781
+ if i % 128 == 0 then control.check() end
782
+ local name = call_name(T, i)
783
+ if literals[name] then
784
+ local site = { call = name, args = {}, ranges = {} }
785
+ for argument in pairs(literals[name]) do
786
+ local value, first, last = literal_argument(T, i, argument)
787
+ if value and not known[name .. ':' .. argument .. ':' .. first] then
788
+ site.args[argument] = { literal = value }
789
+ site.ranges[argument] = { first, last }
790
+ end
791
+ end
792
+ if next(site.args) then parsed[source][#parsed[source] + 1] = site end
793
+ end
794
+ end
795
+ end
796
+ end
797
+ return parsed[source]
798
+ end
799
+ local matched = {}
800
+ for _, site in ipairs(sites(doc)) do
801
+ for _, rule in ipairs(applicable) do
802
+ local range, arg = site.ranges[rule.argument], site.args[rule.argument]
803
+ if (site.call == rule.call or site.api == rule.call) and arg and range
804
+ and range[1] <= offset and offset < range[2]
805
+ and (rule.kind == 'callback' and arg.symbol or rule.kind ~= 'callback' and arg.literal) then
806
+ matched[#matched + 1] = { rule = rule, site = site, arg = arg }
807
+ end
808
+ end
809
+ end
810
+ if #matched == 0 then return nil end
811
+ local result, seen, events = {}, {}, {}
812
+ local deadline = os.clock() + M.SCAN_SECONDS
813
+ local function add(loc)
814
+ local key = loc.uri .. ':' .. loc.range.start.line .. ':' .. loc.range.start.character
815
+ if not seen[key] then result[#result + 1] = loc; seen[key] = true end
816
+ return #result >= M.MAX_RESULTS
817
+ end
818
+ local f = V.add(doc)
819
+ local resolver
820
+ for _, match in ipairs(matched) do
821
+ local rule = match.rule
822
+ if rule.kind == 'event' then events[#events + 1] = match
823
+ elseif rule.kind == 'target' then
824
+ local name = match.arg.literal
825
+ local services = language == 'lua' and service_files(V, match.site, rule)
826
+ local short = name:match('([%w_]+)$')
827
+ if short and name:match('^[%a_][%w_%.:>%-]*$') then
828
+ for _, id in ipairs(V.by_name[short]) do
829
+ control.check()
830
+ if os.clock() > deadline then return result, true end
831
+ local node = V.nodes[id]
832
+ if DECLARABLE[node.kind] and V.files[V.node_file[id]].language == language
833
+ and (name == short or node.qualified == name)
834
+ and (not services or services[V.node_file[id]])
835
+ and qualifier_matches(rule, graph.owner(node.qualified or node.name)) then
836
+ if add(location(V, id)) then return result, true end
837
+ end
838
+ end
839
+ end
840
+ else
841
+ resolver = resolver or require('codeoutline.indirect').query(V, { rule }).resolve_targets
842
+ for _, id in ipairs(resolver(f, match.site, match.arg, rule.kind == 'target')) do
843
+ control.check()
844
+ if os.clock() > deadline then return result, true end
845
+ if add(location(V, id)) then return result, true end
846
+ end
847
+ end
848
+ end
849
+ if #events == 0 then return result, false end
850
+ local files = {}
851
+ for _, file in ipairs(V.file_order) do files[#files + 1] = file end
852
+ for _, file in ipairs(V.new_files) do files[#files + 1] = file end
853
+ table.sort(files, function(a, b) return V.files[a].path < V.files[b].path end)
854
+ for _, file in ipairs(files) do
855
+ control.check()
856
+ if os.clock() > deadline then return result, true end
857
+ local candidate = false
858
+ if V.files[file].language == language then
859
+ for _, match in ipairs(events) do
860
+ if require('codeoutline.indirect').candidate(V.files[file], match.rule.register) then candidate = true; break end
861
+ end
862
+ end
863
+ if candidate then
864
+ local source = source_doc(V, file)
865
+ if source then
866
+ for _, site in ipairs(sites(source)) do
867
+ for _, match in ipairs(events) do
868
+ local rule = match.rule
869
+ local arg, range = site.args[rule.keyArgument], site.ranges[rule.keyArgument]
870
+ if (site.call == rule.register or site.api == rule.register) and arg and range
871
+ and arg.literal == match.arg.literal then
872
+ if add({ uri = source.uri, range = source:range(range[1], range[2]) }) then return result, true end
873
+ end
874
+ end
875
+ end
876
+ end
877
+ end
878
+ end
879
+ return result, false
880
+ end
881
+
882
+ local function legacy_definition(V, doc, offset, rules)
693
883
  if doc:parse().language ~= 'lua' then return nil end
694
- local rules = definition_rules(V.root)
884
+ local legacy = {}
885
+ for _, rule in ipairs(rules) do
886
+ if (not rule.shared or rule.legacy)
887
+ and (not rule.language or rule.language == 'lua') then legacy[#legacy + 1] = rule end
888
+ end
889
+ rules = legacy
695
890
  if #rules == 0 then return nil end
696
891
  local tokens = doc:tokens()
697
892
  local matched = {}
@@ -789,6 +984,27 @@ local function configured_definition(V, doc, offset)
789
984
  return result, false
790
985
  end
791
986
 
987
+ local function configured_definition(V, doc, offset)
988
+ local rules = definition_rules(V.root)
989
+ if #rules == 0 then return nil end
990
+ local shared, incomplete = shared_definition(V, doc, offset, rules)
991
+ local legacy, legacy_incomplete = legacy_definition(V, doc, offset, rules)
992
+ if shared == nil then return legacy, legacy_incomplete end
993
+ local seen = {}
994
+ local function key(loc)
995
+ return loc.uri .. ':' .. loc.range.start.line .. ':' .. loc.range.start.character
996
+ end
997
+ for _, loc in ipairs(shared) do seen[key(loc)] = true end
998
+ for _, loc in ipairs(legacy or {}) do
999
+ if not seen[key(loc)] then
1000
+ if #shared >= M.MAX_RESULTS then return shared, true end
1001
+ seen[key(loc)] = true
1002
+ shared[#shared + 1] = loc
1003
+ end
1004
+ end
1005
+ return shared, incomplete or legacy_incomplete or false
1006
+ end
1007
+
792
1008
  local function at(req)
793
1009
  local doc = request_doc(req)
794
1010
  if not doc then return nil end
@@ -799,7 +1015,7 @@ local function at(req)
799
1015
  local c = cursor(doc, offset)
800
1016
  local targets = {}
801
1017
  if c and c.decl then targets = { V.id_of[f][c.decl] }
802
- elseif c then targets = resolve_site(V, f, c.from, c.name, c.kind, c.recv, c.evidence) end
1018
+ elseif c then targets = resolve_site(V, f, c.from, c.name, c.kind, c.recv, c.evidence, c.first, c.index) end
803
1019
  return V, doc, c, targets, f, offset
804
1020
  end
805
1021
 
@@ -808,7 +1024,12 @@ local handlers = {}
808
1024
  function handlers.lsp_definition(req)
809
1025
  local V, doc, c, targets, f, offset = at(req)
810
1026
  if not V then return false end
811
- local configured, incomplete = configured_definition(V, doc, offset)
1027
+ local configured, incomplete
1028
+ -- Rule arguments are strings or referenced values, never the name of a
1029
+ -- call/declaration itself. Avoid collecting dispatch sites for ordinary F12.
1030
+ if not c or not c.decl and c.kind ~= 'call' and c.kind ~= 'member_call' and c.kind ~= 'new' then
1031
+ configured, incomplete = configured_definition(V, doc, offset)
1032
+ end
812
1033
  if configured then return configured, incomplete end
813
1034
  local line = doc:position(offset).line + 1
814
1035
  local importer = false
@@ -843,12 +1064,21 @@ function handlers.lsp_signature(req)
843
1064
  if not doc then return false end
844
1065
  local T, offset = doc:tokens(), doc:offset(req.position)
845
1066
  if not T then return false end
1067
+ local language = doc:parse().language
846
1068
  local stack = {}
847
1069
  local close = { [')'] = '(', [']'] = '[', ['}'] = '{' }
1070
+ local lua_blocks = { ['function'] = true, ['if'] = true, ['do'] = true, ['repeat'] = true }
848
1071
  for i = 1, T.n do
849
1072
  if T.s[i] - 1 >= offset then break end
850
1073
  if i % 128 == 0 then control.check() end
851
- if T.k[i] == 'op' then
1074
+ if language == 'lua' and T.k[i] == 'kw' then
1075
+ local s = T:text(i)
1076
+ if lua_blocks[s] then
1077
+ stack[#stack + 1] = { token = i, delimiter = s, block = true, argument = 0 }
1078
+ elseif s == 'end' or s == 'until' then
1079
+ while #stack > 0 do if table.remove(stack).block then break end end
1080
+ end
1081
+ elseif T.k[i] == 'op' then
852
1082
  local s = T:text(i)
853
1083
  if s == '(' or s == '[' or s == '{' then
854
1084
  stack[#stack + 1] = { token = i, delimiter = s, argument = 0 }
@@ -860,9 +1090,12 @@ function handlers.lsp_signature(req)
860
1090
  end
861
1091
  end
862
1092
  local call
863
- local kotlin = doc:parse().language == 'kotlin'
1093
+ local kotlin = language == 'kotlin'
864
1094
  for i = #stack, 1, -1 do
865
1095
  local entry = stack[i]
1096
+ -- A callback body is not an argument-editing context for its caller.
1097
+ -- Inner calls can still supply help; after `end` the outer call resumes.
1098
+ if entry.delimiter == 'function' then break end
866
1099
  local name_token = entry.token - 1
867
1100
  if kotlin and entry.delimiter == '(' and T:text(name_token) == '>' then
868
1101
  local depth, k = 1, name_token - 1
@@ -887,8 +1120,13 @@ function handlers.lsp_signature(req)
887
1120
  local signatures, seen = {}, {}
888
1121
  for _, id in ipairs(targets) do
889
1122
  local node = V.nodes[id]
890
- local label = node.sig
891
- if REFERENCE[node.kind] and label and not seen[label] then
1123
+ local foreign = require('codeoutline.foreign').signature(node, doc:parse().language, T:text(name_token))
1124
+ local label = M.detail(V, V.node_file[id], node).sig
1125
+ if foreign and not seen[foreign.label] then
1126
+ foreign.activeParameter = #foreign.parameters > 0 and math.min(call.argument, #foreign.parameters - 1) or nil
1127
+ signatures[#signatures + 1] = foreign
1128
+ seen[foreign.label] = true
1129
+ elseif not foreign and REFERENCE[node.kind] and label and not seen[label] then
892
1130
  local sig_doc = documents.new(uri_of(V, V.node_file[id]), label, 0)
893
1131
  local S = sig_doc:tokens()
894
1132
  local opening
@@ -928,7 +1166,9 @@ function handlers.lsp_hover(req)
928
1166
  for k, id in ipairs(targets) do
929
1167
  if k > M.MAX_HOVER then break end
930
1168
  local node, rec = V.nodes[id], V.files[V.node_file[id]]
931
- parts[#parts + 1] = '```' .. (rec.language or '') .. '\n' .. (node.sig or node.qualified) .. '\n```'
1169
+ local foreign = require('codeoutline.foreign').signature(node, doc:parse().language, doc.source:sub(c.first + 1, c.last))
1170
+ parts[#parts + 1] = '```' .. (foreign and doc:parse().language or rec.language or '') .. '\n'
1171
+ .. (foreign and foreign.label or M.detail(V, V.node_file[id], node).sig or node.qualified) .. '\n```'
932
1172
  parts[#parts + 1] = node.kind .. ' `' .. node.qualified .. '` — ' .. rec.path .. ':' .. node.line
933
1173
  end
934
1174
  return { contents = { kind = 'markdown', value = table.concat(parts, '\n\n') }, range = doc:range(c.first, c.last) }
@@ -990,7 +1230,7 @@ function handlers.lsp_outgoing(req)
990
1230
  for i = 1, #refs.name do
991
1231
  if i % 128 == 0 then control.check() end
992
1232
  if refs.from[i] == from then
993
- for _, dst in ipairs(resolve_site(V, f, from, refs.name[i], refs.kind[i], refs.recv[i], refs.lua_binding and refs.lua_binding[i] or refs.js_receiver and refs.js_receiver[i])) do
1233
+ for _, dst in ipairs(resolve_site(V, f, from, refs.name[i], refs.kind[i], refs.recv[i], nil, refs.start and refs.start[i] or nil, i)) do
994
1234
  local group = groups[dst]
995
1235
  if not group then
996
1236
  group = { to = item(V, dst), fromRanges = {} }
@@ -1016,7 +1256,8 @@ function handlers.lsp_references(req)
1016
1256
  set[id] = true
1017
1257
  end
1018
1258
  set = expand(V, set)
1019
- local name = V.files[V.node_file[targets[1]]].language == 'kotlin' and V.nodes[targets[1]].name or c.name
1259
+ local language = V.files[V.node_file[targets[1]]].language
1260
+ local name = (language == 'kotlin' or language == 'csharp') and V.nodes[targets[1]].name or c.name
1020
1261
  local hits, incomplete = scan(V, set, name)
1021
1262
  local result, t0 = {}, os.clock()
1022
1263
  if req.includeDeclaration then