@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,404 @@
|
|
|
1
|
+
-- index.lua — per-project symbol index: which files exist, what each one
|
|
2
|
+
-- defines and calls, kept fresh incrementally and cached on disk.
|
|
3
|
+
--
|
|
4
|
+
-- local index = require('codeoutline.index')
|
|
5
|
+
-- local idx = index.open('C:/src/proj') -- loads the cache if present
|
|
6
|
+
-- local stats = idx:refresh() -- reparses only changed files
|
|
7
|
+
-- idx:save()
|
|
8
|
+
--
|
|
9
|
+
-- Change detection trusts an unchanged size + mtime once the file was last
|
|
10
|
+
-- read strictly after that mtime second, skipping the read entirely; otherwise
|
|
11
|
+
-- it reads the file and compares size + CRC-32, which still skips the parse.
|
|
12
|
+
-- Tools that restore an older mtime at the same size are not detected.
|
|
13
|
+
|
|
14
|
+
local ci = require('codeoutline.parse')
|
|
15
|
+
local paths = require('codeoutline.path')
|
|
16
|
+
local control = require('codeoutline.control')
|
|
17
|
+
|
|
18
|
+
local M = {}
|
|
19
|
+
|
|
20
|
+
-- Cache version: a checksum of the parser sources, so any parser change
|
|
21
|
+
-- rebuilds stale caches instead of silently reusing old parse results.
|
|
22
|
+
M.VERSION = (function()
|
|
23
|
+
local parts = {}
|
|
24
|
+
for _, m in ipairs({ 'xscan', 'text', 'common', 'parse', 'lang.c', 'lang.cpp', 'lang.lua', 'lang.python',
|
|
25
|
+
'lang.java', 'lang.go', 'lang.js', 'lang.csharp', 'lang.rust' }) do
|
|
26
|
+
local file = package.searchpath and package.searchpath('codeoutline.' .. m, package.path)
|
|
27
|
+
local h = file and io.open(file, 'rb')
|
|
28
|
+
if h then parts[#parts + 1] = h:read('a'); h:close() end
|
|
29
|
+
end
|
|
30
|
+
return 'p3-lazy-encoding-' .. xutils.sha256_hex(table.concat(parts))
|
|
31
|
+
end)()
|
|
32
|
+
-- Generated amalgamations (sqlite3.c, minified bundles) cost more to index
|
|
33
|
+
-- than they give back; skip files above this size unless configured.
|
|
34
|
+
M.MAX_FILE_BYTES = 1500000
|
|
35
|
+
|
|
36
|
+
local Index = {}
|
|
37
|
+
Index.__index = Index
|
|
38
|
+
|
|
39
|
+
local norm = paths.normalize
|
|
40
|
+
|
|
41
|
+
local function home()
|
|
42
|
+
return os.getenv('USERPROFILE') or os.getenv('HOME') or '.'
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
-- Cache file name derived from the root so projects don't collide.
|
|
46
|
+
local function cache_path_for(root)
|
|
47
|
+
assert(xutils and xutils.sha256_hex, 'runtime requires SHA-256')
|
|
48
|
+
return norm(home()) .. '/.codeoutline/cache/' .. xutils.sha256_hex(paths.key(root)) .. '.idx'
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
local function slurp(path)
|
|
52
|
+
local f = io.open(path, 'rb')
|
|
53
|
+
if not f then return nil end
|
|
54
|
+
local s = f:read('a')
|
|
55
|
+
f:close()
|
|
56
|
+
return s
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
local function checksum(s)
|
|
60
|
+
if xcompress and xcompress.crc32 then return xcompress.crc32(s) end
|
|
61
|
+
return xutils.sha256_hex(s)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
-- Characters a shell interprets even inside double quotes (cmd: %VAR%,
|
|
65
|
+
-- !VAR!, the quote itself; sh: $, backtick, backslash) plus newlines. The
|
|
66
|
+
-- file listing runs `rg` through io.popen -- the runtime has no argv spawn on
|
|
67
|
+
-- Windows -- so a root is only accepted when quoting it is inert.
|
|
68
|
+
-- (backslashes are normalized to / first, so any left came from elsewhere)
|
|
69
|
+
local UNSAFE = '["%%!$`\\\r\n]'
|
|
70
|
+
|
|
71
|
+
-- Validate a project root: an existing directory whose path is safe to quote.
|
|
72
|
+
-- Returns the normalized root, or nil and a reason.
|
|
73
|
+
function M.check_root(root)
|
|
74
|
+
local r, err = paths.canonical(root)
|
|
75
|
+
if not r then return nil, err end
|
|
76
|
+
if r:find(UNSAFE) then
|
|
77
|
+
return nil, 'root contains characters that are unsafe in a shell command: ' .. root
|
|
78
|
+
end
|
|
79
|
+
return r
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
function M.open(root, opts)
|
|
83
|
+
opts = opts or {}
|
|
84
|
+
local checked, why = M.check_root(root)
|
|
85
|
+
if not checked then error(why, 2) end
|
|
86
|
+
root = checked
|
|
87
|
+
if opts.lister ~= nil and opts.lister ~= 'rg' and opts.lister ~= 'walk' then error('invalid lister', 2) end
|
|
88
|
+
local max_bytes = opts.max_file_bytes or M.MAX_FILE_BYTES
|
|
89
|
+
assert(type(max_bytes) == 'number' and max_bytes >= 1 and max_bytes < math.huge and max_bytes % 1 == 0,
|
|
90
|
+
'max_file_bytes must be a positive integer')
|
|
91
|
+
local self = setmetatable({
|
|
92
|
+
root = norm(root),
|
|
93
|
+
cache_path = opts.cache_path and norm(opts.cache_path) or cache_path_for(root),
|
|
94
|
+
max_bytes = max_bytes,
|
|
95
|
+
lister = opts.lister, -- 'rg' | 'walk' | nil (rg when it runs here)
|
|
96
|
+
files = {}, -- rel path -> record
|
|
97
|
+
generation = 0, -- bumped on every change; graph caches key on it
|
|
98
|
+
}, Index)
|
|
99
|
+
self.cache_loaded = not opts.rebuild and self:load() or false
|
|
100
|
+
return self
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
function Index:load()
|
|
104
|
+
local data = slurp(self.cache_path)
|
|
105
|
+
if not data or not cmsgpack then return false end
|
|
106
|
+
local ok, files = pcall(function()
|
|
107
|
+
local envelope = cmsgpack.unpack(data)
|
|
108
|
+
assert(type(envelope) == 'table' and type(envelope.payload) == 'string')
|
|
109
|
+
assert(envelope.sha256 == xutils.sha256_hex(envelope.payload))
|
|
110
|
+
local t = cmsgpack.unpack(envelope.payload)
|
|
111
|
+
assert(t.version == M.VERSION and t.root == paths.key(self.root) and type(t.files) == 'table')
|
|
112
|
+
local records = {}
|
|
113
|
+
for _, rec in ipairs(t.files) do
|
|
114
|
+
assert(type(rec) == 'table' and type(rec.path) == 'string')
|
|
115
|
+
assert(not rec.path:find('[\\:%z]') and rec.path:sub(1, 1) ~= '/')
|
|
116
|
+
for part in rec.path:gmatch('[^/]+') do assert(part ~= '..' and part ~= '.') end
|
|
117
|
+
assert(type(rec.nodes) == 'table' and type(rec.refs) == 'table' and type(rec.imports) == 'table')
|
|
118
|
+
assert(type(rec.size) == 'number' and type(rec.language) == 'string')
|
|
119
|
+
assert(rec.encoding == nil or rec.encoding == 'utf-8' or rec.encoding == 'utf-8-bom' or rec.encoding == 'gbk')
|
|
120
|
+
for _, n in ipairs(rec.nodes) do
|
|
121
|
+
assert(type(n.name) == 'string' and type(n.qualified) == 'string' and type(n.kind) == 'string')
|
|
122
|
+
assert(type(n.line) == 'number' and type(n.end_line) == 'number' and n.line >= 1 and n.end_line >= n.line)
|
|
123
|
+
end
|
|
124
|
+
for _, r in ipairs(rec.refs) do assert(type(r.name) == 'string' and type(r.from) == 'number') end
|
|
125
|
+
for _, imp in ipairs(rec.imports) do assert(type(imp.path) == 'string') end
|
|
126
|
+
records[rec.path] = rec
|
|
127
|
+
end
|
|
128
|
+
return records
|
|
129
|
+
end)
|
|
130
|
+
if not ok then self.cache_error = 'invalid or stale cache; rebuilding'; return false end
|
|
131
|
+
self.files = files
|
|
132
|
+
self.generation = self.generation + 1
|
|
133
|
+
self.saved_generation = self.generation
|
|
134
|
+
return true
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
-- Whether the cache file lags the records. Refresh metadata (mtime, checked)
|
|
138
|
+
-- alone does not count: unchanged files are re-verified by checksum anyway.
|
|
139
|
+
function Index:needs_save()
|
|
140
|
+
return self.saved_generation ~= self.generation or self.encoding_dirty == true
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
function Index:save()
|
|
144
|
+
if not (cmsgpack and xutils.temp_file and xutils.replace_file) then
|
|
145
|
+
return false, 'runtime requires cmsgpack, temp_file and replace_file'
|
|
146
|
+
end
|
|
147
|
+
local list = {}
|
|
148
|
+
for _, rec in pairs(self.files) do list[#list + 1] = rec end
|
|
149
|
+
table.sort(list, function(a, b) return a.path < b.path end)
|
|
150
|
+
local payload = cmsgpack.pack({ version = M.VERSION, root = paths.key(self.root), files = list })
|
|
151
|
+
local blob = cmsgpack.pack({ sha256 = xutils.sha256_hex(payload), payload = payload })
|
|
152
|
+
local d = self.cache_path:match('^(.*)/[^/]*$') or '.'
|
|
153
|
+
local made, err = xutils.mkdir_p(d)
|
|
154
|
+
if not made then return false, err end
|
|
155
|
+
local tmp
|
|
156
|
+
tmp, err = xutils.temp_file(d)
|
|
157
|
+
if not tmp then return false, err end
|
|
158
|
+
local f
|
|
159
|
+
f, err = io.open(tmp, 'wb')
|
|
160
|
+
if not f then os.remove(tmp); return false, err end
|
|
161
|
+
local wrote, write_err = f:write(blob)
|
|
162
|
+
local closed, close_err = f:close()
|
|
163
|
+
if not wrote or not closed then os.remove(tmp); return false, write_err or close_err end
|
|
164
|
+
local published
|
|
165
|
+
published, err = xutils.replace_file(tmp, self.cache_path)
|
|
166
|
+
if not published then os.remove(tmp); return false, err end
|
|
167
|
+
self.saved_generation = self.generation
|
|
168
|
+
self.encoding_dirty = false
|
|
169
|
+
return true
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
-- Only query-selected files are decoded. This cache lasts for one query and
|
|
173
|
+
-- holds the exact snapshot used for both reparsing and source rendering.
|
|
174
|
+
function Index:query_source(rel)
|
|
175
|
+
self.query_sources = self.query_sources or {}
|
|
176
|
+
if self.query_sources[rel] then return self.query_sources[rel], false end
|
|
177
|
+
local rec = assert(self.files[rel], 'source is not indexed: ' .. rel)
|
|
178
|
+
local raw = assert(slurp(self:abs(rel)), 'cannot read source: ' .. rel)
|
|
179
|
+
assert(#raw == rec.size and checksum(raw) == rec.crc,
|
|
180
|
+
'source changed during query; retry: ' .. rel)
|
|
181
|
+
assert(xutils.to_utf8, 'rebuild xnet2lua: missing xutils.to_utf8')
|
|
182
|
+
local src, encoding
|
|
183
|
+
if rec.encoding == 'utf-8' then
|
|
184
|
+
src, encoding = raw, rec.encoding
|
|
185
|
+
elseif rec.encoding == 'utf-8-bom' then
|
|
186
|
+
src, encoding = raw:sub(4), rec.encoding
|
|
187
|
+
else
|
|
188
|
+
src, encoding = xutils.to_utf8(raw, rec.encoding == 'gbk' and 'gbk' or 'auto')
|
|
189
|
+
if not src then
|
|
190
|
+
local reason = tostring(encoding)
|
|
191
|
+
-- Minimal EL8/EL9 installs and containers omit the GBK module.
|
|
192
|
+
local hint = reason:find('does not provide GBK', 1, true)
|
|
193
|
+
and ' (install the system GBK converter, e.g. glibc-gconv-extra on EL8/EL9)' or ''
|
|
194
|
+
error(rel .. ': ' .. reason .. hint, 0)
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
local changed = false
|
|
198
|
+
if not rec.encoding then
|
|
199
|
+
if encoding == 'gbk' then
|
|
200
|
+
local parsed = assert(ci.parse(rel, src), 'cannot parse decoded source: ' .. rel)
|
|
201
|
+
parsed.size, parsed.crc, parsed.mtime, parsed.checked = rec.size, rec.crc, rec.mtime, rec.checked
|
|
202
|
+
self.files[rel], rec = parsed, parsed
|
|
203
|
+
self.generation = self.generation + 1
|
|
204
|
+
changed = true
|
|
205
|
+
end
|
|
206
|
+
rec.encoding = encoding
|
|
207
|
+
self.encoding_dirty = true
|
|
208
|
+
end
|
|
209
|
+
self.query_sources[rel] = src
|
|
210
|
+
return src, changed
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
-- Supported source files under the root, honoring .gitignore via ripgrep.
|
|
214
|
+
-- ripgrep lists files honoring every .gitignore; probe once whether it runs
|
|
215
|
+
-- here (Android has no rg, and there a Lua walk takes over).
|
|
216
|
+
local has_rg
|
|
217
|
+
local function rg_available()
|
|
218
|
+
if has_rg == nil then
|
|
219
|
+
has_rg = false
|
|
220
|
+
if io.popen then
|
|
221
|
+
local ok, p = pcall(io.popen, 'rg --version' .. (package.config:sub(1, 1) == '\\' and ' 2>nul' or ' 2>/dev/null'))
|
|
222
|
+
if ok and p then
|
|
223
|
+
has_rg = (p:read('a') or ''):find('ripgrep', 1, true) ~= nil
|
|
224
|
+
p:close()
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
return has_rg
|
|
229
|
+
end
|
|
230
|
+
M.rg_available = rg_available
|
|
231
|
+
|
|
232
|
+
-- Directories the walk never enters, besides hidden ones (like rg's default).
|
|
233
|
+
M.WALK_SKIP = { node_modules = true, __pycache__ = true }
|
|
234
|
+
M.MAX_WALK_FILES = 100000
|
|
235
|
+
|
|
236
|
+
-- Root .gitignore, the common subset: `name`, `*.ext`, `dir/`, `/anchored`,
|
|
237
|
+
-- `a/b` (anchored), `**`. Negations are ignored (keep the file listed).
|
|
238
|
+
local function load_ignore(root)
|
|
239
|
+
local rules = {}
|
|
240
|
+
local f = io.open(root .. '/.gitignore', 'rb')
|
|
241
|
+
if not f then return rules end
|
|
242
|
+
for raw in f:lines() do
|
|
243
|
+
local line = raw:gsub('\r$', ''):gsub('%s+$', '')
|
|
244
|
+
if line ~= '' and not line:match('^#') and not line:match('^!') then
|
|
245
|
+
local dir_only = line:sub(-1) == '/'
|
|
246
|
+
if dir_only then line = line:sub(1, -2) end
|
|
247
|
+
local anchored = line:sub(1, 1) == '/' or line:find('/', 1, true) ~= nil
|
|
248
|
+
line = line:gsub('^/', '')
|
|
249
|
+
local pat = line:gsub('[%^%$%(%)%%%.%[%]%+%-]', '%%%0')
|
|
250
|
+
:gsub('%*%*/', '\1'):gsub('%*%*', '\2'):gsub('%*', '[^/]*'):gsub('%?', '[^/]')
|
|
251
|
+
:gsub('\1', '.-'):gsub('\2', '.*')
|
|
252
|
+
rules[#rules + 1] = { pat = '^' .. pat .. '$', dir_only = dir_only, anchored = anchored }
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
f:close()
|
|
256
|
+
return rules
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
local function ignored(rules, rel, name, is_dir)
|
|
260
|
+
for _, r in ipairs(rules) do
|
|
261
|
+
if (is_dir or not r.dir_only) and (r.anchored and rel or name):find(r.pat) then return true end
|
|
262
|
+
end
|
|
263
|
+
return false
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
-- Supported files under the root without a subprocess (xutils.list_dir).
|
|
267
|
+
function Index:walk_files()
|
|
268
|
+
local out = {}
|
|
269
|
+
assert(xutils and xutils.list_dir, 'file enumeration unavailable: install rg or use a runtime with list_dir')
|
|
270
|
+
local rules = load_ignore(self.root)
|
|
271
|
+
local dirs = { '' }
|
|
272
|
+
local visited = 0
|
|
273
|
+
while #dirs > 0 do
|
|
274
|
+
control.check()
|
|
275
|
+
local rel_dir = table.remove(dirs)
|
|
276
|
+
local entries, truncated = xutils.list_dir(rel_dir == '' and self.root or (self.root .. '/' .. rel_dir), M.MAX_WALK_FILES + 1)
|
|
277
|
+
assert(entries, 'cannot enumerate directory: ' .. rel_dir .. ': ' .. tostring(truncated))
|
|
278
|
+
assert(not truncated, 'directory enumeration limit exceeded')
|
|
279
|
+
for _, e in ipairs(entries) do
|
|
280
|
+
visited = visited + 1
|
|
281
|
+
assert(visited <= M.MAX_WALK_FILES, 'directory enumeration limit exceeded')
|
|
282
|
+
local name = e.name
|
|
283
|
+
local rel = rel_dir == '' and name or (rel_dir .. '/' .. name)
|
|
284
|
+
local st = xutils.stat(self.root .. '/' .. rel)
|
|
285
|
+
if st and st.type ~= 'link' and name:sub(1, 1) ~= '.' and not ignored(rules, rel, name, e.dir) then
|
|
286
|
+
if e.dir then
|
|
287
|
+
if not M.WALK_SKIP[name] then dirs[#dirs + 1] = rel end
|
|
288
|
+
elseif ci.language(name) then
|
|
289
|
+
out[#out + 1] = rel
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
table.sort(out)
|
|
295
|
+
return out
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
function Index:list_files()
|
|
299
|
+
if self.lister == 'walk' or (self.lister ~= 'rg' and not rg_available()) then
|
|
300
|
+
self.enumerator = 'walk'
|
|
301
|
+
return self:walk_files()
|
|
302
|
+
end
|
|
303
|
+
assert(rg_available(), 'rg file enumeration requested but ripgrep is unavailable')
|
|
304
|
+
self.enumerator = 'rg'
|
|
305
|
+
-- self.root passed check_root() in open(); `--` stops rg from reading a
|
|
306
|
+
-- root that starts with '-' as an option (`--pre=<cmd>` runs a command).
|
|
307
|
+
assert(M.check_root(self.root), 'unsafe root')
|
|
308
|
+
-- Filter extensions afterward: positive -g globs override .gitignore.
|
|
309
|
+
local cmd = 'rg --files --null --no-require-git --no-messages -- "' .. self.root .. '"'
|
|
310
|
+
local p = io.popen(cmd .. (package.config:sub(1, 1) == '\\' and ' 2>nul' or ' 2>/dev/null'))
|
|
311
|
+
assert(p, 'cannot start rg file enumeration')
|
|
312
|
+
local out = {}
|
|
313
|
+
local prefix = self.root:sub(-1) == '/' and self.root or self.root .. '/'
|
|
314
|
+
local listing = p:read('a')
|
|
315
|
+
local ok, _, code = p:close()
|
|
316
|
+
assert(ok or code == 1, 'rg file enumeration failed: ' .. tostring(code))
|
|
317
|
+
for line in listing:gmatch('([^%z]+)%z') do
|
|
318
|
+
local path = norm(line)
|
|
319
|
+
if path:sub(1, #prefix) == prefix then path = path:sub(#prefix + 1) end
|
|
320
|
+
if ci.language(path) then
|
|
321
|
+
out[#out + 1] = path
|
|
322
|
+
assert(#out <= M.MAX_WALK_FILES, 'file enumeration limit exceeded')
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
table.sort(out)
|
|
326
|
+
return out
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
-- Bring the index in line with the tree. Returns counts of what changed.
|
|
330
|
+
function Index:refresh()
|
|
331
|
+
local stats = { parsed = 0, unchanged = 0, removed = 0, skipped = 0, failed = 0 }
|
|
332
|
+
local seen = {}
|
|
333
|
+
local has_stat = xutils and xutils.stat
|
|
334
|
+
for _, rel in ipairs(self:list_files()) do
|
|
335
|
+
control.check()
|
|
336
|
+
seen[rel] = true
|
|
337
|
+
local abs = self:abs(rel)
|
|
338
|
+
local rec = self.files[rel]
|
|
339
|
+
local st = has_stat and xutils.stat(abs) or nil
|
|
340
|
+
if st and (st.exists == false or st.type ~= 'file') then
|
|
341
|
+
seen[rel] = nil
|
|
342
|
+
elseif st and st.size and st.size > self.max_bytes then
|
|
343
|
+
stats.skipped = stats.skipped + 1
|
|
344
|
+
seen[rel] = nil
|
|
345
|
+
elseif st and rec and st.mtime and rec.size == st.size and rec.mtime == st.mtime
|
|
346
|
+
and (rec.checked or 0) > st.mtime + 1 then
|
|
347
|
+
-- (size, mtime) proves "unchanged" only once the content was read
|
|
348
|
+
-- strictly after that mtime second: an edit within the same second
|
|
349
|
+
-- keeps both equal forever (the "racy git" problem).
|
|
350
|
+
stats.unchanged = stats.unchanged + 1
|
|
351
|
+
else
|
|
352
|
+
local src = slurp(abs)
|
|
353
|
+
if not src or #src > self.max_bytes then
|
|
354
|
+
stats.skipped = stats.skipped + 1
|
|
355
|
+
seen[rel] = nil
|
|
356
|
+
else
|
|
357
|
+
local crc = checksum(src)
|
|
358
|
+
if rec and rec.size == #src and rec.crc == crc then
|
|
359
|
+
rec.mtime = st and st.mtime or rec.mtime
|
|
360
|
+
rec.checked = os.time()
|
|
361
|
+
stats.unchanged = stats.unchanged + 1
|
|
362
|
+
else
|
|
363
|
+
local ok, r = pcall(ci.parse, rel, src)
|
|
364
|
+
control.check()
|
|
365
|
+
-- A parser exception must not permanently hide a GBK file.
|
|
366
|
+
-- Successful speculative parsing does not scan encoding here.
|
|
367
|
+
if not ok or not r then
|
|
368
|
+
if xutils.to_utf8 then
|
|
369
|
+
local decoded, encoding = xutils.to_utf8(src)
|
|
370
|
+
if decoded then
|
|
371
|
+
ok, r = pcall(ci.parse, rel, decoded)
|
|
372
|
+
if ok and r then r.encoding = encoding end
|
|
373
|
+
end
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
if ok and r then
|
|
377
|
+
r.size, r.crc, r.mtime = #src, crc, st and st.mtime or nil
|
|
378
|
+
r.checked = os.time()
|
|
379
|
+
self.files[rel] = r
|
|
380
|
+
stats.parsed = stats.parsed + 1
|
|
381
|
+
else
|
|
382
|
+
stats.failed = stats.failed + 1
|
|
383
|
+
seen[rel] = nil
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
end
|
|
388
|
+
end
|
|
389
|
+
for rel in pairs(self.files) do
|
|
390
|
+
if not seen[rel] then
|
|
391
|
+
self.files[rel] = nil
|
|
392
|
+
stats.removed = stats.removed + 1
|
|
393
|
+
end
|
|
394
|
+
end
|
|
395
|
+
if stats.parsed > 0 or stats.removed > 0 then self.generation = self.generation + 1 end
|
|
396
|
+
return stats
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
-- Absolute path for a record's relative path.
|
|
400
|
+
function Index:abs(rel)
|
|
401
|
+
return self.root .. '/' .. rel
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
return M
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
local source = assert(xutils.realpath(debug.getinfo(1, 'S').source:sub(2))):gsub('\\', '/')
|
|
2
|
+
local scripts = assert(source:match('^(.*)/codeoutline/[^/]+$'))
|
|
3
|
+
package.path = scripts .. '/?.lua;' .. package.path
|
|
4
|
+
local service = require('codeoutline.service')
|
|
5
|
+
local paths = require('codeoutline.path')
|
|
6
|
+
local control = require('codeoutline.control')
|
|
7
|
+
local shared = assert(xshared.dict('codeoutline_control'))
|
|
8
|
+
|
|
9
|
+
local function execute(req)
|
|
10
|
+
local root = assert(paths.canonical(req.projectPath))
|
|
11
|
+
local allowed = false
|
|
12
|
+
for _, configured in ipairs(req.allowedRoots) do
|
|
13
|
+
if paths.contains(configured, root) then allowed = true; break end
|
|
14
|
+
end
|
|
15
|
+
assert(allowed, 'project path is outside allowed roots')
|
|
16
|
+
if req.method == 'explore' then
|
|
17
|
+
local text, info = service.explore(root, req.query, { budget = req.budget })
|
|
18
|
+
return { text = text, truncated = info.truncated or false, refresh = info.refresh }
|
|
19
|
+
elseif req.method == 'status' then return service.status(root)
|
|
20
|
+
elseif req.method == 'rebuild' then return service.rebuild(root)
|
|
21
|
+
else error('unknown operation') end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
__init = function() assert(xthread.post(1, 'worker_ready')) end,
|
|
26
|
+
__update = function() service.sweep() end,
|
|
27
|
+
__thread_handle = function(_, op, id, req)
|
|
28
|
+
if op ~= 'run' then return end
|
|
29
|
+
control.callback = function()
|
|
30
|
+
if shared:get('shutdown') or shared:get('cancel:' .. id) then error('Request cancelled', 0) end
|
|
31
|
+
if xtimer.now_ms() > req.deadline then error('Indexing deadline exceeded', 0) end
|
|
32
|
+
end
|
|
33
|
+
-- Lua graph/parse loops are interruptible. Native scanning is bounded
|
|
34
|
+
-- by max_file_bytes; explicit checkpoints also cover protected parses.
|
|
35
|
+
debug.sethook(control.check, '', 20000)
|
|
36
|
+
local ok, result = pcall(function() control.check(); return execute(req) end)
|
|
37
|
+
debug.sethook()
|
|
38
|
+
control.callback = nil
|
|
39
|
+
if not ok then pcall(service.forget, req.projectPath) end
|
|
40
|
+
shared:delete('cancel:' .. id)
|
|
41
|
+
assert(xthread.post(1, 'worker_result', id, ok, result))
|
|
42
|
+
end,
|
|
43
|
+
}
|