@codua/codeoutline-win32-x64 0.2.6 → 0.3.0
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 +205 -3
- package/README.zh-CN.md +40 -2
- package/bin/xnet.exe +0 -0
- package/build-info.json +1 -1
- package/package.json +1 -1
- package/scripts/codeoutline/affected.lua +145 -0
- package/scripts/codeoutline/automatic_events.lua +202 -0
- package/scripts/codeoutline/cache_repairs.lua +47 -0
- package/scripts/codeoutline/callers.lua +56 -16
- package/scripts/codeoutline/cargo_toml.lua +125 -0
- package/scripts/codeoutline/command.lua +26 -4
- package/scripts/codeoutline/common.lua +48 -7
- package/scripts/codeoutline/completion.lua +126 -22
- package/scripts/codeoutline/coverage.lua +63 -0
- package/scripts/codeoutline/cpp_bindings.lua +46 -11
- package/scripts/codeoutline/cpp_fragments.lua +143 -0
- package/scripts/codeoutline/cpp_implementations.lua +340 -0
- package/scripts/codeoutline/cpp_includes.lua +156 -0
- package/scripts/codeoutline/cpp_macros.lua +279 -0
- package/scripts/codeoutline/cpp_receivers.lua +342 -0
- package/scripts/codeoutline/cpp_types.lua +285 -29
- package/scripts/codeoutline/definition_candidates.lua +186 -0
- package/scripts/codeoutline/dispatch_aliases.lua +10 -5
- package/scripts/codeoutline/documents.lua +22 -9
- package/scripts/codeoutline/explore.lua +243 -122
- package/scripts/codeoutline/external_headers.lua +176 -0
- package/scripts/codeoutline/foreign.lua +23 -3
- package/scripts/codeoutline/function_values.lua +69 -0
- package/scripts/codeoutline/go_methods.lua +931 -0
- package/scripts/codeoutline/go_modules.lua +329 -0
- package/scripts/codeoutline/graph.lua +322 -67
- package/scripts/codeoutline/implementations.lua +403 -0
- package/scripts/codeoutline/index.lua +161 -28
- package/scripts/codeoutline/index_worker.lua +11 -4
- package/scripts/codeoutline/indirect.lua +4 -2
- package/scripts/codeoutline/js_evidence.lua +2 -1
- package/scripts/codeoutline/js_resolution.lua +40 -12
- package/scripts/codeoutline/js_workspace.lua +325 -0
- package/scripts/codeoutline/lang/c.lua +15 -2
- package/scripts/codeoutline/lang/cpp.lua +32 -18
- package/scripts/codeoutline/lang/csharp.lua +1 -1
- package/scripts/codeoutline/lang/js.lua +56 -20
- package/scripts/codeoutline/lang/rust.lua +9 -1
- package/scripts/codeoutline/language_detect.lua +100 -0
- package/scripts/codeoutline/lsp.lua +173 -23
- package/scripts/codeoutline/lua_bindings.lua +151 -47
- package/scripts/codeoutline/lua_callback_loops.lua +210 -0
- package/scripts/codeoutline/main.lua +32 -7
- package/scripts/codeoutline/mcp.lua +52 -9
- package/scripts/codeoutline/navigation.lua +351 -65
- package/scripts/codeoutline/on_demand.lua +93 -0
- package/scripts/codeoutline/ordinary_bindings.lua +210 -34
- package/scripts/codeoutline/parse.lua +18 -2
- package/scripts/codeoutline/parse_pool.lua +1 -1
- package/scripts/codeoutline/parse_worker.lua +1 -1
- package/scripts/codeoutline/python_bindings.lua +2 -2
- package/scripts/codeoutline/query_filters.lua +65 -0
- package/scripts/codeoutline/query_report.lua +56 -19
- package/scripts/codeoutline/resolution.lua +23 -0
- package/scripts/codeoutline/rust_packages.lua +203 -0
- package/scripts/codeoutline/rust_resolution.lua +301 -0
- package/scripts/codeoutline/search_ranking.lua +70 -0
- package/scripts/codeoutline/service.lua +47 -7
- package/scripts/codeoutline/shared_events.lua +369 -0
- package/scripts/codeoutline/static_dispatch.lua +46 -11
- package/scripts/codeoutline/text.lua +3 -2
- package/scripts/codeoutline/ts_implementations.lua +81 -0
- package/scripts/codeoutline/type_bases.lua +61 -0
- package/scripts/codeoutline/type_hierarchy.lua +324 -0
- package/scripts/codeoutline/type_references.lua +291 -0
- package/scripts/codeoutline/type_uses.lua +260 -0
- package/scripts/codeoutline/update_sequence.lua +1 -1
- package/scripts/codeoutline/version.lua +1 -1
|
@@ -1,8 +1,29 @@
|
|
|
1
1
|
-- Bounded, query-owned explanations; never stored on index records.
|
|
2
2
|
local M = { MAX_PATH_BREAKPOINTS = 4 }
|
|
3
|
+
function M.edge_label(kind)
|
|
4
|
+
if kind == 'ref' then return 'ref' end
|
|
5
|
+
if kind == 'implementation' then return 'possible implementation' end
|
|
6
|
+
if kind == 'automatic:event' then return 'possible event' end
|
|
7
|
+
if kind == 'call' or kind == 'member_call' or kind == 'new' then return nil end
|
|
8
|
+
return kind
|
|
9
|
+
end
|
|
10
|
+
local function renderer(G, initial)
|
|
11
|
+
local lines, references, size = {}, {}, 0
|
|
12
|
+
local function append(line, ids, registration)
|
|
13
|
+
line = require('codeoutline.text').valid(line)
|
|
14
|
+
local files, seen = {}, {}
|
|
15
|
+
local function add(f) if f and not seen[f] then seen[f] = true; files[#files + 1] = f end end
|
|
16
|
+
for _, id in ipairs(ids or {}) do add(G.node_file[id]) end
|
|
17
|
+
if registration then add(G.file_index[registration:match('^(.*):%d+$') or '']) end
|
|
18
|
+
if #files > 0 then references[#references + 1] = { at = size, files = files } end
|
|
19
|
+
lines[#lines + 1] = line; size = size + #line + 1
|
|
20
|
+
end
|
|
21
|
+
for _, line in ipairs(initial) do append(line) end
|
|
22
|
+
return append, function() return table.concat(lines, '\n'), references end
|
|
23
|
+
end
|
|
3
24
|
|
|
4
25
|
function M.new(limit)
|
|
5
|
-
local report = { entries = {}, truncated = false, scope = 'indexed references and configured dispatch rules' }
|
|
26
|
+
local report = { entries = {}, truncated = false, scope = 'indexed references, possible implementations, automatic callbacks and configured dispatch rules' }
|
|
6
27
|
local seen = {}
|
|
7
28
|
function report:add(entry)
|
|
8
29
|
local key = table.concat({ entry.source or '', entry.target or '', entry.line or '',
|
|
@@ -17,7 +38,7 @@ end
|
|
|
17
38
|
|
|
18
39
|
-- Capture only evidence already produced by a failed BFS. The predecessor
|
|
19
40
|
-- table is not retained, and this never loads edges or parses another file.
|
|
20
|
-
function M.capture_paths(report, prev, root)
|
|
41
|
+
function M.capture_paths(report, prev, root, via)
|
|
21
42
|
local result = {}
|
|
22
43
|
for _, entry in ipairs(report.entries) do
|
|
23
44
|
if entry.reason and not entry.target and prev[entry.source] then
|
|
@@ -26,7 +47,9 @@ function M.capture_paths(report, prev, root)
|
|
|
26
47
|
id = prev[id]
|
|
27
48
|
table.insert(path, 1, id)
|
|
28
49
|
end
|
|
29
|
-
|
|
50
|
+
local hops = {}
|
|
51
|
+
for i = 2, #path do hops[#hops + 1] = via and via[path[i]] or { kind = 'call' } end
|
|
52
|
+
result[#result + 1] = { evidence = entry, path = path, hops = hops }
|
|
30
53
|
end
|
|
31
54
|
end
|
|
32
55
|
return result
|
|
@@ -46,7 +69,7 @@ function M.path_breakpoints(report, G, searches, connected)
|
|
|
46
69
|
local priority = e.reason:find('dynamic', 1, true) and 3
|
|
47
70
|
or e.reason == 'no indexed target' and 1 or 2
|
|
48
71
|
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,
|
|
72
|
+
path = site.path, hops = site.hops, source = e.source, line = e.line, call = e.call, reason = e.reason,
|
|
50
73
|
rule = e.rule, argument = e.argument, evidence = e, priority = priority }
|
|
51
74
|
end
|
|
52
75
|
if search.limited or search.incomplete then
|
|
@@ -84,30 +107,44 @@ function M.render_breakpoints(report, G)
|
|
|
84
107
|
if #report.entries == 0 then return nil end
|
|
85
108
|
local graph = require('codeoutline.graph')
|
|
86
109
|
local function label(id) return G.nodes[id].qualified .. ' (' .. graph.where(G, id) .. ')' end
|
|
87
|
-
local
|
|
88
|
-
|
|
110
|
+
local special = false
|
|
111
|
+
for _, entry in ipairs(report.entries) do
|
|
112
|
+
for _, hop in ipairs(entry.hops or {}) do if M.edge_label(hop.kind) then special = true end end
|
|
113
|
+
end
|
|
114
|
+
local append, finish = renderer(G, { special and '\n## Relationship-path breakpoints' or '\n## Call-path breakpoints',
|
|
115
|
+
'Unresolved sites on explored indexed prefixes; these are not proven causes of disconnection.' })
|
|
89
116
|
for _, entry in ipairs(report.entries) do
|
|
90
117
|
local destination = entry.goal and (' toward ' .. label(entry.goal)) or ''
|
|
91
118
|
if entry.status == 'search_limit' then
|
|
92
|
-
|
|
93
|
-
.. '; continuation was not checked completely.'
|
|
119
|
+
append('- Search from ' .. label(entry.from) .. destination .. ': ' .. entry.reason
|
|
120
|
+
.. '; continuation was not checked completely.', { entry.from, entry.goal })
|
|
94
121
|
else
|
|
95
122
|
local path = {}
|
|
96
|
-
for
|
|
123
|
+
for i, id in ipairs(entry.path) do
|
|
124
|
+
if i > 1 then
|
|
125
|
+
local hop = entry.hops and entry.hops[i - 1]
|
|
126
|
+
local kind = M.edge_label(hop and hop.kind or 'call')
|
|
127
|
+
path[#path + 1] = kind and (' -[' .. kind .. ']-> ') or ' -> '
|
|
128
|
+
end
|
|
129
|
+
path[#path + 1] = label(id)
|
|
130
|
+
end
|
|
97
131
|
local file = G.files[G.node_file[entry.source]].path
|
|
98
132
|
local reason = entry.reason
|
|
99
133
|
if entry.rule then reason = reason .. string.format(' (rule #%d, argument %d)', entry.rule, entry.argument) end
|
|
100
|
-
|
|
101
|
-
|
|
134
|
+
local ids = {}; for _, id in ipairs(entry.path) do ids[#ids + 1] = id end
|
|
135
|
+
ids[#ids + 1] = entry.source
|
|
136
|
+
if entry.goal then ids[#ids + 1] = entry.goal end
|
|
137
|
+
append(string.format('- %s; unresolved at %s:%d via %s: %s; continuation%s is unverified.',
|
|
138
|
+
table.concat(path), file, entry.line or 0, entry.call or '?', reason, destination), ids)
|
|
102
139
|
end
|
|
103
140
|
end
|
|
104
|
-
if report.truncated then
|
|
105
|
-
return
|
|
141
|
+
if report.truncated then append('Additional sites or relationship evidence were omitted.') end
|
|
142
|
+
return finish()
|
|
106
143
|
end
|
|
107
144
|
|
|
108
145
|
function M.render(report, G)
|
|
109
|
-
local
|
|
110
|
-
'Scope: indexed references and configured dispatch rules; unresolved does not mean no runtime call.' }
|
|
146
|
+
local append, finish = renderer(G, { '\n## Relationship evidence / unresolved references',
|
|
147
|
+
'Scope: indexed references, possible implementations and configured dispatch rules; unresolved does not mean no runtime call.' })
|
|
111
148
|
for _, e in ipairs(report.entries) do
|
|
112
149
|
local source = G.nodes[e.source]
|
|
113
150
|
local path = source and G.files[G.node_file[e.source]].path or '?'
|
|
@@ -116,11 +153,11 @@ function M.render(report, G)
|
|
|
116
153
|
if e.call then detail = detail .. ' via ' .. e.call end
|
|
117
154
|
if e.rule then detail = detail .. ' (rule #' .. e.rule .. ', argument ' .. e.argument .. ')' end
|
|
118
155
|
if e.registered_at then detail = detail .. '; registered at ' .. e.registered_at end
|
|
119
|
-
|
|
120
|
-
source and source.qualified or '?', target and ' -> ' .. target or '', detail)
|
|
156
|
+
append(string.format('- %s:%d %s%s: %s', path, e.line or 0,
|
|
157
|
+
source and source.qualified or '?', target and ' -> ' .. target or '', detail), { e.source, e.target }, e.registered_at)
|
|
121
158
|
end
|
|
122
|
-
if report.truncated then
|
|
123
|
-
return
|
|
159
|
+
if report.truncated then append('Evidence limit reached; additional explanations omitted.') end
|
|
160
|
+
return finish()
|
|
124
161
|
end
|
|
125
162
|
|
|
126
163
|
return M
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
-- Bounded diagnostics for reference sites actually examined by this query.
|
|
2
|
+
local M = { MAX_SITES = 512 }
|
|
3
|
+
function M.new()
|
|
4
|
+
local result = { scope = 'examined_reference_sites', observed_sites = 0,
|
|
5
|
+
unresolved_sites = 0, ambiguous_sites = 0, heuristic_matches = 0, counts_limited = false }
|
|
6
|
+
local seen = {}
|
|
7
|
+
local function record(file, reference, kind, targets, reason)
|
|
8
|
+
if result.counts_limited or kind ~= 'call' and kind ~= 'member_call' then return end
|
|
9
|
+
local key = file .. ':' .. reference
|
|
10
|
+
if seen[key] then return end
|
|
11
|
+
if result.observed_sites >= M.MAX_SITES then result.counts_limited = true; return end
|
|
12
|
+
seen[key] = true; result.observed_sites = result.observed_sites + 1
|
|
13
|
+
local count = targets and #targets or 0
|
|
14
|
+
reason = reason or ''
|
|
15
|
+
if count == 0 then result.unresolved_sites = result.unresolved_sites + 1 end
|
|
16
|
+
if count > 1 or reason:find('ambigu', 1, true) or reason:find('multiple name/', 1, true) then
|
|
17
|
+
result.ambiguous_sites = result.ambiguous_sites + 1
|
|
18
|
+
end
|
|
19
|
+
if count > 0 and reason:find('name/locality', 1, true) then result.heuristic_matches = result.heuristic_matches + 1 end
|
|
20
|
+
end
|
|
21
|
+
return result, record
|
|
22
|
+
end
|
|
23
|
+
return M
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
-- Local Cargo package identity is configuration evidence owned by one query.
|
|
2
|
+
local M = { MAX_CONFIGS = 1024, MAX_CONFIG_BYTES = 1048576, MAX_TOTAL_BYTES = 8388608,
|
|
3
|
+
MAX_DEPENDENCIES = 512, MAX_DEPTH = 32 }
|
|
4
|
+
local control, paths = require('codeoutline.control'), require('codeoutline.path')
|
|
5
|
+
local function dir(p) return p:match('^(.*)/[^/]+$') or '' end
|
|
6
|
+
local function join(a, b) return a == '' and b or b == '' and a or a .. '/' .. b end
|
|
7
|
+
local function relative(base, p)
|
|
8
|
+
if type(p) ~= 'string' or p:find('[\\:%z]') or p:sub(1, 1) == '/' then error('unsupported or external Cargo path', 0) end
|
|
9
|
+
local parts = {}
|
|
10
|
+
for s in join(base, p):gmatch('[^/]+') do
|
|
11
|
+
if s == '..' then
|
|
12
|
+
if #parts == 0 then error('Cargo path escapes indexed project', 0) end
|
|
13
|
+
parts[#parts] = nil
|
|
14
|
+
elseif s ~= '.' then parts[#parts + 1] = s end
|
|
15
|
+
end
|
|
16
|
+
return table.concat(parts, '/')
|
|
17
|
+
end
|
|
18
|
+
local function crate_name(s) return type(s) == 'string' and s:gsub('-', '_') end
|
|
19
|
+
local function matches(pattern, value)
|
|
20
|
+
if type(pattern) ~= 'string' or pattern:find('[{}%[%]\\%z]') or pattern:find('..', 1, true) then
|
|
21
|
+
error('unsupported Cargo workspace glob', 0)
|
|
22
|
+
end
|
|
23
|
+
pattern = pattern:gsub('^%./', ''):gsub('/+$', '')
|
|
24
|
+
local escaped = pattern:gsub('([^%w*?])', '%%%1'):gsub('%*', '[^/]*'):gsub('%?', '[^/]')
|
|
25
|
+
return value:match('^' .. escaped .. '$') ~= nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
local function context(G, ctx)
|
|
29
|
+
if ctx.rust_packages then return ctx.rust_packages end
|
|
30
|
+
local C = { configs = {}, owners = {}, deps = {}, reaches = {}, probes = 0, bytes = 0 }
|
|
31
|
+
ctx.rust_packages = C
|
|
32
|
+
ctx.rust_stats = ctx.rust_stats or { reads = 0, bytes = 0, packages = 0 }
|
|
33
|
+
function C.read(root)
|
|
34
|
+
if C.configs[root] ~= nil then return C.configs[root] or nil end
|
|
35
|
+
control.check(); C.probes = C.probes + 1
|
|
36
|
+
if C.probes > M.MAX_CONFIGS then error('Cargo configuration probe limit reached', 0) end
|
|
37
|
+
local abs = join(G.root, join(root, 'Cargo.toml'))
|
|
38
|
+
local real = xutils.realpath(abs)
|
|
39
|
+
if not real then C.configs[root] = false; return nil end
|
|
40
|
+
if not paths.contains(G.root, real) then error('Cargo manifest escapes indexed project', 0) end
|
|
41
|
+
local file = io.open(abs, 'rb')
|
|
42
|
+
if not file then C.configs[root] = false; return nil end
|
|
43
|
+
local raw = file:read(M.MAX_CONFIG_BYTES + 1); file:close()
|
|
44
|
+
C.bytes = C.bytes + #raw
|
|
45
|
+
if #raw > M.MAX_CONFIG_BYTES or C.bytes > M.MAX_TOTAL_BYTES then error('Cargo configuration size limit reached', 0) end
|
|
46
|
+
local config = require('codeoutline.cargo_toml').parse(raw); config.root = root
|
|
47
|
+
if config.package and (type(config.package.name) ~= 'string' or not config.package.name:match('^[%w_-]+$')) then
|
|
48
|
+
error('invalid Cargo package name', 0)
|
|
49
|
+
end
|
|
50
|
+
local stat = ctx.rust_stats
|
|
51
|
+
stat.reads, stat.bytes = stat.reads + 1, C.bytes
|
|
52
|
+
if config.package then stat.packages = stat.packages + 1 end
|
|
53
|
+
C.configs[root] = config; return config
|
|
54
|
+
end
|
|
55
|
+
function C.owner(file)
|
|
56
|
+
local directory = dir(G.files[file].path)
|
|
57
|
+
if C.owners[directory] ~= nil then return C.owners[directory] or nil end
|
|
58
|
+
local current = directory
|
|
59
|
+
while true do
|
|
60
|
+
local config = C.read(current)
|
|
61
|
+
if config then
|
|
62
|
+
C.owners[directory] = config.package and config or false
|
|
63
|
+
return config.package and config or nil
|
|
64
|
+
end
|
|
65
|
+
if current == '' then break end
|
|
66
|
+
current = dir(current)
|
|
67
|
+
end
|
|
68
|
+
C.owners[directory] = false
|
|
69
|
+
end
|
|
70
|
+
function C.workspace(owner)
|
|
71
|
+
local override = owner.package.workspace
|
|
72
|
+
if override ~= nil then
|
|
73
|
+
local config = C.read(relative(owner.root, override))
|
|
74
|
+
if not config or not config.workspace then error('Cargo workspace root is unavailable', 0) end
|
|
75
|
+
return config
|
|
76
|
+
end
|
|
77
|
+
local current = owner.root
|
|
78
|
+
while true do
|
|
79
|
+
local config = C.read(current)
|
|
80
|
+
if config and config.workspace then return config end
|
|
81
|
+
if current == '' then return nil end
|
|
82
|
+
current = dir(current)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
function C.dependencies(owner)
|
|
86
|
+
if C.deps[owner.root] then return C.deps[owner.root] end
|
|
87
|
+
local dependencies, result, count = {}, {}, 0
|
|
88
|
+
for key, value in pairs(owner.dependencies or {}) do
|
|
89
|
+
local name, field = key:match('^([%w_-]+)%.([%w_-]+)$')
|
|
90
|
+
if name then
|
|
91
|
+
dependencies[name] = dependencies[name] or {}
|
|
92
|
+
if type(dependencies[name]) ~= 'table' then error('conflicting Cargo dependency assignment', 0) end
|
|
93
|
+
dependencies[name][field] = value
|
|
94
|
+
else dependencies[key] = value end
|
|
95
|
+
end
|
|
96
|
+
for section, value in pairs(owner) do
|
|
97
|
+
local name = section:match('^dependencies%.([%w_-]+)$')
|
|
98
|
+
if name then
|
|
99
|
+
if dependencies[name] then error('conflicting Cargo dependency table', 0) end
|
|
100
|
+
dependencies[name] = value
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
for key, value in pairs(dependencies) do
|
|
104
|
+
control.check(); count = count + 1
|
|
105
|
+
if count > M.MAX_DEPENDENCIES then error('Cargo dependency limit reached', 0) end
|
|
106
|
+
local base, dependency = owner.root, value
|
|
107
|
+
if type(value) == 'table' and value.workspace == true then
|
|
108
|
+
local workspace = C.workspace(owner)
|
|
109
|
+
if not workspace then error('Cargo inherited dependency has no workspace', 0) end
|
|
110
|
+
local rel = owner.root == workspace.root and '' or workspace.root == '' and owner.root or owner.root:sub(#workspace.root + 2)
|
|
111
|
+
for _, pattern in ipairs(workspace.workspace.exclude or {}) do
|
|
112
|
+
if matches(pattern, rel) then error('Cargo package is excluded from workspace', 0) end
|
|
113
|
+
end
|
|
114
|
+
dependency = (workspace['workspace.dependencies'] or {})[key] or workspace['workspace.dependencies.' .. key]
|
|
115
|
+
if not dependency then error('Cargo inherited dependency is not declared: ' .. key, 0) end
|
|
116
|
+
base = workspace.root
|
|
117
|
+
end
|
|
118
|
+
local descriptor = { alias = crate_name(key), source = dependency }
|
|
119
|
+
if type(dependency) == 'table' and dependency.path ~= nil then
|
|
120
|
+
local root = relative(base, dependency.path)
|
|
121
|
+
local target = C.read(root)
|
|
122
|
+
if target and target.package and type(target.package.name) == 'string'
|
|
123
|
+
and (dependency.package == nil or dependency.package == target.package.name) then
|
|
124
|
+
descriptor.package = target
|
|
125
|
+
if dependency.package == nil and target.package.name ~= key then descriptor.package = nil end
|
|
126
|
+
if descriptor.package and dependency.package == nil then
|
|
127
|
+
descriptor.alias = crate_name((target.lib or {}).name or target.package.name)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
if result[descriptor.alias] then error('ambiguous Cargo crate alias: ' .. descriptor.alias, 0) end
|
|
132
|
+
result[descriptor.alias] = descriptor
|
|
133
|
+
end
|
|
134
|
+
C.deps[owner.root] = result; return result
|
|
135
|
+
end
|
|
136
|
+
return C
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
function M.guard(ctx, fn, ...)
|
|
140
|
+
if ctx.rust_error then return nil, ctx.rust_error end
|
|
141
|
+
local ok, a, b = pcall(fn, ...)
|
|
142
|
+
if ok then return a, b end
|
|
143
|
+
if control.is_interrupted(a) then error(a, 0) end
|
|
144
|
+
ctx.rust_incomplete, ctx.rust_error = true, tostring(a)
|
|
145
|
+
return nil, ctx.rust_error
|
|
146
|
+
end
|
|
147
|
+
function M.owner(G, f, ctx)
|
|
148
|
+
if not G.root then return nil end
|
|
149
|
+
return M.guard(ctx, function() return context(G, ctx).owner(f) end)
|
|
150
|
+
end
|
|
151
|
+
function M.dependencies(G, f, ctx)
|
|
152
|
+
return M.guard(ctx, function()
|
|
153
|
+
local C = context(G, ctx); local owner = C.owner(f)
|
|
154
|
+
return owner and C.dependencies(owner) or {}
|
|
155
|
+
end) or {}
|
|
156
|
+
end
|
|
157
|
+
function M.entry(G, owner, ctx, file)
|
|
158
|
+
return M.guard(ctx, function()
|
|
159
|
+
local lib = owner.lib or {}
|
|
160
|
+
local root = relative(owner.root, lib.path or 'src/lib.rs')
|
|
161
|
+
if file and G.files[file].path == join(owner.root, 'src/main.rs') then root = G.files[file].path end
|
|
162
|
+
if file and not lib.path and not G.file_index[root] then root = join(owner.root, 'src/main.rs') end
|
|
163
|
+
local id = G.file_index[root]
|
|
164
|
+
if not id or G.files[id].language ~= 'rust' then return nil, 'Cargo library target is not indexed' end
|
|
165
|
+
if context(G, ctx).owner(id) ~= owner then return nil, 'Cargo target crosses a package boundary' end
|
|
166
|
+
return id
|
|
167
|
+
end)
|
|
168
|
+
end
|
|
169
|
+
function M.crates(G, f, ctx)
|
|
170
|
+
local result = {}
|
|
171
|
+
for alias, descriptor in pairs(M.dependencies(G, f, ctx)) do
|
|
172
|
+
if descriptor.package then result[alias] = M.entry(G, descriptor.package, ctx) end
|
|
173
|
+
end
|
|
174
|
+
local owner = M.owner(G, f, ctx)
|
|
175
|
+
if owner then
|
|
176
|
+
local alias = crate_name((owner.lib or {}).name or owner.package.name)
|
|
177
|
+
if alias and not result[alias] then result[alias] = M.entry(G, owner, ctx) end
|
|
178
|
+
end
|
|
179
|
+
return result
|
|
180
|
+
end
|
|
181
|
+
function M.reaches(G, f, target, ctx)
|
|
182
|
+
local owner = M.owner(G, f, ctx)
|
|
183
|
+
if not owner or not target then return false end
|
|
184
|
+
return M.guard(ctx, function()
|
|
185
|
+
local C, seen = context(G, ctx), {}
|
|
186
|
+
local key = owner.root .. '\0' .. target.root
|
|
187
|
+
if C.reaches[key] ~= nil then return C.reaches[key] end
|
|
188
|
+
local function visit(package, depth)
|
|
189
|
+
if package == target then return true end
|
|
190
|
+
if seen[package.root] then return false end
|
|
191
|
+
if depth > M.MAX_DEPTH then error('Cargo dependency traversal limit reached', 0) end
|
|
192
|
+
seen[package.root] = true
|
|
193
|
+
for _, descriptor in pairs(C.dependencies(package)) do
|
|
194
|
+
control.check()
|
|
195
|
+
if descriptor.package and visit(descriptor.package, depth + 1) then return true end
|
|
196
|
+
end
|
|
197
|
+
return false
|
|
198
|
+
end
|
|
199
|
+
local result = visit(owner, 0); C.reaches[key] = result; return result
|
|
200
|
+
end) or false
|
|
201
|
+
end
|
|
202
|
+
M.dir, M.join, M.relative = dir, join, relative
|
|
203
|
+
return M
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
-- Rust crate/module evidence is produced only for queries, never serialized.
|
|
2
|
+
local M = { MAX_DEPTH = 16, MAX_RESULTS = 128, MAX_ALIAS_FILES = 64, MAX_NODES = 8192 }
|
|
3
|
+
local cargo, control = require('codeoutline.rust_packages'), require('codeoutline.control')
|
|
4
|
+
local function join(a, b) return a == '' and b or b == '' and a or a .. '::' .. b end
|
|
5
|
+
local function text(T, i) return i >= 1 and i <= T.n and T:text(i) or '' end
|
|
6
|
+
|
|
7
|
+
function M.collect(r, T)
|
|
8
|
+
r.rust_imports, r.rust_public, r.has_bindings = {}, {}, true
|
|
9
|
+
for n, node in ipairs(r.nodes) do
|
|
10
|
+
r.rust_public[n] = text(T, node.ti) == 'pub' and text(T, node.ti + 1) ~= '('
|
|
11
|
+
end
|
|
12
|
+
for i = 1, T.n do
|
|
13
|
+
if text(T, i) == 'use' then
|
|
14
|
+
local parent, in_function, last, j = nil, false, i + 1, i + 1
|
|
15
|
+
for n, node in ipairs(r.nodes) do
|
|
16
|
+
if node.ti <= i and i <= node.tj then
|
|
17
|
+
if node.kind == 'namespace' then parent = n
|
|
18
|
+
elseif node.kind == 'function' or node.kind == 'method' then in_function = true end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
while j <= T.n and text(T, j) ~= ';' do
|
|
22
|
+
if T.m[j] and T.m[j] > j then j = T.m[j] end
|
|
23
|
+
j = j + 1
|
|
24
|
+
end
|
|
25
|
+
last = j - 1
|
|
26
|
+
local cursor, count = i + 1, 0
|
|
27
|
+
if text(T, cursor) == '::' then cursor = cursor + 1 end
|
|
28
|
+
local branch
|
|
29
|
+
branch = function(prefix, depth)
|
|
30
|
+
if depth > M.MAX_DEPTH then error('Rust use nesting limit reached', 0) end
|
|
31
|
+
local path = prefix
|
|
32
|
+
while cursor <= last do
|
|
33
|
+
local token = text(T, cursor)
|
|
34
|
+
if token == '{' then
|
|
35
|
+
cursor = cursor + 1
|
|
36
|
+
while cursor <= last and text(T, cursor) ~= '}' do
|
|
37
|
+
branch(path, depth + 1)
|
|
38
|
+
if text(T, cursor) == ',' then cursor = cursor + 1 else break end
|
|
39
|
+
end
|
|
40
|
+
if text(T, cursor) ~= '}' then error('unsupported Rust use group', 0) end
|
|
41
|
+
cursor = cursor + 1; return
|
|
42
|
+
end
|
|
43
|
+
if token == '' or token == ',' or token == '}' then break end
|
|
44
|
+
if token ~= 'self' or path == '' then path = join(path, token) end
|
|
45
|
+
cursor = cursor + 1
|
|
46
|
+
if text(T, cursor) == '::' then cursor = cursor + 1 else break end
|
|
47
|
+
end
|
|
48
|
+
local alias = path:match('([^:]+)$')
|
|
49
|
+
if text(T, cursor) == 'as' then alias = text(T, cursor + 1); cursor = cursor + 2 end
|
|
50
|
+
count = count + 1
|
|
51
|
+
if count > M.MAX_RESULTS then error('Rust use binding limit reached', 0) end
|
|
52
|
+
if not in_function then r.rust_imports[#r.rust_imports + 1] = { path = path, alias = alias,
|
|
53
|
+
parent = parent, public = text(T, i - 1) == 'pub', line = T:line(i) } end
|
|
54
|
+
end
|
|
55
|
+
branch('', 0)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
for _, ref in ipairs(r.refs) do
|
|
59
|
+
local i, path = ref.tok, ref.name
|
|
60
|
+
if text(T, i - 1) == '.' then ref.binding = 'rust:member'
|
|
61
|
+
else
|
|
62
|
+
while text(T, i - 1) == '::' and (T.k[i - 2] == 'id' or T.k[i - 2] == 'kw') do
|
|
63
|
+
path, i = text(T, i - 2) .. '::' .. path, i - 2
|
|
64
|
+
end
|
|
65
|
+
ref.binding = 'rust:path:' .. path
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
local function data(G, f, ctx)
|
|
71
|
+
local rec = G.files[f]
|
|
72
|
+
if rec.rust_imports then return rec end
|
|
73
|
+
return ctx.ordinary and cargo.guard(ctx, function() return ctx.ordinary.data(f) end) or rec
|
|
74
|
+
end
|
|
75
|
+
local function scope_of(G, f, src)
|
|
76
|
+
local node = src and G.nodes[src]
|
|
77
|
+
local p = node and node.parent
|
|
78
|
+
while p do
|
|
79
|
+
local owner = G.files[f].nodes[p]
|
|
80
|
+
if owner.kind == 'namespace' then return owner.qualified end
|
|
81
|
+
p = owner.parent
|
|
82
|
+
end
|
|
83
|
+
return ''
|
|
84
|
+
end
|
|
85
|
+
local function parent_scope(G, f, p) return p and G.files[f].nodes[p].qualified or '' end
|
|
86
|
+
local function nodes(G, f, ctx)
|
|
87
|
+
local list = G.files[f].nodes
|
|
88
|
+
if #list > M.MAX_NODES then
|
|
89
|
+
ctx.rust_incomplete, ctx.rust_error = true, 'Rust module node limit reached'
|
|
90
|
+
return {}
|
|
91
|
+
end
|
|
92
|
+
return list
|
|
93
|
+
end
|
|
94
|
+
local function child(G, entry, name, ctx)
|
|
95
|
+
local wanted, found = join(entry.scope, name), nil
|
|
96
|
+
local rec, detail = G.files[entry.file], data(G, entry.file, ctx)
|
|
97
|
+
for n, node in ipairs(nodes(G, entry.file, ctx)) do
|
|
98
|
+
if node.kind == 'namespace' and node.qualified == wanted then
|
|
99
|
+
if entry.external and not (detail.rust_public or {})[n] then return nil end
|
|
100
|
+
local external = false
|
|
101
|
+
for _, imp in ipairs(rec.imports) do if imp.kind == 'mod' and imp.path == name then external = true; break end end
|
|
102
|
+
if not external then return { file = entry.file, scope = wanted, external = entry.external, root = entry.root } end
|
|
103
|
+
local path = rec.path
|
|
104
|
+
local directory, stem = cargo.dir(path), path:match('([^/]+)%.rs$')
|
|
105
|
+
if not entry.root and stem ~= 'lib' and stem ~= 'main' and stem ~= 'mod' then directory = cargo.join(directory, stem) end
|
|
106
|
+
if entry.scope ~= '' then directory = cargo.join(directory, entry.scope:gsub('::', '/')) end
|
|
107
|
+
for _, candidate in ipairs({ cargo.join(directory, name .. '.rs'), cargo.join(directory, name .. '/mod.rs') }) do
|
|
108
|
+
local id = G.file_index[candidate]
|
|
109
|
+
if id then
|
|
110
|
+
if found then return nil end
|
|
111
|
+
if cargo.owner(G, id, ctx) ~= cargo.owner(G, entry.file, ctx) then return nil end
|
|
112
|
+
found = { file = id, scope = '', external = entry.external }
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
return found
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
local walk
|
|
121
|
+
local function terminal(G, entry, name, ctx, seen, depth)
|
|
122
|
+
local result, rec, detail = {}, G.files[entry.file], data(G, entry.file, ctx)
|
|
123
|
+
local wanted = join(entry.scope, name)
|
|
124
|
+
for n, node in ipairs(nodes(G, entry.file, ctx)) do
|
|
125
|
+
if node.qualified == wanted and node.kind ~= 'namespace'
|
|
126
|
+
and (not entry.external or (detail.rust_public or {})[n]) then
|
|
127
|
+
result[#result + 1] = G.id_of[entry.file][n]
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
if #result > 0 then return { ids = result } end
|
|
131
|
+
for _, imp in ipairs(detail.rust_imports or {}) do
|
|
132
|
+
if imp.alias == name and parent_scope(G, entry.file, imp.parent) == entry.scope
|
|
133
|
+
and (not entry.external or imp.public) then
|
|
134
|
+
local found = walk(G, entry.file, imp.path, ctx, entry.scope, seen, depth + 1)
|
|
135
|
+
if entry.external and found and found.ids then
|
|
136
|
+
local visible = {}
|
|
137
|
+
for _, id in ipairs(found.ids) do
|
|
138
|
+
local f = G.node_file[id]
|
|
139
|
+
for n, local_id in ipairs(G.id_of[f]) do
|
|
140
|
+
if local_id == id and (data(G, f, ctx).rust_public or {})[n] then visible[#visible + 1] = id end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
found.ids = visible
|
|
144
|
+
end
|
|
145
|
+
return found
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
walk = function(G, f, path, ctx, scope, seen, depth)
|
|
150
|
+
control.check()
|
|
151
|
+
if depth > M.MAX_DEPTH then error('Rust module resolution depth limit reached', 0) end
|
|
152
|
+
local key = G.files[f].path .. '\0' .. scope .. '\0' .. path
|
|
153
|
+
if seen[key] then return nil end
|
|
154
|
+
seen[key] = true
|
|
155
|
+
local parts = {}; for name in path:gmatch('[^:]+') do parts[#parts + 1] = name end
|
|
156
|
+
if #parts == 0 then return nil end
|
|
157
|
+
local owner = cargo.owner(G, f, ctx)
|
|
158
|
+
if not owner then return nil end
|
|
159
|
+
local first, index, entry = parts[1], 2, nil
|
|
160
|
+
if first == 'crate' then
|
|
161
|
+
local id = cargo.entry(G, owner, ctx, f)
|
|
162
|
+
if id then entry = { file = id, scope = '', root = true } end
|
|
163
|
+
elseif first == 'self' then entry = { file = f, scope = scope, root = cargo.entry(G, owner, ctx, f) == f }
|
|
164
|
+
elseif first == 'super' then
|
|
165
|
+
if scope ~= '' then entry = { file = f, scope = scope:match('^(.*)::[^:]+$') or '', root = cargo.entry(G, owner, ctx, f) == f }
|
|
166
|
+
else
|
|
167
|
+
local root = cargo.entry(G, owner, ctx, f)
|
|
168
|
+
if root and root ~= f then
|
|
169
|
+
local directory = cargo.dir(G.files[f].path)
|
|
170
|
+
if G.files[f].path:match('/mod%.rs$') then directory = cargo.dir(directory) end
|
|
171
|
+
local candidates = directory == cargo.dir(G.files[root].path) and { G.files[root].path }
|
|
172
|
+
or { directory .. '.rs', cargo.join(directory, 'mod.rs') }
|
|
173
|
+
for _, candidate in ipairs(candidates) do
|
|
174
|
+
local id = G.file_index[candidate]
|
|
175
|
+
if id and cargo.owner(G, id, ctx) == owner then
|
|
176
|
+
if entry then return nil end
|
|
177
|
+
entry = { file = id, scope = '', root = id == root }
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
else
|
|
183
|
+
for _, imp in ipairs(data(G, f, ctx).rust_imports or {}) do
|
|
184
|
+
if imp.alias == first and parent_scope(G, f, imp.parent) == scope then
|
|
185
|
+
local replacement = imp.path
|
|
186
|
+
if #parts > 1 then replacement = replacement .. '::' .. table.concat(parts, '::', 2) end
|
|
187
|
+
if replacement ~= path then return walk(G, f, replacement, ctx, scope, seen, depth + 1) end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
local id = cargo.crates(G, f, ctx)[first]
|
|
191
|
+
if id then entry = { file = id, scope = '', external = cargo.owner(G, id, ctx) ~= owner, root = true }
|
|
192
|
+
else entry, index = { file = f, scope = scope }, 1 end
|
|
193
|
+
end
|
|
194
|
+
if not entry then return nil end
|
|
195
|
+
if first == 'super' and parts[2] == 'super' then
|
|
196
|
+
return walk(G, entry.file, table.concat(parts, '::', 2), ctx, entry.scope, seen, depth + 1)
|
|
197
|
+
end
|
|
198
|
+
while index <= #parts do
|
|
199
|
+
local next_entry = child(G, entry, parts[index], ctx)
|
|
200
|
+
if next_entry then entry = next_entry
|
|
201
|
+
else
|
|
202
|
+
local found = terminal(G, entry, parts[index], ctx, seen, depth)
|
|
203
|
+
if index == #parts then return found end
|
|
204
|
+
if found and found.module then
|
|
205
|
+
found.module.external = entry.external; entry = found.module
|
|
206
|
+
elseif found and found.ids and index + 1 == #parts then
|
|
207
|
+
local ids = {}
|
|
208
|
+
for _, type_id in ipairs(found.ids) do
|
|
209
|
+
local ty, tf = G.nodes[type_id], G.node_file[type_id]
|
|
210
|
+
for _, id in ipairs(G.by_name[parts[index + 1]] or {}) do
|
|
211
|
+
local method, mf = G.nodes[id], G.node_file[id]
|
|
212
|
+
if mf == tf and method.kind == 'method' and method.qualified == ty.qualified .. '::' .. parts[index + 1] then
|
|
213
|
+
for n, local_id in ipairs(G.id_of[mf]) do
|
|
214
|
+
if local_id == id and (not entry.external or (data(G, mf, ctx).rust_public or {})[n]) then ids[#ids + 1] = id end
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
return { ids = ids }
|
|
220
|
+
else return nil end
|
|
221
|
+
end
|
|
222
|
+
index = index + 1
|
|
223
|
+
end
|
|
224
|
+
return { module = entry }
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
function M.path(G, f, path, ctx, scope)
|
|
228
|
+
return cargo.guard(ctx, walk, G, f, path, ctx, scope or '', {}, 0)
|
|
229
|
+
end
|
|
230
|
+
function M.resolve(G, f, src, name, recv, evidence, ctx)
|
|
231
|
+
if not cargo.owner(G, f, ctx) then return ctx.rust_incomplete and {} or nil, ctx.rust_error end
|
|
232
|
+
if evidence == 'rust:member' then return nil end
|
|
233
|
+
local path = evidence and evidence:match('^rust:path:(.*)$') or (recv and recv .. '::' .. name or name)
|
|
234
|
+
local found, reason = M.path(G, f, path, ctx, scope_of(G, f, src))
|
|
235
|
+
return found and found.ids or {}, reason or 'Cargo crate/module target is not indexed or visible'
|
|
236
|
+
end
|
|
237
|
+
function M.imports(G, f, imp, ctx)
|
|
238
|
+
if not cargo.owner(G, f, ctx) then return ctx.rust_incomplete and {} or nil end
|
|
239
|
+
local result, seen = {}, {}
|
|
240
|
+
local function add(id) if id and not seen[id] then seen[id] = true; result[#result + 1] = id end end
|
|
241
|
+
if imp.kind == 'mod' then
|
|
242
|
+
local entry = child(G, { file = f, scope = '' }, imp.path, ctx)
|
|
243
|
+
if entry then add(entry.file) end
|
|
244
|
+
else
|
|
245
|
+
for _, binding in ipairs(data(G, f, ctx).rust_imports or {}) do
|
|
246
|
+
if binding.line == imp.line then
|
|
247
|
+
local found = M.path(G, f, binding.path, ctx, parent_scope(G, f, binding.parent))
|
|
248
|
+
if found then
|
|
249
|
+
if found.module then add(found.module.file) end
|
|
250
|
+
for _, id in ipairs(found.ids or {}) do add(G.node_file[id]) end
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
return result
|
|
256
|
+
end
|
|
257
|
+
function M.members(G, entry, ctx)
|
|
258
|
+
return cargo.guard(ctx, function()
|
|
259
|
+
local result, names, detail = {}, {}, data(G, entry.file, ctx)
|
|
260
|
+
for n, node in ipairs(nodes(G, entry.file, ctx)) do
|
|
261
|
+
if node.kind ~= 'impl' and parent_scope(G, entry.file, node.parent) == entry.scope
|
|
262
|
+
and (not entry.external or (detail.rust_public or {})[n]) then
|
|
263
|
+
names[node.name] = true
|
|
264
|
+
result[#result + 1] = { name = node.name, node = node, file = entry.file, kind = node.kind }
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
for _, imp in ipairs(detail.rust_imports or {}) do
|
|
268
|
+
if imp.public and imp.alias ~= '*' and not names[imp.alias]
|
|
269
|
+
and parent_scope(G, entry.file, imp.parent) == entry.scope then
|
|
270
|
+
local found = M.path(G, entry.file, imp.path, ctx, entry.scope)
|
|
271
|
+
for _, id in ipairs(found and found.ids or {}) do
|
|
272
|
+
result[#result + 1] = { name = imp.alias, node = G.nodes[id], file = G.node_file[id], kind = G.nodes[id].kind }
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
if #result > M.MAX_RESULTS then error('Rust module member limit reached', 0) end
|
|
277
|
+
return result
|
|
278
|
+
end) or {}
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
function M.alias_refs(G, f, wanted, ctx)
|
|
282
|
+
local rec = G.files[f]
|
|
283
|
+
if rec.language ~= 'rust' or #rec.imports == 0 or not cargo.owner(G, f, ctx) then return {} end
|
|
284
|
+
local relevant = false
|
|
285
|
+
for id in pairs(wanted) do
|
|
286
|
+
local target = cargo.owner(G, G.node_file[id], ctx)
|
|
287
|
+
if cargo.reaches(G, f, target, ctx) then relevant = true end
|
|
288
|
+
end
|
|
289
|
+
if not relevant then return {} end
|
|
290
|
+
ctx.rust_alias_files = ctx.rust_alias_files or {}
|
|
291
|
+
if not ctx.rust_alias_files[f] then
|
|
292
|
+
ctx.rust_alias_count = (ctx.rust_alias_count or 0) + 1
|
|
293
|
+
if ctx.rust_alias_count > M.MAX_ALIAS_FILES then ctx.rust_incomplete = true; return {} end
|
|
294
|
+
ctx.rust_alias_files[f] = true
|
|
295
|
+
end
|
|
296
|
+
local refs, result = data(G, f, ctx).refs, {}
|
|
297
|
+
for i, binding in ipairs(refs.binding or {}) do if binding:match('^rust:path:') then result[i] = true end end
|
|
298
|
+
return result
|
|
299
|
+
end
|
|
300
|
+
M.scope = scope_of
|
|
301
|
+
return M
|