@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.
@@ -0,0 +1,201 @@
1
+ -- JS/TS module and receiver evidence; deliberately not a type checker.
2
+ local M = {}
3
+ local control = require('codeoutline.control')
4
+ local function js(rec) return rec and (rec.language == 'javascript' or rec.language == 'typescript') end
5
+ local function dir(p) return p:match('^(.*)/[^/]*$') or '' end
6
+ local function norm(p)
7
+ local parts = {}
8
+ for s in p:gsub('\\', '/'):gmatch('[^/]+') do
9
+ if s == '..' and #parts > 0 and parts[#parts] ~= '..' then parts[#parts] = nil
10
+ elseif s ~= '.' then parts[#parts + 1] = s end
11
+ end
12
+ return (p:sub(1, 1) == '/' and '/' or '') .. table.concat(parts, '/')
13
+ end
14
+
15
+ -- JSONC is tokenized, never executed. Token-aware comma removal preserves strings.
16
+ local function config(path, seen, depth)
17
+ path = norm(path)
18
+ if seen[path] or depth > 8 then return {} end
19
+ seen[path] = true
20
+ local f = io.open(path, 'rb')
21
+ if not f then return {} end
22
+ local raw = f:read(1048577); f:close()
23
+ assert(#raw <= 1048576, 'JS project configuration exceeds 1 MiB: ' .. path)
24
+ local T = require('codeoutline.lang.js').lang:tokenize(raw)
25
+ local parts = {}
26
+ for i = 1, T.n do
27
+ local s, next = T:text(i), T:text(i + 1)
28
+ if not (s == ',' and (next == '}' or next == ']')) then parts[#parts + 1] = s end
29
+ end
30
+ local ok, data = pcall(xutils.json_unpack, table.concat(parts, ' '))
31
+ assert(ok and type(data) == 'table', 'invalid JS project configuration: ' .. path)
32
+ local base = dir(path)
33
+ local effective = {}
34
+ if type(data.extends) == 'string' and data.extends:sub(1, 1) == '.' then
35
+ local parent = norm(base .. '/' .. data.extends)
36
+ if not parent:match('%.json$') then parent = parent .. '.json' end
37
+ effective = config(parent, seen, depth + 1)
38
+ end
39
+ local co = data.compilerOptions or {}
40
+ if type(co.baseUrl) == 'string' then effective.base = norm(base .. '/' .. co.baseUrl) end
41
+ if type(co.paths) == 'table' then effective.paths, effective.origin = co.paths, base end
42
+ return effective
43
+ end
44
+
45
+ function M.context(G)
46
+ local ctx = { modules = {}, exports = {} }
47
+ if G.root then
48
+ for _, name in ipairs({ 'tsconfig.json', 'jsconfig.json', 'tsconfig.base.json' }) do
49
+ local opts = config(G.root .. '/' .. name, {}, 0)
50
+ if opts.paths then
51
+ ctx.base, ctx.patterns = opts.base or opts.origin, {}
52
+ for pattern, targets in pairs(opts.paths) do
53
+ if type(pattern) == 'string' and type(targets) == 'table' then
54
+ local pre, post = pattern:match('^(.-)%*(.-)$')
55
+ ctx.patterns[#ctx.patterns + 1] = { pattern = pattern, pre = pre, post = post, targets = targets }
56
+ end
57
+ end
58
+ table.sort(ctx.patterns, function(a, b)
59
+ local ap, bp = a.pre or a.pattern, b.pre or b.pattern
60
+ if #ap ~= #bp then return #ap > #bp end
61
+ if (a.pre == nil) ~= (b.pre == nil) then return a.pre == nil end
62
+ return a.pattern < b.pattern
63
+ end)
64
+ break
65
+ end
66
+ end
67
+ end
68
+ return ctx
69
+ end
70
+
71
+ local extensions = { '', '.ts', '.tsx', '.js', '.jsx', '.mts', '.cts', '.mjs', '.cjs',
72
+ '/index.ts', '/index.tsx', '/index.js', '/index.jsx', '/index.mts', '/index.mjs' }
73
+ function M.module(G, f, spec, ctx)
74
+ ctx = ctx or M.context(G)
75
+ local key = f .. '\0' .. spec
76
+ if ctx.modules[key] ~= nil then return ctx.modules[key] or nil end
77
+ local bases = {}
78
+ if spec:sub(1, 1) == '.' then bases[1] = norm(dir(G.files[f].path) .. '/' .. spec):gsub('^/', '')
79
+ else
80
+ for _, p in ipairs(ctx.patterns or {}) do
81
+ local capture
82
+ if not p.pre and spec == p.pattern then capture = ''
83
+ elseif p.pre and spec:sub(1, #p.pre) == p.pre and (#p.post == 0 or spec:sub(-#p.post) == p.post)
84
+ and #spec >= #p.pre + #p.post then capture = spec:sub(#p.pre + 1, #spec - #p.post) end
85
+ if capture then
86
+ for _, target in ipairs(p.targets) do
87
+ if type(target) == 'string' then
88
+ local absolute = norm(ctx.base .. '/' .. target:gsub('%*', function() return capture end))
89
+ local root = norm(G.root)
90
+ if absolute:sub(1, #root + 1) == root .. '/' then bases[#bases + 1] = absolute:sub(#root + 2) end
91
+ end
92
+ end
93
+ break
94
+ end
95
+ end
96
+ end
97
+ for _, base in ipairs(bases) do
98
+ local candidates = { base }
99
+ if base:match('%.js$') then candidates[#candidates + 1] = base:gsub('%.js$', '.ts'); candidates[#candidates + 1] = base:gsub('%.js$', '.tsx') end
100
+ for _, ext in ipairs(extensions) do candidates[#candidates + 1] = base .. ext end
101
+ for _, candidate in ipairs(candidates) do
102
+ local target = G.file_index[candidate]
103
+ if target and js(G.files[target]) then ctx.modules[key] = target; return target end
104
+ end
105
+ end
106
+ ctx.modules[key] = false
107
+ end
108
+
109
+ local function exported(G, f, name, ctx, seen, depth)
110
+ control.check()
111
+ if depth > 8 then return {} end
112
+ local key = f .. '\0' .. name
113
+ if depth == 0 and ctx.exports[key] then return ctx.exports[key] end
114
+ if seen[key] then return {} end
115
+ seen[key] = true
116
+ local result, unique = {}, {}
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
+ local rec = G.files[f]
119
+ local entries = rec.js_exports or {}
120
+ local explicit = false
121
+ for _, e in ipairs(entries) do if e.name == name then explicit = true end end
122
+ for _, e in ipairs(entries) do
123
+ if e.name == name or (not explicit and e.name == '*' and name ~= 'default') then
124
+ local want = e.local_name == '*' and name or e.local_name
125
+ if e.path then
126
+ local target = M.module(G, f, e.path, ctx)
127
+ if target then add(exported(G, target, want, ctx, seen, depth + 1)) end
128
+ else
129
+ for _, id in ipairs(G.id_of[f]) do
130
+ local n = G.nodes[id]
131
+ if not n.parent and n.name == want then add({ id }) end
132
+ end
133
+ for _, b in ipairs(rec.js_imports or {}) do
134
+ if b.local_name == want then
135
+ local target = M.module(G, f, b.path, ctx)
136
+ if target then add(exported(G, target, b.imported, ctx, seen, depth + 1)) end
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
142
+ if depth == 0 then ctx.exports[key] = result end
143
+ return result
144
+ end
145
+
146
+ function M.resolve(G, f, name, kind, recv, evidence, ctx)
147
+ local rec = G.files[f]
148
+ if not js(rec) then return nil end
149
+ ctx = ctx or M.context(G)
150
+ local function binding(local_name, member)
151
+ for _, b in ipairs(rec.js_imports or {}) do
152
+ if b.local_name == local_name then
153
+ local target = M.module(G, f, b.path, ctx)
154
+ if not target then return {} end
155
+ if b.imported == '*' then return member and exported(G, target, member, ctx, {}, 0) or {} end
156
+ return exported(G, target, b.imported, ctx, {}, 0)
157
+ end
158
+ end
159
+ end
160
+ if evidence == false then return {} end
161
+ if evidence then
162
+ local node = evidence:match('^node:(%d+)$')
163
+ if node then return { G.id_of[f][tonumber(node)] } end
164
+ local head, tail = evidence:match('^([%w_$]+)%.([%w_$]+)$')
165
+ local classes = binding(head or evidence, tail)
166
+ if classes == nil then
167
+ classes = {}
168
+ for _, id in ipairs(G.id_of[f]) do
169
+ local n = G.nodes[id]
170
+ if not n.parent and n.name == evidence and (n.kind == 'class' or n.kind == 'interface') then classes[#classes + 1] = id end
171
+ end
172
+ end
173
+ local result = {}
174
+ for _, cls in ipairs(classes) do
175
+ if G.nodes[cls].kind == 'class' or G.nodes[cls].kind == 'interface' then
176
+ for _, id in ipairs(G.by_name[name] or {}) do
177
+ local n = G.nodes[id]
178
+ if n.parent and G.id_of[G.node_file[id]][n.parent] == cls then result[#result + 1] = id end
179
+ end
180
+ end
181
+ end
182
+ return result
183
+ end
184
+ if recv then
185
+ for _, b in ipairs(rec.js_imports or {}) do
186
+ if b.local_name == recv then
187
+ if b.imported == '*' then return binding(recv, name) end
188
+ local result = {}
189
+ for _, cls in ipairs(binding(recv) or {}) do
190
+ for _, id in ipairs(G.by_name[name] or {}) do
191
+ local n = G.nodes[id]
192
+ if n.parent and G.id_of[G.node_file[id]][n.parent] == cls then result[#result + 1] = id end
193
+ end
194
+ end
195
+ return result
196
+ end
197
+ end
198
+ else return binding(name) end
199
+ end
200
+
201
+ return M
@@ -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
@@ -192,6 +192,19 @@ function M.parse(path, src, language, opts)
192
192
  local v = string_value(T, arg)
193
193
  if v then r.imports[#r.imports + 1] = { path = v, line = T:line(i), kind = T:text(i) } end
194
194
  end
195
+ elseif k == 'id' and T:text(i):sub(-6) == 'import' and not is(T, i - 1, ':') then
196
+ -- `*import` loader pattern: `ks.import('path')`, `skynet_import 'path'`.
197
+ -- Path-based; resolution keeps it only when the file exists.
198
+ local paren = is(T, i + 1, '(')
199
+ local arg = paren and i + 2 or i + 1
200
+ if T.k[arg] == 'str' and (not paren or is(T, arg + 1, ')') or is(T, arg + 1, ',')) then
201
+ local v = string_value(T, arg)
202
+ if v then
203
+ local kind = T:text(i)
204
+ if is(T, i - 1, '.') and T.k[i - 2] == 'id' then kind = T:text(i - 2) .. '.' .. kind end
205
+ r.imports[#r.imports + 1] = { path = v, line = T:line(i), kind = kind }
206
+ end
207
+ end
195
208
  end
196
209
  end
197
210
 
@@ -214,6 +227,10 @@ function M.parse(path, src, language, opts)
214
227
  if not defs[ref.tok] then kept[#kept + 1] = ref end
215
228
  end
216
229
  r.refs = kept
230
+ if not r.dispatch_only then
231
+ common.assign_refs(r)
232
+ require('codeoutline.lua_bindings').collect(r, T, bm)
233
+ end
217
234
  r.member_ops = nil
218
235
  return common.finish(r, T), T
219
236
  end