@codua/codeoutline-win32-x64 0.1.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/DISTRIBUTION.md +98 -0
- package/LICENSE +24 -0
- package/README.md +132 -0
- package/README.zh-CN.md +132 -0
- package/THIRD_PARTY.md +32 -0
- package/build-info.json +1 -0
- package/codeoutline +4 -0
- package/codeoutline.cmd +3 -0
- package/licenses/libdeflate.txt +22 -0
- package/licenses/lua-cmsgpack.txt +22 -0
- package/licenses/luajit.txt +56 -0
- package/licenses/mbedtls.txt +553 -0
- package/licenses/rpmalloc.txt +12 -0
- package/licenses/xnet2lua.txt +24 -0
- package/licenses/yyjson.txt +21 -0
- package/package.json +1 -0
- package/scripts/codeoutline/cli.lua +32 -0
- package/scripts/codeoutline/command.lua +155 -0
- package/scripts/codeoutline/common.lua +124 -0
- package/scripts/codeoutline/control.lua +4 -0
- package/scripts/codeoutline/explore.lua +500 -0
- package/scripts/codeoutline/graph.lua +380 -0
- package/scripts/codeoutline/http.lua +156 -0
- package/scripts/codeoutline/index.lua +404 -0
- package/scripts/codeoutline/index_worker.lua +43 -0
- package/scripts/codeoutline/lang/c.lua +362 -0
- package/scripts/codeoutline/lang/cpp.lua +539 -0
- package/scripts/codeoutline/lang/csharp.lua +334 -0
- package/scripts/codeoutline/lang/go.lua +233 -0
- package/scripts/codeoutline/lang/java.lua +274 -0
- package/scripts/codeoutline/lang/js.lua +466 -0
- package/scripts/codeoutline/lang/lua.lua +218 -0
- package/scripts/codeoutline/lang/python.lua +184 -0
- package/scripts/codeoutline/lang/rust.lua +276 -0
- package/scripts/codeoutline/main.lua +104 -0
- package/scripts/codeoutline/mcp.lua +188 -0
- package/scripts/codeoutline/parse.lua +49 -0
- package/scripts/codeoutline/path.lua +36 -0
- package/scripts/codeoutline/service.lua +135 -0
- package/scripts/codeoutline/text.lua +43 -0
- package/scripts/codeoutline/version.lua +1 -0
- package/scripts/codeoutline/xscan.lua +625 -0
- package/xnet2lua/bin/xnet.exe +0 -0
- package/xnet2lua/scripts/core/share/xhttp_codec.lua +835 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
-- Native entry point: xnet main.lua STDIO=1 PROJECT=... or HTTP=1 PORT=19876.
|
|
2
|
+
local source = assert(xutils.realpath(debug.getinfo(1, 'S').source:sub(2))):gsub('\\', '/')
|
|
3
|
+
local scripts = assert(source:match('^(.*)/codeoutline/[^/]+$'))
|
|
4
|
+
local install = assert(scripts:match('^(.*)/scripts$'))
|
|
5
|
+
package.path = scripts .. '/?.lua;' .. package.path
|
|
6
|
+
local paths = require('codeoutline.path')
|
|
7
|
+
local mcp = require('codeoutline.mcp')
|
|
8
|
+
local http = require('codeoutline.http')
|
|
9
|
+
local options = { ALLOW_ROOT = {}, ALLOW_HOST = {}, ALLOW_ORIGIN = {} }
|
|
10
|
+
for _, argument in ipairs(arg or {}) do
|
|
11
|
+
local key, value = argument:match('^([A-Z_]+)=(.*)$')
|
|
12
|
+
if key then
|
|
13
|
+
if type(options[key]) == 'table' then table.insert(options[key], value) else options[key] = value end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
assert((options.STDIO == '1') ~= (options.HTTP == '1'), 'Specify exactly one of STDIO=1 or HTTP=1')
|
|
17
|
+
local config = { roots = {}, project = options.PROJECT and assert(paths.canonical(options.PROJECT)),
|
|
18
|
+
workdir = options.STDIO == '1' and paths.canonical('.') or nil, host = options.HOST or '127.0.0.1', port = tonumber(options.PORT or '19876'),
|
|
19
|
+
token = os.getenv('CODEOUTLINE_TOKEN'), hosts = options.ALLOW_HOST, origins = options.ALLOW_ORIGIN }
|
|
20
|
+
assert(config.port and config.port % 1 == 0 and config.port >= 1 and config.port <= 65535, 'Invalid PORT')
|
|
21
|
+
if config.token == '' then config.token = nil end
|
|
22
|
+
for _, root in ipairs(options.ALLOW_ROOT) do config.roots[#config.roots + 1] = assert(paths.canonical(root)) end
|
|
23
|
+
if #config.roots == 0 then config.roots[1] = config.project or assert(paths.canonical('.')) end
|
|
24
|
+
local shared = assert(xshared.create('codeoutline_control', 65536, 256))
|
|
25
|
+
local jobs, next_id, job_count, worker_ready = {}, 0, 0, false
|
|
26
|
+
local endpoint, stdio, input = nil, nil, ''
|
|
27
|
+
local startup = xtimer.now_ms()
|
|
28
|
+
|
|
29
|
+
local function submit(req, done)
|
|
30
|
+
if job_count >= 64 then return nil, 'Request queue is full; retry later' end
|
|
31
|
+
next_id = next_id + 1
|
|
32
|
+
local id = tostring(next_id)
|
|
33
|
+
req.deadline = xtimer.now_ms() + 120000
|
|
34
|
+
jobs[id], job_count = { done = done, request = req }, job_count + 1
|
|
35
|
+
if worker_ready then
|
|
36
|
+
local ok, err = xthread.post(2, 'run', id, req)
|
|
37
|
+
if not ok then jobs[id], job_count = nil, job_count - 1; return nil, tostring(err) end
|
|
38
|
+
end
|
|
39
|
+
return id
|
|
40
|
+
end
|
|
41
|
+
local function cancel(id) if id and jobs[id] then shared:set('cancel:' .. id, true) end end
|
|
42
|
+
local function send(response)
|
|
43
|
+
if response then io.stdout:write(xutils.json_pack(response), '\n'); io.stdout:flush() end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
__tick_ms = 5,
|
|
48
|
+
__init = function()
|
|
49
|
+
assert(xnet.init())
|
|
50
|
+
assert(xthread.create_thread(2, 'INDEX', scripts .. '/codeoutline/index_worker.lua'))
|
|
51
|
+
if options.STDIO == '1' then
|
|
52
|
+
assert(xutils.read_stdin, 'Rebuild runtime for nonblocking stdin support')
|
|
53
|
+
stdio = mcp.new(config, submit, cancel)
|
|
54
|
+
else
|
|
55
|
+
local runtime = options.RUNTIME_ROOT or install .. '/xnet2lua'
|
|
56
|
+
local codec = dofile(runtime .. '/scripts/core/share/xhttp_codec.lua')
|
|
57
|
+
endpoint = http.start(config, submit, cancel, codec)
|
|
58
|
+
io.stderr:write(string.format('[codeoutline] listening http://%s:%d/mcp\n', config.host, config.port))
|
|
59
|
+
end
|
|
60
|
+
end,
|
|
61
|
+
__thread_handle = function(_, op, id, ok, result)
|
|
62
|
+
if op == 'worker_ready' then
|
|
63
|
+
worker_ready = true
|
|
64
|
+
for job_id, job in pairs(jobs) do
|
|
65
|
+
local posted, err = xthread.post(2, 'run', job_id, job.request)
|
|
66
|
+
if not posted then jobs[job_id], job_count = nil, job_count - 1; job.done(false, tostring(err)) end
|
|
67
|
+
end
|
|
68
|
+
elseif op == 'worker_result' then
|
|
69
|
+
local job = jobs[id]
|
|
70
|
+
if job then jobs[id], job_count = nil, job_count - 1; job.done(ok, result) end
|
|
71
|
+
end
|
|
72
|
+
end,
|
|
73
|
+
__update = function()
|
|
74
|
+
if not worker_ready and xtimer.now_ms() - startup > 10000 then error('Index worker failed to start') end
|
|
75
|
+
if stdio then
|
|
76
|
+
local data, err = xutils.read_stdin(65536)
|
|
77
|
+
if not data then
|
|
78
|
+
local incomplete = err == 'eof' and input:find('%S') ~= nil
|
|
79
|
+
if incomplete then send(mcp.error(nil, -32700, 'EOF before message newline')) end
|
|
80
|
+
if err ~= 'eof' then io.stderr:write(tostring(err) .. '\n') end
|
|
81
|
+
stdio:close(); shared:set('shutdown', true); xthread.stop(err == 'eof' and not incomplete and 0 or 1); return
|
|
82
|
+
end
|
|
83
|
+
input = input .. data
|
|
84
|
+
while true do
|
|
85
|
+
local pos = input:find('\n', 1, true)
|
|
86
|
+
if not pos then break end
|
|
87
|
+
local line = input:sub(1, pos - 1)
|
|
88
|
+
input = input:sub(pos + 1)
|
|
89
|
+
if #line > 1048576 then send(mcp.error(nil, -32600, 'Message too large')); xthread.stop(1); return end
|
|
90
|
+
local msg, decode_error = mcp.decode(line)
|
|
91
|
+
if not msg then send(decode_error) else stdio:dispatch(msg, send) end
|
|
92
|
+
end
|
|
93
|
+
if #input > 1048576 then send(mcp.error(nil, -32600, 'Message too large')); xthread.stop(1); return end
|
|
94
|
+
stdio:tick()
|
|
95
|
+
else endpoint.tick() end
|
|
96
|
+
end,
|
|
97
|
+
__uninit = function()
|
|
98
|
+
shared:set('shutdown', true)
|
|
99
|
+
if stdio then stdio:close() end
|
|
100
|
+
if endpoint then endpoint.close() end
|
|
101
|
+
xthread.shutdown_thread(2)
|
|
102
|
+
xnet.uninit()
|
|
103
|
+
end,
|
|
104
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
local paths = require('codeoutline.path')
|
|
2
|
+
local explore = require('codeoutline.explore')
|
|
3
|
+
local text = require('codeoutline.text')
|
|
4
|
+
local version = require('codeoutline.version')
|
|
5
|
+
local M = { VERSION = '2025-06-18' }
|
|
6
|
+
local null = xutils.json_null
|
|
7
|
+
local function array(t) return setmetatable(t or {}, xutils.json_array_mt) end
|
|
8
|
+
local function object(t) return type(t) == 'table' and getmetatable(t) ~= xutils.json_array_mt end
|
|
9
|
+
local project = { type = 'string', minLength = 1,
|
|
10
|
+
description = 'Project directory on the server machine; defaults to PROJECT, the first client root, then the stdio working directory.' }
|
|
11
|
+
M.tools = array({
|
|
12
|
+
{ name = 'codeoutline_explore', description = 'Query symbols, line-numbered source, call paths and neighbors. Refreshes automatically.',
|
|
13
|
+
inputSchema = { type = 'object', properties = { projectPath = project,
|
|
14
|
+
query = { type = 'string', minLength = 1, maxLength = 4096 },
|
|
15
|
+
budget = { type = 'integer', minimum = 256, maximum = 262144, default = 16000 } },
|
|
16
|
+
required = array({ 'query' }), additionalProperties = false } },
|
|
17
|
+
{ name = 'codeoutline_status', description = 'Refresh and report project index, cache, scanner and parser statistics.',
|
|
18
|
+
inputSchema = { type = 'object', properties = { projectPath = project }, additionalProperties = false } },
|
|
19
|
+
{ name = 'codeoutline_rebuild', description = 'Fully reparse a project, bypassing cached records.',
|
|
20
|
+
inputSchema = { type = 'object', properties = { projectPath = project }, additionalProperties = false } },
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
function M.error(id, code, message)
|
|
24
|
+
return { jsonrpc = '2.0', id = id == nil and null or id, error = { code = code, message = message } }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
function M.decode(bytes)
|
|
28
|
+
if text.valid(bytes) ~= bytes then return nil, M.error(nil, -32700, 'Invalid UTF-8 JSON') end
|
|
29
|
+
local ok, value = pcall(xutils.json_unpack, bytes)
|
|
30
|
+
if not ok or value == nil then return nil, M.error(nil, -32700, 'Invalid JSON') end
|
|
31
|
+
if not object(value) then return nil, M.error(nil, -32600, 'Expected a JSON-RPC object') end
|
|
32
|
+
return value
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
-- file:// URI from roots/list to a local path; nil for other schemes.
|
|
36
|
+
function M.uri_path(uri)
|
|
37
|
+
if type(uri) ~= 'string' then return nil end
|
|
38
|
+
local host, path = uri:match('^[Ff][Ii][Ll][Ee]://([^/?#]*)(/[^?#]*)')
|
|
39
|
+
if not path then return nil end
|
|
40
|
+
path = path:gsub('%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end)
|
|
41
|
+
if host ~= '' and host:lower() ~= 'localhost' then return '//' .. host .. path end
|
|
42
|
+
return path:match('^/(%a:.*)$') or path
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
function M.new(config, submit, cancel)
|
|
46
|
+
local self = { initialized = false, ready = false, pending = {}, used = xtimer.now_ms(), roots = false, root_waiters = {}, next_request = 0 }
|
|
47
|
+
|
|
48
|
+
-- client_root: nil = not fetched, false = none usable, string = canonical first root.
|
|
49
|
+
local function finish_roots(result)
|
|
50
|
+
local request = self.roots_request
|
|
51
|
+
self.roots_request = nil
|
|
52
|
+
local first = object(result) and type(result.roots) == 'table' and result.roots[1]
|
|
53
|
+
local root = object(first) and M.uri_path(first.uri)
|
|
54
|
+
root = root and paths.canonical(root) or false
|
|
55
|
+
if not request.stale then self.client_root = root end
|
|
56
|
+
local waiters = self.root_waiters
|
|
57
|
+
self.root_waiters = {}
|
|
58
|
+
for _, done in ipairs(waiters) do done(root or config.workdir) end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
local function default_project(reply, done)
|
|
62
|
+
if config.project then done(config.project); return end
|
|
63
|
+
if not self.roots or self.client_root == false then done(config.workdir); return end
|
|
64
|
+
if self.client_root then done(self.client_root); return end
|
|
65
|
+
self.root_waiters[#self.root_waiters + 1] = done
|
|
66
|
+
if self.roots_request then return end
|
|
67
|
+
self.next_request = self.next_request + 1
|
|
68
|
+
self.roots_request = { id = 'codeoutline-roots-' .. self.next_request, sent = xtimer.now_ms() }
|
|
69
|
+
reply({ jsonrpc = '2.0', id = self.roots_request.id, method = 'roots/list' }, true)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
function self:close()
|
|
73
|
+
for id, p in pairs(self.pending) do
|
|
74
|
+
cancel(p.job); p.reply(nil); self.pending[id] = nil
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
function self:tick()
|
|
79
|
+
local now = xtimer.now_ms()
|
|
80
|
+
if self.roots_request and now - self.roots_request.sent >= 10000 then finish_roots(nil) end
|
|
81
|
+
for _, p in pairs(self.pending) do
|
|
82
|
+
if p.token ~= nil and now - p.last_progress >= 1000 then
|
|
83
|
+
p.progress = p.progress + 1
|
|
84
|
+
p.last_progress = now
|
|
85
|
+
p.reply({ jsonrpc = '2.0', method = 'notifications/progress', params = {
|
|
86
|
+
progressToken = p.token, progress = p.progress, message = 'Indexing/query in progress (elapsed seconds)',
|
|
87
|
+
} }, true)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
function self:dispatch(msg, reply)
|
|
93
|
+
self.used = xtimer.now_ms()
|
|
94
|
+
local id = msg.id
|
|
95
|
+
local valid_id = type(id) == 'string' or type(id) == 'number'
|
|
96
|
+
if msg.jsonrpc == '2.0' and msg.method == nil and valid_id and (msg.result ~= nil or msg.error ~= nil) then
|
|
97
|
+
if self.roots_request and self.roots_request.id == id then finish_roots(msg.result) end
|
|
98
|
+
reply(nil); return
|
|
99
|
+
end
|
|
100
|
+
if msg.jsonrpc ~= '2.0' or type(msg.method) ~= 'string'
|
|
101
|
+
or (id ~= nil and not valid_id) or (msg.params ~= nil and not object(msg.params)) then
|
|
102
|
+
reply(M.error(valid_id and id or nil, -32600, 'Invalid JSON-RPC request')); return
|
|
103
|
+
end
|
|
104
|
+
local method, params = msg.method, msg.params or {}
|
|
105
|
+
if id == nil then
|
|
106
|
+
if method == 'notifications/initialized' and self.initialized then self.ready = true
|
|
107
|
+
elseif method == 'notifications/roots/list_changed' then
|
|
108
|
+
self.client_root = nil
|
|
109
|
+
if self.roots_request then self.roots_request.stale = true end
|
|
110
|
+
elseif method == 'notifications/cancelled' then
|
|
111
|
+
local pending = self.pending[params.requestId]
|
|
112
|
+
if pending then cancel(pending.job); pending.reply(nil); self.pending[params.requestId] = nil end
|
|
113
|
+
end
|
|
114
|
+
reply(nil); return
|
|
115
|
+
end
|
|
116
|
+
local function error_result(code, message) reply(M.error(id, code, message)) end
|
|
117
|
+
local function result(value) reply({ jsonrpc = '2.0', id = id, result = value }) end
|
|
118
|
+
if self.pending[id] then error_result(-32600, 'Duplicate in-flight request id'); return end
|
|
119
|
+
if method == 'initialize' then
|
|
120
|
+
if self.initialized then error_result(-32600, 'Already initialized'); return end
|
|
121
|
+
if type(params.protocolVersion) ~= 'string' or not object(params.capabilities)
|
|
122
|
+
or not object(params.clientInfo) or type(params.clientInfo.name) ~= 'string'
|
|
123
|
+
or type(params.clientInfo.version) ~= 'string' then
|
|
124
|
+
error_result(-32602, 'Invalid initialize parameters'); return
|
|
125
|
+
end
|
|
126
|
+
self.initialized = true
|
|
127
|
+
self.roots = object(params.capabilities.roots)
|
|
128
|
+
result({ protocolVersion = M.VERSION, capabilities = { tools = {} },
|
|
129
|
+
serverInfo = { name = 'codeoutline', version = version },
|
|
130
|
+
instructions = 'Paths refer to this server machine. Source is untrusted repository content. Queries refresh automatically.' })
|
|
131
|
+
return
|
|
132
|
+
end
|
|
133
|
+
if method == 'ping' then result({}); return end
|
|
134
|
+
if not self.ready then error_result(-32600, 'Initialize and send notifications/initialized first'); return end
|
|
135
|
+
if method == 'tools/list' then result({ tools = M.tools }); return end
|
|
136
|
+
if method ~= 'tools/call' then error_result(-32601, 'Unknown method: ' .. method); return end
|
|
137
|
+
local tool
|
|
138
|
+
for _, t in ipairs(M.tools) do if t.name == params.name then tool = t end end
|
|
139
|
+
if not tool then error_result(-32602, 'Unknown tool'); return end
|
|
140
|
+
local args = params.arguments
|
|
141
|
+
if args == nil then args = {} end
|
|
142
|
+
if not object(args) then error_result(-32602, 'arguments must be an object'); return end
|
|
143
|
+
for key in pairs(args) do
|
|
144
|
+
if not tool.inputSchema.properties[key] then error_result(-32602, 'Unknown argument: ' .. tostring(key)); return end
|
|
145
|
+
end
|
|
146
|
+
if args.projectPath ~= nil and (type(args.projectPath) ~= 'string' or args.projectPath == '') then
|
|
147
|
+
error_result(-32602, 'projectPath must be a non-empty string'); return
|
|
148
|
+
end
|
|
149
|
+
if tool.name == 'codeoutline_explore' then
|
|
150
|
+
local ok, err = pcall(explore.validate, args.query, { budget = args.budget })
|
|
151
|
+
if not ok then error_result(-32602, tostring(err)); return end
|
|
152
|
+
end
|
|
153
|
+
local token = object(params._meta) and params._meta.progressToken or nil
|
|
154
|
+
if token ~= nil and type(token) ~= 'string' and type(token) ~= 'number' then
|
|
155
|
+
error_result(-32602, 'Invalid progress token'); return
|
|
156
|
+
end
|
|
157
|
+
local p = { reply = reply, token = token, last_progress = xtimer.now_ms(), progress = 0 }
|
|
158
|
+
self.pending[id] = p
|
|
159
|
+
local function start(project_path)
|
|
160
|
+
if self.pending[id] ~= p then return end
|
|
161
|
+
local root, err
|
|
162
|
+
if project_path then root, err = paths.canonical(project_path)
|
|
163
|
+
else err = 'projectPath is required: no PROJECT, client root or working directory is available' end
|
|
164
|
+
local allowed = false
|
|
165
|
+
if root then for _, r in ipairs(config.roots) do if paths.contains(r, root) then allowed = true; break end end end
|
|
166
|
+
if not root or not allowed then
|
|
167
|
+
self.pending[id] = nil
|
|
168
|
+
result({ content = array({ { type = 'text', text = err or 'projectPath is outside allowed roots' } }), isError = true }); return
|
|
169
|
+
end
|
|
170
|
+
local job, submit_error = submit({ method = tool.name:sub(#'codeoutline_' + 1), projectPath = root,
|
|
171
|
+
allowedRoots = config.roots, query = args.query, budget = args.budget }, function(ok, value)
|
|
172
|
+
if self.pending[id] ~= p then return end
|
|
173
|
+
self.pending[id] = nil
|
|
174
|
+
local output = ok and (tool.name == 'codeoutline_explore' and value.text or xutils.json_pack(value)) or tostring(value)
|
|
175
|
+
result({ content = array({ { type = 'text', text = text.valid(output) } }), isError = not ok })
|
|
176
|
+
end)
|
|
177
|
+
p.job = job
|
|
178
|
+
if not job then
|
|
179
|
+
self.pending[id] = nil
|
|
180
|
+
result({ content = array({ { type = 'text', text = submit_error } }), isError = true })
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
if args.projectPath then start(args.projectPath) else default_project(reply, start) end
|
|
184
|
+
end
|
|
185
|
+
return self
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
return M
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
-- codeoutline/parse.lua — per-file symbol extraction for the code index
|
|
2
|
+
-- behind the CodeExplore tool.
|
|
3
|
+
-- local parse = require('codeoutline.parse')
|
|
4
|
+
-- local result = parse.parse(path, source) -- nil for unsupported files
|
|
5
|
+
--
|
|
6
|
+
-- Tokenizing lives in xscan.lua (the runtime's native `xscan` module when
|
|
7
|
+
-- present, else the pure-Lua implementation); each language is one file
|
|
8
|
+
-- under lang/.
|
|
9
|
+
|
|
10
|
+
local M = {}
|
|
11
|
+
|
|
12
|
+
local parsers = {}
|
|
13
|
+
local function parser(name)
|
|
14
|
+
if not parsers[name] then parsers[name] = require('codeoutline.lang.' .. name) end
|
|
15
|
+
return parsers[name]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
M.EXTENSIONS = {
|
|
19
|
+
c = 'c', h = 'c',
|
|
20
|
+
py = 'python', pyi = 'python',
|
|
21
|
+
java = 'java',
|
|
22
|
+
lua = 'lua',
|
|
23
|
+
cpp = 'cpp', cc = 'cpp', cxx = 'cpp', hpp = 'cpp', hh = 'cpp', hxx = 'cpp', ipp = 'cpp',
|
|
24
|
+
go = 'go',
|
|
25
|
+
js = 'javascript', jsx = 'javascript', mjs = 'javascript', cjs = 'javascript',
|
|
26
|
+
ts = 'typescript', tsx = 'typescript', mts = 'typescript', cts = 'typescript',
|
|
27
|
+
cs = 'csharp',
|
|
28
|
+
rs = 'rust',
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
-- Languages sharing one parser module; the language name is passed along.
|
|
32
|
+
local MODULE = { javascript = 'js', typescript = 'js' }
|
|
33
|
+
|
|
34
|
+
function M.language(path)
|
|
35
|
+
local ext = path:match('%.([%w]+)$')
|
|
36
|
+
return ext and M.EXTENSIONS[ext:lower()]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
function M.parse(path, src)
|
|
40
|
+
local lang = M.language(path)
|
|
41
|
+
if not lang then return nil end
|
|
42
|
+
-- Strip a UTF-8 BOM so it is not read as part of the first token.
|
|
43
|
+
if src:sub(1, 3) == '\239\187\191' then src = src:sub(4) end
|
|
44
|
+
-- .h is shared by C and C++: sniff for C++-only syntax.
|
|
45
|
+
if lang == 'c' and path:match('%.[hH]$') and parser('cpp').looks_cpp(src) then lang = 'cpp' end
|
|
46
|
+
return (parser(MODULE[lang] or lang).parse(path, src, lang))
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
return M
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
local M = {}
|
|
2
|
+
M.windows = package.config:sub(1, 1) == '\\'
|
|
3
|
+
|
|
4
|
+
function M.normalize(p)
|
|
5
|
+
p = p:gsub('\\', '/')
|
|
6
|
+
if p ~= '/' and not p:match('^%a:/$') then p = p:gsub('/+$', '') end
|
|
7
|
+
return p
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
function M.canonical(root)
|
|
11
|
+
if type(root) ~= 'string' or root == '' or root:find('%z') then
|
|
12
|
+
return nil, 'project path must be a non-empty string without NUL'
|
|
13
|
+
end
|
|
14
|
+
if not (xutils and xutils.realpath and xutils.stat) then
|
|
15
|
+
return nil, 'runtime requires xutils.realpath and stat; rebuild the bundled xnet2lua runtime'
|
|
16
|
+
end
|
|
17
|
+
local resolved, err = xutils.realpath(root)
|
|
18
|
+
if not resolved then return nil, 'cannot resolve project path: ' .. tostring(err) end
|
|
19
|
+
resolved = M.normalize(resolved)
|
|
20
|
+
local st = xutils.stat(resolved)
|
|
21
|
+
if not st or st.type ~= 'directory' then return nil, 'project path is not a directory: ' .. root end
|
|
22
|
+
return resolved
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
function M.key(root)
|
|
26
|
+
root = M.normalize(root)
|
|
27
|
+
return M.windows and root:lower() or root
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
function M.contains(root, candidate)
|
|
31
|
+
root, candidate = M.key(root), M.key(candidate)
|
|
32
|
+
local prefix = root:sub(-1) == '/' and root or root .. '/'
|
|
33
|
+
return candidate == root or candidate:sub(1, #prefix) == prefix
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
return M
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
-- Requests within one Lua state are serialized by the owning transport.
|
|
2
|
+
local index = require('codeoutline.index')
|
|
3
|
+
local graph = require('codeoutline.graph')
|
|
4
|
+
local explore = require('codeoutline.explore')
|
|
5
|
+
local paths = require('codeoutline.path')
|
|
6
|
+
local M = {}
|
|
7
|
+
local projects = {}
|
|
8
|
+
local sequence = 0
|
|
9
|
+
local limits = { max_projects = 8, idle_seconds = 900 }
|
|
10
|
+
|
|
11
|
+
function M.configure(opts)
|
|
12
|
+
for _, name in ipairs({ 'max_projects', 'idle_seconds' }) do
|
|
13
|
+
local v = opts[name]
|
|
14
|
+
if v ~= nil then
|
|
15
|
+
assert(type(v) == 'number' and v >= 1 and v < math.huge and v % 1 == 0, name .. ' must be a positive integer')
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
for name in pairs(limits) do if opts[name] ~= nil then limits[name] = opts[name] end end
|
|
19
|
+
M.sweep()
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
function M.sweep(now)
|
|
23
|
+
now = now or os.time()
|
|
24
|
+
local count = 0
|
|
25
|
+
for key, p in pairs(projects) do
|
|
26
|
+
if now - p.used >= limits.idle_seconds then projects[key] = nil else count = count + 1 end
|
|
27
|
+
end
|
|
28
|
+
while count > limits.max_projects do
|
|
29
|
+
local oldest
|
|
30
|
+
for key, p in pairs(projects) do
|
|
31
|
+
if not oldest or p.order < projects[oldest].order then oldest = key end
|
|
32
|
+
end
|
|
33
|
+
projects[oldest] = nil
|
|
34
|
+
count = count - 1
|
|
35
|
+
end
|
|
36
|
+
return count
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
local function key_for(root)
|
|
40
|
+
local canonical, err = index.check_root(root)
|
|
41
|
+
assert(canonical, err)
|
|
42
|
+
return paths.key(canonical), canonical
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
function M.get(root, opts)
|
|
46
|
+
opts = opts or {}
|
|
47
|
+
local key, canonical = key_for(root)
|
|
48
|
+
M.sweep()
|
|
49
|
+
local p = projects[key]
|
|
50
|
+
if p then
|
|
51
|
+
for _, name in ipairs({ 'cache_path', 'max_file_bytes', 'lister' }) do
|
|
52
|
+
if opts[name] ~= nil and opts[name] ~= p.opts[name] then
|
|
53
|
+
error('project option changed; forget the project before changing ' .. name, 2)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
else
|
|
57
|
+
p = { idx = index.open(canonical, opts), opts = {
|
|
58
|
+
cache_path = opts.cache_path, max_file_bytes = opts.max_file_bytes, lister = opts.lister,
|
|
59
|
+
} }
|
|
60
|
+
end
|
|
61
|
+
local t0 = os.clock()
|
|
62
|
+
local stats = p.idx:refresh()
|
|
63
|
+
if not p.G or p.G.generation ~= p.idx.generation then p.G = graph.build(p.idx) end
|
|
64
|
+
-- A failed save leaves the index behind its cache, so the next call retries.
|
|
65
|
+
if p.idx:needs_save() then
|
|
66
|
+
local saved, err = p.idx:save()
|
|
67
|
+
stats.cache_saved, stats.cache_error = saved, err
|
|
68
|
+
end
|
|
69
|
+
stats.seconds = os.clock() - t0
|
|
70
|
+
sequence = sequence + 1
|
|
71
|
+
p.used, p.stats, p.order = os.time(), stats, sequence
|
|
72
|
+
projects[key] = p
|
|
73
|
+
M.sweep()
|
|
74
|
+
return p.idx, p.G, stats
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
function M.explore(root, query, opts)
|
|
78
|
+
explore.validate(query, opts)
|
|
79
|
+
local idx, G, stats = M.get(root, opts)
|
|
80
|
+
idx.query_sources = {}
|
|
81
|
+
local ok, text, info = pcall(function()
|
|
82
|
+
while true do
|
|
83
|
+
local output, details = explore.run(G, idx, query, opts)
|
|
84
|
+
if not details.encoding_retry then return output, details end
|
|
85
|
+
G = graph.build(idx)
|
|
86
|
+
end
|
|
87
|
+
end)
|
|
88
|
+
idx.query_sources = nil
|
|
89
|
+
-- A partial encoding repair can precede an error in another file. Keep
|
|
90
|
+
-- the resident graph consistent even when the query itself fails.
|
|
91
|
+
local p = projects[paths.key(idx.root)]
|
|
92
|
+
if p.G.generation ~= idx.generation then
|
|
93
|
+
p.G = G.generation == idx.generation and G or graph.build(idx)
|
|
94
|
+
end
|
|
95
|
+
if idx:needs_save() then
|
|
96
|
+
local saved, err = idx:save()
|
|
97
|
+
stats.cache_saved, stats.cache_error = saved, err
|
|
98
|
+
end
|
|
99
|
+
if not ok then error(text, 2) end
|
|
100
|
+
info.refresh = stats
|
|
101
|
+
return text, info
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
function M.status(root, opts)
|
|
105
|
+
local idx, G, stats = M.get(root, opts)
|
|
106
|
+
local languages = {}
|
|
107
|
+
for _, rec in ipairs(G.files) do languages[rec.language] = (languages[rec.language] or 0) + 1 end
|
|
108
|
+
return { projectPath = idx.root, files = #G.files, symbols = #G.nodes, edges = G.edge_count,
|
|
109
|
+
generation = idx.generation, languages = languages, refresh = stats,
|
|
110
|
+
cachePath = idx.cache_path, cacheLoaded = idx.cache_loaded, cacheWarning = idx.cache_error,
|
|
111
|
+
enumerator = idx.enumerator, scanner = xscan and os.getenv('XSCAN_PURE_LUA') ~= '1' and 'native' or 'lua',
|
|
112
|
+
residentProjects = M.sweep(), maxProjects = limits.max_projects, idleSeconds = limits.idle_seconds }
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
-- Forget drops memory only. Rebuild bypasses cache and publishes after success.
|
|
116
|
+
function M.forget(root)
|
|
117
|
+
local key = key_for(root)
|
|
118
|
+
local existed = projects[key] ~= nil
|
|
119
|
+
projects[key] = nil
|
|
120
|
+
return existed
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
function M.rebuild(root, opts)
|
|
124
|
+
local key = key_for(root)
|
|
125
|
+
local previous = projects[key]
|
|
126
|
+
local config = {}
|
|
127
|
+
for k, v in pairs(opts or (previous and previous.opts) or {}) do config[k] = v end
|
|
128
|
+
config.rebuild = true
|
|
129
|
+
projects[key] = nil
|
|
130
|
+
local ok, result = pcall(M.status, root, config)
|
|
131
|
+
if not ok then projects[key] = previous; error(result, 2) end
|
|
132
|
+
return result
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
return M
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
local M = {}
|
|
2
|
+
|
|
3
|
+
-- Preserve valid UTF-8 at byte boundaries, and replace malformed source bytes
|
|
4
|
+
-- before emitting protocol text (source files are not necessarily UTF-8).
|
|
5
|
+
function M.valid(s)
|
|
6
|
+
local out, i = {}, 1
|
|
7
|
+
while i <= #s do
|
|
8
|
+
local b = s:byte(i)
|
|
9
|
+
local n = b < 128 and 1 or b >= 194 and b <= 223 and 2
|
|
10
|
+
or b >= 224 and b <= 239 and 3 or b >= 240 and b <= 244 and 4 or 0
|
|
11
|
+
local ok = n > 0 and i + n - 1 <= #s
|
|
12
|
+
for j = 1, n - 1 do
|
|
13
|
+
local c = s:byte(i + j)
|
|
14
|
+
if not c or c < 128 or c > 191 then ok = false end
|
|
15
|
+
end
|
|
16
|
+
local c = s:byte(i + 1) or 0
|
|
17
|
+
if (b == 224 and c < 160) or (b == 237 and c > 159)
|
|
18
|
+
or (b == 240 and c < 144) or (b == 244 and c > 143) then ok = false end
|
|
19
|
+
out[#out + 1] = ok and s:sub(i, i + n - 1) or '\239\191\189'
|
|
20
|
+
i = i + (ok and n or 1)
|
|
21
|
+
end
|
|
22
|
+
return table.concat(out)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
function M.prefix(s, limit)
|
|
26
|
+
local last = math.min(#s, math.max(0, limit))
|
|
27
|
+
if last < #s then
|
|
28
|
+
while last > 0 and s:byte(last + 1) >= 128 and s:byte(last + 1) <= 191 do last = last - 1 end
|
|
29
|
+
end
|
|
30
|
+
return s:sub(1, last)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
function M.limit(s, budget, omitted)
|
|
34
|
+
s = M.valid(s)
|
|
35
|
+
local truncated = #s > budget or omitted
|
|
36
|
+
if truncated then
|
|
37
|
+
local marker = '\n[Results truncated; narrow the query or increase budget.]'
|
|
38
|
+
s = M.prefix(s, budget - #marker) .. marker
|
|
39
|
+
end
|
|
40
|
+
return s, truncated == true
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
return M
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
return '0.1.0'
|