@codua/codeoutline-win32-x64 0.3.9 → 0.5.0

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.
@@ -601,7 +601,9 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
601
601
  if value.incomplete then
602
602
  if trace then trace:record('limit', { id = id, method = req.method, uri = req.uri,
603
603
  position = req.position, epoch = navigation_epoch }) end
604
- local message = req.method == 'lsp_definition'
604
+ local message = value.bm25_building
605
+ and 'CodeOutline: building the BM25 definition index in the background; retry shortly'
606
+ or req.method == 'lsp_definition'
605
607
  and 'CodeOutline: definition lookup reached a candidate, source or visibility limit; some definitions may be omitted'
606
608
  or req.method == 'lsp_references'
607
609
  and 'CodeOutline: results were truncated; too many matching references'
@@ -45,6 +45,8 @@ M.tools = array({
45
45
  { name = 'codeoutline_explore', description = 'Query symbols, line-numbered source, call paths and neighbors. Explicit unknown-suffix project file paths are parsed on demand using supported syntax or a lang: hint. Unresolved calls and string commands may include explicitly unconfirmed candidates; these are not call edges and require source inspection. Long bodies preserve relevant call-site windows within the output budget. Optional lang:/kind:/path:/name: filters select starting symbols only; dependencies may cross languages and files. Refreshes automatically.',
46
46
  inputSchema = { type = 'object', properties = { projectPath = project,
47
47
  query = { type = 'string', minLength = 1, maxLength = 4096 },
48
+ bm25Match = {type = 'string', enum = array({'any', 'all'}), default = 'any',
49
+ description = 'When the normal lookup misses, require any or all tokenized query terms in the BM25 fallback.'},
48
50
  budget = { type = 'integer', minimum = 256, maximum = 262144, default = 16000 } },
49
51
  required = array({ 'query' }), additionalProperties = false } },
50
52
  { name = 'codeoutline_status', description = 'Refresh and report project index, cache, scanner and parser statistics.',
@@ -185,7 +187,7 @@ function M.new(config, submit, cancel)
185
187
  codeoutline_type_hierarchy = type_hierarchy, codeoutline_type_references = type_references, codeoutline_affected = affected })[tool.name]
186
188
  if engine then
187
189
  local ok, err = pcall(engine.validate, tool.name == 'codeoutline_affected' and args.files or args.query, { budget = args.budget, depth = args.depth,
188
- max_nodes = args.max_nodes, direction = args.direction })
190
+ max_nodes = args.max_nodes, direction = args.direction, bm25_match = args.bm25Match })
189
191
  if not ok then error_result(-32602, tostring(err)); return end
190
192
  end
191
193
  local token = object(params._meta) and params._meta.progressToken or nil
@@ -205,7 +207,7 @@ function M.new(config, submit, cancel)
205
207
  end
206
208
  local job, submit_error = submit({ method = tool.name:sub(#'codeoutline_' + 1), projectPath = root,
207
209
  allowedRoots = config.roots, query = args.query, files = args.files, budget = args.budget, depth = args.depth,
208
- max_nodes = args.max_nodes, direction = args.direction }, function(ok, value)
210
+ max_nodes = args.max_nodes, direction = args.direction, bm25_match = args.bm25Match }, function(ok, value)
209
211
  if self.pending[id] ~= p then return end
210
212
  self.pending[id] = nil
211
213
  local output = ok and (engine and value.text or xutils.json_pack(value)) or tostring(value)
@@ -218,6 +220,7 @@ function M.new(config, submit, cancel)
218
220
  metadata.state = metadata.coverage.partial and 'partial' or 'ok'
219
221
  metadata.generation = value.generation
220
222
  metadata.resolution = value.resolution
223
+ metadata.bm25 = value.bm25
221
224
  else metadata.error = coverage.error(output) end
222
225
  end
223
226
  result({ content = array({ { type = 'text', text = text.valid(output) } }), isError = not ok,
@@ -32,6 +32,8 @@ function M.summarize(info)
32
32
  if info.unresolved_files and #info.unresolved_files > 0 then add('unresolved_files') end
33
33
  if info.files_without_symbols and #info.files_without_symbols > 0 then add('files_without_symbols') end
34
34
  if info.on_demand_rejected and #info.on_demand_rejected > 0 then add('on_demand_rejected') end
35
+ if info.bm25 and info.bm25.state == 'building' then add('bm25_building') end
36
+ if info.bm25 and info.bm25.state == 'unavailable' then add('bm25_unavailable') end
35
37
  if info.unresolved and #info.unresolved > 0 then add('unresolved_types') end
36
38
  if (info.unresolved_sites or 0) > 0 then add('unresolved_type_sites') end
37
39
  if (info.ambiguous_sites or 0) > 0 then add('ambiguous_type_sites') end
@@ -8,6 +8,16 @@ local types = { class = true, struct = true, union = true, interface = true, rec
8
8
  local values = { variable = true, constant = true, field = true, enum_member = true }
9
9
  local function normalized(name) return (name:gsub('::', '.'):gsub(':', '.'):gsub('^%.', '')) end
10
10
 
11
+ local function lua_receiver_score(receiver, qualified, path)
12
+ local owner = graph.owner(normalized(qualified))
13
+ if owner and owner:match('[^.]+$') == receiver then return 350 end
14
+ path, receiver = path:lower(), receiver:lower()
15
+ local filename = path:match('[^/]+$')
16
+ if filename and (filename:gsub('%.lua$', '')) == receiver then return 300 end
17
+ if ('/' .. path):find('/' .. receiver .. '/', 1, true) then return 200 end
18
+ return 0
19
+ end
20
+
11
21
  -- An empty answer leaves the editor and the agent nothing to inspect, so a
12
22
  -- failed binding still gets name-based suggestions. They remain unconfirmed
13
23
  -- candidates: never call edges, references or index data.
@@ -22,7 +32,7 @@ function M.allowed(site)
22
32
  or site.kind == 'member_call' or site.kind == 'member_symbol'
23
33
  end
24
34
 
25
- function M.find(G, f, site, ctx, lua_self, lua_initializers)
35
+ function M.find(G, f, site, ctx, lua_self, lua_initializers, candidates)
26
36
  local info = { truncated = site and site.macro_state == 'limited' or false,
27
37
  scanned = 0, reason = 'exact name; binding unconfirmed' }
28
38
  if not lua_self and not M.allowed(site) then return {}, info end
@@ -44,10 +54,10 @@ function M.find(G, f, site, ctx, lua_self, lua_initializers)
44
54
  qualified = qualified or normalized(site.recv .. '.' .. site.name)
45
55
  elseif not unresolved_receiver and (site.kind == 'member_call' or site.kind == 'member_symbol') then return {}, info end
46
56
  end
47
- local list = G.by_name[site.name] or {}
57
+ local list = candidates or G.by_name[site.name] or {}
48
58
  -- A Lua member alias is only reachable through its dotted name; its bare
49
59
  -- key stays unpublished unless the owner is a configured export table.
50
- local dotted = language == 'lua' and qualified and G.by_name[qualified]
60
+ local dotted = not candidates and language == 'lua' and qualified and G.by_name[qualified]
51
61
  if dotted and dotted ~= list then
52
62
  local merged, seen = {}, {}
53
63
  for _, id in ipairs(list) do merged[#merged + 1], seen[id] = id, true end
@@ -57,6 +67,10 @@ function M.find(G, f, site, ctx, lua_self, lua_initializers)
57
67
  list = merged
58
68
  end
59
69
  local source_dir = G.files[f].path:match('^(.*)/') or ''
70
+ local receiver = language == 'lua' and not lua_self and site.recv
71
+ -- These are hints for unconfirmed candidates. The source can import a
72
+ -- module conditionally even when the resolver cannot bind its receiver.
73
+ local imported = receiver and graph.visible_dependencies(G, f, ctx)
60
74
  -- `bare` drops the receiver qualifier: the member name alone still names a
61
75
  -- definition worth offering, including top-level method definitions.
62
76
  local function scan(qualifier, bare)
@@ -101,9 +115,13 @@ function M.find(G, f, site, ctx, lua_self, lua_initializers)
101
115
  or initialized_member)
102
116
  and (qualifier or bare or node.kind ~= 'method' and node.kind ~= 'constructor') then
103
117
  local score = g == f and 100 or (rec.path:match('^(.*)/') or '') == source_dir and 20 or 0
118
+ if receiver then
119
+ score = score + lua_receiver_score(receiver, node.qualified, rec.path)
120
+ if imported[g] then score = score + 150 end
121
+ end
104
122
  if node.kind ~= 'prototype' and not node.decl then score = score + 1 end
105
123
  local bucket = node.kind == 'macro' and macro_hits or hits
106
- bucket[#bucket + 1] = { id = id, score = score }
124
+ bucket[#bucket + 1] = { id = id, score = score, order = i }
107
125
  end
108
126
  end
109
127
  return hits, macro_hits, scanned
@@ -134,6 +152,8 @@ function M.find(G, f, site, ctx, lua_self, lua_initializers)
134
152
  hits = kept
135
153
  table.sort(hits, function(a, b)
136
154
  if a.score ~= b.score then return a.score > b.score end
155
+ -- Keep the lexical shortlist's order when semantic hints tie.
156
+ if candidates and site.recv then return a.order < b.order end
137
157
  return graph.before(G, a.id, b.id)
138
158
  end)
139
159
  local ids = {}
@@ -29,6 +29,8 @@ function M.validate(query, opts)
29
29
  assert(text.valid(query) == query, 'query must be valid UTF-8')
30
30
  require('codeoutline.query.query_filters').parse(query)
31
31
  assert(opts == nil or type(opts) == 'table', 'options must be a table')
32
+ local match = opts and opts.bm25_match
33
+ assert(match == nil or match == 'any' or match == 'all', 'bm25Match must be any or all')
32
34
  local budget = M.DEFAULT_BUDGET
33
35
  if opts and opts.budget ~= nil then budget = opts.budget end
34
36
  assert(type(budget) == 'number' and budget >= M.MIN_BUDGET and budget <= M.MAX_BUDGET and budget % 1 == 0,
@@ -431,6 +433,34 @@ end
431
433
 
432
434
  -- Run a query over symbol tables (graph.build). Returns the text and a table
433
435
  -- with what was selected; relationships resolve per query.
436
+ local function bm25_seeds(G, idx, filters, lookup, match)
437
+ local predicate = require('codeoutline.query.query_filters')
438
+ local accept
439
+ if filters.count > 0 then
440
+ accept = function(path, name, kind)
441
+ local record = idx.files[path]
442
+ return record and predicate.node_fields(filters, record, kind, name)
443
+ end
444
+ end
445
+ local hits, state = lookup(filters.text, accept, match)
446
+ local ranked, seen = {}, {}
447
+ for _, hit in ipairs(hits or {}) do
448
+ local file = G.file_index[hit.path]
449
+ for i, id in ipairs(file and G.id_of[file] or {}) do
450
+ if i % 128 == 0 then control.check() end
451
+ local node = G.nodes[id]
452
+ if not seen[id] and node.line == hit.start_line and node.kind == hit.kind
453
+ and (node.end_line or node.line) == hit.end_line and text.valid(node.qualified) == hit.symbol
454
+ and predicate.id(filters, G, id) then
455
+ ranked[#ranked + 1] = {id = id, s = hit.score}
456
+ seen[id] = true
457
+ break
458
+ end
459
+ end
460
+ end
461
+ return ranked, {state = state, matches = #ranked, match = match}
462
+ end
463
+
434
464
  local function run(G, idx, query, opts)
435
465
  control.check()
436
466
  opts = opts or {}
@@ -444,6 +474,10 @@ local function run(G, idx, query, opts)
444
474
  file_cache, source_cache = {}, {}
445
475
  local filters = require('codeoutline.query.query_filters').parse(query)
446
476
  local ranked, named_files = find_seeds(G, filters.text, filters, ranking)
477
+ local bm25
478
+ if #ranked == 0 and #named_files == 0 and filters.text ~= '' and opts.bm25_fallback then
479
+ ranked, bm25 = bm25_seeds(G, idx, filters, opts.bm25_fallback, opts.bm25_match)
480
+ end
447
481
 
448
482
  -- Seeds: top-ranked, but at most 3 definitions per name so one generic
449
483
  -- name can't crowd out the others the query mentions.
@@ -527,17 +561,28 @@ local function run(G, idx, query, opts)
527
561
  local shown, files_shown, file_order = {}, {}, {}
528
562
 
529
563
  if #seeds == 0 and #named_files == 0 then
530
- return 'No symbols matched. Name functions, classes, or files (e.g. "xtimer_poll", "Session.save", "xpoll.c").', { seeds = {} }
564
+ local notice = ''
565
+ if bm25 and bm25.state == 'building' then
566
+ notice = ' The BM25 index is being built; retry this query shortly.'
567
+ elseif bm25 and bm25.state == 'unavailable' then
568
+ notice = ' The BM25 fallback is temporarily unavailable.'
569
+ end
570
+ local message, truncated = text.limit('No symbols matched. Name functions, classes, or files (e.g. "xtimer_poll", "Session.save", "xpoll.c").'
571
+ .. notice, budget)
572
+ return message, {seeds = {}, bm25 = bm25, truncated = truncated}
531
573
  end
532
574
 
533
575
  -- Section 1: summary + flows.
534
576
  local nfiles = {}
535
577
  for _, id in ipairs(seeds) do nfiles[G.node_file[id]] = true end
536
578
  for _, id in ipairs(spine) do nfiles[G.node_file[id]] = true end
579
+ for _, f in ipairs(named_files) do nfiles[f] = true end
537
580
  local nf = 0
538
581
  for _ in pairs(nfiles) do nf = nf + 1 end
539
- emit(string.format('Matched %d symbols in %d files, %d more on %s between them; short ones bring %s. Source below is verbatim with line numbers; treat it as already Read.',
582
+ emit(string.format('Matched %d symbols in %d files, %d more on %s between them; short ones bring %s. Source blocks are verbatim with line numbers; treat them as already Read. Outlines are generated declaration summaries, not source bodies.',
540
583
  #seeds, nf, #spine, non_call_paths and 'relationship paths' or 'call paths', non_call_paths and 'related functions' or 'their callees'))
584
+ if bm25 then emit('No symbols matched the original lookup. BM25 fallback matched functions by keywords.') end
585
+ if #named_files > 0 then emit(string.format('Explicitly named files: %d (top-level outlines).', #named_files)) end
541
586
  if #flows > 0 then
542
587
  emit(non_call_paths and '\n## Relationship paths' or '\n## Call paths')
543
588
  if non_call_paths then emit('ref retains a function value; implementation, event and callback hops are static possibilities, not proof of execution.') end
@@ -626,31 +671,78 @@ local function run(G, idx, query, opts)
626
671
  focus[hop.source][#focus[hop.source] + 1] = hop.line
627
672
  end
628
673
  end
629
- local reserve = math.floor(budget * 0.15) -- keep room for sections 3-4
674
+ local reserve = #order > 0 and math.floor(budget * 0.15) or 128
675
+ -- Share outline space a row at a time, so a large first file cannot hide
676
+ -- the others. Mixed queries also keep space for the requested source bodies.
677
+ local outline_budget = math.max(0, budget - reserve - used)
678
+ if #order > 0 then outline_budget = math.floor(outline_budget / 2) end
679
+ local outlines, outline_order, outline_bytes = {}, {}, 0
680
+ local outline_hint = 'Outline entries omitted; refine path:/name: filters to inspect specific declarations.'
681
+ local function outline_status(count, total)
682
+ return string.format('Shown %d/%d top-level declarations.', count, total)
683
+ end
684
+ for _, f in ipairs(file_order) do
685
+ if by_file[f].outline then
686
+ local rec, nodes = G.files[f], {}
687
+ for _, node in ipairs(rec.nodes) do
688
+ if not node.parent and node.kind ~= 'macro' and require('codeoutline.query.query_filters').node(filters, rec, node) then
689
+ nodes[#nodes + 1] = node
690
+ end
691
+ end
692
+ local header = '\n## ' .. rec.path .. ' (outline)\n'
693
+ local overhead = #header + #outline_status(#nodes, #nodes) + #outline_hint + 3
694
+ if overhead <= outline_budget then
695
+ checked_source(idx, rec.path)
696
+ outlines[f] = { nodes = nodes, rows = {}, header = header }
697
+ outline_order[#outline_order + 1] = f
698
+ outline_budget = outline_budget - overhead
699
+ else omitted = true end
700
+ end
701
+ end
702
+ local progress = true
703
+ while progress do
704
+ progress = false
705
+ for _, f in ipairs(outline_order) do
706
+ local outline = outlines[f]
707
+ local node = outline.nodes[#outline.rows + 1]
708
+ if node and not outline.full then
709
+ control.check()
710
+ local row = text.valid(string.format('%d\t%s %s', node.line, node.kind,
711
+ require('codeoutline.query.navigation').detail(G, f, node).sig or node.name))
712
+ if #outline.rows < 1024 and #row + 1 <= outline_budget then
713
+ outline.rows[#outline.rows + 1] = row
714
+ outline_budget = outline_budget - #row - 1
715
+ progress = true
716
+ else outline.full = true end
717
+ end
718
+ end
719
+ end
720
+ for _, f in ipairs(outline_order) do
721
+ local outline = outlines[f]
722
+ local cut = #outline.rows < #outline.nodes
723
+ outline.block = outline.header .. outline_status(#outline.rows, #outline.nodes)
724
+ .. (#outline.rows > 0 and '\n' .. table.concat(outline.rows, '\n') or '')
725
+ .. (cut and '\n' .. outline_hint or '')
726
+ omitted = omitted or cut
727
+ outline_bytes = outline_bytes + #outline.block + 1
728
+ end
630
729
  for _, f in ipairs(file_order) do
631
730
  local rec = G.files[f]
632
731
  local header = '\n## ' .. rec.path
633
732
  local items = by_file[f]
634
733
  table.sort(items, function(a, b) return G.nodes[a.id].line < G.nodes[b.id].line end)
635
734
  local wrote_header = false
636
- if items.outline and used + #header + 16 < budget - reserve then
637
- checked_source(idx, rec.path)
638
- local ol = {}
639
- for _, node in ipairs(rec.nodes) do
640
- if not node.parent and node.kind ~= 'macro' and require('codeoutline.query.query_filters').node(filters, rec, node) then
641
- ol[#ol + 1] = string.format('%d\t%s %s', node.line, node.kind, require('codeoutline.query.navigation').detail(G, f, node).sig or node.name)
642
- end
643
- if #ol >= 60 then ol[#ol + 1] = '\t...'; break end
644
- end
645
- local block = header .. ' (outline)\n' .. table.concat(ol, '\n')
646
- if used + #block < budget - reserve then emit(block, location_refs({ f })); wrote_header = true else omitted = true end
647
- elseif items.outline then omitted = true end
735
+ if outlines[f] then
736
+ local block = outlines[f].block
737
+ outline_bytes = outline_bytes - #block - 1
738
+ emit(block, location_refs({ f })); wrote_header = true
739
+ end
648
740
  for _, item in ipairs(items) do
649
- if not shown[item.id] and used + #header + 16 < budget - reserve then
741
+ if not shown[item.id] and used + #header + 16 < budget - reserve - outline_bytes then
650
742
  local block, source_cut = node_block(G, idx, item.id, item.full, focus[item.id])
651
743
  omitted = omitted or source_cut
652
744
  local need = #block + (wrote_header and 0 or #header + 1) + 1
653
- if used + need < budget - reserve then
745
+ if used + need < budget - reserve - outline_bytes then
654
746
  if not wrote_header then emit(header, location_refs({ f })); wrote_header = true end
655
747
  emit(block, location_refs({ f }))
656
748
  shown[item.id] = true
@@ -760,7 +852,7 @@ local function run(G, idx, query, opts)
760
852
  evidence = G.report.entries, evidence_truncated = evidence_cut or G.report.truncated,
761
853
  unresolved_candidates = candidates.entries, unresolved_candidates_truncated = candidate_cut or false,
762
854
  path_search_limited = path_limited, evidence_scope = G.report.scope,
763
- path_breakpoints = breakpoints.entries, path_breakpoints_truncated = breakpoint_cut }
855
+ path_breakpoints = breakpoints.entries, path_breakpoints_truncated = breakpoint_cut, bm25 = bm25 }
764
856
  end
765
857
 
766
858
  function M.run(...)
@@ -807,27 +807,30 @@ end
807
807
  -- Decode literals without running source code. Lua long strings and quoted escapes.
808
808
  local string_value = require('codeoutline.analysis.lua.lua_literal').decode
809
809
 
810
- local function call_name(T, i)
810
+ local function call_name(T, i, qualified_separators)
811
811
  if T.k[i] ~= 'id' or T:text(i + 1) ~= '(' then return nil end
812
+ local separators = qualified_separators or LUA_MEMBER
812
813
  local name, first = T:text(i), i
813
- while T.k[first - 2] == 'id' and (T:text(first - 1) == '.' or T:text(first - 1) == ':') do
814
+ while T.k[first - 2] == 'id' and separators[T:text(first - 1)] do
814
815
  name = T:text(first - 2) .. T:text(first - 1) .. name
815
816
  first = first - 2
816
817
  end
817
- if T:text(first - 1) == 'function' then return nil end
818
+ local previous = T:text(first - 1)
819
+ if previous == 'function' or qualified_separators and qualified_separators[previous] then return nil end
818
820
  return name
819
821
  end
820
822
 
821
823
  -- Find a single literal argument, skipping nested brackets and anonymous functions.
822
- local function literal_argument(T, call, argument)
824
+ local function literal_argument(T, call, argument, selected)
823
825
  local open, number, first = call + 1, 1, call + 2
824
826
  local close = T.m[open]
825
827
  if not close then return nil end
826
828
  local i = first
827
829
  while i <= close do
830
+ if selected and i > selected + 1 then return nil end
828
831
  local token = T:text(i)
829
832
  if i == close or token == ',' then
830
- if number == argument then
833
+ if number == argument or first == selected then
831
834
  if i == first + 1 and T.k[first] == 'str' then
832
835
  return string_value(T:text(first)), T.s[first] - 1, T.e[first]
833
836
  end
@@ -909,6 +912,7 @@ end
909
912
  local function shared_definition(V, doc, offset, rules)
910
913
  local record = doc:parse()
911
914
  local language = record.language
915
+ local indirect = require('codeoutline.analysis.indirect')
912
916
  local names, applicable, literals = {}, {}, {}
913
917
  local function literal_position(name, argument)
914
918
  literals[name] = literals[name] or {}
@@ -916,7 +920,7 @@ local function shared_definition(V, doc, offset, rules)
916
920
  end
917
921
  for _, rule in ipairs(rules) do
918
922
  if rule.shared and (not rule.language or rule.language == language)
919
- and require('codeoutline.analysis.indirect').candidate(record, rule.call) then
923
+ and indirect.candidate(record, rule.call) then
920
924
  applicable[#applicable + 1] = rule
921
925
  names[rule.call] = true
922
926
  if rule.register then names[rule.register] = true end
@@ -976,7 +980,7 @@ local function shared_definition(V, doc, offset, rules)
976
980
  end
977
981
  end
978
982
  if #matched == 0 then return nil end
979
- local result, seen, events = {}, {}, {}
983
+ local result, seen, events, event_keys = {}, {}, {}, {}
980
984
  local deadline = os.clock() + M.SCAN_SECONDS
981
985
  local function add(loc)
982
986
  local key = loc.uri .. ':' .. loc.range.start.line .. ':' .. loc.range.start.character
@@ -987,7 +991,9 @@ local function shared_definition(V, doc, offset, rules)
987
991
  local resolver
988
992
  for _, match in ipairs(matched) do
989
993
  local rule = match.rule
990
- if rule.kind == 'event' then events[#events + 1] = match
994
+ if rule.kind == 'event' then
995
+ events[#events + 1] = match
996
+ event_keys[match.arg.literal] = true
991
997
  elseif rule.kind == 'target' then
992
998
  local name = match.arg.literal
993
999
  local services = language == 'lua' and service_files(V, match.site, rule)
@@ -1006,7 +1012,7 @@ local function shared_definition(V, doc, offset, rules)
1006
1012
  end
1007
1013
  end
1008
1014
  else
1009
- resolver = resolver or require('codeoutline.analysis.indirect').query(V, { rule }).resolve_targets
1015
+ resolver = resolver or indirect.query(V, { rule }).resolve_targets
1010
1016
  for _, id in ipairs(resolver(f, match.site, match.arg, rule.kind == 'target')) do
1011
1017
  control.check()
1012
1018
  if os.clock() > deadline then return result, true end
@@ -1025,12 +1031,12 @@ local function shared_definition(V, doc, offset, rules)
1025
1031
  local candidate = false
1026
1032
  if V.files[file].language == language then
1027
1033
  for _, match in ipairs(events) do
1028
- if require('codeoutline.analysis.indirect').candidate(V.files[file], match.rule.register) then candidate = true; break end
1034
+ if indirect.candidate(V.files[file], match.rule.register) then candidate = true; break end
1029
1035
  end
1030
1036
  end
1031
1037
  if candidate then
1032
1038
  local source = source_doc(V, file)
1033
- if source then
1039
+ if source and (language ~= 'lua' or indirect.has_lua_literal(source.source, event_keys)) then
1034
1040
  for _, site in ipairs(sites(source)) do
1035
1041
  for _, match in ipairs(events) do
1036
1042
  local rule = match.rule
@@ -1213,9 +1219,9 @@ local function lua_self_member(V, f, c)
1213
1219
  and (c.kind == 'member_call' or c.kind == 'member_symbol')
1214
1220
  end
1215
1221
 
1216
- local function definition_tags(V, f, c)
1222
+ local function definition_tags(V, f, c, candidates)
1217
1223
  local self_fallback = lua_self_member(V, f, c) and c.evidence and c.evidence:match('^lua%-self:%d+$')
1218
- local ids, info = require('codeoutline.query.definition_candidates').find(V, f, c, V.js_context, self_fallback, true)
1224
+ local ids, info = require('codeoutline.query.definition_candidates').find(V, f, c, V.js_context, self_fallback, true, candidates)
1219
1225
  local result, seen, bytes, started, count = {}, {}, 0, os.clock(), 0
1220
1226
  for _, id in ipairs(ids) do
1221
1227
  local g = V.node_file[id]
@@ -1290,9 +1296,7 @@ function handlers.lsp_implementation(req)
1290
1296
  return result, relationships.incomplete
1291
1297
  end
1292
1298
 
1293
- function handlers.lsp_definition(req)
1294
- local V, doc, c, targets, f, offset = at(req)
1295
- if not V then return false end
1299
+ local function resolved_definition(V, doc, c, targets, f, offset)
1296
1300
  if not c and (V.files[f].language == 'javascript' or V.files[f].language == 'typescript') then
1297
1301
  local events = require('codeoutline.analysis.shared_events').context(V, V.js_context)
1298
1302
  local found, recognized = events.definition(f, offset, doc:position(offset).line + 1)
@@ -1348,7 +1352,7 @@ function handlers.lsp_definition(req)
1348
1352
  local result = {}
1349
1353
  for _, id in ipairs(ids) do result[#result + 1] = (location(V, id)) end
1350
1354
  return result, truncated or V.js_context.include_incomplete or V.js_context.workspace_incomplete
1351
- or V.js_context.go_incomplete or V.js_context.go_methods_incomplete or V.js_context.cpp_receivers_incomplete or V.js_context.rust_incomplete or V.js_context.receiver_incomplete
1355
+ or V.js_context.go_incomplete or V.js_context.go_methods_incomplete or V.js_context.cpp_receivers_incomplete or V.js_context.rust_incomplete or V.js_context.receiver_incomplete, true
1352
1356
  end
1353
1357
  if c.decl and is_decl(V.nodes[targets[1]]) then
1354
1358
  -- From a declaration, go to its definitions.
@@ -1363,6 +1367,172 @@ function handlers.lsp_definition(req)
1363
1367
  return result, V.js_context.include_incomplete or V.js_context.workspace_incomplete or V.js_context.go_incomplete or V.js_context.go_methods_incomplete or V.js_context.cpp_receivers_incomplete or V.js_context.rust_incomplete or V.js_context.receiver_incomplete
1364
1368
  end
1365
1369
 
1370
+ local function token_after(T, offset)
1371
+ local low, high = 1, T.n + 1
1372
+ while low < high do
1373
+ local middle = math.floor((low + high) / 2)
1374
+ if T.e[middle] <= offset then low = middle + 1 else high = middle end
1375
+ end
1376
+ return low
1377
+ end
1378
+
1379
+ local function lexical_literal(T, token, language)
1380
+ if T.k[token] ~= 'str' or T.e[token] - T.s[token] > 1024 then return nil end
1381
+ local value = T:text(token)
1382
+ if language == 'lua' then return string_value(value) end
1383
+ local quote = value:sub(1, 1)
1384
+ if #value >= 2 and (quote == '"' or quote == "'" or quote == '`') and value:sub(-1) == quote then
1385
+ return value:sub(2, -2)
1386
+ end
1387
+ return value
1388
+ end
1389
+
1390
+ -- A direct string argument belongs to its innermost call. Skip balanced
1391
+ -- expressions in earlier arguments when looking for the opening parenthesis.
1392
+ local function literal_call(T, token, language)
1393
+ local i, after = token - 1, token + 1
1394
+ while T.k[i] == 'nl' do i = i - 1 end
1395
+ while T.k[after] == 'nl' do after = after + 1 end
1396
+ local first, last = T:text(i), T:text(after)
1397
+ if (first ~= '(' and first ~= ',') or (last ~= ')' and last ~= ',') then return nil end
1398
+ local scanned = 0
1399
+ while i > 0 do
1400
+ scanned = scanned + 1
1401
+ if scanned % 128 == 0 then control.check() end
1402
+ local word, pair = T:text(i), T.m[i]
1403
+ if pair and pair < i then
1404
+ i = pair
1405
+ elseif word == '(' then
1406
+ if not pair or pair <= token then return nil end
1407
+ local lua = language == 'lua'
1408
+ local name = call_name(T, i - 1, lua and LUA_MEMBER or MEMBER)
1409
+ if name and (not lua or literal_argument(T, i - 1, nil, token)) then return name end
1410
+ return nil
1411
+ elseif word == '[' or word == '{' then
1412
+ return nil
1413
+ end
1414
+ i = i - 1
1415
+ end
1416
+ end
1417
+
1418
+ local function lexical_ids(V, hits)
1419
+ local ids, seen = {}, {}
1420
+ for _, hit in ipairs(hits) do
1421
+ control.check()
1422
+ local file = V.file_index[hit.path]
1423
+ for i, id in ipairs(V.id_of[file] or {}) do
1424
+ if i % 128 == 0 then control.check() end
1425
+ local node = V.nodes[id]
1426
+ if not seen[id] and node.kind == hit.kind and node.qualified == hit.symbol
1427
+ and (V.drafts[file] or node.line == hit.start_line) then
1428
+ ids[#ids + 1], seen[id] = id, true
1429
+ end
1430
+ end
1431
+ end
1432
+ return ids
1433
+ end
1434
+
1435
+ local function lexical_locations(V, ids, query, origin, offset, origin_call)
1436
+ local comparisons, other, seen = {}, {}, {}
1437
+ local literals = { [query] = true }
1438
+ local has_lua_literal = require('codeoutline.analysis.indirect').has_lua_literal
1439
+ local truncated = false
1440
+ for _, id in ipairs(ids) do
1441
+ control.check()
1442
+ local file = V.node_file[id]
1443
+ local record, draft = V.files[file], V.drafts[file]
1444
+ if (draft and #draft.source or record.size or 0) > M.MAX_FILE_BYTES then
1445
+ truncated = true
1446
+ else
1447
+ local offsets = seen[file]
1448
+ if offsets == nil then
1449
+ local doc = source_doc(V, file)
1450
+ local relevant = doc and (record.language == 'lua' and has_lua_literal(doc.source, literals)
1451
+ or doc.source:find(query, 1, true))
1452
+ offsets = relevant and {} or false
1453
+ seen[file] = offsets
1454
+ end
1455
+ if offsets then
1456
+ -- Populate the shared position cache before locating the node.
1457
+ local _, T = M.full(V, file)
1458
+ local doc, node = locate(V, id)
1459
+ if T and node and node.start_byte and node.end_byte then
1460
+ local start, stop = token_after(T, node.start_byte), token_after(T, node.end_byte) - 1
1461
+ for i = start, stop do
1462
+ if i % 128 == 0 then control.check() end
1463
+ local first, last = T.s[i] - 1, T.e[i]
1464
+ if not offsets[first] and (file ~= origin or offset < first or offset >= last)
1465
+ and lexical_literal(T, i, record.language) == query
1466
+ and (not origin_call or literal_call(T, i, record.language) ~= origin_call) then
1467
+ offsets[first] = true
1468
+ local matches = (T:text(i - 1) == '==' or T:text(i + 1) == '==') and comparisons or other
1469
+ if #matches < 20 then
1470
+ matches[#matches + 1] = {uri = doc.uri, range = doc:range(first, last)}
1471
+ else truncated = true end
1472
+ end
1473
+ end
1474
+ end
1475
+ end
1476
+ end
1477
+ end
1478
+ for _, result in ipairs(other) do
1479
+ if #comparisons >= 20 then truncated = true; break end
1480
+ comparisons[#comparisons + 1] = result
1481
+ end
1482
+ return comparisons, truncated
1483
+ end
1484
+
1485
+ local function lexical_definition(req, V, c, f, offset, deadline, allow_tags)
1486
+ local query = c and c.name
1487
+ local origin_call
1488
+ if not query then
1489
+ local _, T = M.full(V, f)
1490
+ if T then
1491
+ local token = token_after(T, offset)
1492
+ if token <= T.n and T.s[token] - 1 <= offset then
1493
+ local language = V.files[f].language
1494
+ query = lexical_literal(T, token, language)
1495
+ if query then origin_call = literal_call(T, token, language) end
1496
+ end
1497
+ end
1498
+ end
1499
+ if not query or #query > 256 or not query:find('[%w_]') then return nil end
1500
+ local hits, incomplete = req.bm25_definition(V.idx, query, deadline, c and c.recv)
1501
+ if not hits or #hits == 0 then return {}, incomplete end
1502
+ if c and not allow_tags then return {}, incomplete end
1503
+ local ids = lexical_ids(V, hits)
1504
+ if not c then
1505
+ local locations, truncated = lexical_locations(V, ids, query, f, offset, origin_call)
1506
+ return locations, incomplete or truncated
1507
+ end
1508
+ local found, truncated = definition_tags(V, f, c, ids)
1509
+ local locations = {}
1510
+ for _, id in ipairs(found) do locations[#locations + 1] = (location(V, id)) end
1511
+ return locations, incomplete or truncated
1512
+ end
1513
+
1514
+ function handlers.lsp_definition(req)
1515
+ local started = xtimer.now_ms()
1516
+ local V, doc, c, targets, f, offset = at(req)
1517
+ if not V then return false end
1518
+ local primary, incomplete, allow_tags = resolved_definition(V, doc, c, targets, f, offset)
1519
+ if #primary > 0 or not req.bm25_definition then return primary, incomplete end
1520
+ local deadline = math.min(started + 900, xtimer.now_ms() + 250)
1521
+ if xtimer.now_ms() >= deadline then return primary, incomplete end
1522
+ local previous, expired = control.callback, {}
1523
+ control.callback = function()
1524
+ if previous then previous() end
1525
+ if xtimer.now_ms() >= deadline then error(expired, 0) end
1526
+ end
1527
+ local ok, result, limited = pcall(lexical_definition, req, V, c, f, offset, deadline, allow_tags)
1528
+ control.callback = previous
1529
+ if not ok then
1530
+ if result == expired or tostring(result):find('source changed during query', 1, true) then return primary, true end
1531
+ error(result, 0)
1532
+ end
1533
+ return result or primary, incomplete or not not limited, limited == 'bm25_building'
1534
+ end
1535
+
1366
1536
  -- Display tags may be truncated before a heavily annotated SDK declaration's
1367
1537
  -- argument list. Recover only the selected function's bounded source span.
1368
1538
  local function external_signature(entry)
@@ -1752,9 +1922,9 @@ function M.handles(method) return handlers[method] ~= nil end
1752
1922
 
1753
1923
  -- Returns { result = value (false for null), incomplete = bool } or { error = message }.
1754
1924
  function M.execute(req)
1755
- local result, incomplete = handlers[req.method](req)
1925
+ local result, incomplete, bm25_building = handlers[req.method](req)
1756
1926
  if type(result) == 'table' and result.error then return result end
1757
- return { result = result, incomplete = incomplete or false }
1927
+ return { result = result, incomplete = incomplete or false, bm25_building = bm25_building or nil }
1758
1928
  end
1759
1929
 
1760
1930
  return M
@@ -54,8 +54,12 @@ function M.file(filters, rec)
54
54
  return matches(filters.languages, rec.language, true) and matches(filters.paths, (rec.path:gsub('\\', '/')))
55
55
  end
56
56
 
57
+ function M.node_fields(filters, rec, kind, qualified)
58
+ return M.file(filters, rec) and matches(filters.kinds, kind, true) and matches(filters.names, qualified)
59
+ end
60
+
57
61
  function M.node(filters, rec, node)
58
- return M.file(filters, rec) and matches(filters.kinds, node.kind, true) and matches(filters.names, node.qualified)
62
+ return M.node_fields(filters, rec, node.kind, node.qualified)
59
63
  end
60
64
 
61
65
  function M.id(filters, G, id)