@codua/codeoutline-win32-x64 0.2.4 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,16 @@
1
1
  -- Explicit dispatch contracts. No wildcard API guesses or source evaluation.
2
2
  local control = require('codeoutline.control')
3
3
  local M = {}
4
+ local aliases = require('codeoutline.dispatch_aliases')
4
5
  local member = { ['.'] = true, [':'] = true, ['::'] = true, ['->'] = true }
5
6
 
6
- local function value(T, a, b)
7
+ local function value(T, a, b, language)
7
8
  if a == b and T.k[a] == 'str' then
8
9
  local raw = T:text(a)
10
+ if language == 'lua' then
11
+ local literal = require('codeoutline.lua_literal').decode(raw)
12
+ return literal and { literal = literal } or false
13
+ end
9
14
  local q = raw:sub(1, 1)
10
15
  if (q == '"' or q == "'") and raw:sub(-1) == q then
11
16
  local body = raw:sub(2, -2)
@@ -25,6 +30,7 @@ end
25
30
 
26
31
  function M.collect(r, T)
27
32
  local sites, calls = {}, {}
33
+ local expand = r.collect_dispatch and aliases.prepare(r, T)
28
34
  for index, ref in ipairs(r.refs) do
29
35
  if index % 128 == 0 then control.check() end
30
36
  local x = ref.tok
@@ -33,53 +39,63 @@ function M.collect(r, T)
33
39
  if close then
34
40
  local first = x
35
41
  while first > 2 and member[T:text(first - 1)] and T.k[first - 2] == 'id' do first = first - 2 end
36
- local call = value(T, first, x)
42
+ local call = value(T, first, x, r.language)
37
43
  if call then calls[call.symbol] = true end
38
- if not r.collect_dispatch or (type(r.collect_dispatch) == 'table' and not r.collect_dispatch[call and call.symbol]) then goto next_ref end
39
- local args, a, i = {}, x + 2, x + 2
44
+ if not r.collect_dispatch then goto next_ref end
45
+ local api = call and expand(call, first)
46
+ if not api or not api.symbol or type(r.collect_dispatch) == 'table'
47
+ and not r.collect_dispatch[call.symbol] and not r.collect_dispatch[api.symbol] then goto next_ref end
48
+ local args, ranges, a, i = {}, r.ranges and {} or nil, x + 2, x + 2
40
49
  while i <= close do
41
50
  if i == close or T:text(i) == ',' then
42
- args[#args + 1] = value(T, a, i - 1)
51
+ args[#args + 1] = expand(value(T, a, i - 1, r.language), a)
52
+ if ranges then ranges[#args] = a < i and { T.s[a] - 1, T.e[i - 1] } or false end
43
53
  a = i + 1
44
- elseif T:match(i) and T:match(i) > i then i = T:match(i) end
54
+ elseif T:match(i) and T:match(i) > i then i = T:match(i)
55
+ elseif r.language == 'lua' and T.k[i] == 'kw' and T:text(i) == 'function' then
56
+ local depth = 1
57
+ while depth > 0 and i < close do
58
+ i = i + 1
59
+ local kw = T.k[i] == 'kw' and T:text(i)
60
+ if kw == 'function' or kw == 'if' or kw == 'do' or kw == 'repeat' then depth = depth + 1
61
+ elseif kw == 'end' or kw == 'until' then depth = depth - 1 end
62
+ end
63
+ if depth > 0 then break end
64
+ end
45
65
  i = i + 1
46
66
  end
47
- if call then sites[#sites + 1] = { call = call.symbol, args = args, from = ref.from, line = ref.line } end
67
+ if call then sites[#sites + 1] = { call = call.symbol, api = api.symbol,
68
+ args = args, ranges = ranges, from = ref.from, line = ref.line } end
48
69
  end
49
70
  end
50
71
  ::next_ref::
51
72
  end
73
+ if not r.collect_dispatch and next(calls) then aliases.hints(r, T, calls) end
52
74
  local names = {}
53
75
  for name in pairs(calls) do names[#names + 1] = name end
54
76
  table.sort(names)
55
77
  if #names > 0 then r.dispatch_calls = names end
56
- if #sites > 0 then r.dispatch_sites = sites end
78
+ if r.collect_dispatch then r.dispatch_sites = sites end
57
79
  end
58
80
 
59
81
  local function rules_for(G, rules)
60
- if rules == nil and G.root then
61
- local f = io.open(G.root .. '/.codeoutline.json', 'rb')
62
- if f then
63
- local raw = f:read(65537); f:close()
64
- assert(#raw <= 65536, '.codeoutline.json exceeds 64 KiB')
65
- local ok, config = pcall(xutils.json_unpack, raw)
66
- assert(ok and type(config) == 'table', 'invalid .codeoutline.json')
67
- rules = config.dispatchRules
82
+ local config = require('codeoutline.rules')
83
+ if rules ~= nil then return config.dispatch(rules) end
84
+ return config.load(G.root).dispatch
85
+ end
86
+
87
+ function M.candidate(rec, call)
88
+ if rec.dispatch_sites then return true end -- explicit in-memory parse option
89
+ for _, name in ipairs(rec.dispatch_calls or {}) do
90
+ if name == call then return true end
91
+ if name:sub(1, 1) == '@' then
92
+ local root = name:sub(2)
93
+ if root == call or call:sub(1, #root + 1) == root .. '.'
94
+ or call:sub(1, #root + 1) == root .. ':'
95
+ or call:sub(1, #root + 2) == root .. '->' then return true end
68
96
  end
69
97
  end
70
- rules = rules or {}
71
- assert(type(rules) == 'table' and #rules <= 32, 'dispatchRules must contain at most 32 rules')
72
- for _, r in ipairs(rules) do
73
- assert(type(r) == 'table' and type(r.call) == 'string' and r.call:match('^[%a_][%w_%.:>%-]*$'), 'invalid dispatchRules.call')
74
- assert(r.kind == 'target' or r.kind == 'callback' or r.kind == 'event', 'invalid dispatchRules.kind')
75
- assert(r.language == nil or type(r.language) == 'string', 'invalid dispatchRules.language')
76
- for _, key in ipairs(r.kind == 'event' and { 'argument', 'keyArgument', 'handlerArgument' } or { 'argument' }) do
77
- local n = r[key]
78
- assert(type(n) == 'number' and n >= 1 and n <= 32 and n % 1 == 0, 'invalid dispatchRules.' .. key)
79
- end
80
- if r.kind == 'event' then assert(type(r.register) == 'string' and r.register:match('^[%a_][%w_%.:>%-]*$'), 'invalid dispatchRules.register') end
81
- end
82
- return rules
98
+ return false
83
99
  end
84
100
 
85
101
  function M.query(G, rules, report)
@@ -89,12 +105,8 @@ function M.query(G, rules, report)
89
105
  local names, cache, order, bytes, registrations_by_rule = {}, {}, {}, 0, {}
90
106
  local dependencies = {}
91
107
  for _, rule in ipairs(rules) do names[rule.call] = true; if rule.register then names[rule.register] = true end end
92
- local function candidate(rec, call)
93
- if rec.dispatch_sites then return true end -- explicit in-memory parse option
94
- for _, name in ipairs(rec.dispatch_calls or {}) do if name == call then return true end end
95
- return false
96
- end
97
- local function sites(f)
108
+ local candidate = M.candidate
109
+ local function sites(f, literals)
98
110
  local rec = G.files[f]
99
111
  if rec.dispatch_sites then return rec.dispatch_sites end
100
112
  if cache[f] then return cache[f].sites end
@@ -107,6 +119,24 @@ function M.query(G, rules, report)
107
119
  if rec.encoding ~= 'utf-8' and rec.encoding ~= 'utf-8-bom' then
108
120
  source = assert(xutils.to_utf8(raw, rec.encoding == 'gbk' and 'gbk' or 'auto'))
109
121
  end
122
+ -- Target rules can only obtain a literal command from this file. Reject
123
+ -- broad module hints before scope/registry analysis, but decode escaped
124
+ -- Lua literals so aliases and alternate spellings remain candidates.
125
+ if literals and rec.language == 'lua' then
126
+ local relevant = false
127
+ for name in pairs(literals) do if source:find(name, 1, true) then relevant = true; break end end
128
+ if not relevant and source:find('\\', 1, true) then
129
+ local T = require('codeoutline.lang.lua').lang:tokenize(source)
130
+ local decode = require('codeoutline.lua_literal').decode
131
+ for i = 1, T.n do
132
+ if i % 4096 == 0 then control.check() end
133
+ if T.k[i] == 'str' and literals[decode(T:text(i))] then relevant = true; break end
134
+ end
135
+ end
136
+ -- Do not cache negatives: later incoming calls can request other
137
+ -- targets within this same query, and outgoing needs all sites.
138
+ if not relevant then return {} end
139
+ end
110
140
  local parsed = assert(require('codeoutline.parse').parse(rec.path, source,
111
141
  { ranges = false, dispatch = names, dispatch_only = true }))
112
142
  assert(#parsed.nodes == #rec.nodes, 'source encoding changed; refresh index before indirect query: ' .. rec.path)
@@ -126,6 +156,37 @@ function M.query(G, rules, report)
126
156
  return result
127
157
  end
128
158
  local function targets(f, site, arg, literal)
159
+ if arg and not literal and arg.node then return { G.id_of[f][arg.node] } end
160
+ if arg and not literal and arg.import then
161
+ local imp = arg.import
162
+ local rec = G.files[f]
163
+ if rec.language == 'javascript' or rec.language == 'typescript' then
164
+ local js = require('codeoutline.js_resolution')
165
+ dependencies.js = dependencies.js or js.context(G)
166
+ return js.imported(G, f, imp.path, arg.member, dependencies.js) or {}
167
+ elseif rec.language == 'lua' then
168
+ local bindings = require('codeoutline.lua_bindings')
169
+ dependencies.lua = dependencies.lua or bindings.context(G)
170
+ local result = {}
171
+ for _, file in ipairs(bindings.modules(G, { path = imp.path, kind = imp.loader }, dependencies.lua)) do
172
+ local record = G.files[file]
173
+ local exports = require('codeoutline.foreign').exports(record, 'lua', imp.path) or record.lua_exports
174
+ local id = exports and exports[arg.member]
175
+ if id then result[#result + 1] = G.id_of[file][id] end
176
+ end
177
+ return result
178
+ elseif rec.language == 'python' then
179
+ local base = imp.path:gsub('%.', '/')
180
+ local file = G.file_index[base .. '.py'] or G.file_index[base .. '/__init__.py']
181
+ local result = {}
182
+ for _, id in ipairs(file and G.id_of[file] or {}) do
183
+ local n = G.nodes[id]
184
+ if not n.parent and n.name == arg.member then result[#result + 1] = id end
185
+ end
186
+ return result
187
+ end
188
+ return {}
189
+ end
129
190
  local name
130
191
  if arg then
131
192
  if literal then name = arg.literal else name = arg.symbol end
@@ -158,7 +219,7 @@ function M.query(G, rules, report)
158
219
  if (not rule.language or rule.language == rec.language) and candidate(rec, rule.register) then
159
220
  for _, site in ipairs(sites(f)) do
160
221
  local key = site.args[rule.keyArgument]
161
- if site.call == rule.register and key and key.literal then
222
+ if (site.call == rule.register or site.api == rule.register) and key and key.literal then
162
223
  local bucket = rec.language .. '\0' .. key.literal
163
224
  registrations[bucket] = registrations[bucket] or {}
164
225
  for _, id in ipairs(targets(f, site, site.args[rule.handlerArgument])) do
@@ -175,6 +236,18 @@ function M.query(G, rules, report)
175
236
  end
176
237
  local function scan(only, wanted)
177
238
  local out = {}
239
+ local literals
240
+ if wanted then
241
+ local targets_only = true
242
+ for _, rule in ipairs(rules) do if rule.kind ~= 'target' then targets_only = false; break end end
243
+ if targets_only then
244
+ literals = {}
245
+ for id in pairs(wanted) do
246
+ local node = G.nodes[id]
247
+ literals[node.name], literals[node.qualified or node.name] = true, true
248
+ end
249
+ end
250
+ end
178
251
  local files = only and { G.node_file[only] } or G.file_order
179
252
  for _, f in ipairs(files) do
180
253
  control.check()
@@ -183,9 +256,9 @@ function M.query(G, rules, report)
183
256
  if (not rule.language or rule.language == rec.language) and candidate(rec, rule.call) then
184
257
  -- Process every applicable rule while this file is available,
185
258
  -- even if event registration scans evict it from the FIFO.
186
- file_sites = file_sites or sites(f)
259
+ file_sites = file_sites or sites(f, literals)
187
260
  for _, site in ipairs(file_sites) do
188
- if site.call == rule.call and site.from > 0 and (not only or G.id_of[f][site.from] == only) then
261
+ if (site.call == rule.call or site.api == rule.call) and site.from > 0 and (not only or G.id_of[f][site.from] == only) then
189
262
  local ids, arg, registered = {}, site.args[rule.argument], nil
190
263
  if rule.kind == 'event' then
191
264
  registered = registrations(ri, rule)
@@ -219,7 +292,8 @@ function M.query(G, rules, report)
219
292
  end
220
293
  return out
221
294
  end
222
- return { outgoing = function(id) return scan(id)[id] or {} end, incoming = function(wanted) return scan(nil, wanted) end }
295
+ return { outgoing = function(id) return scan(id)[id] or {} end, incoming = function(wanted) return scan(nil, wanted) end,
296
+ resolve_targets = targets }
223
297
  end
224
298
 
225
299
  return M
@@ -82,7 +82,7 @@ function M.collect(r, T)
82
82
  scopes[i] = stack[#stack] or 0
83
83
  if stack[#stack] and T.m[stack[#stack]] == i then stack[#stack] = nil end
84
84
  end
85
- local bindings, parameters = {}, {}
85
+ local bindings, parameters, alias_tokens = {}, {}, {}
86
86
  local callable_at = {}
87
87
  for _, n in ipairs(r.nodes) do
88
88
  if n.kind == 'function' or n.kind == 'method' or n.kind == 'constructor' then
@@ -141,6 +141,10 @@ function M.collect(r, T)
141
141
  if after and T:text(after) ~= '=' and T:text(after) ~= ',' and T:text(after) ~= ')' and T:text(after) ~= ';' then typ = nil end
142
142
  elseif next == '=' and T:text(i + 2) == 'new' then typ = type_name(T, i + 3) end
143
143
  local a, b = scope_for(i, parameter)
144
+ local eq = next == '=' and i + 1 or after
145
+ if r.ordinary_bindings and declaration and eq and T:text(eq) == '=' and T.k[eq + 1] == 'id' then
146
+ alias_tokens[i] = require('codeoutline.dispatch_aliases').simple(T, eq + 1, r.language, true) and 'simple' or true
147
+ end
144
148
  bindings[#bindings + 1] = { name = T:text(i), type = typ or '?', token = i, a = a, b = b,
145
149
  declaration = declaration or parameter, target = declaration and callable_at[i] or nil }
146
150
  end
@@ -151,6 +155,17 @@ function M.collect(r, T)
151
155
  by_name[binding.name] = by_name[binding.name] or {}
152
156
  by_name[binding.name][#by_name[binding.name] + 1] = binding
153
157
  end
158
+ -- Reuse dispatch's bounded lexical validation in explicit query parses.
159
+ -- Ordinary indexing leaves this extra evidence ungenerated.
160
+ local expand
161
+ local function alias_evidence(name, at, token)
162
+ if alias_tokens[token] ~= 'simple' then return '?' end
163
+ expand = expand or require('codeoutline.dispatch_aliases').prepare(r, T)
164
+ local v = expand({ symbol = name }, at)
165
+ if v and v.node then return 'node:' .. v.node end
166
+ if v and v.import then return 'import:' .. v.import.path .. '\0' .. v.member end
167
+ return '?'
168
+ end
154
169
  for index, ref in ipairs(r.refs) do
155
170
  if index % 128 == 0 then control.check() end
156
171
  local target = ref.recv or ref.name
@@ -163,13 +178,26 @@ function M.collect(r, T)
163
178
  end
164
179
  end
165
180
  if best then
166
- if not ref.recv and best.target and (best.token <= ref.tok or best.hoisted) then ref.js_receiver = 'node:' .. best.target
181
+ if not ref.recv and alias_tokens[best.token] then ref.js_receiver = alias_evidence(ref.name, ref.tok, best.token)
182
+ elseif not ref.recv and best.target and (best.token <= ref.tok or best.hoisted) then ref.js_receiver = 'node:' .. best.target
167
183
  else ref.js_receiver = ref.recv and best.token <= ref.tok and best.type or '?' end
168
184
  end
169
185
  if ref.kind == 'member_call' and (not ref.recv or T:text(ref.tok - 3) == '.' or T:text(ref.tok - 3) == '?.') then
170
186
  ref.js_receiver = '?' -- A field/call-result chain is not the local variable sharing its tail.
171
187
  end
172
188
  end
189
+ if next(alias_tokens) then
190
+ for _, e in ipairs(r.js_exports) do
191
+ if not e.path then
192
+ for _, b in ipairs(by_name[e.local_name] or {}) do
193
+ if b.a == 0 and alias_tokens[b.token] then
194
+ e.binding = alias_evidence(e.local_name, T.n, b.token)
195
+ break
196
+ end
197
+ end
198
+ end
199
+ end
200
+ end
173
201
  end
174
202
 
175
203
  return M
@@ -117,6 +117,21 @@ local function exported(G, f, name, ctx, seen, depth)
117
117
  local function add(ids) for _, id in ipairs(ids) do if not unique[id] then unique[id] = true; result[#result + 1] = id end end end
118
118
  local rec = G.files[f]
119
119
  local entries = rec.js_exports or {}
120
+ -- Only variable exports may need the new lexical alias analysis. Ordinary
121
+ -- declared exports continue to resolve entirely from the resident index.
122
+ if ctx.ordinary and not rec.ordinary_bindings then
123
+ local needed = false
124
+ for _, e in ipairs(entries) do
125
+ if e.name == name and not e.path then
126
+ for _, id in ipairs(G.by_name[e.local_name] or {}) do
127
+ local n = G.nodes[id]
128
+ if G.node_file[id] == f and not n.parent and n.kind == 'variable' then needed = true; break end
129
+ end
130
+ end
131
+ if needed then break end
132
+ end
133
+ if needed then entries = ctx.ordinary.data(f).js_exports or entries end
134
+ end
120
135
  local explicit = false
121
136
  for _, e in ipairs(entries) do if e.name == name then explicit = true end end
122
137
  for _, e in ipairs(entries) do
@@ -125,6 +140,16 @@ local function exported(G, f, name, ctx, seen, depth)
125
140
  if e.path then
126
141
  local target = M.module(G, f, e.path, ctx)
127
142
  if target then add(exported(G, target, want, ctx, seen, depth + 1)) end
143
+ elseif e.binding then
144
+ local node = e.binding:match('^node:(%d+)$')
145
+ local path, member = e.binding:match('^import:(.-)%z(.*)$')
146
+ if node then
147
+ local id = G.id_of[f][tonumber(node)]
148
+ if id then add({ id }) end
149
+ elseif path then
150
+ local target = M.module(G, f, path, ctx)
151
+ if target then add(exported(G, target, member, ctx, seen, depth + 1)) end
152
+ end
128
153
  else
129
154
  for _, id in ipairs(G.id_of[f]) do
130
155
  local n = G.nodes[id]
@@ -143,6 +168,13 @@ local function exported(G, f, name, ctx, seen, depth)
143
168
  return result
144
169
  end
145
170
 
171
+ -- Shared named/namespace import lookup for query-owned callback aliases.
172
+ function M.imported(G, f, path, name, ctx)
173
+ ctx = ctx or M.context(G)
174
+ local target = M.module(G, f, path, ctx)
175
+ return target and name ~= '' and exported(G, target, name, ctx, {}, 0) or {}
176
+ end
177
+
146
178
  function M.resolve(G, f, name, kind, recv, evidence, ctx)
147
179
  local rec = G.files[f]
148
180
  if not js(rec) then return nil end
@@ -161,6 +193,8 @@ function M.resolve(G, f, name, kind, recv, evidence, ctx)
161
193
  if evidence then
162
194
  local node = evidence:match('^node:(%d+)$')
163
195
  if node then return { G.id_of[f][tonumber(node)] } end
196
+ local path, member = evidence:match('^import:(.-)%z(.*)$')
197
+ if path then return M.imported(G, f, path, member, ctx) end
164
198
  local head, tail = evidence:match('^([%w_$]+)%.([%w_$]+)$')
165
199
  local classes = binding(head or evidence, tail)
166
200
  if classes == nil then
@@ -190,10 +190,22 @@ parse_members = function(T, r, a, b, parent, class_name)
190
190
  local j = i
191
191
  while j <= b and not is(T, j, ';') do j = j + 1 end
192
192
  local x = kw(T, i, 'using') and i + 1 or i + 2
193
- if kw(T, x, 'static') then x = x + 1 end
194
- if T.k[x] == 'id' and is(T, x + 1, '=') then x = x + 2 end -- alias
193
+ local global = t == 'global'
194
+ local static = kw(T, x, 'static')
195
+ if static or T:text(x) == 'unsafe' then x = x + 1 end
196
+ local alias = T.k[x] == 'id' and is(T, x + 1, '=') and T:text(x) or nil
197
+ if alias then x = x + 2 end
198
+ local target = x
199
+ if T:text(x) == 'global' and is(T, x + 1, '::') then x = x + 2 end
195
200
  local path = dotted(T, x)
196
- if path ~= '' then r.imports[#r.imports + 1] = { path = path, line = T:line(i) } end
201
+ if path ~= '' or global then r.imports[#r.imports + 1] = { path = path, line = T:line(i), global = global or nil } end
202
+ if r.ordinary_bindings and (not parent or r.nodes[parent].kind == 'namespace') then
203
+ local owner = parent and r.nodes[parent]
204
+ r.csharp_usings = r.csharp_usings or {}
205
+ r.csharp_usings[#r.csharp_usings + 1] = { alias = alias, static = static or nil, global = global or nil,
206
+ path = common.sig(T, target, j - 1, 4096), scope = owner and owner.qualified or '',
207
+ first = owner and T.s[owner.ti] - 1 or 0, last = owner and T.e[owner.tj] or #T.src }
208
+ end
197
209
  i = j + 1
198
210
  elseif k == 'kw' and t == 'namespace' then
199
211
  local name, x = dotted(T, i + 1)
@@ -27,6 +27,19 @@ local function literal(T, i)
27
27
  return s:sub(2, -2)
28
28
  end
29
29
 
30
+ -- Shared lexical table-member lookup for normal references and query-only
31
+ -- configured callback aliases. Results are local parser node indices.
32
+ function M.local_members(nodes, scopes, name, token, scope, finish)
33
+ local members = {}
34
+ for index, n in ipairs(nodes) do
35
+ if n.ti >= token and n.ti <= finish and scopes[n.ti] == scope
36
+ and (n.qualified == name .. '.' .. n.name or n.qualified == name .. ':' .. n.name) then
37
+ members[n.name] = index
38
+ end
39
+ end
40
+ return members
41
+ end
42
+
30
43
  function M.collect(r, T, bm)
31
44
  r.lua_imports, r.lua_exports, r.lua_locals = {}, {}, {}
32
45
  local scopes, stack, params, bindings = {}, {}, {}, {}
@@ -83,15 +96,12 @@ function M.collect(r, T, bm)
83
96
  end
84
97
  end
85
98
  end
99
+ -- Query-only lexical bindings travel with the cached tokens, not the index.
100
+ T.lua_bindings = bindings
86
101
  for name, list in pairs(bindings) do
87
102
  for _, b in ipairs(list) do
88
103
  if b.hint:match('^lua%-local:') then
89
- local members = {}
90
- for index, n in ipairs(r.nodes) do
91
- local scope = scopes[n.ti]
92
- if n.ti >= b.token and n.ti <= b.b and scope == b.a
93
- and (n.qualified == name .. '.' .. n.name or n.qualified == name .. ':' .. n.name) then members[n.name] = index end
94
- end
104
+ local members = M.local_members(r.nodes, scopes, name, b.token, b.a, b.b)
95
105
  r.lua_locals[#r.lua_locals + 1] = members
96
106
  b.hint = 'lua-local:' .. #r.lua_locals
97
107
  end
@@ -302,9 +312,10 @@ function M.modules(G, imp, ctx)
302
312
  local path = normalize(ctx.directory .. '/' .. imp.path)
303
313
  local f = G.file_index[path]
304
314
  if f and G.files[f].language == 'lua' then result[1] = f end
305
- if not result[1] and not loaders[imp.kind] then
306
- -- `*import` pattern loader: bind when any indexed Lua file's path
307
- -- ends with the quoted path, regardless of directory prefix.
315
+ if not result[1] then
316
+ -- No file at that exact path: bind any indexed Lua file whose path
317
+ -- ends with the quoted one, however deep the workspace root sits
318
+ -- above the Lua tree (`dofile('scripts/x.lua')` opened from a parent).
308
319
  local want = normalize(imp.path)
309
320
  local base = want:match('([^/]+)$')
310
321
  if base then
@@ -329,6 +340,65 @@ function M.native_modules(G, name, ctx)
329
340
  return require('codeoutline.foreign').modules(G, 'lua', name, ctx)
330
341
  end
331
342
 
343
+ -- Shared query-time receiver resolution for completion and navigation. Reuse
344
+ -- the parser's lexical bindings and loader contracts; never execute Lua.
345
+ function M.member(G, target, name)
346
+ local result = {}
347
+ for _, f in ipairs(target and target.modules or {}) do
348
+ local rec = G.files[f]
349
+ local exports = require('codeoutline.foreign').exports(rec, 'lua', target.module_name) or rec.lua_exports
350
+ local n = exports and exports[name]
351
+ if n then result[#result + 1] = { node = rec.nodes[n], file = f, index = n } end
352
+ end
353
+ return result
354
+ end
355
+
356
+ function M.return_target(G, node, ctx)
357
+ local name = require('codeoutline.foreign').returns(node, 'lua')
358
+ local files = name and M.native_modules(G, name, ctx)
359
+ if files and #files > 0 then return { modules = files, module_name = name } end
360
+ end
361
+
362
+ function M.receiver(G, rec, T, name, offset, ctx, depth)
363
+ depth = depth or 0
364
+ if depth >= 6 then return nil, true end
365
+ local best
366
+ for _, b in ipairs((T.lua_bindings or {})[name] or {}) do
367
+ if T.s[b.token] - 1 < offset and (b.a == 0 or T.s[b.a] - 1 <= offset)
368
+ and (b.a == 0 or not T.e[b.b] or offset < T.e[b.b])
369
+ and (not best or b.a > best.a or b.a == best.a and b.token > best.token) then best = b end
370
+ end
371
+ if not best then
372
+ local files = M.native_modules(G, name, ctx)
373
+ return #files > 0 and { modules = files, module_name = name } or nil, (T.lua_bindings or {})[name] ~= nil
374
+ end
375
+ local imported = tonumber(best.hint:match('^lua:(%d+)$'))
376
+ if imported then
377
+ local imp = rec.lua_imports[imported]
378
+ return { modules = M.modules(G, imp, ctx), module_name = imp.path }, true
379
+ end
380
+ local i = best.token + 2
381
+ if best.imported or T:text(best.token + 1) ~= '=' or T.k[i] ~= 'id' then return nil, true end
382
+ local target = M.receiver(G, rec, T, T:text(i), T.s[best.token] - 1, ctx, depth + 1)
383
+ i = i + 1
384
+ while target and (T:text(i) == '.' or T:text(i) == ':') and T.k[i + 1] == 'id' do
385
+ local close = T:text(i + 2) == '(' and T.m[i + 2]
386
+ if not close then return nil, true end
387
+ local next_target
388
+ for _, entry in ipairs(M.member(G, target, T:text(i + 1))) do
389
+ local value = M.return_target(G, entry.node, ctx)
390
+ if not value or next_target and next_target.module_name ~= value.module_name then return nil, true end
391
+ next_target = value
392
+ end
393
+ target, i = next_target, close + 1
394
+ end
395
+ -- Reject expressions whose value is not the simple receiver/call chain.
396
+ local tail = T:text(i)
397
+ if T.k[i] == 'op' and tail ~= ';' and tail ~= ')' and tail ~= ','
398
+ or tail == 'and' or tail == 'or' then return nil, true end
399
+ return target, true
400
+ end
401
+
332
402
  function M.resolve(G, f, name, hint, ctx, recv)
333
403
  if G.files[f].language ~= 'lua' then return nil end
334
404
  if not hint or hint == '' then
@@ -0,0 +1,38 @@
1
+ -- Decode Lua string syntax without evaluating source code.
2
+ local M = {}
3
+ function M.decode(raw)
4
+ local eq, body = raw:match('^%[(=*)%[(.*)%]%1%]$')
5
+ if eq then return body:gsub('^\r?\n', '', 1) end
6
+ local quote = raw:sub(1, 1)
7
+ if (quote ~= '"' and quote ~= "'") or raw:sub(-1) ~= quote then return nil end
8
+ if not raw:find('\\', 1, true) then return raw:sub(2, -2) end
9
+ local escaped = { a = '\a', b = '\b', f = '\f', n = '\n', r = '\r', t = '\t', v = '\v',
10
+ ['\\'] = '\\', ['"'] = '"', ["'"] = "'", ['\n'] = '\n' }
11
+ local out, i, stop = {}, 2, #raw - 1
12
+ while i <= stop do
13
+ local c = raw:sub(i, i)
14
+ if c ~= '\\' then out[#out + 1] = c; i = i + 1
15
+ else
16
+ c = raw:sub(i + 1, i + 1); i = i + 2
17
+ if escaped[c] then out[#out + 1] = escaped[c]
18
+ elseif c == 'z' then
19
+ while i <= stop and raw:sub(i, i):match('%s') do i = i + 1 end
20
+ elseif c == '\r' then
21
+ if raw:sub(i, i) == '\n' then i = i + 1 end
22
+ out[#out + 1] = '\n'
23
+ elseif c == 'x' then
24
+ local hex = raw:sub(i, i + 1)
25
+ if not hex:match('^%x%x$') then return nil end
26
+ out[#out + 1] = string.char(tonumber(hex, 16)); i = i + 2
27
+ elseif c:match('%d') then
28
+ local digits = (c .. raw:sub(i, stop)):match('^%d%d?%d?')
29
+ local n = tonumber(digits)
30
+ if n > 255 then return nil end
31
+ out[#out + 1] = string.char(n); i = i + #digits - 1
32
+ else return nil end
33
+ end
34
+ end
35
+ return table.concat(out)
36
+ end
37
+
38
+ return M