@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
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
-- Ordinary binding evidence belongs to one query, never to the resident index.
|
|
2
|
+
local M = {}
|
|
3
|
+
local control = require('codeoutline.control')
|
|
4
|
+
local function js(rec) return rec.language == 'javascript' or rec.language == 'typescript' end
|
|
5
|
+
local function evidence(rec, i)
|
|
6
|
+
local refs = rec.refs
|
|
7
|
+
return refs.binding and refs.binding[i] or refs.lua_binding and refs.lua_binding[i]
|
|
8
|
+
or refs.js_receiver and refs.js_receiver[i]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
function M.context(G, ctx, source, options)
|
|
12
|
+
options = options or {}
|
|
13
|
+
local cache, order, bytes = {}, {}, 0
|
|
14
|
+
local max_files, max_bytes = options.max_files or 16, options.max_bytes or 8 * 1024 * 1024
|
|
15
|
+
local C = { stats = { parses = 0, files = 0, bytes = 0 } }
|
|
16
|
+
local hints = {}
|
|
17
|
+
local function alias_hints(f)
|
|
18
|
+
if hints[f] and hints[f].record == G.files[f] then return hints[f].roots end
|
|
19
|
+
local result = {}
|
|
20
|
+
for _, name in ipairs(G.files[f].dispatch_calls or {}) do
|
|
21
|
+
if name:sub(1, 1) == '@' then result[name:sub(2):match('^([%w_$]+)')] = true end
|
|
22
|
+
end
|
|
23
|
+
hints[f] = { record = G.files[f], roots = result }
|
|
24
|
+
return result
|
|
25
|
+
end
|
|
26
|
+
function C.data(f)
|
|
27
|
+
local rec = G.files[f]
|
|
28
|
+
if rec.ordinary_bindings then return rec end -- explicit in-memory parse
|
|
29
|
+
if cache[f] and cache[f].record == rec then return cache[f].data end
|
|
30
|
+
if not G.root and not source then return rec end
|
|
31
|
+
control.check()
|
|
32
|
+
local raw = source and source(f)
|
|
33
|
+
if not raw then
|
|
34
|
+
local file = assert(io.open(G.root .. '/' .. rec.path, 'rb'), 'cannot read binding source: ' .. rec.path)
|
|
35
|
+
raw = file:read('a'); file:close()
|
|
36
|
+
local crc = xcompress and xcompress.crc32 and xcompress.crc32(raw) or xutils.sha256_hex(raw)
|
|
37
|
+
assert(#raw == rec.size and crc == rec.crc, 'source changed during query; retry: ' .. rec.path)
|
|
38
|
+
if rec.encoding ~= 'utf-8' and rec.encoding ~= 'utf-8-bom' then
|
|
39
|
+
raw = assert(xutils.to_utf8(raw, rec.encoding == 'gbk' and 'gbk' or 'auto'))
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
local parsed = assert(require('codeoutline.parse').parse(rec.path, raw,
|
|
43
|
+
{ ranges = false, ordinary_bindings = true }))
|
|
44
|
+
local function check(ok) assert(ok, 'source changed; refresh index before binding query: ' .. rec.path) end
|
|
45
|
+
check(#parsed.nodes == #rec.nodes and #parsed.refs.name == #rec.refs.name)
|
|
46
|
+
for i, n in ipairs(parsed.nodes) do
|
|
47
|
+
check(n.qualified == rec.nodes[i].qualified and n.line == rec.nodes[i].line)
|
|
48
|
+
end
|
|
49
|
+
for i, name in ipairs(parsed.refs.name) do
|
|
50
|
+
check(name == rec.refs.name[i] and parsed.refs.from[i] == rec.refs.from[i]
|
|
51
|
+
and parsed.refs.line[i] == rec.refs.line[i] and parsed.refs.kind[i] == rec.refs.kind[i])
|
|
52
|
+
end
|
|
53
|
+
-- Keep only compact query evidence; source, tokens, and duplicate symbols die here.
|
|
54
|
+
local data = { refs = { binding = parsed.refs.binding, js_receiver = parsed.refs.js_receiver },
|
|
55
|
+
js_exports = parsed.js_exports, python_imports = parsed.python_imports, cpp_aliases = parsed.cpp_aliases,
|
|
56
|
+
csharp_usings = parsed.csharp_usings, csharp_statics = parsed.csharp_statics,
|
|
57
|
+
csharp_scopes = parsed.csharp_scopes, csharp_parameters = parsed.csharp_parameters, csharp_access = parsed.csharp_access }
|
|
58
|
+
local weight = #raw + #parsed.refs.name * 128 + #(parsed.js_exports or {}) * 256
|
|
59
|
+
+ #(parsed.python_imports or {}) * 256 + #(parsed.cpp_aliases or {}) * 256
|
|
60
|
+
+ #(parsed.csharp_usings or {}) * 256 + #(parsed.csharp_scopes or {}) * 256
|
|
61
|
+
+ #(parsed.csharp_parameters or {}) * 128 + #parsed.nodes * (rec.language == 'csharp' and 32 or 0)
|
|
62
|
+
if cache[f] then
|
|
63
|
+
bytes = bytes - cache[f].weight; cache[f] = nil
|
|
64
|
+
for i, old in ipairs(order) do if old == f then table.remove(order, i); break end end
|
|
65
|
+
end
|
|
66
|
+
while #order > 0 and (#order >= max_files or bytes + weight > max_bytes) do
|
|
67
|
+
local old = table.remove(order, 1); bytes = bytes - cache[old].weight; cache[old] = nil
|
|
68
|
+
end
|
|
69
|
+
if weight <= max_bytes then
|
|
70
|
+
cache[f] = { record = rec, data = data, weight = weight }
|
|
71
|
+
order[#order + 1] = f; bytes = bytes + weight
|
|
72
|
+
end
|
|
73
|
+
C.stats.parses, C.stats.files, C.stats.bytes = C.stats.parses + 1, #order, bytes
|
|
74
|
+
return data
|
|
75
|
+
end
|
|
76
|
+
function C.evidence(f, i)
|
|
77
|
+
local rec = G.files[f]
|
|
78
|
+
local value = evidence(rec, i)
|
|
79
|
+
local kind = rec.refs.kind[i]
|
|
80
|
+
if kind ~= 'call' and kind ~= 'member_call' and not (rec.language == 'csharp' and kind == 'new') then return value end
|
|
81
|
+
-- JS already keeps receiver barriers. Only a bare blocked call can be
|
|
82
|
+
-- a newly supported lexical alias; typed/member calls need no reparse.
|
|
83
|
+
if js(rec) and (kind ~= 'call' or value ~= '?') then return value end
|
|
84
|
+
if js(rec) and not next(alias_hints(f)) then return value end
|
|
85
|
+
local recv = rec.refs.recv[i]
|
|
86
|
+
if rec.language == 'python' and (recv == 'self' or recv == 'cls') then return value end
|
|
87
|
+
if rec.language == 'cpp' and (not recv or recv == 'this') then return value end
|
|
88
|
+
if js(rec) or rec.language == 'python' or rec.language == 'csharp' or rec.language == 'cpp' and kind == 'member_call' then
|
|
89
|
+
return evidence(C.data(f), i) or value
|
|
90
|
+
end
|
|
91
|
+
return value
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
-- Renamed calls can only originate in the target file or an import chain
|
|
95
|
+
-- leading to it. Build this cheap reverse map from existing import records.
|
|
96
|
+
-- No source is opened while constructing the candidate set.
|
|
97
|
+
local reverse
|
|
98
|
+
function C.candidates(wanted)
|
|
99
|
+
local found, queue = {}, {}
|
|
100
|
+
for id in pairs(wanted) do
|
|
101
|
+
local f = G.node_file[id]
|
|
102
|
+
if (js(G.files[f]) or G.files[f].language == 'python') and not found[f] then
|
|
103
|
+
found[f] = true; queue[#queue + 1] = f
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
if #queue == 0 then return found end
|
|
107
|
+
if not reverse then
|
|
108
|
+
reverse = {}
|
|
109
|
+
local function add(f, target)
|
|
110
|
+
if target then reverse[target] = reverse[target] or {}; reverse[target][f] = true end
|
|
111
|
+
end
|
|
112
|
+
local files = {}
|
|
113
|
+
for _, f in ipairs(G.file_order) do files[#files + 1] = f end
|
|
114
|
+
for _, f in ipairs(G.new_files or {}) do files[#files + 1] = f end
|
|
115
|
+
for _, f in ipairs(files) do
|
|
116
|
+
local rec = G.files[f]
|
|
117
|
+
control.check()
|
|
118
|
+
if js(rec) then
|
|
119
|
+
for _, imp in ipairs(rec.imports) do add(f, require('codeoutline.js_resolution').module(G, f, imp.path, ctx)) end
|
|
120
|
+
elseif rec.language == 'python' then
|
|
121
|
+
local module = require('codeoutline.python_bindings').module
|
|
122
|
+
for _, imp in ipairs(rec.imports) do
|
|
123
|
+
add(f, module(G, f, imp.path))
|
|
124
|
+
for _, name in ipairs(imp.names or {}) do
|
|
125
|
+
local path = imp.path
|
|
126
|
+
if path:sub(-1) ~= '.' then path = path .. '.' end
|
|
127
|
+
add(f, module(G, f, path .. name))
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
local at = 1
|
|
134
|
+
while at <= #queue do
|
|
135
|
+
control.check()
|
|
136
|
+
for f in pairs(reverse[queue[at]] or {}) do
|
|
137
|
+
if not found[f] then found[f] = true; queue[#queue + 1] = f end
|
|
138
|
+
end
|
|
139
|
+
at = at + 1
|
|
140
|
+
end
|
|
141
|
+
return found
|
|
142
|
+
end
|
|
143
|
+
function C.aliases(f, wanted, candidates)
|
|
144
|
+
if G.files[f].language == 'csharp' then
|
|
145
|
+
return require('codeoutline.csharp_bindings').incoming_aliases(G, f, wanted, ctx)
|
|
146
|
+
end
|
|
147
|
+
if not candidates[f] then return {} end
|
|
148
|
+
local rec, hits, resolved = G.files[f], {}, {}
|
|
149
|
+
local imports = {}
|
|
150
|
+
local relevant = false
|
|
151
|
+
for id in pairs(wanted) do if G.node_file[id] == f then relevant = true; break end end
|
|
152
|
+
local function matches(ids)
|
|
153
|
+
for _, id in ipairs(ids) do if wanted[id] then return true end end
|
|
154
|
+
return false
|
|
155
|
+
end
|
|
156
|
+
if js(rec) then
|
|
157
|
+
local resolver = require('codeoutline.js_resolution')
|
|
158
|
+
for _, imp in ipairs(rec.js_imports or {}) do
|
|
159
|
+
local target = resolver.module(G, f, imp.path, ctx)
|
|
160
|
+
if target and candidates[target] and (imp.imported == '*'
|
|
161
|
+
or matches(resolver.imported(G, f, imp.path, imp.imported, ctx))) then
|
|
162
|
+
relevant = true; imports[imp.local_name] = true
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
elseif not relevant and rec.language == 'python' then
|
|
166
|
+
local resolver = require('codeoutline.python_bindings')
|
|
167
|
+
for _, imp in ipairs(rec.imports) do
|
|
168
|
+
local target = resolver.module(G, f, imp.path)
|
|
169
|
+
if target and candidates[target] then
|
|
170
|
+
if not imp.names then relevant = true; break end
|
|
171
|
+
for _, name in ipairs(imp.names) do
|
|
172
|
+
if matches(resolver.imported(G, f, imp.path, name, ctx)) then relevant = true; break end
|
|
173
|
+
local path = imp.path .. (imp.path:sub(-1) == '.' and '' or '.') .. name
|
|
174
|
+
local sub = resolver.module(G, f, path)
|
|
175
|
+
if sub and candidates[sub] then relevant = true; break end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
if relevant then break end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
if not relevant then return hits end
|
|
182
|
+
local analyze = not js(rec)
|
|
183
|
+
if js(rec) then
|
|
184
|
+
local roots = alias_hints(f)
|
|
185
|
+
for id in pairs(wanted) do
|
|
186
|
+
if G.node_file[id] == f and roots[G.nodes[id].name] then analyze = true; break end
|
|
187
|
+
end
|
|
188
|
+
for name in pairs(imports) do if roots[name] then analyze = true; break end end
|
|
189
|
+
end
|
|
190
|
+
for i, kind in ipairs(rec.refs.kind) do
|
|
191
|
+
if kind == 'call' or kind == 'member_call' or imports[rec.refs.name[i]] or imports[rec.refs.recv[i]] then
|
|
192
|
+
local value = analyze and C.evidence(f, i) or evidence(rec, i)
|
|
193
|
+
local path = value and (value:match('^import:(.-)%z') or value:match('^py:import:(.-)%z'))
|
|
194
|
+
if path then
|
|
195
|
+
local resolver = js(rec) and require('codeoutline.js_resolution') or require('codeoutline.python_bindings')
|
|
196
|
+
local target = resolver.module(G, f, path, ctx)
|
|
197
|
+
if not target or not candidates[target] then goto next_ref end
|
|
198
|
+
end
|
|
199
|
+
if value and (value:match('^node:') or value:match('^import:') or value:match('^py:'))
|
|
200
|
+
or imports[rec.refs.name[i]] or imports[rec.refs.recv[i]] then
|
|
201
|
+
if value == '' then value = nil end
|
|
202
|
+
local key = value or rec.refs.name[i] .. '\0' .. (rec.refs.recv[i] or '')
|
|
203
|
+
local targets = resolved[key]
|
|
204
|
+
if not targets then
|
|
205
|
+
if js(rec) then targets = require('codeoutline.js_resolution').resolve(G, f,
|
|
206
|
+
rec.refs.name[i], kind, rec.refs.recv[i], value, ctx)
|
|
207
|
+
else targets = require('codeoutline.python_bindings').resolve(G, f, value, ctx) end
|
|
208
|
+
targets = targets or {}; resolved[key] = targets
|
|
209
|
+
end
|
|
210
|
+
for _, id in ipairs(targets) do if wanted[id] then hits[i] = true; break end end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
::next_ref::
|
|
214
|
+
end
|
|
215
|
+
return hits
|
|
216
|
+
end
|
|
217
|
+
return C
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
return M
|
|
@@ -49,6 +49,8 @@ function M.parse_tokens(path, src, opts)
|
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
-- opts.ranges=false emits compact index nodes; default parsing retains positions.
|
|
52
|
+
-- opts.dispatch_only=true is transient query data, not an index record: it
|
|
53
|
+
-- retains parser nodes/refs and skips binding evidence and record packing.
|
|
52
54
|
function M.parse(path, src, opts)
|
|
53
55
|
return (M.parse_tokens(path, src, opts))
|
|
54
56
|
end
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
-- Explicit Python imports and lexical barriers, shared by graph and navigation.
|
|
2
|
+
-- No sys.path execution or project-wide suffix guesses.
|
|
3
|
+
local M = {}
|
|
4
|
+
local aliases = require('codeoutline.dispatch_aliases')
|
|
5
|
+
local control = require('codeoutline.control')
|
|
6
|
+
|
|
7
|
+
function M.collect(r, T)
|
|
8
|
+
if r.language ~= 'python' then return end
|
|
9
|
+
local imports = aliases.imports(r, T)
|
|
10
|
+
local expand = aliases.prepare(r, T)
|
|
11
|
+
r.python_imports = {}
|
|
12
|
+
for _, imp in ipairs(imports) do
|
|
13
|
+
local nested = false
|
|
14
|
+
for _, n in ipairs(r.nodes) do
|
|
15
|
+
if n.ti <= imp.token and imp.token <= n.tj then nested = true; break end
|
|
16
|
+
end
|
|
17
|
+
if not nested then
|
|
18
|
+
local value = expand({ symbol = imp.name }, T.n)
|
|
19
|
+
r.python_imports[#r.python_imports + 1] = {
|
|
20
|
+
name = imp.name, path = imp.path, imported = imp.imported,
|
|
21
|
+
blocked = not value or not value.import or nil,
|
|
22
|
+
}
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
for i, ref in ipairs(r.refs) do
|
|
26
|
+
if i % 128 == 0 then control.check() end
|
|
27
|
+
if (ref.kind == 'call' or ref.kind == 'member_call') and ref.recv ~= 'self' and ref.recv ~= 'cls' then
|
|
28
|
+
local symbol, first = ref.name, ref.tok
|
|
29
|
+
while T:text(first - 1) == '.' and T.k[first - 2] == 'id' do
|
|
30
|
+
symbol = T:text(first - 2) .. '.' .. symbol; first = first - 2
|
|
31
|
+
end
|
|
32
|
+
local value = expand({ symbol = symbol }, ref.tok)
|
|
33
|
+
if value == false or ref.kind == 'member_call' and first == ref.tok then ref.binding = 'py:?'
|
|
34
|
+
elseif value and value.import then
|
|
35
|
+
ref.binding = 'py:import:' .. value.import.path .. '\0' .. value.member
|
|
36
|
+
elseif value and value.node and not ref.recv then ref.binding = 'py:node:' .. value.node end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
local function directory(path) return path:match('^(.*)/[^/]*$') or '' end
|
|
42
|
+
local function module(G, f, path)
|
|
43
|
+
local dots = path:match('^(%.+)')
|
|
44
|
+
local base = ''
|
|
45
|
+
if dots then
|
|
46
|
+
base = directory(G.files[f].path)
|
|
47
|
+
for _ = 2, #dots do
|
|
48
|
+
if base == '' then return nil end
|
|
49
|
+
base = directory(base)
|
|
50
|
+
end
|
|
51
|
+
path = path:sub(#dots + 1)
|
|
52
|
+
end
|
|
53
|
+
local rel = (base ~= '' and base .. '/' or '') .. path:gsub('%.', '/')
|
|
54
|
+
rel = rel:gsub('/$', '')
|
|
55
|
+
for _, candidate in ipairs({ rel .. '.py', rel .. '/__init__.py' }) do
|
|
56
|
+
local id = G.file_index[candidate]
|
|
57
|
+
if id then return id end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
local function exported(G, f, member, seen, depth, ctx)
|
|
62
|
+
control.check()
|
|
63
|
+
if depth > 8 then return {} end
|
|
64
|
+
local key = f .. '\0' .. member
|
|
65
|
+
if seen[key] then return {} end
|
|
66
|
+
seen[key] = true
|
|
67
|
+
local result = {}
|
|
68
|
+
for _, id in ipairs(G.id_of[f]) do
|
|
69
|
+
local node = G.nodes[id]
|
|
70
|
+
if not node.parent and node.name == member then result[#result + 1] = id end
|
|
71
|
+
end
|
|
72
|
+
if #result > 0 then return result end
|
|
73
|
+
local head, tail = member:match('^([^.]+)%.(.*)$')
|
|
74
|
+
head = head or member
|
|
75
|
+
local rec = ctx and ctx.ordinary and ctx.ordinary.data(f) or G.files[f]
|
|
76
|
+
for _, imp in ipairs(rec.python_imports or {}) do
|
|
77
|
+
if imp.name == head then
|
|
78
|
+
if imp.blocked then return {} end
|
|
79
|
+
local target = module(G, f, imp.path)
|
|
80
|
+
local want = imp.imported == '*' and (tail or '') or imp.imported .. (tail and '.' .. tail or '')
|
|
81
|
+
if target then
|
|
82
|
+
for _, id in ipairs(exported(G, target, want, seen, depth + 1, ctx)) do result[#result + 1] = id end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
-- A package may expose a submodule without re-exporting its declarations.
|
|
87
|
+
if head and tail and G.files[f].path:match('/__init__%.py$') then
|
|
88
|
+
local sub = G.file_index[directory(G.files[f].path) .. '/' .. head .. '.py']
|
|
89
|
+
or G.file_index[directory(G.files[f].path) .. '/' .. head .. '/__init__.py']
|
|
90
|
+
if sub then
|
|
91
|
+
for _, id in ipairs(exported(G, sub, tail, seen, depth + 1, ctx)) do result[#result + 1] = id end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
return result
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
function M.resolve(G, f, evidence, ctx)
|
|
98
|
+
if G.files[f].language ~= 'python' or not evidence or evidence:sub(1, 3) ~= 'py:' then return nil end
|
|
99
|
+
local node = evidence:match('^py:node:(%d+)$')
|
|
100
|
+
if node then return { G.id_of[f][tonumber(node)] } end
|
|
101
|
+
local path, member = evidence:match('^py:import:(.-)%z(.*)$')
|
|
102
|
+
if not path then return {} end
|
|
103
|
+
local target = module(G, f, path)
|
|
104
|
+
return target and exported(G, target, member, {}, 0, ctx) or {}
|
|
105
|
+
end
|
|
106
|
+
M.module = module
|
|
107
|
+
function M.imported(G, f, path, name, ctx)
|
|
108
|
+
local target = module(G, f, path)
|
|
109
|
+
return target and exported(G, target, name, {}, 0, ctx) or {}
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
return M
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
-- Bounded, query-owned explanations; never stored on index records.
|
|
2
|
+
local M = { MAX_PATH_BREAKPOINTS = 4 }
|
|
3
|
+
|
|
4
|
+
function M.new(limit)
|
|
5
|
+
local report = { entries = {}, truncated = false, scope = 'indexed references and configured dispatch rules' }
|
|
6
|
+
local seen = {}
|
|
7
|
+
function report:add(entry)
|
|
8
|
+
local key = table.concat({ entry.source or '', entry.target or '', entry.line or '',
|
|
9
|
+
entry.kind or '', entry.reason or '', entry.rule or '', entry.call or '' }, ':')
|
|
10
|
+
if seen[key] then return end
|
|
11
|
+
if #self.entries >= (limit or 64) then self.truncated = true; return end
|
|
12
|
+
seen[key] = true
|
|
13
|
+
self.entries[#self.entries + 1] = entry
|
|
14
|
+
end
|
|
15
|
+
return report
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
-- Capture only evidence already produced by a failed BFS. The predecessor
|
|
19
|
+
-- table is not retained, and this never loads edges or parses another file.
|
|
20
|
+
function M.capture_paths(report, prev, root)
|
|
21
|
+
local result = {}
|
|
22
|
+
for _, entry in ipairs(report.entries) do
|
|
23
|
+
if entry.reason and not entry.target and prev[entry.source] then
|
|
24
|
+
local path, id = { entry.source }, entry.source
|
|
25
|
+
while id ~= root do
|
|
26
|
+
id = prev[id]
|
|
27
|
+
table.insert(path, 1, id)
|
|
28
|
+
end
|
|
29
|
+
result[#result + 1] = { evidence = entry, path = path }
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
return result
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
function M.path_breakpoints(report, G, searches, connected)
|
|
36
|
+
local candidates = {}
|
|
37
|
+
for _, search in ipairs(searches) do
|
|
38
|
+
local goal
|
|
39
|
+
for _, target in ipairs(search.targets) do
|
|
40
|
+
-- A reverse path already explains how these two symbols connect.
|
|
41
|
+
if not (connected[target] and connected[target][search.root]) then goal = target; break end
|
|
42
|
+
end
|
|
43
|
+
if goal or search.single then
|
|
44
|
+
for _, site in ipairs(search.sites) do
|
|
45
|
+
local e = site.evidence
|
|
46
|
+
local priority = e.reason:find('dynamic', 1, true) and 3
|
|
47
|
+
or e.reason == 'no indexed target' and 1 or 2
|
|
48
|
+
candidates[#candidates + 1] = { status = 'unresolved', from = search.root, goal = goal,
|
|
49
|
+
path = site.path, source = e.source, line = e.line, call = e.call, reason = e.reason,
|
|
50
|
+
rule = e.rule, argument = e.argument, evidence = e, priority = priority }
|
|
51
|
+
end
|
|
52
|
+
if search.limited or search.incomplete then
|
|
53
|
+
candidates[#candidates + 1] = { status = 'search_limit', from = search.root, goal = goal,
|
|
54
|
+
path = { search.root }, reason = search.incomplete and 'relationship expansion limit reached'
|
|
55
|
+
or 'path depth/frontier limit reached', priority = 0 }
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
table.sort(candidates, function(a, b)
|
|
60
|
+
if a.priority ~= b.priority then return a.priority > b.priority end
|
|
61
|
+
if #a.path ~= #b.path then return #a.path > #b.path end
|
|
62
|
+
if a.from ~= b.from then return require('codeoutline.graph').before(G, a.from, b.from) end
|
|
63
|
+
if (a.line or 0) ~= (b.line or 0) then return (a.line or 0) < (b.line or 0) end
|
|
64
|
+
return (a.call or '') < (b.call or '')
|
|
65
|
+
end)
|
|
66
|
+
local result, seen = { entries = {}, truncated = report.truncated }, {}
|
|
67
|
+
for _, entry in ipairs(candidates) do
|
|
68
|
+
local key = entry.evidence and (entry.source .. ':' .. (entry.line or 0) .. ':' .. (entry.call or ''))
|
|
69
|
+
or ('limit:' .. math.min(entry.from, entry.goal or entry.from)
|
|
70
|
+
.. ':' .. math.max(entry.from, entry.goal or entry.from))
|
|
71
|
+
if not seen[key] then
|
|
72
|
+
seen[key] = true
|
|
73
|
+
if #result.entries >= M.MAX_PATH_BREAKPOINTS then result.truncated = true
|
|
74
|
+
else
|
|
75
|
+
entry.evidence, entry.priority = nil, nil
|
|
76
|
+
result.entries[#result.entries + 1] = entry
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
return result
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
function M.render_breakpoints(report, G)
|
|
84
|
+
if #report.entries == 0 then return nil end
|
|
85
|
+
local graph = require('codeoutline.graph')
|
|
86
|
+
local function label(id) return G.nodes[id].qualified .. ' (' .. graph.where(G, id) .. ')' end
|
|
87
|
+
local lines = { '\n## Call-path breakpoints',
|
|
88
|
+
'Unresolved sites on explored indexed prefixes; these are not proven causes of disconnection.' }
|
|
89
|
+
for _, entry in ipairs(report.entries) do
|
|
90
|
+
local destination = entry.goal and (' toward ' .. label(entry.goal)) or ''
|
|
91
|
+
if entry.status == 'search_limit' then
|
|
92
|
+
lines[#lines + 1] = '- Search from ' .. label(entry.from) .. destination .. ': ' .. entry.reason
|
|
93
|
+
.. '; continuation was not checked completely.'
|
|
94
|
+
else
|
|
95
|
+
local path = {}
|
|
96
|
+
for _, id in ipairs(entry.path) do path[#path + 1] = label(id) end
|
|
97
|
+
local file = G.files[G.node_file[entry.source]].path
|
|
98
|
+
local reason = entry.reason
|
|
99
|
+
if entry.rule then reason = reason .. string.format(' (rule #%d, argument %d)', entry.rule, entry.argument) end
|
|
100
|
+
lines[#lines + 1] = string.format('- %s; unresolved at %s:%d via %s: %s; continuation%s is unverified.',
|
|
101
|
+
table.concat(path, ' -> '), file, entry.line or 0, entry.call or '?', reason, destination)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
if report.truncated then lines[#lines + 1] = 'Additional sites or relationship evidence were omitted.' end
|
|
105
|
+
return table.concat(lines, '\n')
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
function M.render(report, G)
|
|
109
|
+
local lines = { '\n## Relationship evidence / unresolved references',
|
|
110
|
+
'Scope: indexed references and configured dispatch rules; unresolved does not mean no runtime call.' }
|
|
111
|
+
for _, e in ipairs(report.entries) do
|
|
112
|
+
local source = G.nodes[e.source]
|
|
113
|
+
local path = source and G.files[G.node_file[e.source]].path or '?'
|
|
114
|
+
local target = e.target and G.nodes[e.target].qualified
|
|
115
|
+
local detail = e.reason or e.kind
|
|
116
|
+
if e.call then detail = detail .. ' via ' .. e.call end
|
|
117
|
+
if e.rule then detail = detail .. ' (rule #' .. e.rule .. ', argument ' .. e.argument .. ')' end
|
|
118
|
+
if e.registered_at then detail = detail .. '; registered at ' .. e.registered_at end
|
|
119
|
+
lines[#lines + 1] = string.format('- %s:%d %s%s: %s', path, e.line or 0,
|
|
120
|
+
source and source.qualified or '?', target and ' -> ' .. target or '', detail)
|
|
121
|
+
end
|
|
122
|
+
if report.truncated then lines[#lines + 1] = 'Evidence limit reached; additional explanations omitted.' end
|
|
123
|
+
return table.concat(lines, '\n')
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
return M
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
-- One project-local contract, two consumers. Legacy arrays keep their scope.
|
|
2
|
+
local M = {}
|
|
3
|
+
local function copy(value)
|
|
4
|
+
local result = {}; for k, v in pairs(value) do result[k] = v end; return result
|
|
5
|
+
end
|
|
6
|
+
local function array(value, label)
|
|
7
|
+
if value == nil then value = {} end
|
|
8
|
+
assert(type(value) == 'table' and #value <= 32, label .. ' must contain at most 32 rules')
|
|
9
|
+
local count = 0
|
|
10
|
+
for k in pairs(value) do
|
|
11
|
+
assert(type(k) == 'number' and k >= 1 and k <= #value and k % 1 == 0, label .. ' must be an array')
|
|
12
|
+
count = count + 1
|
|
13
|
+
end
|
|
14
|
+
assert(count == #value, label .. ' must be an array')
|
|
15
|
+
return value
|
|
16
|
+
end
|
|
17
|
+
local function position(r, key, label)
|
|
18
|
+
local n = r[key]
|
|
19
|
+
assert(type(n) == 'number' and n >= 1 and n <= 32 and n % 1 == 0,
|
|
20
|
+
label .. '.' .. key .. ' must be an integer from 1 to 32')
|
|
21
|
+
end
|
|
22
|
+
local function call(r, key, label)
|
|
23
|
+
assert(type(r[key]) == 'string' and r[key]:match('^[%a_][%w_%.:>%-]*$'), 'invalid ' .. label .. '.' .. key)
|
|
24
|
+
end
|
|
25
|
+
local function qualifiers(r, label)
|
|
26
|
+
if r.qualifiers == nil then return nil end
|
|
27
|
+
local result = {}
|
|
28
|
+
assert(type(r.qualifiers) == 'table', label .. '.qualifiers must be an array of names')
|
|
29
|
+
local count = 0
|
|
30
|
+
for k, q in pairs(r.qualifiers) do
|
|
31
|
+
assert(type(k) == 'number' and k >= 1 and k <= #r.qualifiers and k % 1 == 0,
|
|
32
|
+
label .. '.qualifiers must be an array of names')
|
|
33
|
+
assert(type(q) == 'string' and q:match('^[%a_][%w_]*$'), label .. '.qualifiers must contain plain names')
|
|
34
|
+
result[q], count = true, count + 1
|
|
35
|
+
end
|
|
36
|
+
assert(count == #r.qualifiers, label .. '.qualifiers must be an array of names')
|
|
37
|
+
return result
|
|
38
|
+
end
|
|
39
|
+
local function dispatch(r, label)
|
|
40
|
+
assert(type(r) == 'table', 'invalid ' .. label .. ' rule')
|
|
41
|
+
call(r, 'call', label); position(r, 'argument', label)
|
|
42
|
+
assert(r.language == nil or type(r.language) == 'string', 'invalid ' .. label .. '.language')
|
|
43
|
+
assert(r.kind == 'target' or r.kind == 'callback' or r.kind == 'event', 'invalid ' .. label .. '.kind')
|
|
44
|
+
if r.serviceArgument ~= nil then
|
|
45
|
+
assert(r.kind == 'target', 'serviceArgument requires a target rule')
|
|
46
|
+
position(r, 'serviceArgument', label)
|
|
47
|
+
end
|
|
48
|
+
if r.kind == 'event' then
|
|
49
|
+
call(r, 'register', label); position(r, 'keyArgument', label); position(r, 'handlerArgument', label)
|
|
50
|
+
end
|
|
51
|
+
local result = copy(r)
|
|
52
|
+
-- Declaration qualifiers restrict editor candidates, not graph inference.
|
|
53
|
+
result.qualifiers = nil
|
|
54
|
+
return result
|
|
55
|
+
end
|
|
56
|
+
local function definition(r, label)
|
|
57
|
+
assert(type(r) == 'table', 'invalid ' .. label .. ' rule')
|
|
58
|
+
assert(r.language == nil or r.language == 'lua', label .. ' currently support language lua')
|
|
59
|
+
call(r, 'call', label); position(r, 'argument', label)
|
|
60
|
+
local result = copy(r)
|
|
61
|
+
result.targetKind = r.targetKind or 'call'
|
|
62
|
+
assert(result.targetKind == 'call' or result.targetKind == 'declaration' or result.targetKind == 'key',
|
|
63
|
+
"invalid " .. label .. '.targetKind')
|
|
64
|
+
if result.targetKind == 'call' then
|
|
65
|
+
call(r, 'target', label); position(r, 'targetArgument', label)
|
|
66
|
+
else
|
|
67
|
+
assert(r.target == nil and r.targetArgument == nil, 'declaration and key rules must not set target or targetArgument')
|
|
68
|
+
result.qualifiers = qualifiers(r, label)
|
|
69
|
+
end
|
|
70
|
+
return result
|
|
71
|
+
end
|
|
72
|
+
function M.read(root)
|
|
73
|
+
local f = root and io.open(root .. '/.codeoutline.json', 'rb')
|
|
74
|
+
if not f then return {} end
|
|
75
|
+
local raw = f:read(65537); f:close()
|
|
76
|
+
assert(#raw <= 65536, '.codeoutline.json exceeds 64 KiB')
|
|
77
|
+
local ok, config = pcall(xutils.json_unpack, raw)
|
|
78
|
+
assert(ok and type(config) == 'table', 'invalid .codeoutline.json')
|
|
79
|
+
return config
|
|
80
|
+
end
|
|
81
|
+
function M.normalize(config)
|
|
82
|
+
local result = { definitions = {}, dispatch = {} }
|
|
83
|
+
local seen = { definitions = {}, dispatch = {} }
|
|
84
|
+
local function add(consumer, r)
|
|
85
|
+
local fields = consumer == 'dispatch' and { 'kind', 'language', 'call', 'argument', 'register', 'keyArgument', 'handlerArgument', 'serviceArgument' }
|
|
86
|
+
or { 'language', 'call', 'argument', 'targetKind', 'target', 'targetArgument', 'serviceArgument' }
|
|
87
|
+
local key = {}
|
|
88
|
+
for _, f in ipairs(fields) do key[#key + 1] = tostring(r[f]) end
|
|
89
|
+
if r.qualifiers then
|
|
90
|
+
local q = {}; for name in pairs(r.qualifiers) do q[#q + 1] = name end; table.sort(q)
|
|
91
|
+
key[#key + 1] = table.concat(q, ',')
|
|
92
|
+
end
|
|
93
|
+
key = table.concat(key, '\0')
|
|
94
|
+
if not seen[consumer][key] then
|
|
95
|
+
seen[consumer][key] = r
|
|
96
|
+
result[consumer][#result[consumer] + 1] = r
|
|
97
|
+
assert(#result[consumer] <= 32, 'combined ' .. consumer .. ' rules must contain at most 32 rules')
|
|
98
|
+
elseif consumer == 'definitions' and not r.shared then
|
|
99
|
+
-- Keep the legacy literal/method matcher when an equivalent shared
|
|
100
|
+
-- contract is also configured; their alias semantics differ.
|
|
101
|
+
seen[consumer][key].legacy = true
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
for _, r in ipairs(array(config.rules, 'rules')) do
|
|
105
|
+
assert(type(r) == 'table', 'invalid rules rule')
|
|
106
|
+
if r.kind == 'definition' then add('definitions', definition(r, 'rules'))
|
|
107
|
+
else
|
|
108
|
+
local d = dispatch(r, 'rules')
|
|
109
|
+
add('dispatch', d)
|
|
110
|
+
local n = copy(d); n.shared = true
|
|
111
|
+
if d.kind == 'event' then n.targetKind, n.target, n.targetArgument = 'call', d.register, d.keyArgument
|
|
112
|
+
elseif d.kind == 'target' then n.targetKind, n.qualifiers = 'declaration', qualifiers(r, 'rules')
|
|
113
|
+
else n.targetKind = 'callback' end
|
|
114
|
+
add('definitions', n)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
for _, r in ipairs(array(config.definitionRules, 'definitionRules')) do add('definitions', definition(r, 'definitionRules')) end
|
|
118
|
+
for _, r in ipairs(array(config.dispatchRules, 'dispatchRules')) do add('dispatch', dispatch(r, 'dispatchRules')) end
|
|
119
|
+
return result
|
|
120
|
+
end
|
|
121
|
+
function M.load(root) return M.normalize(M.read(root)) end
|
|
122
|
+
function M.dispatch(rules)
|
|
123
|
+
local result = {}
|
|
124
|
+
for _, r in ipairs(array(rules, 'dispatchRules')) do result[#result + 1] = dispatch(r, 'dispatchRules') end
|
|
125
|
+
return result
|
|
126
|
+
end
|
|
127
|
+
return M
|
|
@@ -144,14 +144,14 @@ function M.resident(root)
|
|
|
144
144
|
return p.idx, p.idx.symbol_tables
|
|
145
145
|
end
|
|
146
146
|
|
|
147
|
-
function
|
|
148
|
-
|
|
147
|
+
local function run_query(engine, root, query, opts)
|
|
148
|
+
engine.validate(query, opts)
|
|
149
149
|
local idx, _, stats = refresh(root, opts, true)
|
|
150
150
|
idx.query_sources = {}
|
|
151
151
|
local ok, result = pcall(function()
|
|
152
152
|
while true do
|
|
153
153
|
-- A GBK repair commits the decoded file, which renumbers its nodes.
|
|
154
|
-
local output, details =
|
|
154
|
+
local output, details = engine.run(idx:symbols(), idx, query, opts)
|
|
155
155
|
control.check()
|
|
156
156
|
if not details.encoding_retry then return { text = output, info = details } end
|
|
157
157
|
end
|
|
@@ -166,6 +166,14 @@ function M.explore(root, query, opts)
|
|
|
166
166
|
return result.text, result.info
|
|
167
167
|
end
|
|
168
168
|
|
|
169
|
+
function M.explore(root, query, opts)
|
|
170
|
+
return run_query(explore, root, query, opts)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
function M.callers(root, query, opts)
|
|
174
|
+
return run_query(require('codeoutline.callers'), root, query, opts)
|
|
175
|
+
end
|
|
176
|
+
|
|
169
177
|
function M.status(root, opts)
|
|
170
178
|
local idx, G, stats = M.get(root, opts)
|
|
171
179
|
local languages = {}
|