@codua/codeoutline-win32-x64 0.2.4 → 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.
@@ -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
@@ -186,7 +191,8 @@ local function recursive(node, recv)
186
191
  end
187
192
 
188
193
  -- Targets of one occurrence of name in file f, enclosed by node index from.
189
- 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
190
196
  local src = from > 0 and V.id_of[f][from] or nil
191
197
  local direct, paired = deps(V, f)
192
198
  local rkind, accept = kind, nil
@@ -212,6 +218,22 @@ local function resolve_site(V, f, from, name, kind, recv, evidence)
212
218
  if #targets == 0 and (kind == 'call' or kind == 'ref') then
213
219
  targets = graph.resolve(V, f, src, name, kind, recv or nil, direct, paired, PROTOTYPE, evidence, V.js_context) or {}
214
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
215
237
  if C_FAMILY[V.files[f].language] then
216
238
  -- C binds only what its includes make visible; a lone far definition
217
239
  -- (another library's macro) is not the one a system header declares.
@@ -241,8 +263,8 @@ local function find_cursor(doc, offset, strict)
241
263
  local a = refs.start[i]
242
264
  if a and inside(a, refs.stop[i]) then
243
265
  return { from = refs.from[i], name = refs.name[i], kind = refs.kind[i], recv = refs.recv[i],
244
- evidence = refs.lua_binding and refs.lua_binding[i] or refs.js_receiver and refs.js_receiver[i],
245
- 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 }
246
268
  end
247
269
  end
248
270
  end
@@ -277,20 +299,80 @@ local function cursor(doc, offset)
277
299
  from, span = n, node.end_byte - node.start_byte
278
300
  end
279
301
  end
280
- 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 }
281
310
  end
282
311
 
283
312
  -- Source document for file f: its overlay, else the disk file read once per request.
284
313
  local function source_doc(V, f)
285
- 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
286
318
  local doc = V.disk[f]
287
319
  if doc == nil then
288
- doc = documents.read(V.idx:abs(V.files[f].path)) or false
320
+ doc = documents.read(V.idx:abs(rec.path), indexed) or false
289
321
  V.disk[f] = doc
290
322
  end
291
323
  return doc or nil
292
324
  end
293
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
+
294
376
  local function uri_of(V, f)
295
377
  local doc = source_doc(V, f)
296
378
  return doc and doc.uri or documents.uri(V.idx:abs(V.files[f].path))
@@ -328,7 +410,7 @@ local function item(V, id)
328
410
  local loc, doc, hit = location(V, id)
329
411
  local node = V.nodes[id]
330
412
  local n, f = index_of(V, id)
331
- 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,
332
414
  range = hit and doc:range(hit.start_byte, hit.end_byte) or loc.range, selectionRange = loc.range,
333
415
  data = { root = V.root, path = V.files[f].path, index = n, name = node.name, qualified = node.qualified } }
334
416
  end
@@ -419,12 +501,13 @@ local function scan(V, targets, name)
419
501
  local files = {}
420
502
  for i, f in ipairs(V.file_order) do files[i] = f end
421
503
  for _, f in ipairs(V.new_files) do files[#files + 1] = f end
504
+ local candidates = V.js_context.ordinary.candidates(targets)
422
505
  for _, f in ipairs(files) do
423
506
  control.check()
507
+ local alias_refs = V.js_context.ordinary.aliases(f, targets, candidates)
424
508
  local refs = V.files[f].refs
425
509
  local names = refs.name
426
510
  local aliases = {}
427
- for _, binding in ipairs(V.files[f].js_imports or {}) do aliases[binding.local_name] = true end
428
511
  if V.files[f].language == 'kotlin' then
429
512
  for _, imp in ipairs(V.files[f].imports) do
430
513
  if imp.alias and imp.path:match('([^.]+)$') == name then aliases[imp.alias] = true end
@@ -432,9 +515,11 @@ local function scan(V, targets, name)
432
515
  end
433
516
  for i = 1, #names do
434
517
  if i % 4096 == 0 then control.check() end
435
- if wanted[names[i]] 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
436
521
  if os.clock() > deadline then return hits, true end
437
- 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
438
523
  if targets[dst] then hits[#hits + 1] = { f, i }; break end
439
524
  end
440
525
  if #hits >= M.MAX_RESULTS then return hits, true end
@@ -548,83 +633,11 @@ end
548
633
 
549
634
  -- Project-local declarative rules; read per request so configuration edits are live.
550
635
  local function definition_rules(root)
551
- local file = io.open(root .. '/.codeoutline.json', 'rb')
552
- if not file then return {} end
553
- local raw = file:read(65537); file:close()
554
- assert(#raw <= 65536, '.codeoutline.json exceeds 64 KiB')
555
- local ok, config = pcall(xutils.json_unpack, raw)
556
- assert(ok and type(config) == 'table', 'invalid .codeoutline.json')
557
- local rules = config.definitionRules or {}
558
- assert(type(rules) == 'table' and #rules <= 32, 'definitionRules must contain at most 32 rules')
559
- for _, rule in ipairs(rules) do
560
- assert(type(rule) == 'table' and (rule.language == nil or rule.language == 'lua'),
561
- 'definitionRules currently support language lua')
562
- assert(type(rule.call) == 'string' and rule.call:match('^[%a_][%w_%.:]*$'),
563
- 'definitionRules.call must be a qualified function name')
564
- assert(type(rule.argument) == 'number' and rule.argument >= 1 and rule.argument <= 32
565
- and rule.argument % 1 == 0,
566
- 'definitionRules.argument must be an integer from 1 to 32')
567
- local kind = rule.targetKind or 'call'
568
- assert(kind == 'call' or kind == 'declaration' or kind == 'key',
569
- "definitionRules.targetKind must be 'call', 'declaration' or 'key'")
570
- if kind ~= 'call' then
571
- assert(rule.target == nil and rule.targetArgument == nil,
572
- 'declaration and key rules must not set target or targetArgument')
573
- if rule.qualifiers ~= nil then
574
- assert(type(rule.qualifiers) == 'table', 'definitionRules.qualifiers must be an array of names')
575
- local qualifiers = {}
576
- for _, q in ipairs(rule.qualifiers) do
577
- assert(type(q) == 'string' and q:match('^[%a_][%w_]*$'),
578
- 'definitionRules.qualifiers must contain plain names')
579
- qualifiers[q] = true
580
- end
581
- rule.qualifiers = qualifiers
582
- end
583
- else
584
- assert(type(rule.target) == 'string' and rule.target:match('^[%a_][%w_%.:]*$'),
585
- 'definitionRules.target must be a qualified function name')
586
- assert(type(rule.targetArgument) == 'number' and rule.targetArgument >= 1
587
- and rule.targetArgument <= 32 and rule.targetArgument % 1 == 0,
588
- 'definitionRules.targetArgument must be an integer from 1 to 32')
589
- end
590
- end
591
- return rules
636
+ return require('codeoutline.rules').load(root).definitions
592
637
  end
593
638
 
594
639
  -- Decode literals without running source code. Lua long strings and quoted escapes.
595
- local function string_value(raw)
596
- local eq, body = raw:match('^%[(=*)%[(.*)%]%1%]$')
597
- if eq then return body:gsub('^\r?\n', '', 1) end
598
- local quote = raw:sub(1, 1)
599
- if (quote ~= '"' and quote ~= "'") or raw:sub(-1) ~= quote then return nil end
600
- local escaped = { a = '\a', b = '\b', f = '\f', n = '\n', r = '\r', t = '\t', v = '\v',
601
- ['\\'] = '\\', ['"'] = '"', ["'"] = "'", ['\n'] = '\n' }
602
- local out, i, stop = {}, 2, #raw - 1
603
- while i <= stop do
604
- local c = raw:sub(i, i)
605
- if c ~= '\\' then out[#out + 1] = c; i = i + 1
606
- else
607
- c = raw:sub(i + 1, i + 1); i = i + 2
608
- if escaped[c] then out[#out + 1] = escaped[c]
609
- elseif c == 'z' then
610
- while i <= stop and raw:sub(i, i):match('%s') do i = i + 1 end
611
- elseif c == '\r' then
612
- if raw:sub(i, i) == '\n' then i = i + 1 end
613
- out[#out + 1] = '\n'
614
- elseif c == 'x' then
615
- local hex = raw:sub(i, i + 1)
616
- if not hex:match('^%x%x$') then return nil end
617
- out[#out + 1] = string.char(tonumber(hex, 16)); i = i + 2
618
- elseif c:match('%d') then
619
- local digits = (c .. raw:sub(i, stop)):match('^%d%d?%d?')
620
- local n = tonumber(digits)
621
- if n > 255 then return nil end
622
- out[#out + 1] = string.char(n); i = i + #digits - 1
623
- else return nil end
624
- end
625
- end
626
- return table.concat(out)
627
- end
640
+ local string_value = require('codeoutline.lua_literal').decode
628
641
 
629
642
  local function call_name(T, i)
630
643
  if T.k[i] ~= 'id' or T:text(i + 1) ~= '(' then return nil end
@@ -697,9 +710,183 @@ local function call_matches(name, rule_name)
697
710
  return name:find(':') ~= nil and name:match('([%w_]+)$') == rule_name
698
711
  end
699
712
 
700
- 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)
701
883
  if doc:parse().language ~= 'lua' then return nil end
702
- 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
703
890
  if #rules == 0 then return nil end
704
891
  local tokens = doc:tokens()
705
892
  local matched = {}
@@ -797,6 +984,27 @@ local function configured_definition(V, doc, offset)
797
984
  return result, false
798
985
  end
799
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
+
800
1008
  local function at(req)
801
1009
  local doc = request_doc(req)
802
1010
  if not doc then return nil end
@@ -807,7 +1015,7 @@ local function at(req)
807
1015
  local c = cursor(doc, offset)
808
1016
  local targets = {}
809
1017
  if c and c.decl then targets = { V.id_of[f][c.decl] }
810
- 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
811
1019
  return V, doc, c, targets, f, offset
812
1020
  end
813
1021
 
@@ -816,7 +1024,12 @@ local handlers = {}
816
1024
  function handlers.lsp_definition(req)
817
1025
  local V, doc, c, targets, f, offset = at(req)
818
1026
  if not V then return false end
819
- 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
820
1033
  if configured then return configured, incomplete end
821
1034
  local line = doc:position(offset).line + 1
822
1035
  local importer = false
@@ -851,12 +1064,21 @@ function handlers.lsp_signature(req)
851
1064
  if not doc then return false end
852
1065
  local T, offset = doc:tokens(), doc:offset(req.position)
853
1066
  if not T then return false end
1067
+ local language = doc:parse().language
854
1068
  local stack = {}
855
1069
  local close = { [')'] = '(', [']'] = '[', ['}'] = '{' }
1070
+ local lua_blocks = { ['function'] = true, ['if'] = true, ['do'] = true, ['repeat'] = true }
856
1071
  for i = 1, T.n do
857
1072
  if T.s[i] - 1 >= offset then break end
858
1073
  if i % 128 == 0 then control.check() end
859
- 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
860
1082
  local s = T:text(i)
861
1083
  if s == '(' or s == '[' or s == '{' then
862
1084
  stack[#stack + 1] = { token = i, delimiter = s, argument = 0 }
@@ -868,9 +1090,12 @@ function handlers.lsp_signature(req)
868
1090
  end
869
1091
  end
870
1092
  local call
871
- local kotlin = doc:parse().language == 'kotlin'
1093
+ local kotlin = language == 'kotlin'
872
1094
  for i = #stack, 1, -1 do
873
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
874
1099
  local name_token = entry.token - 1
875
1100
  if kotlin and entry.delimiter == '(' and T:text(name_token) == '>' then
876
1101
  local depth, k = 1, name_token - 1
@@ -896,7 +1121,7 @@ function handlers.lsp_signature(req)
896
1121
  for _, id in ipairs(targets) do
897
1122
  local node = V.nodes[id]
898
1123
  local foreign = require('codeoutline.foreign').signature(node, doc:parse().language, T:text(name_token))
899
- local label = node.sig
1124
+ local label = M.detail(V, V.node_file[id], node).sig
900
1125
  if foreign and not seen[foreign.label] then
901
1126
  foreign.activeParameter = #foreign.parameters > 0 and math.min(call.argument, #foreign.parameters - 1) or nil
902
1127
  signatures[#signatures + 1] = foreign
@@ -943,7 +1168,7 @@ function handlers.lsp_hover(req)
943
1168
  local node, rec = V.nodes[id], V.files[V.node_file[id]]
944
1169
  local foreign = require('codeoutline.foreign').signature(node, doc:parse().language, doc.source:sub(c.first + 1, c.last))
945
1170
  parts[#parts + 1] = '```' .. (foreign and doc:parse().language or rec.language or '') .. '\n'
946
- .. (foreign and foreign.label or node.sig or node.qualified) .. '\n```'
1171
+ .. (foreign and foreign.label or M.detail(V, V.node_file[id], node).sig or node.qualified) .. '\n```'
947
1172
  parts[#parts + 1] = node.kind .. ' `' .. node.qualified .. '` — ' .. rec.path .. ':' .. node.line
948
1173
  end
949
1174
  return { contents = { kind = 'markdown', value = table.concat(parts, '\n\n') }, range = doc:range(c.first, c.last) }
@@ -1005,7 +1230,7 @@ function handlers.lsp_outgoing(req)
1005
1230
  for i = 1, #refs.name do
1006
1231
  if i % 128 == 0 then control.check() end
1007
1232
  if refs.from[i] == from then
1008
- 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
1009
1234
  local group = groups[dst]
1010
1235
  if not group then
1011
1236
  group = { to = item(V, dst), fromRanges = {} }
@@ -1031,7 +1256,8 @@ function handlers.lsp_references(req)
1031
1256
  set[id] = true
1032
1257
  end
1033
1258
  set = expand(V, set)
1034
- 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
1035
1261
  local hits, incomplete = scan(V, set, name)
1036
1262
  local result, t0 = {}, os.clock()
1037
1263
  if req.includeDeclaration then