@codua/codeoutline-win32-x64 0.2.1 → 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.
- package/README.md +1 -0
- package/bin/xnet.exe +0 -0
- package/build-info.json +1 -1
- package/package.json +1 -1
- package/scripts/codeoutline/callers.lua +116 -0
- package/scripts/codeoutline/command.lua +7 -3
- package/scripts/codeoutline/common.lua +29 -3
- package/scripts/codeoutline/completion.lua +162 -62
- package/scripts/codeoutline/cpp_bindings.lua +116 -0
- package/scripts/codeoutline/cpp_types.lua +209 -0
- package/scripts/codeoutline/csharp_bindings.lua +498 -0
- package/scripts/codeoutline/dispatch_aliases.lua +474 -0
- package/scripts/codeoutline/documents.lua +11 -1
- package/scripts/codeoutline/explore.lua +73 -12
- package/scripts/codeoutline/foreign.lua +125 -0
- package/scripts/codeoutline/foreign_lua.lua +273 -0
- package/scripts/codeoutline/graph.lua +80 -22
- package/scripts/codeoutline/index.lua +15 -6
- package/scripts/codeoutline/index_worker.lua +15 -7
- package/scripts/codeoutline/indirect.lua +148 -50
- package/scripts/codeoutline/js_evidence.lua +30 -2
- package/scripts/codeoutline/js_resolution.lua +34 -0
- package/scripts/codeoutline/lang/csharp.lua +15 -3
- package/scripts/codeoutline/lang/lua.lua +17 -2
- package/scripts/codeoutline/lsp.lua +58 -34
- package/scripts/codeoutline/lua_bindings.lua +242 -34
- package/scripts/codeoutline/lua_literal.lua +38 -0
- package/scripts/codeoutline/main.lua +24 -3
- package/scripts/codeoutline/mcp.lua +13 -4
- package/scripts/codeoutline/navigation.lua +337 -96
- package/scripts/codeoutline/ordinary_bindings.lua +220 -0
- package/scripts/codeoutline/parse.lua +2 -0
- package/scripts/codeoutline/python_bindings.lua +112 -0
- package/scripts/codeoutline/query_report.lua +126 -0
- package/scripts/codeoutline/rules.lua +127 -0
- package/scripts/codeoutline/service.lua +11 -3
- package/scripts/codeoutline/static_dispatch.lua +534 -0
- package/scripts/codeoutline/update_sequence.lua +1 -1
- package/scripts/codeoutline/version.lua +1 -1
|
@@ -2,6 +2,22 @@
|
|
|
2
2
|
local M = {}
|
|
3
3
|
local control = require('codeoutline.control')
|
|
4
4
|
local loaders = { require = true, dofile = true, loadfile = true }
|
|
5
|
+
local EXPORT_OWNER = '__exports'
|
|
6
|
+
-- Loader call at token j: a known loader name, or any dotted/plain name whose
|
|
7
|
+
-- last segment ends in `import` (`ks.import`, `skynet_import`). Returns the
|
|
8
|
+
-- kind recorded in lua_imports, the callee's last token, and the base name
|
|
9
|
+
-- used for shadow checks.
|
|
10
|
+
local function loader_call(T, j)
|
|
11
|
+
if T.k[j] ~= 'id' then return nil end
|
|
12
|
+
local k = j
|
|
13
|
+
while T.k[k + 1] == 'op' and T:text(k + 1) == '.' and T.k[k + 2] == 'id' do k = k + 2 end
|
|
14
|
+
local name = T:text(k)
|
|
15
|
+
if k == j and loaders[name] then return name, k, name end
|
|
16
|
+
if name:sub(-6) ~= 'import' then return nil end
|
|
17
|
+
local parts = {}
|
|
18
|
+
for x = j, k, 2 do parts[#parts + 1] = T:text(x) end
|
|
19
|
+
return table.concat(parts, '.'), k, T:text(j)
|
|
20
|
+
end
|
|
5
21
|
local function literal(T, i)
|
|
6
22
|
if T.k[i] ~= 'str' then return nil end
|
|
7
23
|
local s = T:text(i)
|
|
@@ -11,13 +27,26 @@ local function literal(T, i)
|
|
|
11
27
|
return s:sub(2, -2)
|
|
12
28
|
end
|
|
13
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
|
+
|
|
14
43
|
function M.collect(r, T, bm)
|
|
15
44
|
r.lua_imports, r.lua_exports, r.lua_locals = {}, {}, {}
|
|
16
45
|
local scopes, stack, params, bindings = {}, {}, {}, {}
|
|
17
46
|
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
47
|
if stack[#stack] and bm[stack[#stack]] == i then stack[#stack] = nil end
|
|
48
|
+
scopes[i] = stack[#stack] or 0
|
|
49
|
+
if bm[i] and bm[i] > i then stack[#stack + 1] = i end
|
|
21
50
|
if T:text(i) == 'function' and T.k[i] == 'kw' then
|
|
22
51
|
local p = i + 1
|
|
23
52
|
while p < (bm[i] or i) and T:text(p) ~= '(' do p = p + 1 end
|
|
@@ -37,19 +66,21 @@ function M.collect(r, T, bm)
|
|
|
37
66
|
elseif prev == 'local' or (next == '=' and prev ~= '.' and prev ~= ':' and prev ~= '{' and prev ~= ',') then
|
|
38
67
|
local hint = '?'
|
|
39
68
|
if next == '=' and T:text(i + 2) == '{' then hint = 'lua-local:' .. T:text(i) end
|
|
40
|
-
local
|
|
41
|
-
if next == '='
|
|
42
|
-
|
|
69
|
+
local kind, call, base
|
|
70
|
+
if next == '=' then kind, call, base = loader_call(T, i + 2) end
|
|
71
|
+
local imported = kind ~= nil
|
|
72
|
+
if kind then
|
|
43
73
|
local arg = T:text(call + 1) == '(' and call + 2 or call + 1
|
|
44
74
|
local path = literal(T, arg)
|
|
45
75
|
local close = T:text(call + 1) == '(' and T.m[call + 1] or arg
|
|
46
|
-
if close and ((close == arg) or (close == arg + 1)
|
|
76
|
+
if close and ((close == arg) or (close == arg + 1)
|
|
77
|
+
or (close > arg and T:text(arg + 1) == ',')) and path then
|
|
47
78
|
-- A concatenation/operation after the loader is not its return value.
|
|
48
79
|
local tail = T:text(close + 1)
|
|
49
80
|
if tail ~= '..' and tail ~= '.' and tail ~= ':' and tail ~= 'or' and tail ~= 'and'
|
|
50
81
|
and tail ~= '(' and tail ~= '[' and tail ~= '+' and tail ~= '-' and tail ~= '*' and tail ~= '/' then
|
|
51
82
|
local shadowed = false
|
|
52
|
-
for _, b in ipairs(bindings[
|
|
83
|
+
for _, b in ipairs(bindings[base] or {}) do
|
|
53
84
|
if b.token < call and b.a <= call and b.b >= call then shadowed = true end
|
|
54
85
|
end
|
|
55
86
|
if not shadowed then
|
|
@@ -65,15 +96,12 @@ function M.collect(r, T, bm)
|
|
|
65
96
|
end
|
|
66
97
|
end
|
|
67
98
|
end
|
|
99
|
+
-- Query-only lexical bindings travel with the cached tokens, not the index.
|
|
100
|
+
T.lua_bindings = bindings
|
|
68
101
|
for name, list in pairs(bindings) do
|
|
69
102
|
for _, b in ipairs(list) do
|
|
70
103
|
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
|
|
104
|
+
local members = M.local_members(r.nodes, scopes, name, b.token, b.a, b.b)
|
|
77
105
|
r.lua_locals[#r.lua_locals + 1] = members
|
|
78
106
|
b.hint = 'lua-local:' .. #r.lua_locals
|
|
79
107
|
end
|
|
@@ -91,21 +119,88 @@ function M.collect(r, T, bm)
|
|
|
91
119
|
end
|
|
92
120
|
end
|
|
93
121
|
|
|
122
|
+
local function alias(into, key, target)
|
|
123
|
+
for index, n in ipairs(r.nodes) do
|
|
124
|
+
if not n.parent and n.name == target and n.qualified == target and (n.kind == 'function' or n.kind == 'method') then
|
|
125
|
+
into[key] = index
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
-- A global alias is a statement of its own: the right-hand side is a bare
|
|
130
|
+
-- identifier and nothing but a separator follows it on that line. The key
|
|
131
|
+
-- is dotted (`g_exports.name`) or a literal (`g_exports['name']`).
|
|
132
|
+
local globals = {}
|
|
133
|
+
for i = 1, T.n - 4 do
|
|
134
|
+
if i % 4096 == 0 then control.check() end
|
|
135
|
+
local owner = T:text(i)
|
|
136
|
+
if T.k[i] == 'id' and not bindings[owner] and T:text(i - 1) ~= '.' and T:text(i - 1) ~= ':' then
|
|
137
|
+
local key, eq
|
|
138
|
+
if T:text(i + 1) == '.' and T.k[i + 2] == 'id' then key, eq = T:text(i + 2), i + 3
|
|
139
|
+
elseif T:text(i + 1) == '[' and T.m[i + 1] == i + 3 and T.k[i + 2] == 'str' then key, eq = literal(T, i + 2), i + 4 end
|
|
140
|
+
local target = key and T:text(eq) == '=' and T.k[eq + 1] == 'id' and T:text(eq + 1)
|
|
141
|
+
if key and T:text(eq) == '=' then
|
|
142
|
+
globals[owner] = globals[owner] or {}
|
|
143
|
+
globals[owner][key] = nil
|
|
144
|
+
end
|
|
145
|
+
if target and target ~= key and (T.l[eq + 2] ~= T.l[eq + 1] or T:text(eq + 2) == ';') then
|
|
146
|
+
alias(globals[owner], key, target)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
-- Keep candidates independent of project configuration, including in caches.
|
|
151
|
+
for owner, exports in pairs(globals) do
|
|
152
|
+
for name, index in pairs(exports) do
|
|
153
|
+
local node = r.nodes[index]
|
|
154
|
+
node.lua_aliases = node.lua_aliases or {}
|
|
155
|
+
node.lua_aliases[owner] = node.lua_aliases[owner] or {}
|
|
156
|
+
node.lua_aliases[owner][#node.lua_aliases[owner] + 1] = name
|
|
157
|
+
end
|
|
158
|
+
end
|
|
94
159
|
-- A single unconditional top-level return is the supported module contract.
|
|
160
|
+
-- Files with no module-level return may use the `__exports` contract below;
|
|
161
|
+
-- conditional or multiple returns invalidate both contracts. Function depth
|
|
162
|
+
-- comes from token blocks, so returns inside anonymous callbacks (which are
|
|
163
|
+
-- not nodes) do not pose as module-level returns.
|
|
164
|
+
local fn_close, depth = {}, 0
|
|
165
|
+
for i = 1, T.n do
|
|
166
|
+
if T.k[i] == 'kw' and T:text(i) == 'function' and bm[i] then fn_close[bm[i]] = true end
|
|
167
|
+
end
|
|
95
168
|
local ret
|
|
96
169
|
for i = 1, T.n do
|
|
97
|
-
if T
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
170
|
+
if T.k[i] == 'kw' and T:text(i) == 'function' then depth = depth + 1 end
|
|
171
|
+
if T:text(i) == 'return' and T.k[i] == 'kw' and depth == 0 then
|
|
172
|
+
if ret or scopes[i] ~= 0 then ret = false else ret = i end
|
|
173
|
+
end
|
|
174
|
+
if fn_close[i] then depth = depth - 1 end
|
|
175
|
+
if ret == false then break end
|
|
176
|
+
end
|
|
177
|
+
if not ret then
|
|
178
|
+
-- Return-less module (kslua-style): top-level `__exports` members are the
|
|
179
|
+
-- contract, unless the file defines `__exports` itself.
|
|
180
|
+
if ret == false or bindings[EXPORT_OWNER] and #bindings[EXPORT_OWNER] > 0 then return end
|
|
181
|
+
local count = 0
|
|
182
|
+
for _, n in ipairs(r.nodes) do
|
|
183
|
+
if not n.parent and (n.kind == 'function' or n.kind == 'method')
|
|
184
|
+
and (n.qualified == EXPORT_OWNER .. '.' .. n.name or n.qualified == EXPORT_OWNER .. ':' .. n.name) then
|
|
185
|
+
count = count + 1
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
if count == 0 then return end
|
|
189
|
+
for index, n in ipairs(r.nodes) do
|
|
190
|
+
if not n.parent and (n.kind == 'function' or n.kind == 'method')
|
|
191
|
+
and (n.qualified == EXPORT_OWNER .. '.' .. n.name or n.qualified == EXPORT_OWNER .. ':' .. n.name) then
|
|
192
|
+
r.lua_exports[n.name] = index
|
|
101
193
|
end
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
194
|
+
end
|
|
195
|
+
for i = 1, T.n - 4 do
|
|
196
|
+
if scopes[i] == 0 and T.k[i] == 'id' and T:text(i) == EXPORT_OWNER and T:text(i + 1) == '.' and T.k[i + 2] == 'id'
|
|
197
|
+
and T:text(i + 3) == '=' and T:text(i + 4) ~= 'function' then
|
|
198
|
+
r.lua_exports[T:text(i + 2)] = nil
|
|
199
|
+
if T.k[i + 4] == 'id' and (T.l[i + 5] ~= T.l[i + 4] or T:text(i + 5) == ';') then alias(r.lua_exports, T:text(i + 2), T:text(i + 4)) end
|
|
105
200
|
end
|
|
106
201
|
end
|
|
202
|
+
return
|
|
107
203
|
end
|
|
108
|
-
if not ret then return end
|
|
109
204
|
local owner, table_start
|
|
110
205
|
if T.k[ret + 1] == 'id' and (ret + 1 == T.n or T:text(ret + 2) == ';') then
|
|
111
206
|
owner = T:text(ret + 1)
|
|
@@ -121,23 +216,17 @@ function M.collect(r, T, bm)
|
|
|
121
216
|
for i = table_start + 1, ret - 1 do
|
|
122
217
|
if scopes[i] == 0 and T:text(i) == owner and T:text(i + 1) == '=' then r.lua_exports = {}; return end
|
|
123
218
|
end
|
|
219
|
+
r.lua_owner = owner
|
|
124
220
|
for index, n in ipairs(r.nodes) do
|
|
125
221
|
if not n.parent and n.ti >= table_start and (n.qualified == owner .. '.' .. n.name or n.qualified == owner .. ':' .. n.name)
|
|
126
222
|
and (n.kind == 'function' or n.kind == 'method') then r.lua_exports[n.name] = index end
|
|
127
223
|
end
|
|
128
224
|
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
225
|
if table_start then
|
|
137
226
|
local i, close = table_start + 1, T.m[table_start]
|
|
138
227
|
while i < close do
|
|
139
228
|
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
|
|
229
|
+
and (T:text(i + 3) == ',' or T:text(i + 3) == ';' or i + 3 == close) then alias(r.lua_exports, T:text(i), T:text(i + 2)) end
|
|
141
230
|
if T.m[i] and T.m[i] > i then i = T.m[i]
|
|
142
231
|
elseif bm[i] and bm[i] > i then i = bm[i] end
|
|
143
232
|
i = i + 1
|
|
@@ -147,7 +236,7 @@ function M.collect(r, T, bm)
|
|
|
147
236
|
if scopes[i] == 0 and T:text(i) == owner and T:text(i + 1) == '.' and T.k[i + 2] == 'id'
|
|
148
237
|
and T:text(i + 3) == '=' and T:text(i + 4) ~= 'function' then
|
|
149
238
|
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
|
|
239
|
+
if T.k[i + 4] == 'id' and (T.l[i + 5] ~= T.l[i + 4] or T:text(i + 5) == ';') then alias(r.lua_exports, T:text(i + 2), T:text(i + 4)) end
|
|
151
240
|
end
|
|
152
241
|
end
|
|
153
242
|
end
|
|
@@ -161,13 +250,26 @@ local function normalize(p)
|
|
|
161
250
|
return table.concat(parts, '/')
|
|
162
251
|
end
|
|
163
252
|
function M.context(G)
|
|
164
|
-
local ctx = { directory = '', modules = {} }
|
|
253
|
+
local ctx = { directory = '', modules = {}, global_export_tables = { 'g_exports' } }
|
|
165
254
|
local f = G.root and io.open(G.root .. '/.codeoutline.json', 'rb')
|
|
166
255
|
if f then
|
|
167
256
|
local raw = f:read(65537); f:close()
|
|
168
257
|
assert(#raw <= 65536, '.codeoutline.json exceeds 64 KiB')
|
|
169
258
|
local ok, config = pcall(xutils.json_unpack, raw)
|
|
170
259
|
assert(ok and type(config) == 'table', 'invalid .codeoutline.json')
|
|
260
|
+
local owners = config.luaGlobalExportTables
|
|
261
|
+
if owners ~= nil then
|
|
262
|
+
assert(type(owners) == 'table', 'luaGlobalExportTables must be an array of Lua identifiers')
|
|
263
|
+
local seen, names = {}, {}
|
|
264
|
+
for key, name in pairs(owners) do
|
|
265
|
+
assert(type(key) == 'number' and key >= 1 and key <= #owners and key % 1 == 0
|
|
266
|
+
and type(name) == 'string' and name:match('^[%a_][%w_]*$'),
|
|
267
|
+
'luaGlobalExportTables must be an array of Lua identifiers')
|
|
268
|
+
if not seen[name] then names[#names + 1], seen[name] = name, true end
|
|
269
|
+
end
|
|
270
|
+
table.sort(names)
|
|
271
|
+
ctx.global_export_tables = names
|
|
272
|
+
end
|
|
171
273
|
local value = config.luaRuntimeDirectory
|
|
172
274
|
if value ~= nil then
|
|
173
275
|
assert(type(value) == 'string' and not value:match('^[/\\]') and not value:find(':', 1, true), 'luaRuntimeDirectory must be project-relative')
|
|
@@ -177,12 +279,25 @@ function M.context(G)
|
|
|
177
279
|
end
|
|
178
280
|
return ctx
|
|
179
281
|
end
|
|
282
|
+
-- Project-selected aliases share the normal name index and overlay path.
|
|
283
|
+
local EMPTY = {}
|
|
284
|
+
function M.aliases(node, owners)
|
|
285
|
+
if not node.lua_aliases then return EMPTY end
|
|
286
|
+
local names, seen = {}, {}
|
|
287
|
+
for _, owner in ipairs(owners) do
|
|
288
|
+
for _, name in ipairs((node.lua_aliases or {})[owner] or {}) do
|
|
289
|
+
if not seen[name] then names[#names + 1], seen[name] = name, true end
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
return names
|
|
293
|
+
end
|
|
180
294
|
function M.modules(G, imp, ctx)
|
|
181
295
|
ctx = ctx or M.context(G)
|
|
182
296
|
local key = imp.kind .. '\0' .. imp.path
|
|
183
297
|
if ctx.modules[key] then return ctx.modules[key] end
|
|
184
298
|
local result = {}
|
|
185
299
|
if imp.kind == 'require' then
|
|
300
|
+
for _, f in ipairs(M.native_modules(G, imp.path, ctx)) do result[#result + 1] = f end
|
|
186
301
|
local sub = imp.path:gsub('%.', '/')
|
|
187
302
|
for _, want in ipairs({ sub .. '.lua', sub .. '/init.lua' }) do
|
|
188
303
|
local candidates = {}
|
|
@@ -197,12 +312,103 @@ function M.modules(G, imp, ctx)
|
|
|
197
312
|
local path = normalize(ctx.directory .. '/' .. imp.path)
|
|
198
313
|
local f = G.file_index[path]
|
|
199
314
|
if f and G.files[f].language == 'lua' then result[1] = f end
|
|
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).
|
|
319
|
+
local want = normalize(imp.path)
|
|
320
|
+
local base = want:match('([^/]+)$')
|
|
321
|
+
if base then
|
|
322
|
+
local candidates, seen = {}, {}
|
|
323
|
+
for _, g in ipairs(G.by_base[base] or {}) do candidates[#candidates + 1] = g end
|
|
324
|
+
for _, g in ipairs(G.new_files or {}) do candidates[#candidates + 1] = g end
|
|
325
|
+
for _, g in ipairs(candidates) do
|
|
326
|
+
local rec = G.files[g]
|
|
327
|
+
if rec and rec.language == 'lua' and not seen[g]
|
|
328
|
+
and (rec.path == want or rec.path:sub(-#want - 1) == '/' .. want) then
|
|
329
|
+
seen[g] = true
|
|
330
|
+
result[#result + 1] = g
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
end
|
|
200
335
|
end
|
|
201
336
|
ctx.modules[key] = result
|
|
202
337
|
return result
|
|
203
338
|
end
|
|
204
|
-
function M.
|
|
205
|
-
|
|
339
|
+
function M.native_modules(G, name, ctx)
|
|
340
|
+
return require('codeoutline.foreign').modules(G, 'lua', name, ctx)
|
|
341
|
+
end
|
|
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
|
+
|
|
402
|
+
function M.resolve(G, f, name, hint, ctx, recv)
|
|
403
|
+
if G.files[f].language ~= 'lua' then return nil end
|
|
404
|
+
if not hint or hint == '' then
|
|
405
|
+
local result = {}
|
|
406
|
+
for _, target in ipairs(M.native_modules(G, recv, ctx)) do
|
|
407
|
+
local n = require('codeoutline.foreign').exports(G.files[target], 'lua', recv)[name]
|
|
408
|
+
if n then result[#result + 1] = G.id_of[target][n] end
|
|
409
|
+
end
|
|
410
|
+
return #result > 0 and result or nil
|
|
411
|
+
end
|
|
206
412
|
if hint == '?' then return {} end
|
|
207
413
|
local local_index = tonumber(hint:match('^lua%-local:(%d+)$'))
|
|
208
414
|
if local_index then
|
|
@@ -214,7 +420,9 @@ function M.resolve(G, f, name, hint, ctx)
|
|
|
214
420
|
if not imp then return nil end
|
|
215
421
|
local result = {}
|
|
216
422
|
for _, target in ipairs(M.modules(G, imp, ctx)) do
|
|
217
|
-
local
|
|
423
|
+
local rec = G.files[target]
|
|
424
|
+
local exports = require('codeoutline.foreign').exports(rec, 'lua', imp.path) or rec.lua_exports
|
|
425
|
+
local n = (exports or {})[name]
|
|
218
426
|
if n then result[#result + 1] = G.id_of[target][n] end
|
|
219
427
|
end
|
|
220
428
|
return result
|
|
@@ -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
|
|
@@ -77,6 +77,21 @@ local function submit(req, done)
|
|
|
77
77
|
return id
|
|
78
78
|
end
|
|
79
79
|
local function cancel(id) if id and jobs[id] then shared:set('cancel:' .. id, true) end end
|
|
80
|
+
-- LSP queries wait in their own coroutine. MCP and background refresh jobs
|
|
81
|
+
-- retain the callback/post transport above.
|
|
82
|
+
local function await_worker(req, started)
|
|
83
|
+
if job_count >= 64 then return false, 'Request queue is full; retry later' end
|
|
84
|
+
next_id = next_id + 1
|
|
85
|
+
local id = tostring(next_id)
|
|
86
|
+
req.deadline = xtimer.now_ms() + 120000
|
|
87
|
+
jobs[id], job_count = { request = req, rpc = true, waiter = coroutine.running() }, job_count + 1
|
|
88
|
+
started(id)
|
|
89
|
+
if not worker_ready then coroutine.yield() end
|
|
90
|
+
local called, ok, result = pcall(xthread.rpc, 2, 'run_rpc', math.max(1, req.deadline - xtimer.now_ms()), id, req)
|
|
91
|
+
jobs[id], job_count = nil, job_count - 1
|
|
92
|
+
if not called then return false, tostring(ok) end
|
|
93
|
+
return ok, result
|
|
94
|
+
end
|
|
80
95
|
local function send(response)
|
|
81
96
|
if response then
|
|
82
97
|
io.stdout:write(lsp_mode and lsp.frame(response) or (xutils.json_pack(response) .. '\n'))
|
|
@@ -119,7 +134,8 @@ return {
|
|
|
119
134
|
assert(xutils.read_stdin, 'Rebuild runtime for nonblocking stdin support')
|
|
120
135
|
if lsp_mode then
|
|
121
136
|
assert(xutils.stdout_binary and xutils.stdout_binary(), 'Rebuild runtime for binary stdout support')
|
|
122
|
-
|
|
137
|
+
xtimer.init(16) -- RPC deadlines use the runtime's timer callbacks.
|
|
138
|
+
stdio = lsp.new(config, send, submit, cancel, stop, await_worker)
|
|
123
139
|
else stdio = mcp.new(config, submit, cancel) end
|
|
124
140
|
else
|
|
125
141
|
-- Packages ship the codec as lib/xhttp_codec.lua; a source checkout
|
|
@@ -150,8 +166,13 @@ return {
|
|
|
150
166
|
table.sort(waiting, function(a, b) return tonumber(a) < tonumber(b) end)
|
|
151
167
|
for _, job_id in ipairs(waiting) do
|
|
152
168
|
local job = jobs[job_id]
|
|
153
|
-
|
|
154
|
-
|
|
169
|
+
if job.rpc then
|
|
170
|
+
local resumed, err = coroutine.resume(job.waiter)
|
|
171
|
+
if not resumed then error(err) end
|
|
172
|
+
else
|
|
173
|
+
local posted, err = xthread.post(2, 'run', job_id, job.request)
|
|
174
|
+
if not posted then jobs[job_id], job_count = nil, job_count - 1; job.done(false, tostring(err)) end
|
|
175
|
+
end
|
|
155
176
|
end
|
|
156
177
|
elseif op == 'worker_result' then
|
|
157
178
|
local job = jobs[id]
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
local paths = require('codeoutline.path')
|
|
2
2
|
local explore = require('codeoutline.explore')
|
|
3
|
+
local callers = require('codeoutline.callers')
|
|
3
4
|
local text = require('codeoutline.text')
|
|
4
5
|
local version = require('codeoutline.version')
|
|
5
6
|
local M = { VERSION = '2025-06-18' }
|
|
@@ -9,6 +10,13 @@ local function object(t) return type(t) == 'table' and getmetatable(t) ~= xutils
|
|
|
9
10
|
local project = { type = 'string', minLength = 1,
|
|
10
11
|
description = 'Project directory on the server machine; defaults to PROJECT, the first client root, then the stdio working directory.' }
|
|
11
12
|
M.tools = array({
|
|
13
|
+
{ name = 'codeoutline_callers', description = 'Find potential callers recursively, with shortest call paths and affected files. Matches an exact symbol or qualified name; defaults to 3 levels. Refreshes automatically.',
|
|
14
|
+
inputSchema = { type = 'object', properties = { projectPath = project,
|
|
15
|
+
query = { type = 'string', minLength = 1, maxLength = 4096 },
|
|
16
|
+
depth = { type = 'integer', minimum = 1, maximum = 8, default = 3 },
|
|
17
|
+
max_nodes = { type = 'integer', minimum = 1, maximum = 2000, default = 200 },
|
|
18
|
+
budget = { type = 'integer', minimum = 256, maximum = 262144, default = 16000 } },
|
|
19
|
+
required = array({ 'query' }), additionalProperties = false } },
|
|
12
20
|
{ name = 'codeoutline_explore', description = 'Query symbols, line-numbered source, call paths and neighbors. Refreshes automatically.',
|
|
13
21
|
inputSchema = { type = 'object', properties = { projectPath = project,
|
|
14
22
|
query = { type = 'string', minLength = 1, maxLength = 4096 },
|
|
@@ -146,8 +154,9 @@ function M.new(config, submit, cancel)
|
|
|
146
154
|
if args.projectPath ~= nil and (type(args.projectPath) ~= 'string' or args.projectPath == '') then
|
|
147
155
|
error_result(-32602, 'projectPath must be a non-empty string'); return
|
|
148
156
|
end
|
|
149
|
-
if tool.name == 'codeoutline_explore' then
|
|
150
|
-
local
|
|
157
|
+
if tool.name == 'codeoutline_explore' or tool.name == 'codeoutline_callers' then
|
|
158
|
+
local engine = tool.name == 'codeoutline_callers' and callers or explore
|
|
159
|
+
local ok, err = pcall(engine.validate, args.query, { budget = args.budget, depth = args.depth, max_nodes = args.max_nodes })
|
|
151
160
|
if not ok then error_result(-32602, tostring(err)); return end
|
|
152
161
|
end
|
|
153
162
|
local token = object(params._meta) and params._meta.progressToken or nil
|
|
@@ -166,10 +175,10 @@ function M.new(config, submit, cancel)
|
|
|
166
175
|
result({ content = array({ { type = 'text', text = err or 'projectPath is outside allowed roots' } }), isError = true }); return
|
|
167
176
|
end
|
|
168
177
|
local job, submit_error = submit({ method = tool.name:sub(#'codeoutline_' + 1), projectPath = root,
|
|
169
|
-
allowedRoots = config.roots, query = args.query, budget = args.budget }, function(ok, value)
|
|
178
|
+
allowedRoots = config.roots, query = args.query, budget = args.budget, depth = args.depth, max_nodes = args.max_nodes }, function(ok, value)
|
|
170
179
|
if self.pending[id] ~= p then return end
|
|
171
180
|
self.pending[id] = nil
|
|
172
|
-
local output = ok and (tool.name == 'codeoutline_explore' and value.text or xutils.json_pack(value)) or tostring(value)
|
|
181
|
+
local output = ok and ((tool.name == 'codeoutline_explore' or tool.name == 'codeoutline_callers') and value.text or xutils.json_pack(value)) or tostring(value)
|
|
173
182
|
result({ content = array({ { type = 'text', text = text.valid(output) } }), isError = not ok })
|
|
174
183
|
end)
|
|
175
184
|
p.job = job
|