@codua/codeoutline-win32-x64 0.2.0 → 0.2.1

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.
@@ -0,0 +1,384 @@
1
+ -- Kotlin declarations and call sites over xscan; deliberately not a type checker.
2
+ local xscan = require('codeoutline.xscan')
3
+ local common = require('codeoutline.common')
4
+ local M = {}
5
+
6
+ M.config = {
7
+ keywords = { 'as', 'break', 'class', 'continue', 'do', 'else', 'false', 'for', 'fun',
8
+ 'if', 'in', 'interface', 'is', 'null', 'object', 'package', 'return', 'super',
9
+ 'this', 'throw', 'true', 'try', 'typealias', 'typeof', 'val', 'var', 'when', 'while', 'import' },
10
+ ops = { '===', '!==', '..<', '?.', '?:', '!!', '::', '->', '==', '!=', '<=', '>=',
11
+ '&&', '||', '++', '--', '+=', '-=', '*=', '/=', '%=', '..' },
12
+ line_comment = { '//' }, block_comment = { { '/*', '*/' } }, nested_comments = true,
13
+ strings = { { '"""', '"""', multiline = true }, { '"', '"', escape = '\\' },
14
+ { "'", "'", escape = '\\' }, { '`', '`' } },
15
+ }
16
+ M.lang = xscan.lang(M.config)
17
+
18
+ local MOD = {}
19
+ for s in ([[public private protected internal final open abstract sealed data enum annotation value
20
+ inner companion override lateinit const tailrec operator infix inline noinline crossinline
21
+ external suspend expect actual reified vararg out]]):gmatch('%S+') do MOD[s] = true end
22
+ local DECL = { class = true, interface = true, object = true, ['fun'] = true,
23
+ val = true, var = true, typealias = true }
24
+ local CONTINUE = { ['='] = true, ['.'] = true, ['?.'] = true, ['?:'] = true, [','] = true,
25
+ [':'] = true, ['->'] = true, ['+'] = true, ['-'] = true, ['*'] = true, ['/'] = true,
26
+ ['&&'] = true, ['||'] = true, ['in'] = true, ['is'] = true, ['as'] = true, ['by'] = true }
27
+ local function is(T, i, s) return T.k[i] == 'op' and T:text(i) == s end
28
+ local function word(T, i, s) return (T.k[i] == 'id' or T.k[i] == 'kw') and T:text(i) == s end
29
+
30
+ -- xscan handles ordinary/raw strings, but Kotlin templates can nest quoted
31
+ -- expressions: "${render("}")}". Hide template interiors before tokenizing,
32
+ -- preserving every byte offset/newline; their expressions are not indexed.
33
+ local function template_source(src)
34
+ if not src:find('${', 1, true) then return src end
35
+ local quoted, expression
36
+ local function comment(i)
37
+ if src:sub(i, i + 1) == '//' then return src:find('\n', i + 2, true) or #src + 1 end
38
+ if src:sub(i, i + 1) ~= '/*' then return nil end
39
+ local depth = 1
40
+ i = i + 2
41
+ while i <= #src and depth > 0 do
42
+ local pair = src:sub(i, i + 1)
43
+ if pair == '/*' then depth = depth + 1; i = i + 2
44
+ elseif pair == '*/' then depth = depth - 1; i = i + 2
45
+ else i = i + 1 end
46
+ end
47
+ return i
48
+ end
49
+ quoted = function(i, depth)
50
+ if depth > 64 then return #src + 1 end
51
+ local delimiter = src:sub(i, i)
52
+ if delimiter == '"' and src:sub(i, i + 2) == '"""' then delimiter = '"""' end
53
+ local k = i + #delimiter
54
+ while k <= #src do
55
+ if src:sub(k, k + #delimiter - 1) == delimiter then return k + #delimiter end
56
+ local c = src:sub(k, k)
57
+ if c == '\\' and delimiter ~= '"""' and delimiter ~= '`' then k = k + 2
58
+ elseif delimiter:sub(1, 1) == '"' and src:sub(k, k + 1) == '${' then k = expression(k + 2, depth + 1)
59
+ else k = k + 1 end
60
+ end
61
+ return #src + 1
62
+ end
63
+ expression = function(i, depth)
64
+ local braces = 1
65
+ while i <= #src and braces > 0 do
66
+ local c, after = src:sub(i, i), comment(i)
67
+ if after then i = after
68
+ elseif c == '"' or c == "'" or c == '`' then i = quoted(i, depth + 1)
69
+ else
70
+ if c == '{' then braces = braces + 1 elseif c == '}' then braces = braces - 1 end
71
+ i = i + 1
72
+ end
73
+ end
74
+ return i
75
+ end
76
+ local parts, start, i = {}, 1, 1
77
+ while i <= #src do
78
+ local c, after = src:sub(i, i), comment(i)
79
+ if after then i = after
80
+ elseif c == '"' or c == "'" or c == '`' then
81
+ local last = quoted(i, 0)
82
+ if c == '"' then
83
+ local width = src:sub(i, i + 2) == '"""' and 3 or 1
84
+ parts[#parts + 1] = src:sub(start, i + width - 1)
85
+ parts[#parts + 1] = src:sub(i + width, last - width - 1):gsub('[^\r\n]', ' ')
86
+ start = last - width
87
+ end
88
+ i = last
89
+ else i = i + 1 end
90
+ end
91
+ parts[#parts + 1] = src:sub(start)
92
+ return table.concat(parts)
93
+ end
94
+
95
+ local function angles(T, i, b)
96
+ local d = 0
97
+ for j = i, b do
98
+ if is(T, j, '<') then d = d + 1
99
+ elseif is(T, j, '>') then d = d - 1; if d == 0 then return j end
100
+ elseif is(T, j, ';') or is(T, j, '{') or is(T, j, '=') then return nil end
101
+ end
102
+ end
103
+
104
+ local function annotation(T, i, b, skip)
105
+ local j = i + 1
106
+ if is(T, j + 1, ':') then j = j + 2 end -- @file:JvmName, @get:Inject
107
+ if is(T, j, '[') and T.m[j] then return T.m[j] + 1 end
108
+ while j <= b and (T.k[j] == 'id' or T.k[j] == 'kw') do
109
+ skip[j] = true
110
+ j = j + 1
111
+ if is(T, j, '.') then j = j + 1 else break end
112
+ end
113
+ if is(T, j, '(') and T.m[j] then j = T.m[j] + 1 end
114
+ return j
115
+ end
116
+
117
+ -- End of a semicolon-free expression. Groups are skipped whole; operators,
118
+ -- chained receivers and control-expression bodies continue across newlines.
119
+ local function expression_end(T, a, b)
120
+ local j = a
121
+ while j <= b do
122
+ if is(T, j, ';') then return j - 1 end
123
+ if j > a then
124
+ local s, prev = T:text(j), T:text(j - 1)
125
+ if T.k[j] == 'kw' and DECL[s] and s ~= 'object' then return j - 1 end
126
+ if T:line(j) > T:eline(j - 1) and not CONTINUE[prev]
127
+ and not is(T, j, '.') and not is(T, j, '?.') and not is(T, j, '{')
128
+ and not word(T, j, 'else') and not word(T, j, 'catch') and not word(T, j, 'finally')
129
+ and not (is(T, j - 1, ')') and T.m[j - 1]
130
+ and (word(T, T.m[j - 1] - 1, 'if') or word(T, T.m[j - 1] - 1, 'when'))) then
131
+ return j - 1
132
+ end
133
+ end
134
+ if T.m[j] and T.m[j] > j then j = T.m[j] end
135
+ j = j + 1
136
+ end
137
+ return b
138
+ end
139
+
140
+ local function type_end(T, a, b)
141
+ local j = a
142
+ while j <= b do
143
+ if is(T, j, '=') or is(T, j, '{') or is(T, j, ';') or word(T, j, 'by') then break end
144
+ if j > a and (word(T, j, 'get') or word(T, j, 'set') or word(T, j, 'where')) then break end
145
+ if j > a and T:line(j) > T:eline(j - 1) and not CONTINUE[T:text(j - 1)] then break end
146
+ if is(T, j, '<') then j = angles(T, j, b) or j
147
+ elseif T.m[j] and T.m[j] > j then j = T.m[j] end
148
+ j = j + 1
149
+ end
150
+ return j - 1
151
+ end
152
+
153
+ local scope
154
+ local function add(r, T, start, last, ni, parent, kind, sig_end, extra)
155
+ local n = extra or {}
156
+ n.kind, n.name, n.ni, n.parent = kind, n.name or T:text(ni), ni, parent
157
+ n.ti, n.tj, n.sig = start, math.max(start, last), common.sig(T, start, sig_end or last)
158
+ return common.add_node(r, T, n)
159
+ end
160
+
161
+ local function properties(T, r, a, b, parent, skip)
162
+ local j = a
163
+ while j <= b do
164
+ if (word(T, j, 'val') or word(T, j, 'var')) and T.k[j + 1] == 'id' then
165
+ local ni, last = j + 1, j + 1
166
+ if is(T, ni + 1, ':') then
167
+ last = ni + 2
168
+ while last <= b and not is(T, last, ',') and not is(T, last, '=') do
169
+ if is(T, last, '<') then last = angles(T, last, b) or last
170
+ elseif T.m[last] and T.m[last] > last then last = T.m[last] end
171
+ last = last + 1
172
+ end
173
+ last = last - 1
174
+ end
175
+ add(r, T, j, last, ni, parent, 'property', last,
176
+ { type = last > ni + 1 and common.sig(T, ni + 2, last) or nil })
177
+ skip[ni] = true
178
+ j = last
179
+ elseif T.m[j] and T.m[j] > j then j = T.m[j] end
180
+ j = j + 1
181
+ end
182
+ end
183
+
184
+ scope = function(T, r, a, b, parent, skip)
185
+ local i = a
186
+ while i <= b do
187
+ local start, j, mods = i, i, {}
188
+ while j <= b do
189
+ if is(T, j, '@') then j = annotation(T, j, b, skip)
190
+ elseif T.k[j] == 'id' and MOD[T:text(j)] then mods[T:text(j)] = true; j = j + 1
191
+ else break end
192
+ end
193
+ local s = T:text(j)
194
+ local declaration = T.k[j] == 'kw' and DECL[s]
195
+ if s == 'package' or s == 'import' then
196
+ local parts, k = {}, j + 1
197
+ while k <= b and (T.k[k] == 'id' or T.k[k] == 'kw') do
198
+ parts[#parts + 1] = T:text(k); skip[k] = true; k = k + 1
199
+ if is(T, k, '.') then
200
+ k = k + 1
201
+ if is(T, k, '*') then parts[#parts + 1] = '*'; k = k + 1; break end
202
+ else break end
203
+ end
204
+ local path = table.concat(parts, '.')
205
+ if s == 'package' then r.package, r.prefix = path, path
206
+ else
207
+ local alias
208
+ if word(T, k, 'as') and T.k[k + 1] == 'id' then alias = T:text(k + 1); k = k + 2 end
209
+ r.imports[#r.imports + 1] = { path = path, line = T:line(j), alias = alias }
210
+ end
211
+ i = k
212
+ elseif declaration and (s == 'class' or s == 'interface' or s == 'object') then
213
+ local ni = j + 1
214
+ local named = T.k[ni] == 'id'
215
+ if not named and not mods.companion then
216
+ -- Anonymous object expressions: retain their members under the enclosing scope.
217
+ i = j + 1
218
+ else
219
+ local k, ctor, bases, in_bases = named and ni + 1 or ni, nil, {}, false
220
+ if not named then ni = j end
221
+ local last = k - 1
222
+ while k <= b and not is(T, k, '{') and not is(T, k, ';') do
223
+ if k > last + 1 and T:line(k) > T:eline(k - 1) and not CONTINUE[T:text(k - 1)]
224
+ and not is(T, k, ':') and not word(T, k, 'where') then break end
225
+ if T.k[k] == 'kw' and DECL[T:text(k)] then break end
226
+ if is(T, k, '<') then k = angles(T, k, b) or k
227
+ elseif is(T, k, '(') and T.m[k] then
228
+ if not in_bases then ctor = k end
229
+ k = T.m[k]
230
+ elseif is(T, k, ':') then in_bases = true
231
+ elseif in_bases and T.k[k] == 'id' and (is(T, k - 1, ':') or is(T, k - 1, ',')) then
232
+ local parts = { T:text(k) }
233
+ while is(T, k + 1, '.') and T.k[k + 2] == 'id' do k = k + 2; parts[#parts + 1] = T:text(k) end
234
+ bases[#bases + 1] = table.concat(parts, '.')
235
+ end
236
+ k = k + 1
237
+ end
238
+ local body = is(T, k, '{') and k or nil
239
+ local close = body and (T.m[body] or b) or k - 1
240
+ local kind = mods.enum and 'enum' or mods.annotation and 'annotation_type' or s == 'interface' and 'interface' or 'class'
241
+ local id = add(r, T, start, close, ni, parent, kind, k - 1,
242
+ { name = named and T:text(ni) or 'Companion', bases = #bases > 0 and bases or nil,
243
+ singleton = s == 'object' or nil })
244
+ skip[ni] = true
245
+ if ctor then properties(T, r, ctor + 1, T.m[ctor] - 1, id, skip) end
246
+ if body then
247
+ local first = body + 1
248
+ if mods.enum then
249
+ while first < close and not is(T, first, ';') do
250
+ if T.k[first] ~= 'id' or MOD[T:text(first)] then break end
251
+ local name, e = first, first + 1
252
+ if is(T, e, '(') and T.m[e] then e = T.m[e] + 1 end
253
+ if is(T, e, '{') and T.m[e] then e = T.m[e] + 1 end
254
+ add(r, T, name, e - 1, name, id, 'enum_member'); skip[name] = true
255
+ first = e
256
+ if is(T, first, ',') then first = first + 1 else break end
257
+ end
258
+ end
259
+ scope(T, r, first, close - 1, id, skip)
260
+ end
261
+ i = close + 1
262
+ end
263
+ elseif declaration and s == 'fun' then
264
+ local k, ni, opening = j + 1
265
+ if is(T, k, '<') then k = (angles(T, k, b) or k) + 1 end
266
+ while k <= b do
267
+ if is(T, k, '(') and T.m[k] then
268
+ if T.k[k - 1] == 'id' then ni, opening = k - 1, k; break end
269
+ k = T.m[k]
270
+ elseif is(T, k, '<') then k = angles(T, k, b) or k
271
+ elseif is(T, k, '{') or is(T, k, '=') or is(T, k, ';') then break end
272
+ k = k + 1
273
+ end
274
+ if not opening then i = j + 1
275
+ else
276
+ local after, typ = T.m[opening] + 1, nil
277
+ if is(T, after, ':') then
278
+ local e = type_end(T, after + 1, b)
279
+ typ = e >= after + 1 and common.sig(T, after + 1, e) or nil
280
+ after = e + 1
281
+ end
282
+ -- Generic constraints belong to the header, not a body.
283
+ if word(T, after, 'where') then after = type_end(T, after, b) + 1 end
284
+ local body = is(T, after, '{') and after or nil
285
+ local expr = is(T, after, '=') and after + 1 or nil
286
+ local last = body and (T.m[body] or b) or expr and math.max(after, expression_end(T, expr, b)) or after - 1
287
+ local owner = parent and r.nodes[parent]
288
+ local kind = owner and (owner.kind == 'class' or owner.kind == 'interface' or owner.kind == 'enum') and 'method' or 'function'
289
+ local id = add(r, T, start, last, ni, parent, kind, after - 1,
290
+ { type = typ, abstract = not body and not expr or nil,
291
+ static = not parent and mods.private or nil,
292
+ receiver = is(T, ni - 1, '.') and common.sig(T, j + 1, ni - 2) or nil })
293
+ skip[ni] = true
294
+ if body then scope(T, r, body + 1, last - 1, id, skip)
295
+ elseif expr then scope(T, r, expr, last, id, skip) end
296
+ i = last + 1
297
+ end
298
+ elseif declaration and (s == 'val' or s == 'var' or s == 'typealias') and T.k[j + 1] == 'id' then
299
+ local ni, k, typ = j + 1, j + 2, nil
300
+ if s == 'typealias' and is(T, k, '<') then k = (angles(T, k, b) or k) + 1 end
301
+ local receiver
302
+ if s ~= 'typealias' then
303
+ if is(T, k, '<') then k = (angles(T, k, b) or k) + 1 end
304
+ while (is(T, k, '.') or is(T, k, '?.')) and T.k[k + 1] == 'id' do
305
+ receiver = common.sig(T, j + 1, k - 1)
306
+ ni, k = k + 1, k + 2
307
+ end
308
+ end
309
+ if is(T, k, ':') then
310
+ local e = type_end(T, k + 1, b)
311
+ typ = e >= k + 1 and common.sig(T, k + 1, e) or nil; k = e + 1
312
+ end
313
+ local last = (is(T, k, '=') or word(T, k, 'by')) and math.max(k, expression_end(T, k + 1, b)) or k - 1
314
+ -- Accessors extend the property range, and their calls belong to it.
315
+ while word(T, last + 1, 'get') or word(T, last + 1, 'set') do
316
+ local ac = last + 1
317
+ skip[ac] = true
318
+ local e = is(T, ac + 1, '(') and T.m[ac + 1] or ac
319
+ e = e or ac
320
+ if is(T, e + 1, ':') then e = type_end(T, e + 2, b) end
321
+ if is(T, e + 1, '{') then last = T.m[e + 1] or b
322
+ elseif is(T, e + 1, '=') then last = math.max(e + 1, expression_end(T, e + 2, b))
323
+ else last = e end
324
+ end
325
+ local owner = parent and r.nodes[parent]
326
+ local kind = s == 'typealias' and 'typedef' or owner and
327
+ (owner.kind == 'class' or owner.kind == 'interface' or owner.kind == 'enum') and 'property' or 'variable'
328
+ local id = add(r, T, start, last, ni, parent, kind, s == 'typealias' and last or k - 1,
329
+ { type = typ, receiver = receiver, static = not parent and mods.private or nil })
330
+ skip[ni] = true
331
+ if k <= last then scope(T, r, k + 1, last, id, skip) end
332
+ i = last + 1
333
+ elseif word(T, j, 'constructor') and parent and is(T, j + 1, '(') and T.m[j + 1] then
334
+ local k = T.m[j + 1] + 1
335
+ while k <= b and not is(T, k, '{') and T:line(k) == T:eline(k - 1) do k = k + 1 end
336
+ local body = is(T, k, '{') and k
337
+ local last = body and (T.m[body] or b) or k - 1
338
+ local id = add(r, T, start, last, j, parent, 'constructor', k - 1, { name = r.nodes[parent].name })
339
+ skip[j] = true
340
+ if body then scope(T, r, body + 1, last - 1, id, skip) end
341
+ i = last + 1
342
+ elseif is(T, j, '{') then
343
+ local last = T.m[j] or b
344
+ scope(T, r, j + 1, last - 1, parent, skip)
345
+ i = last + 1
346
+ else
347
+ if word(T, j, 'init') then skip[j] = true end
348
+ i = math.max(i + 1, j + 1)
349
+ end
350
+ end
351
+ end
352
+
353
+ function M.parse(path, src, language, opts)
354
+ local T = M.lang:tokenize(template_source(src))
355
+ T.src = src
356
+ -- Escaped identifiers are lexed as one token. Keep byte positions and text
357
+ -- consistent for both native and Lua scanners (native methods use raw data).
358
+ for i = 1, T.n do
359
+ if T.k[i] == 'str' and T:text(i):sub(1, 1) == '`' and T:text(i):sub(-1) == '`' then
360
+ T.k[i], T.s[i], T.e[i] = 'id', T.s[i] + 1, T.e[i] - 1
361
+ end
362
+ end
363
+ function T:text(i) return self.s[i] and self.src:sub(self.s[i], self.e[i]) or '' end
364
+ local r, skip = common.new(path, 'kotlin', opts), {}
365
+ r.package = '' -- Kotlin's default package is shared across source directories.
366
+ scope(T, r, 1, T.n, nil, skip)
367
+ for i = 1, T.n do
368
+ if T.k[i] == 'id' and not skip[i] then
369
+ local nexti = i + 1
370
+ if is(T, nexti, '<') then nexti = (angles(T, nexti, T.n) or nexti - 1) + 1 end
371
+ local call = is(T, nexti, '(') or is(T, nexti, '{')
372
+ local ref = is(T, i - 1, '::')
373
+ if call or ref then
374
+ local member = is(T, i - 1, '.') or is(T, i - 1, '?.') or ref
375
+ local recv = member and (T.k[i - 2] == 'id' or word(T, i - 2, 'this') or word(T, i - 2, 'super')) and T:text(i - 2) or nil
376
+ r.refs[#r.refs + 1] = { tok = i, name = T:text(i), kind = ref and 'ref' or member and 'member_call' or 'call',
377
+ recv = recv, line = T:line(i) }
378
+ end
379
+ end
380
+ end
381
+ return common.finish(r, T), T
382
+ end
383
+
384
+ return M
@@ -214,6 +214,8 @@ function M.parse(path, src, language, opts)
214
214
  if not defs[ref.tok] then kept[#kept + 1] = ref end
215
215
  end
216
216
  r.refs = kept
217
+ common.assign_refs(r)
218
+ require('codeoutline.lua_bindings').collect(r, T, bm)
217
219
  r.member_ops = nil
218
220
  return common.finish(r, T), T
219
221
  end
@@ -0,0 +1,222 @@
1
+ -- Static Lua module return bindings. Never execute indexed source.
2
+ local M = {}
3
+ local control = require('codeoutline.control')
4
+ local loaders = { require = true, dofile = true, loadfile = true }
5
+ local function literal(T, i)
6
+ if T.k[i] ~= 'str' then return nil end
7
+ local s = T:text(i)
8
+ local long = s:match('^%[(=*)%[')
9
+ if long then return s:sub(#long + 3, -#long - 3):gsub('^\r?\n', '', 1) end
10
+ if s:find('\\', 1, true) then return nil end
11
+ return s:sub(2, -2)
12
+ end
13
+
14
+ function M.collect(r, T, bm)
15
+ r.lua_imports, r.lua_exports, r.lua_locals = {}, {}, {}
16
+ local scopes, stack, params, bindings = {}, {}, {}, {}
17
+ for i = 1, T.n do
18
+ if bm[i] and bm[i] > i then stack[#stack + 1] = i end
19
+ scopes[i] = stack[#stack] or 0
20
+ if stack[#stack] and bm[stack[#stack]] == i then stack[#stack] = nil end
21
+ if T:text(i) == 'function' and T.k[i] == 'kw' then
22
+ local p = i + 1
23
+ while p < (bm[i] or i) and T:text(p) ~= '(' do p = p + 1 end
24
+ for j = p + 1, (T.m[p] or p) - 1 do if T.k[j] == 'id' then params[j] = i end end
25
+ end
26
+ end
27
+ local function add(name, token, scope, hint, imported)
28
+ bindings[name] = bindings[name] or {}
29
+ bindings[name][#bindings[name] + 1] = { token = token, a = scope, b = bm[scope] or T.n, hint = hint, imported = imported }
30
+ end
31
+ for i = 1, T.n do
32
+ if i % 4096 == 0 then control.check() end
33
+ if T.k[i] == 'id' then
34
+ local prev, next = T:text(i - 1), T:text(i + 1)
35
+ if params[i] then add(T:text(i), i, params[i], '?')
36
+ elseif prev == 'function' and T:text(i - 2) == 'local' then add(T:text(i), i, scopes[i - 2], '')
37
+ elseif prev == 'local' or (next == '=' and prev ~= '.' and prev ~= ':' and prev ~= '{' and prev ~= ',') then
38
+ local hint = '?'
39
+ if next == '=' and T:text(i + 2) == '{' then hint = 'lua-local:' .. T:text(i) end
40
+ local imported = next == '=' and loaders[T:text(i + 2)]
41
+ if next == '=' and loaders[T:text(i + 2)] then
42
+ local kind, call = T:text(i + 2), i + 2
43
+ local arg = T:text(call + 1) == '(' and call + 2 or call + 1
44
+ local path = literal(T, arg)
45
+ local close = T:text(call + 1) == '(' and T.m[call + 1] or arg
46
+ if close and ((close == arg) or (close == arg + 1)) and path then
47
+ -- A concatenation/operation after the loader is not its return value.
48
+ local tail = T:text(close + 1)
49
+ if tail ~= '..' and tail ~= '.' and tail ~= ':' and tail ~= 'or' and tail ~= 'and'
50
+ and tail ~= '(' and tail ~= '[' and tail ~= '+' and tail ~= '-' and tail ~= '*' and tail ~= '/' then
51
+ local shadowed = false
52
+ for _, b in ipairs(bindings[kind] or {}) do
53
+ if b.token < call and b.a <= call and b.b >= call then shadowed = true end
54
+ end
55
+ if not shadowed then
56
+ r.lua_imports[#r.lua_imports + 1] = { path = path, kind = kind }
57
+ hint = 'lua:' .. #r.lua_imports
58
+ if kind == 'loadfile' then hint = '?' end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ -- Keep ordinary local function/table lookup; block unknown imported receivers.
64
+ add(T:text(i), i, scopes[i], hint, imported)
65
+ end
66
+ end
67
+ end
68
+ for name, list in pairs(bindings) do
69
+ for _, b in ipairs(list) do
70
+ if b.hint:match('^lua%-local:') then
71
+ local members = {}
72
+ for index, n in ipairs(r.nodes) do
73
+ local scope = T:text(n.ti) == 'function' and scopes[n.ti - 1] or scopes[n.ti]
74
+ if n.ti >= b.token and n.ti <= b.b and scope == b.a
75
+ and (n.qualified == name .. '.' .. n.name or n.qualified == name .. ':' .. n.name) then members[n.name] = index end
76
+ end
77
+ r.lua_locals[#r.lua_locals + 1] = members
78
+ b.hint = 'lua-local:' .. #r.lua_locals
79
+ end
80
+ end
81
+ end
82
+ for _, ref in ipairs(r.refs) do
83
+ do
84
+ local best
85
+ for _, b in ipairs(bindings[ref.recv or ref.name] or {}) do
86
+ if b.token <= ref.tok and b.a <= ref.tok and b.b >= ref.tok
87
+ and (not best or b.a > best.a or (b.a == best.a and b.token > best.token)) then best = b end
88
+ end
89
+ if best and (ref.recv or best.imported) then ref.lua_binding = ref.recv and best.hint or '?' end
90
+ if ref.recv and (T:text(ref.tok - 3) == '.' or T:text(ref.tok - 3) == ':') then ref.lua_binding = '?' end
91
+ end
92
+ end
93
+
94
+ -- A single unconditional top-level return is the supported module contract.
95
+ local ret
96
+ for i = 1, T.n do
97
+ if T:text(i) == 'return' and T.k[i] == 'kw' then
98
+ local in_function = false
99
+ for _, n in ipairs(r.nodes) do
100
+ if (n.kind == 'function' or n.kind == 'method') and n.ti <= i and n.tj >= i then in_function = true; break end
101
+ end
102
+ if not in_function then
103
+ if ret or scopes[i] ~= 0 then r.lua_exports = {}; return end
104
+ ret = i
105
+ end
106
+ end
107
+ end
108
+ if not ret then return end
109
+ local owner, table_start
110
+ if T.k[ret + 1] == 'id' and (ret + 1 == T.n or T:text(ret + 2) == ';') then
111
+ owner = T:text(ret + 1)
112
+ for i = 1, ret - 1 do
113
+ if T:text(i) == owner and T:text(i + 1) == '=' and T:text(i + 2) == '{' and scopes[i] == 0 then table_start = i + 2 end
114
+ end
115
+ elseif T:text(ret + 1) == '{' and T.m[ret + 1] then
116
+ table_start = ret + 1
117
+ owner = r.path:match('([^/\\]+)%.lua$') or r.path
118
+ else return end
119
+ if owner then
120
+ if not table_start or not T.m[table_start] then return end
121
+ for i = table_start + 1, ret - 1 do
122
+ if scopes[i] == 0 and T:text(i) == owner and T:text(i + 1) == '=' then r.lua_exports = {}; return end
123
+ end
124
+ for index, n in ipairs(r.nodes) do
125
+ if not n.parent and n.ti >= table_start and (n.qualified == owner .. '.' .. n.name or n.qualified == owner .. ':' .. n.name)
126
+ and (n.kind == 'function' or n.kind == 'method') then r.lua_exports[n.name] = index end
127
+ end
128
+ end
129
+ local function alias(key, target)
130
+ for index, n in ipairs(r.nodes) do
131
+ if not n.parent and n.name == target and n.qualified == target and (n.kind == 'function' or n.kind == 'method') then
132
+ r.lua_exports[key] = index
133
+ end
134
+ end
135
+ end
136
+ if table_start then
137
+ local i, close = table_start + 1, T.m[table_start]
138
+ while i < close do
139
+ if T.k[i] == 'id' and T:text(i + 1) == '=' and T.k[i + 2] == 'id'
140
+ and (T:text(i + 3) == ',' or T:text(i + 3) == ';' or i + 3 == close) then alias(T:text(i), T:text(i + 2)) end
141
+ if T.m[i] and T.m[i] > i then i = T.m[i]
142
+ elseif bm[i] and bm[i] > i then i = bm[i] end
143
+ i = i + 1
144
+ end
145
+ end
146
+ for i = 1, ret - 1 do
147
+ if scopes[i] == 0 and T:text(i) == owner and T:text(i + 1) == '.' and T.k[i + 2] == 'id'
148
+ and T:text(i + 3) == '=' and T:text(i + 4) ~= 'function' then
149
+ r.lua_exports[T:text(i + 2)] = nil
150
+ if T.k[i + 4] == 'id' and (T.l[i + 5] ~= T.l[i + 4] or T:text(i + 5) == ';') then alias(T:text(i + 2), T:text(i + 4)) end
151
+ end
152
+ end
153
+ end
154
+
155
+ local function normalize(p)
156
+ local parts = {}
157
+ for s in p:gsub('\\', '/'):gmatch('[^/]+') do
158
+ if s == '..' and #parts > 0 and parts[#parts] ~= '..' then parts[#parts] = nil
159
+ elseif s ~= '.' then parts[#parts + 1] = s end
160
+ end
161
+ return table.concat(parts, '/')
162
+ end
163
+ function M.context(G)
164
+ local ctx = { directory = '', modules = {} }
165
+ local f = G.root and io.open(G.root .. '/.codeoutline.json', 'rb')
166
+ if f then
167
+ local raw = f:read(65537); f:close()
168
+ assert(#raw <= 65536, '.codeoutline.json exceeds 64 KiB')
169
+ local ok, config = pcall(xutils.json_unpack, raw)
170
+ assert(ok and type(config) == 'table', 'invalid .codeoutline.json')
171
+ local value = config.luaRuntimeDirectory
172
+ if value ~= nil then
173
+ assert(type(value) == 'string' and not value:match('^[/\\]') and not value:find(':', 1, true), 'luaRuntimeDirectory must be project-relative')
174
+ ctx.directory = normalize(value)
175
+ assert(not ctx.directory:match('^%.%.'), 'luaRuntimeDirectory must stay inside project')
176
+ end
177
+ end
178
+ return ctx
179
+ end
180
+ function M.modules(G, imp, ctx)
181
+ ctx = ctx or M.context(G)
182
+ local key = imp.kind .. '\0' .. imp.path
183
+ if ctx.modules[key] then return ctx.modules[key] end
184
+ local result = {}
185
+ if imp.kind == 'require' then
186
+ local sub = imp.path:gsub('%.', '/')
187
+ for _, want in ipairs({ sub .. '.lua', sub .. '/init.lua' }) do
188
+ local candidates = {}
189
+ for _, f in ipairs(G.by_base[want:match('([^/]+)$')] or {}) do candidates[#candidates + 1] = f end
190
+ for _, f in ipairs(G.new_files or {}) do candidates[#candidates + 1] = f end
191
+ for _, f in ipairs(candidates) do
192
+ local rec = G.files[f]
193
+ if rec.language == 'lua' and (rec.path == want or rec.path:sub(-#want - 1) == '/' .. want) then result[#result + 1] = f end
194
+ end
195
+ end
196
+ elseif not imp.path:match('^[/\\]') and not imp.path:find(':', 1, true) then
197
+ local path = normalize(ctx.directory .. '/' .. imp.path)
198
+ local f = G.file_index[path]
199
+ if f and G.files[f].language == 'lua' then result[1] = f end
200
+ end
201
+ ctx.modules[key] = result
202
+ return result
203
+ end
204
+ function M.resolve(G, f, name, hint, ctx)
205
+ if G.files[f].language ~= 'lua' or not hint or hint == '' then return nil end
206
+ if hint == '?' then return {} end
207
+ local local_index = tonumber(hint:match('^lua%-local:(%d+)$'))
208
+ if local_index then
209
+ local n = ((G.files[f].lua_locals or {})[local_index] or {})[name]
210
+ return n and { G.id_of[f][n] } or {}
211
+ end
212
+ local index = tonumber(hint:match('^lua:(%d+)$'))
213
+ local imp = index and (G.files[f].lua_imports or {})[index]
214
+ if not imp then return nil end
215
+ local result = {}
216
+ for _, target in ipairs(M.modules(G, imp, ctx)) do
217
+ local n = (G.files[target].lua_exports or {})[name]
218
+ if n then result[#result + 1] = G.id_of[target][n] end
219
+ end
220
+ return result
221
+ end
222
+ return M