@codua/codeoutline-win32-x64 0.2.6 → 0.3.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.
Files changed (73) hide show
  1. package/README.md +205 -3
  2. package/README.zh-CN.md +40 -2
  3. package/bin/xnet.exe +0 -0
  4. package/build-info.json +1 -1
  5. package/package.json +1 -1
  6. package/scripts/codeoutline/affected.lua +145 -0
  7. package/scripts/codeoutline/automatic_events.lua +202 -0
  8. package/scripts/codeoutline/cache_repairs.lua +47 -0
  9. package/scripts/codeoutline/callers.lua +56 -16
  10. package/scripts/codeoutline/cargo_toml.lua +125 -0
  11. package/scripts/codeoutline/command.lua +26 -4
  12. package/scripts/codeoutline/common.lua +48 -7
  13. package/scripts/codeoutline/completion.lua +126 -22
  14. package/scripts/codeoutline/coverage.lua +63 -0
  15. package/scripts/codeoutline/cpp_bindings.lua +46 -11
  16. package/scripts/codeoutline/cpp_fragments.lua +143 -0
  17. package/scripts/codeoutline/cpp_implementations.lua +340 -0
  18. package/scripts/codeoutline/cpp_includes.lua +156 -0
  19. package/scripts/codeoutline/cpp_macros.lua +279 -0
  20. package/scripts/codeoutline/cpp_receivers.lua +342 -0
  21. package/scripts/codeoutline/cpp_types.lua +285 -29
  22. package/scripts/codeoutline/definition_candidates.lua +186 -0
  23. package/scripts/codeoutline/dispatch_aliases.lua +10 -5
  24. package/scripts/codeoutline/documents.lua +22 -9
  25. package/scripts/codeoutline/explore.lua +243 -122
  26. package/scripts/codeoutline/external_headers.lua +176 -0
  27. package/scripts/codeoutline/foreign.lua +23 -3
  28. package/scripts/codeoutline/function_values.lua +69 -0
  29. package/scripts/codeoutline/go_methods.lua +931 -0
  30. package/scripts/codeoutline/go_modules.lua +329 -0
  31. package/scripts/codeoutline/graph.lua +322 -67
  32. package/scripts/codeoutline/implementations.lua +403 -0
  33. package/scripts/codeoutline/index.lua +161 -28
  34. package/scripts/codeoutline/index_worker.lua +11 -4
  35. package/scripts/codeoutline/indirect.lua +4 -2
  36. package/scripts/codeoutline/js_evidence.lua +2 -1
  37. package/scripts/codeoutline/js_resolution.lua +40 -12
  38. package/scripts/codeoutline/js_workspace.lua +325 -0
  39. package/scripts/codeoutline/lang/c.lua +15 -2
  40. package/scripts/codeoutline/lang/cpp.lua +32 -18
  41. package/scripts/codeoutline/lang/csharp.lua +1 -1
  42. package/scripts/codeoutline/lang/js.lua +56 -20
  43. package/scripts/codeoutline/lang/rust.lua +9 -1
  44. package/scripts/codeoutline/language_detect.lua +100 -0
  45. package/scripts/codeoutline/lsp.lua +173 -23
  46. package/scripts/codeoutline/lua_bindings.lua +151 -47
  47. package/scripts/codeoutline/lua_callback_loops.lua +210 -0
  48. package/scripts/codeoutline/main.lua +32 -7
  49. package/scripts/codeoutline/mcp.lua +52 -9
  50. package/scripts/codeoutline/navigation.lua +351 -65
  51. package/scripts/codeoutline/on_demand.lua +93 -0
  52. package/scripts/codeoutline/ordinary_bindings.lua +210 -34
  53. package/scripts/codeoutline/parse.lua +18 -2
  54. package/scripts/codeoutline/parse_pool.lua +1 -1
  55. package/scripts/codeoutline/parse_worker.lua +1 -1
  56. package/scripts/codeoutline/python_bindings.lua +2 -2
  57. package/scripts/codeoutline/query_filters.lua +65 -0
  58. package/scripts/codeoutline/query_report.lua +56 -19
  59. package/scripts/codeoutline/resolution.lua +23 -0
  60. package/scripts/codeoutline/rust_packages.lua +203 -0
  61. package/scripts/codeoutline/rust_resolution.lua +301 -0
  62. package/scripts/codeoutline/search_ranking.lua +70 -0
  63. package/scripts/codeoutline/service.lua +47 -7
  64. package/scripts/codeoutline/shared_events.lua +369 -0
  65. package/scripts/codeoutline/static_dispatch.lua +46 -11
  66. package/scripts/codeoutline/text.lua +3 -2
  67. package/scripts/codeoutline/ts_implementations.lua +81 -0
  68. package/scripts/codeoutline/type_bases.lua +61 -0
  69. package/scripts/codeoutline/type_hierarchy.lua +324 -0
  70. package/scripts/codeoutline/type_references.lua +291 -0
  71. package/scripts/codeoutline/type_uses.lua +260 -0
  72. package/scripts/codeoutline/update_sequence.lua +1 -1
  73. package/scripts/codeoutline/version.lua +1 -1
@@ -13,15 +13,19 @@ local control = require('codeoutline.control')
13
13
  local lsp = require('codeoutline.lsp')
14
14
  local parse = require('codeoutline.parse')
15
15
  -- Reference scans stop at a result count or time budget and report truncation.
16
- 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,
17
17
  MAX_FILE_BYTES = 1048576, CACHE_FILES = 64 }
18
18
 
19
19
  local sessions = {}
20
+ -- Pure per-file binding evidence, shared across navigation requests. Its
21
+ -- existing 16-file/about-8-MiB FIFO is keyed by source-node identity; each
22
+ -- query still validates source before reusing a shared entry.
23
+ local binding_cache = { entries = {}, order = {}, bytes = 0 }
20
24
  local ANY = setmetatable({}, { __index = function() return true end })
21
25
  -- Bare identifiers bind only nearby, except type names used across files.
22
26
  local TYPES = { class = true, struct = true, union = true, interface = true, enum = true, record = true,
23
- typedef = true, namespace = true, annotation_type = true }
24
- -- references and call hierarchy cover callables; types need complete reference semantics.
27
+ typedef = true, namespace = true, annotation_type = true, trait = true }
28
+ -- Call hierarchy covers callables; type references use explicit type-site evidence.
25
29
  local REFERENCE = { ['function'] = true, method = true, macro = true, constructor = true,
26
30
  prototype = true, destructor = true }
27
31
  local CALLS = { ['function'] = true, method = true, macro = true, constructor = true,
@@ -70,16 +74,18 @@ end
70
74
  -- files. Overlay files and nodes take ids after every resident one; masked
71
75
  -- resident nodes drop out of name lists, which keep path and node order.
72
76
  -- Nothing is written to the resident tables.
73
- local function view(session, root)
77
+ local function view(session, root, include_paths, only)
74
78
  local idx = service.resident(root)
75
79
  local G = idx:symbols()
76
80
  local V = setmetatable({ root = root, idx = idx, drafts = {}, disk = {}, dep_cache = {}, new_files = {},
77
81
  next_file = G.next_file, next_node = G.next_node, next_dir = G.next_dir }, { __index = G })
78
82
  V.js_context = require('codeoutline.js_resolution').context(V)
83
+ V.js_context.include_paths = include_paths
79
84
  V.js_context.lua = require('codeoutline.lua_bindings').context(V)
80
85
  V.js_context.ordinary = require('codeoutline.ordinary_bindings').context(V, V.js_context, function(f)
81
86
  return V.drafts[f] and V.drafts[f].source
82
- end)
87
+ end, { cache = binding_cache, parsed = function(f) return M.full(V, f) end })
88
+ V.js_context.lua.ordinary = V.js_context.ordinary
83
89
  for _, name in ipairs({ 'files', 'nodes', 'node_file', 'id_of', 'file_index', 'file_dir' }) do
84
90
  V[name] = setmetatable({}, { __index = G[name] })
85
91
  end
@@ -88,6 +94,11 @@ local function view(session, root)
88
94
  local rel = doc.path and relative(root, doc.path)
89
95
  local f = rel and V.file_index[rel]
90
96
  if not rel or (f and V.drafts[f]) then return f end
97
+ local hint = f and V.files[f].cpp_fragment
98
+ and not require('codeoutline.language_detect').resolve('', doc.language, false) and V.files[f].language or nil
99
+ if doc.indexed_language ~= hint then
100
+ doc.indexed_language, doc.parsed, doc.full, doc.branch_tokens = hint, nil, nil, nil
101
+ end
91
102
  local rec = doc:parse()
92
103
  if not rec.language then return nil end
93
104
  if f then masked[f] = true
@@ -143,7 +154,7 @@ local function view(session, root)
143
154
  local drafts = {}
144
155
  for _, doc in pairs(sessions[session] or {}) do drafts[#drafts + 1] = doc end
145
156
  table.sort(drafts, function(a, b) return a.uri < b.uri end)
146
- for _, doc in ipairs(drafts) do V.add(doc) end
157
+ for _, doc in ipairs(drafts) do if not only or doc.uri == only then V.add(doc) end end
147
158
  return V
148
159
  end
149
160
 
@@ -153,20 +164,7 @@ end
153
164
  local function deps(V, f)
154
165
  local entry = V.dep_cache[f]
155
166
  if not entry then
156
- local direct, paired = graph.dependencies(V, f, V.js_context)
157
- if C_FAMILY[V.files[f].language] then
158
- local queue = {}
159
- for g in pairs(direct) do queue[#queue + 1] = g end
160
- local i = 1
161
- while i <= #queue and #queue < M.MAX_INCLUDES do
162
- local more, sources = graph.dependencies(V, queue[i])
163
- for h in pairs(sources) do paired[h] = true end
164
- for h in pairs(more) do
165
- if not direct[h] and h ~= f then direct[h] = true; queue[#queue + 1] = h end
166
- end
167
- i = i + 1
168
- end
169
- end
167
+ local direct, paired = graph.visible_dependencies(V, f, V.js_context)
170
168
  entry = { direct, paired }
171
169
  V.dep_cache[f] = entry
172
170
  end
@@ -191,14 +189,27 @@ local function recursive(node, recv)
191
189
  end
192
190
 
193
191
  -- Targets of one occurrence of name in file f, enclosed by node index from.
194
- local function resolve_site(V, f, from, name, kind, recv, evidence, offset, index)
192
+ local function resolve_site(V, f, from, name, kind, recv, evidence, offset, index, cursor_line, definition)
193
+ if kind == 'type' then
194
+ local site = type(evidence) == 'table' and evidence
195
+ if not site then return {}, 'unsupported type position' end
196
+ return require('codeoutline.type_references').resolve(V, f, from > 0 and V.id_of[f][from] or nil,
197
+ site, V.js_context, nil, definition)
198
+ end
195
199
  if index then evidence = V.js_context.ordinary.evidence(f, index) end
200
+ if V.files[f].language == 'go' and offset and evidence ~= 'go:?' and not (evidence and evidence:match('^go:expr:')) then
201
+ if recv then evidence = 'go:recv:' .. offset
202
+ elseif kind == 'member_call' or kind == 'member_symbol' then evidence = 'go:expr:' .. offset end
203
+ end
196
204
  local src = from > 0 and V.id_of[f][from] or nil
197
205
  local direct, paired = deps(V, f)
198
206
  local rkind, accept = kind, nil
199
207
  if kind == 'symbol' then rkind, accept = 'call', ANY
200
- elseif kind == 'member_symbol' then rkind, accept = 'member_call', ANY end
201
- local targets = graph.resolve(V, f, src, name, rkind, recv or nil, direct, paired, accept, evidence, V.js_context) or {}
208
+ elseif kind == 'member_symbol' then rkind, accept = 'member_call', ANY
209
+ elseif definition and kind == 'member_call' and V.files[f].language == 'cpp' then accept = ANY end
210
+ local site_line = index and V.files[f].refs.line[index] or cursor_line
211
+ local targets, reason, macro_state = graph.resolve(V, f, src, name, rkind, recv or nil, direct, paired, accept, evidence, V.js_context, site_line)
212
+ targets = targets or {}
202
213
  if kind == 'symbol' then
203
214
  local kept = {}
204
215
  for _, id in ipairs(targets) do
@@ -210,13 +221,13 @@ local function resolve_site(V, f, from, name, kind, recv, evidence, offset, inde
210
221
  targets = kept
211
222
  end
212
223
  -- The resolver never binds a ref to its enclosing node, which hides recursion.
213
- if #targets == 0 and src and V.nodes[src].name == name and (kind == 'call' or recursive(V.nodes[src], recv)) then
224
+ if #targets == 0 and reason ~= 'unknown or unsupported macro visibility' and src and V.nodes[src].name == name and (kind == 'call' or recursive(V.nodes[src], recv)) then
214
225
  targets = { src }
215
226
  end
216
227
  -- Definitions too far apart to choose between: a visible prototype still
217
228
  -- names the function, and going to its definition lists them all.
218
229
  if #targets == 0 and (kind == 'call' or kind == 'ref') then
219
- targets = graph.resolve(V, f, src, name, kind, recv or nil, direct, paired, PROTOTYPE, evidence, V.js_context) or {}
230
+ targets = graph.resolve(V, f, src, name, kind, recv or nil, direct, paired, PROTOTYPE, evidence, V.js_context, site_line) or {}
220
231
  end
221
232
  -- A native object: the local's initializer chain names the metatable whose
222
233
  -- methods the C side registers (`local pool = mod.open()`, then `pool:read()`).
@@ -245,18 +256,24 @@ local function resolve_site(V, f, from, name, kind, recv, evidence, offset, inde
245
256
  end
246
257
  targets = kept
247
258
  end
248
- return targets
259
+ return targets, reason, evidence, macro_state
249
260
  end
250
261
 
251
262
  -- What the cursor names: a declaration, an indexed reference, or another identifier.
252
- local function find_cursor(doc, offset, strict)
253
- local rec = doc:parse()
263
+ local function find_cursor(doc, offset, strict, parsed)
264
+ local rec = parsed or doc:parse()
254
265
  local function inside(a, b) return a <= offset and (offset < b or (not strict and offset == b)) end
255
266
  for n, node in ipairs(rec.nodes) do
256
267
  if node.name_start and inside(node.name_start, node.name_end) then
257
268
  return { decl = n, name = node.name, first = node.name_start, last = node.name_end }
258
269
  end
259
270
  end
271
+ for _, site in ipairs(rec.type_sites or {}) do
272
+ if inside(site.first, site.last) then
273
+ return { from = site.from, name = site.name, kind = 'type', recv = site.recv, evidence = site,
274
+ first = site.first, last = site.last }
275
+ end
276
+ end
260
277
  local refs = rec.refs
261
278
  if refs and refs.start then
262
279
  for i = 1, #refs.name do
@@ -270,16 +287,87 @@ local function find_cursor(doc, offset, strict)
270
287
  end
271
288
  end
272
289
 
273
- local function cursor(doc, offset)
290
+ local macro_body_scanners = {}
291
+ local function macro_body_cursor(doc, offset, rec)
292
+ if not C_FAMILY[rec.language] then return nil end
293
+ local line = doc:position(offset).line + 1
294
+ while line > 1 do
295
+ local previous = doc.source:sub(doc.lines[line - 1] + 1, doc.lines[line]):gsub('[\r\n]+$', '')
296
+ if not previous:match('\\$') then break end
297
+ line = line - 1
298
+ end
299
+ if not doc.source:sub(doc.lines[line] + 1, (doc.lines[line + 1] or #doc.source)):match('^%s*#%s*define%s') then return nil end
300
+ local T = doc:tokens(true)
301
+ for i = 1, T.n do
302
+ if T.s[i] - 1 > offset then break end
303
+ if T.k[i] == 'dir' and T.s[i] - 1 <= offset and offset < T.e[i] then
304
+ local text = T:text(i)
305
+ local name_start, name = text:match('^#%s*define%s+()([%a_][%w_]*)')
306
+ if not name then return nil end
307
+ local start = T.s[i] - 1
308
+ if offset < start + name_start - 1 + #name then return nil end
309
+ local scanner = macro_body_scanners[rec.language]
310
+ if not scanner then
311
+ local config = {}
312
+ for k, v in pairs(require('codeoutline.lang.' .. rec.language).config) do config[k] = v end
313
+ config.directive, config.pp_first_branch = nil, nil
314
+ scanner = require('codeoutline.xscan').lang(config)
315
+ macro_body_scanners[rec.language] = scanner
316
+ end
317
+ local body = scanner:tokenize(text)
318
+ local macro, parameters = nil, {}
319
+ for n = 1, body.n do if body.s[n] == name_start then macro = n; break end end
320
+ if not macro then return false end
321
+ local first = macro + 1
322
+ if body:text(first) == '(' and body.s[first] == body.e[macro] + 1 and body.m[first] then
323
+ local close = body.m[first]
324
+ for n = first + 1, close - 1 do if body.k[n] == 'id' then parameters[body:text(n)] = true end end
325
+ first = close + 1
326
+ end
327
+ for n = first, body.n do
328
+ local a, b = start + body.s[n] - 1, start + body.e[n]
329
+ if a <= offset and offset < b and body.k[n] == 'id' then
330
+ local word = body:text(n)
331
+ if parameters[word] then return false end
332
+ local op = body:text(n - 1)
333
+ local member = op == '.' or op == '->' or op == '::'
334
+ return { from = 0, name = word, kind = member and (body:text(n + 1) == '(' and 'member_call' or 'member_symbol')
335
+ or (body:text(n + 1) == '(' and 'call' or 'symbol'),
336
+ recv = member and body.k[n - 2] == 'id' and body:text(n - 2) or nil, first = a, last = b }
337
+ end
338
+ end
339
+ return false
340
+ end
341
+ end
342
+ end
343
+
344
+ local function cursor(doc, offset, V, f, include_inactive)
345
+ if include_inactive then
346
+ local macro = macro_body_cursor(doc, offset, doc:parse())
347
+ if macro ~= nil then return macro or nil end
348
+ end
274
349
  local found = find_cursor(doc, offset, true) or find_cursor(doc, offset, false)
350
+ if found and found.decl then return found end
351
+ local rec, T
352
+ if V then rec, T = M.full(V, f) end
353
+ local typed = rec and (find_cursor(doc, offset, true, rec) or find_cursor(doc, offset, false, rec))
354
+ if typed then return typed end
275
355
  if found then return found end
276
- local rec, T = doc:parse(), doc:tokens()
356
+ rec = rec or doc:parse()
357
+ T = T or doc:tokens()
277
358
  if not T then return nil end
278
- local t
279
- for i = 1, T.n do
280
- local a = T.s[i] - 1
281
- if a > offset then break end
282
- if T.k[i] == 'id' and offset <= T.e[i] then t = i end
359
+ local function token_at(tokens)
360
+ local hit
361
+ for i = 1, tokens.n do
362
+ local a = tokens.s[i] - 1
363
+ if a > offset then break end
364
+ if tokens.k[i] == 'id' and offset <= tokens.e[i] then hit = i end
365
+ end
366
+ return hit
367
+ end
368
+ local t = token_at(T)
369
+ if not t and include_inactive and C_FAMILY[rec.language] then
370
+ T = doc:tokens(true); t = token_at(T)
283
371
  end
284
372
  if not t then return nil end
285
373
  local kind, recv = 'symbol', nil
@@ -305,6 +393,11 @@ local function cursor(doc, offset)
305
393
  while (T:text(first - 1) == '.' or T:text(first - 1) == '::') and T.k[first - 2] == 'id' do first = first - 2 end
306
394
  local name = require('codeoutline.csharp_bindings').name(T.src:sub(T.s[first], T.e[t]))
307
395
  evidence = name and 'cs:name:' .. name .. '\0' .. (T.s[first] - 1)
396
+ elseif C_FAMILY[rec.language] then
397
+ evidence = require('codeoutline.cpp_receivers').evidence(T, t)
398
+ if not evidence and rec.language == 'cpp' and (not recv or T:text(t - 1) == '::') then
399
+ evidence = require('codeoutline.cpp_types').evidence(T, t)
400
+ end
308
401
  end
309
402
  return { from = from, name = T:text(t), kind = kind, recv = recv, first = T.s[t] - 1, last = T.e[t], evidence = evidence }
310
403
  end
@@ -326,36 +419,57 @@ end
326
419
  -- Full parse (record plus tokens) of one file, cached per record. Shared with
327
420
  -- completion, which needs tokens for types this index does not store.
328
421
  local full_cache, full_order = setmetatable({}, { __mode = 'k' }), {}
422
+ local function cache_full(key, entry)
423
+ if not full_cache[key] then full_order[#full_order + 1] = key end
424
+ full_cache[key] = entry
425
+ while #full_order > M.CACHE_FILES do full_cache[table.remove(full_order, 1)] = nil end
426
+ end
427
+
329
428
  function M.full(V, f, native)
330
429
  local draft = V.drafts and V.drafts[f]
331
430
  if draft then
332
431
  draft.full = draft.full or {}
333
432
  local entry = draft.full
334
433
  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 })
434
+ entry.rec, entry.tokens = parse.parse_tokens(draft.path or V.files[f].path, draft.source,
435
+ { foreign = native, language = draft:parse().language, ordinary_bindings = true })
336
436
  entry.foreign = native
337
437
  end
338
438
  return entry.rec, entry.tokens, draft
339
439
  end
340
440
  local key = V.files[f]
341
441
  local entry = full_cache[key]
342
- if entry == nil or native and not entry.foreign then
343
- local fresh = entry == nil
442
+ if entry == nil or not entry.tokens or native and not entry.foreign then
344
443
  if (key.size or 0) > M.MAX_FILE_BYTES then return nil end
345
444
  local doc = source_doc(V, f)
346
445
  -- Read/parse failures are transient, unlike a successful empty result.
347
446
  if not doc then return nil end
348
- local rec, tokens = parse.parse_tokens(key.path, doc.source, { foreign = native })
447
+ local rec, tokens = parse.parse_tokens(key.path, doc.source,
448
+ { foreign = native, language = key.language, ordinary_bindings = true })
349
449
  if not rec then return nil end
350
450
  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
451
+ cache_full(key, entry)
354
452
  end
355
453
  if not entry then return nil end
356
454
  return entry.rec, entry.tokens
357
455
  end
358
456
 
457
+ -- A target's exact positions need a full parse too. Reuse the same bounded
458
+ -- cache as completion/details instead of reparsing large headers on every
459
+ -- definition/hover/reference request. Callers must obtain doc from source_doc
460
+ -- first: it validates the on-disk size/checksum even on a cache hit. Drafts
461
+ -- keep their own versioned parse and never enter this shared cache.
462
+ local function position_record(V, f, doc)
463
+ if V.drafts[f] or (V.files[f].size or #doc.source) > M.MAX_FILE_BYTES then return doc:parse() end
464
+ local key = V.files[f]
465
+ local entry = full_cache[key]
466
+ if not entry then
467
+ entry = { rec = doc:parse() }
468
+ cache_full(key, entry)
469
+ end
470
+ return entry.rec
471
+ end
472
+
359
473
  -- Restore display/type details without attaching them to resident index nodes.
360
474
  function M.detail(V, f, node)
361
475
  if node.sig then return node end
@@ -388,7 +502,7 @@ local function locate(V, id)
388
502
  local n, f = index_of(V, id)
389
503
  local node, doc = V.nodes[id], source_doc(V, f)
390
504
  if not doc then return nil end
391
- local nodes = doc:parse().nodes
505
+ local nodes = position_record(V, f, doc).nodes
392
506
  local hit = nodes[n]
393
507
  if hit and hit.name == node.name and hit.qualified == node.qualified and hit.name_start then return doc, hit end
394
508
  hit = nil
@@ -441,7 +555,7 @@ end
441
555
  local function site_range(V, f, i)
442
556
  local doc = source_doc(V, f)
443
557
  if not doc then return nil end
444
- local refs, indexed = doc:parse().refs, V.files[f].refs
558
+ local refs, indexed = position_record(V, f, doc).refs, V.files[f].refs
445
559
  if not refs or not refs.start then return nil end
446
560
  local j = i
447
561
  if refs.name[i] ~= indexed.name[i] or refs.line[i] ~= indexed.line[i] then
@@ -470,6 +584,19 @@ local function is_decl(node) return not not graph.is_decl(node) end
470
584
  -- A declaration and its definitions (prototypes, in-class declarations) as one target.
471
585
  local function expand(V, set)
472
586
  local out = {}
587
+ local function same_declaration(id, candidate)
588
+ local a, b = V.nodes[id], V.nodes[candidate]
589
+ local f, g = V.node_file[id], V.node_file[candidate]
590
+ local language = V.files[f].language
591
+ if not C_FAMILY[language] then return true end
592
+ local owner_a = language == 'cpp' and require('codeoutline.cpp_types').owner(V, f, a, V.js_context)
593
+ local owner_b = V.files[g].language == 'cpp' and require('codeoutline.cpp_types').owner(V, g, b, V.js_context)
594
+ if owner_a and owner_b then return owner_a == owner_b end
595
+ if not require('codeoutline.cpp_includes').policy(V, f, V.js_context).configured then return true end
596
+ if owner_a or owner_b then return false end
597
+ local source, header = is_decl(a) and g or f, is_decl(a) and f or g
598
+ return graph.directly_includes(V, source, header, V.js_context)
599
+ end
473
600
  for id in pairs(set) do
474
601
  out[id] = true
475
602
  local node = V.nodes[id]
@@ -477,7 +604,7 @@ local function expand(V, set)
477
604
  for _, cid in ipairs(V.by_name[node.name]) do
478
605
  local c = V.nodes[cid]
479
606
  if c.qualified == node.qualified and is_decl(c) ~= is_decl(node)
480
- and graph.family(V.files[V.node_file[cid]].language) == family then out[cid] = true end
607
+ and graph.family(V.files[V.node_file[cid]].language) == family and same_declaration(id, cid) then out[cid] = true end
481
608
  end
482
609
  end
483
610
  return out
@@ -497,6 +624,11 @@ local function scan(V, targets, name)
497
624
  local wanted = { [name] = true }
498
625
  for id in pairs(targets) do
499
626
  for _, alias in ipairs(require('codeoutline.lua_bindings').aliases(V.nodes[id], V.lua_global_export_tables)) do wanted[alias] = true end
627
+ local f = V.node_file[id]
628
+ if V.files[f].language == 'lua' then
629
+ local n = id - V.id_of[f][1] + 1
630
+ for alias, index in pairs(V.files[f].lua_exports or {}) do if index == n then wanted[alias] = true end end
631
+ end
500
632
  end
501
633
  local files = {}
502
634
  for i, f in ipairs(V.file_order) do files[i] = f end
@@ -505,7 +637,16 @@ local function scan(V, targets, name)
505
637
  for _, f in ipairs(files) do
506
638
  control.check()
507
639
  local alias_refs = V.js_context.ordinary.aliases(f, targets, candidates)
640
+ if V.files[f].language == 'rust' then
641
+ for i in pairs(require('codeoutline.rust_resolution').alias_refs(V, f, targets, V.js_context)) do alias_refs[i] = true end
642
+ end
508
643
  local refs = V.files[f].refs
644
+ local lua_evidence = refs.lua_binding
645
+ if V.files[f].language == 'lua' and not lua_evidence then
646
+ for _, imp in ipairs(V.files[f].lua_imports or {}) do
647
+ if imp.member and wanted[imp.member] then lua_evidence = V.js_context.ordinary.data(f).refs.lua_binding; break end
648
+ end
649
+ end
509
650
  local names = refs.name
510
651
  local aliases = {}
511
652
  if V.files[f].language == 'kotlin' then
@@ -517,7 +658,7 @@ local function scan(V, targets, name)
517
658
  if i % 4096 == 0 then control.check() end
518
659
  if wanted[names[i]] or aliases[names[i]] or aliases[refs.recv[i]]
519
660
  or alias_refs[i]
520
- or (refs.lua_binding and refs.lua_binding[i] ~= '') then
661
+ or (lua_evidence and lua_evidence[i] ~= '') then
521
662
  if os.clock() > deadline then return hits, true end
522
663
  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
523
664
  if targets[dst] then hits[#hits + 1] = { f, i }; break end
@@ -526,18 +667,25 @@ local function scan(V, targets, name)
526
667
  end
527
668
  end
528
669
  end
529
- return hits, false
670
+ return hits, V.js_context.include_incomplete or V.js_context.workspace_incomplete or V.js_context.go_incomplete or V.js_context.go_methods_incomplete
671
+ or V.js_context.rust_incomplete or false
530
672
  end
531
673
 
532
674
  -- Files one import record resolves to, through the graph's dependency
533
675
  -- rules: a temporary record holding only that import.
534
676
  function M.import_files(V, f, imp)
535
677
  local rec = V.files[f]
536
- local fake = { path = rec.language == 'go' and '\0/import.go' or rec.path, language = rec.language,
678
+ if C_FAMILY[rec.language] then return (require('codeoutline.cpp_includes').resolve(V, f, imp, V.js_context)) end
679
+ if rec.language == 'rust' then
680
+ local files = require('codeoutline.rust_resolution').imports(V, f, imp, V.js_context)
681
+ if files then return files end
682
+ end
683
+ local fake = { path = rec.path, language = rec.language,
537
684
  nodes = {}, imports = { imp } }
538
685
  rawset(V.files, -1, fake)
539
- local ok, direct = pcall(graph.dependencies, V, -1)
686
+ local ok, direct = pcall(graph.dependencies, V, -1, V.js_context, true)
540
687
  rawset(V.files, -1, nil)
688
+ if not ok and control.is_interrupted(direct) then error(direct, 0) end
541
689
  local files = {}
542
690
  if ok then for g in pairs(direct) do files[#files + 1] = g end end
543
691
  table.sort(files, function(x, y) return V.files[x].path < V.files[y].path end)
@@ -606,19 +754,28 @@ end
606
754
 
607
755
  -- Locations of the files an import on this line names: project files by the
608
756
  -- dependency rules, and C/C++ headers found in the include directories.
609
- local function import_targets(V, f, line, include_paths)
757
+ local function import_targets(V, f, line)
610
758
  local rec, result, seen = V.files[f], {}, {}
611
759
  local function add(uri)
612
760
  if not seen[uri] then seen[uri] = true; result[#result + 1] = { uri = uri, range = line_range(1) } end
613
761
  end
614
762
  for _, imp in ipairs(rec.imports or {}) do
615
763
  if imp.line == line then
616
- local files = imp.system and {} or M.import_files(V, f, imp)
764
+ if rec.language == 'lua' and imp.kind == 'require' then
765
+ for _, id in ipairs(require('codeoutline.foreign').lua_entrypoints(V, imp.path)) do
766
+ local loc = location(V, id)
767
+ if loc and not seen[loc.uri] then seen[loc.uri] = true; result[#result + 1] = loc end
768
+ end
769
+ end
770
+ local files, selected, header
771
+ if C_FAMILY[rec.language] then
772
+ files, selected, header = require('codeoutline.cpp_includes').resolve(V, f, imp, V.js_context)
773
+ if imp.system and not selected then files = {} end
774
+ else files = M.import_files(V, f, imp) end
617
775
  for _, g in ipairs(files) do add(uri_of(V, g)) end
618
- if #files == 0 and C_FAMILY[rec.language] then
776
+ if #files == 0 and header then add(documents.uri(header)) end
777
+ if #files == 0 and not header and C_FAMILY[rec.language] then
619
778
  local dirs = {}
620
- if not imp.system then dirs[1] = (V.idx:abs(rec.path):match('^(.*)/[^/]*$')) end
621
- for _, dir in ipairs(include_paths or {}) do dirs[#dirs + 1] = paths.normalize(dir) end
622
779
  for _, dir in ipairs(probe_includes()) do dirs[#dirs + 1] = dir end
623
780
  for _, dir in ipairs(dirs) do
624
781
  local file = dir .. '/' .. imp.path:gsub('\\', '/')
@@ -1008,22 +1165,89 @@ end
1008
1165
  local function at(req)
1009
1166
  local doc = request_doc(req)
1010
1167
  if not doc then return nil end
1011
- local V = view(req.session, req.root)
1168
+ local V = view(req.session, req.root, req.includePaths)
1012
1169
  local f = V.add(doc)
1013
1170
  if not f then return nil end
1014
1171
  local offset = doc:offset(req.position)
1015
- local c = cursor(doc, offset)
1016
- local targets = {}
1172
+ local c = cursor(doc, offset, V, f, req.method == 'lsp_definition')
1173
+ local targets, reason, evidence, macro_state = {}
1017
1174
  if c and c.decl then targets = { V.id_of[f][c.decl] }
1018
- elseif c then targets = resolve_site(V, f, c.from, c.name, c.kind, c.recv, c.evidence, c.first, c.index) end
1175
+ elseif c then
1176
+ c.line = doc:position(c.first).line + 1
1177
+ targets, reason, evidence, macro_state = resolve_site(V, f, c.from, c.name, c.kind, c.recv, c.evidence, c.first, c.index, c.line, req.method == 'lsp_definition')
1178
+ c.reason, c.evidence, c.macro_state = reason, c.kind == 'type' and c.evidence or evidence, macro_state
1179
+ end
1019
1180
  return V, doc, c, targets, f, offset
1020
1181
  end
1021
1182
 
1022
1183
  local handlers = {}
1023
1184
 
1185
+ -- Best-effort idle work: share the exact caches used by navigation, rather
1186
+ -- than retaining answers that could outlive a document/configuration change.
1187
+ function handlers.lsp_warm(req)
1188
+ local doc = request_doc(req)
1189
+ if not doc or #doc.source > M.MAX_FILE_BYTES then return { files = 0 } end
1190
+ local V = view(req.session, req.root, req.includePaths, req.uri)
1191
+ local f = V.add(doc)
1192
+ if not f then return { files = 0 } end
1193
+ local started, files, bytes = xtimer.now_ms(), 0, 0
1194
+ local function warm(target)
1195
+ control.check()
1196
+ local draft = V.drafts[target]
1197
+ if not draft then
1198
+ local path = paths.key(V.idx:abs(V.files[target].path))
1199
+ for _, candidate in pairs(sessions[req.session] or {}) do
1200
+ if candidate.path and paths.key(candidate.path) == path then draft = candidate; break end
1201
+ end
1202
+ end
1203
+ local size = draft and #draft.source or V.files[target].size or 0
1204
+ if files >= 4 or size > M.MAX_FILE_BYTES or bytes + size > 2 * M.MAX_FILE_BYTES
1205
+ or xtimer.now_ms() - started >= 500 then return false end
1206
+ if draft then V.add(draft) end
1207
+ local rec = M.full(V, target)
1208
+ if not rec then return false end
1209
+ files, bytes = files + 1, bytes + size
1210
+ local lang = rec.language
1211
+ if require('codeoutline.type_uses').languages[lang] or lang == 'python' or lang == 'javascript'
1212
+ or lang == 'lua' then V.js_context.ordinary.data(target) end
1213
+ return true
1214
+ end
1215
+ warm(f)
1216
+ local direct = graph.dependencies(V, f, V.js_context, true)
1217
+ local candidates = {}
1218
+ for target in pairs(direct) do if target ~= f then candidates[#candidates + 1] = target end end
1219
+ table.sort(candidates, function(a, b) return V.files[a].path < V.files[b].path end)
1220
+ for _, target in ipairs(candidates) do
1221
+ if files >= 4 or xtimer.now_ms() - started >= 500 then break end
1222
+ warm(target)
1223
+ end
1224
+ return { files = files }
1225
+ end
1226
+
1227
+ function handlers.lsp_implementation(req)
1228
+ local V, _, _, targets = at(req)
1229
+ if not V then return {} end
1230
+ local relationships = require('codeoutline.implementations').context(V, V.js_context)
1231
+ local result, seen = {}, {}
1232
+ for _, target in ipairs(targets) do
1233
+ for _, id in ipairs(relationships.outgoing(target)) do
1234
+ if not seen[id] then seen[id] = true; result[#result + 1] = (location(V, id)) end
1235
+ end
1236
+ end
1237
+ return result, relationships.incomplete
1238
+ end
1239
+
1024
1240
  function handlers.lsp_definition(req)
1025
1241
  local V, doc, c, targets, f, offset = at(req)
1026
1242
  if not V then return false end
1243
+ if not c and (V.files[f].language == 'javascript' or V.files[f].language == 'typescript') then
1244
+ local events = require('codeoutline.shared_events').context(V, V.js_context)
1245
+ local found, recognized = events.definition(f, offset, doc:position(offset).line + 1)
1246
+ if recognized then
1247
+ local result = {}; for _, id in ipairs(found) do result[#result + 1] = (location(V, id)) end
1248
+ return result, events.incomplete or V.js_context.workspace_incomplete
1249
+ end
1250
+ end
1027
1251
  local configured, incomplete
1028
1252
  -- Rule arguments are strings or referenced values, never the name of a
1029
1253
  -- call/declaration itself. Avoid collecting dispatch sites for ordinary F12.
@@ -1032,6 +1256,14 @@ function handlers.lsp_definition(req)
1032
1256
  end
1033
1257
  if configured then return configured, incomplete end
1034
1258
  local line = doc:position(offset).line + 1
1259
+ if #targets == 0 and c and c.kind == 'call' and C_FAMILY[V.files[f].language] then
1260
+ -- Unknown build flags block execution binding, but F12 can still show
1261
+ -- the macro declaration sites that may be active at this call.
1262
+ local _, definitions = require('codeoutline.cpp_macros').resolve(V, f, c.name, line, V.js_context, true)
1263
+ if definitions and #definitions > 0 then targets = definitions end
1264
+ end
1265
+ -- Selecting a loader's member asks for that value, not the imported file.
1266
+ if #targets == 0 and c and type(c.evidence) == 'string' and c.evidence:match('^lua%-value:') then return {} end
1035
1267
  local importer = false
1036
1268
  if c and not c.decl then
1037
1269
  -- `require`/`dofile` themselves name the file they load, not a same-named project function.
@@ -1041,8 +1273,39 @@ function handlers.lsp_definition(req)
1041
1273
  end
1042
1274
  if #targets == 0 or importer then
1043
1275
  -- An import line: `require "a.b"`, `dofile("x.lua")`, `#include <x.h>`, `from . import m`.
1044
- local files = import_targets(V, f, line, req.includePaths)
1045
- if #files > 0 or #targets == 0 then return files end
1276
+ local files = import_targets(V, f, line)
1277
+ if #files > 0 or importer then
1278
+ return files, 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.rust_incomplete
1279
+ end
1280
+ end
1281
+ if #targets == 0 then
1282
+ if C_FAMILY[V.files[f].language] then
1283
+ local entries, info = require('codeoutline.external_headers').find(V, f, c, V.js_context, probe_includes)
1284
+ if #entries > 0 then
1285
+ local result = {}
1286
+ for _, entry in ipairs(entries) do
1287
+ local header = documents.new(documents.uri(entry.file), entry.source)
1288
+ result[#result + 1] = { uri = header.uri, range = header:range(entry.node.name_start, entry.node.name_end) }
1289
+ end
1290
+ return result, info.truncated
1291
+ end
1292
+ end
1293
+ local ids, info = require('codeoutline.definition_candidates').find(V, f, c, V.js_context)
1294
+ local result, seen, bytes, started = {}, {}, 0, os.clock()
1295
+ local count = 0
1296
+ for _, id in ipairs(ids) do
1297
+ local g = V.node_file[id]
1298
+ local size = V.files[g].size or 0
1299
+ if not seen[g] then
1300
+ if count >= 4 or size > M.MAX_FILE_BYTES or bytes + size > 2 * M.MAX_FILE_BYTES
1301
+ or os.clock() - started >= 0.5 then info.truncated = true; break end
1302
+ seen[g], bytes, count = true, bytes + size, count + 1
1303
+ end
1304
+ local target_doc, hit = locate(V, id)
1305
+ if hit then result[#result + 1] = { uri = target_doc.uri, range = target_doc:range(hit.name_start, hit.name_end) } end
1306
+ end
1307
+ return result, info.truncated or V.js_context.include_incomplete or V.js_context.workspace_incomplete
1308
+ 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
1046
1309
  end
1047
1310
  if c.decl and is_decl(V.nodes[targets[1]]) then
1048
1311
  -- From a declaration, go to its definitions.
@@ -1054,7 +1317,7 @@ function handlers.lsp_definition(req)
1054
1317
  end
1055
1318
  local result = {}
1056
1319
  for _, id in ipairs(targets) do result[#result + 1] = (location(V, id)) end
1057
- return result
1320
+ 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
1058
1321
  end
1059
1322
 
1060
1323
  -- Follow unmatched delimiters up to the cursor. Commas in nested calls,
@@ -1189,7 +1452,7 @@ local function positions_left(t0, incomplete)
1189
1452
  end
1190
1453
 
1191
1454
  function handlers.lsp_incoming(req)
1192
- local V = view(req.session, req.root)
1455
+ local V = view(req.session, req.root, req.includePaths)
1193
1456
  local id = relocate(V, req.data)
1194
1457
  if not id then return {} end
1195
1458
  local name = V.nodes[id].name
@@ -1213,7 +1476,7 @@ function handlers.lsp_incoming(req)
1213
1476
  end
1214
1477
 
1215
1478
  function handlers.lsp_outgoing(req)
1216
- local V = view(req.session, req.root)
1479
+ local V = view(req.session, req.root, req.includePaths)
1217
1480
  local f = type(req.data) == 'table' and type(req.data.path) == 'string' and V.file_index[req.data.path]
1218
1481
  if not f then return {} end
1219
1482
  if not V.drafts[f] then
@@ -1242,13 +1505,36 @@ function handlers.lsp_outgoing(req)
1242
1505
  end
1243
1506
  end
1244
1507
  end
1245
- return result
1508
+ 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
1246
1509
  end
1247
1510
 
1248
1511
  function handlers.lsp_references(req)
1249
1512
  local V, _, c, targets = at(req)
1250
- if not V or #targets == 0 then return {} end
1513
+ if not V then return {} end
1514
+ if #targets == 0 then return {}, 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 end
1251
1515
  local set = {}
1516
+ local type_ids = {}
1517
+ for _, id in ipairs(targets) do if TYPES[V.nodes[id].kind] then type_ids[#type_ids + 1] = id end end
1518
+ if #type_ids > 0 then
1519
+ local engine = require('codeoutline.type_references')
1520
+ for _, id in ipairs(type_ids) do
1521
+ if not engine.kinds[V.nodes[id].kind] or not require('codeoutline.type_uses').languages[V.files[V.node_file[id]].language] then
1522
+ return { error = 'Type references currently support explicit C/C++, C#, Java, Kotlin, TypeScript, Go and Rust positions' }
1523
+ end
1524
+ end
1525
+ local details = engine.query(V, { query_source = function(_, path)
1526
+ local source = source_doc(V, V.file_index[path]); return source and source.source, false
1527
+ end }, type_ids, { cache = binding_cache, max_nodes = M.MAX_RESULTS, include_paths = req.includePaths,
1528
+ time_limit_ms = 600, max_files = 16 })
1529
+ local result = {}
1530
+ if req.includeDeclaration then for _, target in ipairs(details.targets) do result[#result + 1] = location(V, target.id) end end
1531
+ for _, ref in ipairs(details.references) do
1532
+ if #result >= M.MAX_RESULTS then details.incomplete = true; break end
1533
+ local source = source_doc(V, V.file_index[ref.file])
1534
+ if source then result[#result + 1] = { uri = uri_of(V, V.file_index[ref.file]), range = source:range(ref.first, ref.last) } end
1535
+ end
1536
+ return result, details.incomplete
1537
+ end
1252
1538
  for _, id in ipairs(targets) do
1253
1539
  if not REFERENCE[V.nodes[id].kind] then
1254
1540
  return { error = 'References currently support functions, methods, constructors and macros only' }