@codua/codeoutline-win32-x64 0.2.0 → 0.2.4

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.
@@ -79,7 +79,7 @@ local function segments(name)
79
79
  end
80
80
 
81
81
  local SOURCE_EXT = {}
82
- for e in ('c h py pyi java lua cpp cc cxx hpp hh hxx'):gmatch('%a+') do SOURCE_EXT[e] = true end
82
+ for e in ('c h py pyi java kt kts lua cpp cc cxx hpp hh hxx'):gmatch('%a+') do SOURCE_EXT[e] = true end
83
83
 
84
84
  -- Query -> candidate terms: qualified chains, identifiers, file names.
85
85
  local function terms(query)
@@ -234,9 +234,10 @@ local function path_between(G, a, b, depth)
234
234
  end
235
235
  end
236
236
  frontier = next_frontier
237
- if #frontier == 0 or #frontier > 20000 then break end
237
+ if #frontier == 0 then return nil end
238
+ if #frontier > 20000 then return nil, true end
238
239
  end
239
- return nil
240
+ return nil, #frontier > 0
240
241
  end
241
242
 
242
243
  local file_cache = {}
@@ -245,7 +246,7 @@ local function file_lines(idx, rel)
245
246
  local lines = file_cache[key]
246
247
  if lines then return lines end
247
248
  lines = {}
248
- local src = idx:query_source(rel)
249
+ local src = assert(idx:query_source(rel), 'cannot read source: ' .. rel)
249
250
  if src then
250
251
  for line in (src .. '\n'):gmatch('([^\n]*)\n') do lines[#lines + 1] = (line:gsub('\r$', '')) end
251
252
  end
@@ -293,7 +294,7 @@ local function node_block(G, idx, id, full)
293
294
  render_range(lines, b - M.CUT_TAIL + 1, b, out)
294
295
  end
295
296
  end
296
- return table.concat(out, '\n')
297
+ return table.concat(out, '\n'), span > full
297
298
  end
298
299
 
299
300
  local function label(G, id)
@@ -307,7 +308,10 @@ function M.run(G, idx, query, opts)
307
308
  control.check()
308
309
  opts = opts or {}
309
310
  local budget = M.validate(query, opts)
310
- G = graph.query(G, opts)
311
+ local query_opts = {}
312
+ for key, value in pairs(opts) do query_opts[key] = value end
313
+ query_opts.explain = true
314
+ G = graph.query(G, query_opts)
311
315
  file_cache = {}
312
316
  local ranked, named_files = find_seeds(G, query)
313
317
 
@@ -334,10 +338,12 @@ function M.run(G, idx, query, opts)
334
338
 
335
339
  -- Spine: nodes on call paths between seeds.
336
340
  local flows, spine, on_spine = {}, {}, {}
341
+ local path_limited = false
337
342
  for _, a in ipairs(seeds) do
338
343
  for _, b in ipairs(seeds) do
339
344
  if a ~= b then
340
- local p = path_between(G, a, b, M.PATH_DEPTH)
345
+ local p, limited = path_between(G, a, b, M.PATH_DEPTH)
346
+ path_limited = path_limited or limited or false
341
347
  if p then
342
348
  local names = {}
343
349
  for _, id in ipairs(p) do
@@ -429,11 +435,21 @@ function M.run(G, idx, query, opts)
429
435
  end
430
436
  end
431
437
  for f in pairs(selected) do
432
- local _, repaired = idx:query_source(G.files[f].path)
438
+ local path = G.files[f].path -- a decoding repair replaces this file ID
439
+ local source, repaired = idx:query_source(path)
440
+ assert(source, 'cannot read source: ' .. path)
433
441
  changed = repaired or changed
434
442
  end
435
443
  if changed then return nil, { encoding_retry = true } end
436
444
 
445
+ local evidence_text = require('codeoutline.query_report').render(G.report, G)
446
+ local evidence_budget = math.min(4096, math.floor(budget / 4))
447
+ local evidence_cut = #evidence_text > evidence_budget
448
+ if evidence_budget >= 256 then
449
+ emit(text.limit(evidence_text, evidence_budget, G.report.truncated))
450
+ else evidence_cut = #G.report.entries > 0 end
451
+ omitted = omitted or evidence_cut or G.report.truncated or #flows > M.LIST_CAP
452
+
437
453
  local reserve = math.floor(budget * 0.15) -- keep room for sections 3-4
438
454
  for _, f in ipairs(file_order) do
439
455
  local rec = G.files[f]
@@ -454,7 +470,8 @@ function M.run(G, idx, query, opts)
454
470
  end
455
471
  for _, item in ipairs(items) do
456
472
  if not shown[item.id] then
457
- local block = node_block(G, idx, item.id, item.full)
473
+ local block, source_cut = node_block(G, idx, item.id, item.full)
474
+ omitted = omitted or source_cut
458
475
  local need = #block + (wrote_header and 0 or #header + 1) + 1
459
476
  if used + need < budget - reserve then
460
477
  if not wrote_header then emit(header); wrote_header = true end
@@ -479,6 +496,8 @@ function M.run(G, idx, query, opts)
479
496
  if not seen_in[src] and #callers < M.LIST_CAP then
480
497
  seen_in[src] = true
481
498
  callers[#callers + 1] = string.format('%s (%s:%d)', G.nodes[src].name, G.files[G.node_file[src]].path, inn[i + 1])
499
+ .. (inn[i + 2]:match('^indirect:') and (' [' .. inn[i + 2] .. ']') or '')
500
+ elseif not seen_in[src] then omitted = true
482
501
  end
483
502
  end
484
503
  for i = 1, #out, graph.EDGE do
@@ -486,6 +505,8 @@ function M.run(G, idx, query, opts)
486
505
  if not seen_out[dst] and #callees < M.LIST_CAP then
487
506
  seen_out[dst] = true
488
507
  callees[#callees + 1] = G.nodes[dst].name .. ' (' .. graph.where(G, dst) .. ')'
508
+ .. (out[i + 2]:match('^indirect:') and (' [' .. out[i + 2] .. ']') or '')
509
+ elseif not seen_out[dst] then omitted = true
489
510
  end
490
511
  end
491
512
  if #callers > 0 or #callees > 0 then
@@ -521,9 +542,17 @@ function M.run(G, idx, query, opts)
521
542
  table.insert(parts, 1, 'Call-path expansion limit reached; relationships are incomplete.')
522
543
  omitted = true
523
544
  end
545
+ if path_limited then
546
+ table.insert(parts, 1, 'Call-path search reached its depth/frontier limit; no path found is not proof of no runtime connection.')
547
+ end
548
+ if evidence_cut or G.report.truncated then
549
+ table.insert(parts, 1, 'Relationship explanations omitted due to evidence/output limits.')
550
+ end
524
551
  local result, truncated = text.limit(table.concat(parts, '\n'), budget, omitted or #pointers > 0)
525
552
  return result, { seeds = seeds, spine = spine, shown = shown, flows = flows, truncated = truncated,
526
- relationships_incomplete = G.incomplete or false, query_stats = G.stats }
553
+ relationships_incomplete = G.incomplete or false, query_stats = G.stats,
554
+ evidence = G.report.entries, evidence_truncated = evidence_cut or G.report.truncated,
555
+ path_search_limited = path_limited, evidence_scope = G.report.scope }
527
556
  end
528
557
 
529
558
  return M
@@ -0,0 +1,60 @@
1
+ -- Language-neutral native exports. Adapters supply registration evidence and
2
+ -- guest parameters; implementation nodes retain their original host signature.
3
+ local M = {}
4
+
5
+ function M.register(rec, language, module, exports)
6
+ rec.foreign_exports = rec.foreign_exports or {}
7
+ rec.foreign_exports[language] = rec.foreign_exports[language] or {}
8
+ rec.foreign_exports[language][module] = exports
9
+ end
10
+
11
+ function M.exports(rec, language, module)
12
+ return rec.foreign_exports and rec.foreign_exports[language] and rec.foreign_exports[language][module]
13
+ end
14
+
15
+ function M.collect(rec, T)
16
+ require('codeoutline.foreign_lua').collect(rec, T)
17
+ end
18
+
19
+ function M.modules(G, language, name, ctx)
20
+ ctx = ctx or {}
21
+ if not ctx.foreign then
22
+ ctx.foreign = {}
23
+ local seen = {}
24
+ local function add(f)
25
+ if seen[f] then return end
26
+ seen[f] = true
27
+ for lang, modules in pairs(G.files[f].foreign_exports or {}) do
28
+ ctx.foreign[lang] = ctx.foreign[lang] or {}
29
+ for module in pairs(modules) do
30
+ local names = ctx.foreign[lang]
31
+ names[module] = names[module] or {}
32
+ table.insert(names[module], f)
33
+ end
34
+ end
35
+ end
36
+ for _, f in ipairs(G.file_order or {}) do add(f) end
37
+ for _, f in ipairs(G.new_files or {}) do add(f) end
38
+ end
39
+ return (ctx.foreign[language] or {})[name] or {}
40
+ end
41
+
42
+ -- Parameter labels are structured data, never reparsed as host-language code.
43
+ function M.signature(node, language, name)
44
+ local contract = node.foreign_signatures and node.foreign_signatures[language]
45
+ if not contract then return nil end
46
+ local labels, parameters = {}, {}
47
+ for i, p in ipairs(contract.parameters) do
48
+ local label = (p.name or ('arg' .. i)) .. (p.optional and '?' or '') .. ':' .. (p.type or 'unknown')
49
+ if p.default then label = label .. ' = ' .. p.default end
50
+ labels[#labels + 1] = label
51
+ parameters[#parameters + 1] = { label = label }
52
+ end
53
+ if contract.unknown_tail then
54
+ labels[#labels + 1] = '...:unknown'
55
+ parameters[#parameters + 1] = { label = '...:unknown' }
56
+ end
57
+ return { label = (name or node.name) .. '(' .. table.concat(labels, ', ') .. ')', parameters = parameters }
58
+ end
59
+
60
+ return M
@@ -0,0 +1,146 @@
1
+ -- Lua C API adapter for the language-neutral foreign interface model.
2
+ local M = {}
3
+ local control = require('codeoutline.control')
4
+ local function literal(T, i)
5
+ local s = T:text(i)
6
+ if T.k[i] ~= 'str' or s:find('\\', 1, true) then return nil end
7
+ return s:sub(2, -2)
8
+ end
9
+
10
+ local READ = {
11
+ luaL_checkinteger = 'int', luaL_optinteger = 'int', lua_tointeger = 'int', lua_tointegerx = 'int',
12
+ luaL_checknumber = 'number', luaL_optnumber = 'number', lua_tonumber = 'number', lua_tonumberx = 'number',
13
+ luaL_checkstring = 'string', luaL_checklstring = 'string', luaL_optstring = 'string', luaL_optlstring = 'string',
14
+ lua_tostring = 'string', lua_tolstring = 'string', lua_toboolean = 'boolean',
15
+ luaL_checkudata = 'userdata', luaL_testudata = 'userdata', lua_touserdata = 'userdata',
16
+ luaL_checkany = 'unknown', luaL_checktype = 'unknown',
17
+ lua_isinteger = 'int', lua_isnumber = 'number', lua_isstring = 'string', lua_isboolean = 'boolean',
18
+ lua_istable = 'table', lua_isfunction = 'function', lua_isthread = 'thread', lua_isuserdata = 'userdata',
19
+ }
20
+ local TYPES = { LUA_TNUMBER = 'number', LUA_TSTRING = 'string', LUA_TBOOLEAN = 'boolean',
21
+ LUA_TTABLE = 'table', LUA_TFUNCTION = 'function', LUA_TTHREAD = 'thread',
22
+ LUA_TUSERDATA = 'userdata', LUA_TLIGHTUSERDATA = 'lightuserdata', LUA_TNIL = 'nil' }
23
+
24
+ local function parameters(T, node)
25
+ local state
26
+ for i = node.ti, node.tj - 2 do
27
+ if T:text(i) == 'lua_State' and T:text(i + 1) == '*' and T.k[i + 2] == 'id' then
28
+ state = T:text(i + 2); break
29
+ end
30
+ end
31
+ local slots, highest, unknown = {}, 0, false
32
+ for i = node.ti, node.tj do
33
+ if i % 4096 == 0 then control.check() end
34
+ local api = T:text(i)
35
+ if READ[api] and T:text(i + 1) == '(' and T:text(i + 2) == state and T:text(i + 3) == ',' then
36
+ local position = tonumber(T:text(i + 4))
37
+ local ending = T:text(i + 5)
38
+ if position and position > 0 and position <= 64 and position == math.floor(position)
39
+ and (ending == ',' or ending == ')') then
40
+ highest = math.max(highest, position)
41
+ local p = slots[position] or { types = {} }
42
+ slots[position] = p
43
+ local kind = READ[api]
44
+ if api == 'luaL_checktype' then kind = TYPES[T:text(i + 6)] or 'unknown' end
45
+ if api == 'luaL_checkudata' or api == 'luaL_testudata' then kind = literal(T, i + 6) or kind end
46
+ p.types[kind] = true
47
+ local optional = api:match('^luaL_opt') ~= nil
48
+ if p.optional == nil then p.optional = optional else p.optional = p.optional and optional end
49
+ if optional then
50
+ local close = T.m[i + 1]
51
+ local last = close and close - 1
52
+ if last and api == 'luaL_optlstring' then
53
+ last = i + 6
54
+ while last < close and T:text(last) ~= ',' do last = last + 1 end
55
+ last = last - 1
56
+ end
57
+ if last and last >= i + 6 then p.default = T.src:sub(T.s[i + 6], T.e[last]) end
58
+ end
59
+ -- Only a direct assignment supplies a public argument name.
60
+ -- Casts between '=' and the API call are harmless; expressions aren't.
61
+ local j = i - 1
62
+ while j >= node.ti and T:text(j) ~= ';' and T:text(j) ~= '{' and T:text(j) ~= '}' do
63
+ if T:text(j) == '=' then
64
+ local direct = j == i - 1 or (T:text(j + 1) == '(' and T.m[j + 1] == i - 1)
65
+ if direct and not api:match('^lua_is') and api ~= 'luaL_checktype'
66
+ and T.k[j - 1] == 'id' and T:text(j - 2) ~= '.' and T:text(j - 2) ~= '->' then
67
+ local name = T:text(j - 1)
68
+ if not p.name then p.name = name elseif p.name ~= name then p.ambiguous = true end
69
+ end
70
+ break
71
+ end
72
+ j = j - 1
73
+ end
74
+ elseif T:text(i + 4) ~= '-' and T:text(i + 4) ~= 'lua_upvalueindex' then
75
+ unknown = true
76
+ end
77
+ end
78
+ end
79
+ local result, used = { parameters = {}, unknown_tail = unknown }, {}
80
+ for i = 1, highest do
81
+ local p = slots[i] or { types = {} }
82
+ p.types.unknown = nil
83
+ local types = {}
84
+ for kind in pairs(p.types) do types[#types + 1] = kind end
85
+ table.sort(types)
86
+ local name = not p.ambiguous and p.name or nil
87
+ if name and used[name] then name = nil end
88
+ if name then used[name] = true end
89
+ result.parameters[i] = { name = name, type = #types > 0 and table.concat(types, '|') or 'unknown',
90
+ optional = p.optional or nil, default = p.optional and p.default or nil }
91
+ end
92
+ return result
93
+ end
94
+
95
+ -- Only explicit registration tables wired inside luaopen_* are exports.
96
+ -- Keep the original C nodes, including their source ranges and signatures.
97
+ function M.collect(r, T)
98
+ local tables, functions = {}, {}
99
+ for n, node in ipairs(r.nodes) do
100
+ if node.kind == 'function' then functions[node.name] = n end
101
+ end
102
+ for i = 1, T.n do
103
+ if i % 4096 == 0 then control.check() end
104
+ if T:text(i) == 'luaL_Reg' and T.k[i + 1] == 'id' then
105
+ local j = i + 2
106
+ if T:text(j) == '[' and T.m[j] then j = T.m[j] + 1 end
107
+ if T:text(j) == '=' and T:text(j + 1) == '{' and T.m[j + 1] then
108
+ local entries = {}
109
+ for k = j + 2, T.m[j + 1] - 1 do
110
+ if T:text(k) == '{' and T.k[k + 1] == 'str' and T:text(k + 2) == ',' then
111
+ local name, target = literal(T, k + 1), functions[T:text(k + 3)]
112
+ if name and target and T:text(k + 4) == '}' then entries[name] = target end
113
+ end
114
+ end
115
+ tables[T:text(i + 1)] = entries
116
+ end
117
+ end
118
+ end
119
+ for _, node in ipairs(r.nodes) do
120
+ local module = node.kind == 'function' and node.name:match('^luaopen_(.+)$')
121
+ if module then
122
+ local exports = {}
123
+ for i = node.ti, node.tj do
124
+ local call = T:text(i)
125
+ -- A fresh table replaces registrations for earlier metatables.
126
+ if T:text(i + 1) == '(' and (call == 'luaL_newlib' or call == 'lua_newtable'
127
+ or call == 'lua_createtable' or call == 'luaL_newmetatable') then exports = {} end
128
+ if T:text(i + 1) == '(' and T:text(i + 3) == ',' then
129
+ local arg = (call == 'luaL_newlib' or call == 'luaL_setfuncs') and i + 4
130
+ if call == 'luaL_register' and T:text(i + 5) == ',' then arg = i + 6 end
131
+ for name, n in pairs(tables[arg and T:text(arg)] or {}) do exports[name] = n end
132
+ end
133
+ end
134
+ if next(exports) then
135
+ require('codeoutline.foreign').register(r, 'lua', module, exports)
136
+ for _, n in pairs(exports) do
137
+ local target = r.nodes[n]
138
+ target.foreign_signatures = target.foreign_signatures or {}
139
+ target.foreign_signatures.lua = target.foreign_signatures.lua or parameters(T, target)
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
145
+
146
+ return M
@@ -27,6 +27,8 @@
27
27
  -- to MAX_TIES targets instead of guessing.
28
28
 
29
29
  local control = require('codeoutline.control')
30
+ local js_resolution = require('codeoutline.js_resolution')
31
+ local lua_bindings = require('codeoutline.lua_bindings')
30
32
  local M = {}
31
33
 
32
34
  M.EDGE = 3
@@ -66,7 +68,7 @@ local function push3(t, k, a, b, c)
66
68
  end
67
69
 
68
70
  -- Files a file depends on, resolved from its #include / import records.
69
- local function resolve_deps(G, f)
71
+ local function resolve_deps(G, f, js_context)
70
72
  local rec = G.files[f]
71
73
  local deps = {}
72
74
  local function add(g) if g and g ~= f then deps[g] = true end end
@@ -125,19 +127,7 @@ local function resolve_deps(G, f)
125
127
  -- require 'a.b' -> a/b.lua or a/b/init.lua anywhere under the root
126
128
  -- (package.path prefixes like scripts/?.lua vary per project);
127
129
  -- dofile/loadfile take a path.
128
- local wants
129
- if imp.kind == 'require' then
130
- local sub = path:gsub('%.', '/')
131
- wants = { sub .. '.lua', sub .. '/init.lua' }
132
- else
133
- wants = { (path:gsub('\\', '/'):gsub('^%./', '')) }
134
- end
135
- for _, want in ipairs(wants) do
136
- for _, g in ipairs(G.by_base[basename(want)] or {}) do
137
- local p = G.files[g].path
138
- if p == want or p:sub(-(#want + 1)) == '/' .. want then add(g) end
139
- end
140
- end
130
+ for _, g in ipairs(lua_bindings.modules(G, imp, js_context and js_context.lua)) do add(g) end
141
131
  elseif rec.language == 'go' then
142
132
  -- import "github.com/org/repo/pkg/sub": the package is the directory
143
133
  -- whose path ends with the longest matching suffix of the import
@@ -155,22 +145,7 @@ local function resolve_deps(G, f)
155
145
  if hit then break end
156
146
  end
157
147
  elseif rec.language == 'javascript' or rec.language == 'typescript' then
158
- -- relative specifiers only; bare package names live in node_modules
159
- if path:sub(1, 1) == '.' then
160
- local base = here
161
- local rel = path
162
- while true do
163
- if rel:sub(1, 2) == './' then rel = rel:sub(3)
164
- elseif rel:sub(1, 3) == '../' then base = dirname(base); rel = rel:sub(4)
165
- else break end
166
- end
167
- local stemp = (base ~= '' and (base .. '/') or '') .. rel
168
- stemp = stemp:gsub('%.[mc]?js$', '') -- TS sources imported as ./x.js
169
- for _, ext in ipairs({ '', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs', '.mts',
170
- '/index.ts', '/index.tsx', '/index.js', '/index.mjs' }) do
171
- add(G.file_index[stemp .. ext])
172
- end
173
- end
148
+ add(js_resolution.module(G, f, path, js_context))
174
149
  elseif rec.language == 'csharp' then
175
150
  for _, g in ipairs(G.ns_files[path] or {}) do add(g) end
176
151
  elseif rec.language == 'rust' then
@@ -202,9 +177,12 @@ local function resolve_deps(G, f)
202
177
  if found then break end
203
178
  end
204
179
  end
205
- elseif rec.language == 'java' then
180
+ elseif rec.language == 'java' or rec.language == 'kotlin' then
206
181
  if path:sub(-2) == '.*' then
207
182
  for _, g in ipairs(G.pkg_files[path:sub(1, -3)] or {}) do add(g) end
183
+ if rec.language == 'kotlin' then
184
+ for _, id in ipairs(G.by_owner[path:sub(1, -3)] or {}) do add(G.node_file[id]) end
185
+ end
208
186
  else
209
187
  for _, id in ipairs(G.by_qualified[path] or {}) do add(G.node_file[id]) end
210
188
  end
@@ -212,7 +190,7 @@ local function resolve_deps(G, f)
212
190
  end
213
191
  -- Java: the whole package is visible without imports; Go: the whole
214
192
  -- directory is one package; C#: the file's own namespaces.
215
- if rec.language == 'java' and rec.package then
193
+ if (rec.language == 'java' or rec.language == 'kotlin') and rec.package then
216
194
  for _, g in ipairs(G.pkg_files[rec.package] or {}) do add(g) end
217
195
  elseif rec.language == 'go' then
218
196
  for _, g in ipairs(G.dir_files[here] or {}) do add(g) end
@@ -291,7 +269,7 @@ local function score(G, f, src_id, ref_kind, recv, cid, deps, paired, owner)
291
269
  -- method project-wide (str.format -> some format()) is mostly wrong,
292
270
  -- so only local candidates qualify.
293
271
  return nil
294
- elseif ref_kind == 'call' and G.files[f].language == 'java' and src_id then
272
+ elseif ref_kind == 'call' and (G.files[f].language == 'java' or G.files[f].language == 'kotlin') and src_id then
295
273
  -- unqualified call inside a class: implicit this
296
274
  if owner and c.parent and G.id_of[cf][c.parent] == owner then s = s + 30 end
297
275
  end
@@ -305,23 +283,48 @@ local function is_decl(node) return node.kind == 'prototype' or node.decl end
305
283
  local scored_ids, scored_vals = {}, {}
306
284
 
307
285
  -- accept overrides the node kinds the ref kind may bind to (navigation only).
308
- local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept)
286
+ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept, evidence, js_context)
309
287
  if kind == 'annotation' then return nil end
310
288
  accept = accept or (kind == 'new' and NEWABLE) or (kind == 'ref' and VALUE) or CALLABLE
289
+ if evidence == '' then evidence = nil end
290
+ local strategy = 'js/ts binding'
291
+ local exact = js_resolution.resolve(G, f, name, kind, recv, evidence, js_context)
292
+ if exact == nil then
293
+ strategy = 'lua binding'
294
+ exact = lua_bindings.resolve(G, f, name, evidence, js_context and js_context.lua, recv)
295
+ end
296
+ if exact ~= nil then
297
+ local result = {}
298
+ for _, id in ipairs(exact) do if accept[G.nodes[id].kind] then result[#result + 1] = id end end
299
+ return result, #result == 0 and 'binding target unresolved' or strategy
300
+ end
311
301
  local cands = G.by_name[name]
312
- if not cands then return nil end
302
+ if G.files[f].language == 'kotlin' and not recv then
303
+ for _, imp in ipairs(G.files[f].imports) do
304
+ if (imp.alias or imp.path:match('([^.]+)$')) == name then
305
+ cands = {}
306
+ -- by_name also includes unsaved LSP overlays; by_qualified is
307
+ -- a resident index and may still name the masked disk nodes.
308
+ for _, id in ipairs(G.by_name[imp.path:match('([^.]+)$')] or {}) do
309
+ if G.nodes[id].qualified == imp.path then cands[#cands + 1] = id end
310
+ end
311
+ break
312
+ end
313
+ end
314
+ end
315
+ if not cands then return nil, 'no indexed target' end
313
316
  -- Score first: score() rejects other languages and statics of other
314
317
  -- files, so "is there a real definition?" is only asked among the
315
318
  -- candidates this call could actually reach. (Asking it over all
316
319
  -- same-named nodes let a Python `def work` hide the C prototype of
317
320
  -- `work`, after which the Python def was filtered out too.)
318
321
  local self_call = kind == 'member_call' and src_id and (recv == 'self' or recv == 'this' or recv == 'cls')
319
- local owner = (self_call or (kind == 'call' and src_id and G.files[f].language == 'java'))
322
+ local owner = (self_call or (kind == 'call' and src_id and (G.files[f].language == 'java' or G.files[f].language == 'kotlin')))
320
323
  and owner_class(G, src_id) or nil
321
324
  -- score() rejects an unknown-receiver member call to an unrelated file;
322
325
  -- checking placement first skips most candidates of common method names.
323
326
  local local_only = kind == 'member_call' and not self_call
324
- local n, has_def = 0, false
327
+ local n, has_def, has_name = 0, false, false
325
328
  for i = 1, math.min(#cands, M.MAX_CANDIDATES) do
326
329
  local cid = cands[i]
327
330
  local node = G.nodes[cid]
@@ -332,26 +335,33 @@ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept)
332
335
  n = n + 1
333
336
  scored_ids[n], scored_vals[n] = cid, s
334
337
  if not is_decl(node) then has_def = true end
338
+ if node.name == name then has_name = true end
335
339
  end
336
340
  end
337
341
  end
338
- -- Prefer definitions: drop prototypes / in-class declarations when a
339
- -- reachable definition exists.
340
- local best, ties = nil, {}
342
+ -- Prefer real names over export aliases (which may be platform stubs),
343
+ -- then definitions over prototypes / in-class declarations.
344
+ local best, ties, tie_count = nil, {}, 0
341
345
  for i = 1, n do
342
346
  local cid, s = scored_ids[i], scored_vals[i]
343
- if not (has_def and is_decl(G.nodes[cid])) then
347
+ if not (has_def and is_decl(G.nodes[cid])) and (not has_name or G.nodes[cid].name == name) then
344
348
  if not best or s > best then
345
- best, ties = s, { cid }
346
- elseif s == best and #ties < M.MAX_TIES then
347
- ties[#ties + 1] = cid
349
+ best, ties, tie_count = s, { cid }, 1
350
+ elseif s == best then
351
+ tie_count = tie_count + 1
352
+ if #ties < M.MAX_TIES then ties[#ties + 1] = cid end
348
353
  end
349
354
  end
350
355
  end
351
356
  -- A lone candidate far away (score 0) is still a match for a unique
352
357
  -- name; many far candidates with nothing to separate them is a guess.
353
- if best and best <= 0 and #ties > 1 then return nil end
354
- return ties
358
+ if best and best <= 0 and #ties > 1 then
359
+ return nil, 'ambiguous name candidates' .. (#cands > M.MAX_CANDIDATES and '; candidate scan limit reached' or '')
360
+ end
361
+ local reason = #ties == 0 and 'no eligible target' or #ties > 1 and 'multiple name/locality candidates' or 'name/locality match'
362
+ if #cands > M.MAX_CANDIDATES then reason = reason .. '; candidate scan limit reached' end
363
+ if tie_count > M.MAX_TIES then reason = reason .. '; tied candidate limit reached' end
364
+ return ties, reason
355
365
  end
356
366
 
357
367
  -- Files compare by path; nodes by file path, then node order (ids ascend
@@ -396,6 +406,7 @@ end
396
406
  function M.new()
397
407
  return { files = {}, file_index = {}, file_order = {}, file_dir = {}, dir_ids = {}, nodes = {}, node_file = {},
398
408
  id_of = {}, by_name = {}, by_lname = {}, by_qualified = {}, by_owner = {}, by_base = {}, by_stem = {}, pkg_files = {},
409
+ lua_global_export_tables = { 'g_exports' },
399
410
  children = {}, dir_files = {}, dir_by_base = {}, ns_files = {},
400
411
  next_file = 0, next_node = 0, next_dir = 0, file_count = 0, node_count = 0 }
401
412
  end
@@ -447,6 +458,7 @@ function M.add_file(G, rel, rec)
447
458
  G.nodes[id], G.node_file[id] = node, f
448
459
  ids[n] = id
449
460
  insert(G, G.by_name, node.name, id, node_before)
461
+ for _, alias in ipairs(lua_bindings.aliases(node, G.lua_global_export_tables)) do insert(G, G.by_name, alias, id, node_before) end
450
462
  insert(G, G.by_lname, node.name:lower(), id, node_before)
451
463
  insert(G, G.by_qualified, node.qualified, id, node_before)
452
464
  local owner = M.owner(node.qualified)
@@ -464,6 +476,7 @@ function M.remove_file(G, rel)
464
476
  for _, id in ipairs(ids) do
465
477
  local node = G.nodes[id]
466
478
  remove(G, G.by_name, node.name, id, node_before)
479
+ for _, alias in ipairs(lua_bindings.aliases(node, G.lua_global_export_tables)) do remove(G, G.by_name, alias, id, node_before) end
467
480
  remove(G, G.by_lname, node.name:lower(), id, node_before)
468
481
  remove(G, G.by_qualified, node.qualified, id, node_before)
469
482
  local owner = M.owner(node.qualified)
@@ -486,8 +499,10 @@ end
486
499
 
487
500
  -- Symbol tables for every record of idx. Checkpoints per file: a cancelled
488
501
  -- build is simply discarded.
489
- function M.build(idx)
502
+ function M.build(idx, lua_context)
490
503
  local G = M.new()
504
+ G.root = idx.root
505
+ G.lua_global_export_tables = (lua_context or lua_bindings.context(G)).global_export_tables
491
506
  local paths = {}
492
507
  for rel in pairs(idx.files) do paths[#paths + 1] = rel end
493
508
  table.sort(paths)
@@ -509,21 +524,38 @@ function M.query(symbols, opts)
509
524
  local Q = setmetatable({ out = {}, inn = {},
510
525
  stats = { expanded = 0, incoming_scans = 0, resolved_refs = 0 }, incomplete = false }, { __index = symbols })
511
526
  local dependencies, references = {}, {}
527
+ local js_context = js_resolution.context(symbols)
528
+ js_context.lua = lua_bindings.context(symbols)
529
+ local report = opts.explain and require('codeoutline.query_report').new() or nil
530
+ Q.report = report
531
+ local indirect = require('codeoutline.indirect').query(symbols, opts.dispatch_rules, report)
512
532
  local function deps(f)
513
533
  local entry = dependencies[f]
514
534
  if not entry then
515
- local direct, paired = resolve_deps(symbols, f)
535
+ local direct, paired = resolve_deps(symbols, f, js_context)
516
536
  entry = { direct, paired }; dependencies[f] = entry
517
537
  end
518
538
  return entry[1], entry[2]
519
539
  end
520
- local function targets(f, i)
540
+ local function targets(f, i, explain)
521
541
  local rec, ids = symbols.files[f], symbols.id_of[f]
522
542
  local refs = rec.refs
523
543
  local direct, paired = deps(f)
524
544
  Q.stats.resolved_refs = Q.stats.resolved_refs + 1
525
- return resolve_ref(symbols, f, ids[refs.from[i]], refs.name[i], refs.kind[i],
526
- refs.recv[i] or nil, direct, paired)
545
+ local found, reason = resolve_ref(symbols, f, ids[refs.from[i]], refs.name[i], refs.kind[i],
546
+ refs.recv[i] or nil, direct, paired, nil, refs.lua_binding and refs.lua_binding[i] or refs.js_receiver and refs.js_receiver[i], js_context)
547
+ if report and explain and (refs.kind[i] == 'call' or refs.kind[i] == 'member_call') then
548
+ if (not found or #found == 0) and explain == true then
549
+ report:add({ source = ids[refs.from[i]], line = refs.line[i], call = refs.name[i], reason = reason or 'unresolved reference' })
550
+ else
551
+ for _, dst in ipairs(found or {}) do
552
+ if explain == true or explain[dst] then
553
+ report:add({ source = ids[refs.from[i]], target = dst, line = refs.line[i], kind = reason })
554
+ end
555
+ end
556
+ end
557
+ end
558
+ return found
527
559
  end
528
560
  setmetatable(Q.out, { __index = function(cache, id)
529
561
  control.check()
@@ -541,9 +573,14 @@ function M.query(symbols, opts)
541
573
  references[f] = grouped
542
574
  end
543
575
  local edges = {}
576
+ for _, edge in ipairs(indirect.outgoing(id)) do
577
+ edges[#edges + 1] = edge.target
578
+ edges[#edges + 1] = edge.line
579
+ edges[#edges + 1] = edge.kind
580
+ end
544
581
  for _, i in ipairs(grouped[id] or {}) do
545
582
  control.check()
546
- for _, dst in ipairs(targets(f, i) or {}) do
583
+ for _, dst in ipairs(targets(f, i, true) or {}) do
547
584
  edges[#edges + 1] = dst
548
585
  edges[#edges + 1] = rec.refs.line[i]
549
586
  edges[#edges + 1] = rec.refs.kind[i]
@@ -558,25 +595,41 @@ function M.query(symbols, opts)
558
595
  for _, id in ipairs(ids) do
559
596
  if rawget(self.inn, id) == nil then
560
597
  wanted[id], names[symbols.nodes[id].name], found = true, true, true
598
+ for _, alias in ipairs(lua_bindings.aliases(symbols.nodes[id], symbols.lua_global_export_tables)) do names[alias] = true end
561
599
  end
562
600
  end
563
601
  if not found then return end
564
602
  local incoming = {}
565
603
  for id in pairs(wanted) do incoming[id] = {} end
604
+ local indirect_incoming = indirect.incoming(wanted)
605
+ for _, f in ipairs(symbols.file_order) do
606
+ for _, src in ipairs(symbols.id_of[f]) do
607
+ for _, edge in ipairs(indirect_incoming[src] or {}) do
608
+ if wanted[edge.target] then push3(incoming, edge.target, src, edge.line, edge.kind) end
609
+ end
610
+ end
611
+ end
566
612
  self.stats.incoming_scans = self.stats.incoming_scans + 1
567
613
  -- Hot loop over every reference: numeric loops, hoisted fields, and the
568
614
  -- selective name test first. Checkpoints run per file and per chunk.
569
615
  local files = symbols.files
570
616
  for _, f in ipairs(symbols.file_order) do
571
617
  control.check()
618
+ local aliases = {}
619
+ for _, binding in ipairs(files[f].js_imports or {}) do aliases[binding.local_name] = true end
620
+ if files[f].language == 'kotlin' then
621
+ for _, imp in ipairs(files[f].imports) do
622
+ if imp.alias and names[imp.path:match('([^.]+)$')] then aliases[imp.alias] = true end
623
+ end
624
+ end
572
625
  local refs = files[f].refs
573
626
  local ref_names, froms = refs.name, refs.from
574
627
  local count = #ref_names
575
628
  for first = 1, count, 4096 do
576
629
  if first > 1 then control.check() end
577
630
  for i = first, math.min(count, first + 4095) do
578
- if names[ref_names[i]] and froms[i] > 0 then
579
- for _, dst in ipairs(targets(f, i) or {}) do
631
+ if (names[ref_names[i]] or aliases[ref_names[i]] or aliases[refs.recv[i]] or (refs.lua_binding and refs.lua_binding[i] ~= '')) and froms[i] > 0 then
632
+ for _, dst in ipairs(targets(f, i, wanted) or {}) do
580
633
  if wanted[dst] then
581
634
  push3(incoming, dst, symbols.id_of[f][froms[i]], refs.line[i], refs.kind[i])
582
635
  end