@codua/codeoutline-win32-x64 0.1.9 → 0.2.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.
Files changed (35) hide show
  1. package/README.md +32 -5
  2. package/README.zh-CN.md +32 -5
  3. package/THIRD_PARTY.md +3 -3
  4. package/bin/xnet.exe +0 -0
  5. package/build-info.json +1 -1
  6. package/package.json +1 -1
  7. package/scripts/codeoutline/cli.lua +2 -2
  8. package/scripts/codeoutline/command.lua +9 -1
  9. package/scripts/codeoutline/common.lua +36 -5
  10. package/scripts/codeoutline/completion.lua +913 -0
  11. package/scripts/codeoutline/control.lua +5 -1
  12. package/scripts/codeoutline/documents.lua +154 -0
  13. package/scripts/codeoutline/explore.lua +32 -7
  14. package/scripts/codeoutline/graph.lua +264 -68
  15. package/scripts/codeoutline/index.lua +168 -48
  16. package/scripts/codeoutline/index_worker.lua +108 -17
  17. package/scripts/codeoutline/lang/c.lua +22 -12
  18. package/scripts/codeoutline/lang/cpp.lua +13 -12
  19. package/scripts/codeoutline/lang/csharp.lua +21 -21
  20. package/scripts/codeoutline/lang/go.lua +10 -10
  21. package/scripts/codeoutline/lang/java.lua +9 -9
  22. package/scripts/codeoutline/lang/js.lua +15 -14
  23. package/scripts/codeoutline/lang/lua.lua +9 -6
  24. package/scripts/codeoutline/lang/python.lua +6 -6
  25. package/scripts/codeoutline/lang/rust.lua +23 -15
  26. package/scripts/codeoutline/lsp.lua +574 -0
  27. package/scripts/codeoutline/main.lua +28 -5
  28. package/scripts/codeoutline/navigation.lua +934 -0
  29. package/scripts/codeoutline/parse.lua +8 -2
  30. package/scripts/codeoutline/parse_pool.lua +189 -0
  31. package/scripts/codeoutline/parse_worker.lua +24 -0
  32. package/scripts/codeoutline/service.lua +82 -31
  33. package/scripts/codeoutline/update_sequence.lua +1 -1
  34. package/scripts/codeoutline/version.lua +1 -1
  35. package/licenses/rpmalloc.txt +0 -12
@@ -1,4 +1,8 @@
1
- -- Per-Lua-state cancellation checkpoint installed only by the service worker.
1
+ -- Per-Lua-state checkpoint for worker cancellation or cooperative indexing.
2
2
  local M = {}
3
+ local error_mt = { __tostring = function(e) return e.message end }
4
+ function M.cancelled(message) return setmetatable({ code = 'cancelled', message = message or 'Request cancelled' }, error_mt) end
5
+ function M.deadline() return setmetatable({ code = 'deadline', message = 'Indexing deadline exceeded' }, error_mt) end
6
+ function M.is_interrupted(value) return getmetatable(value) == error_mt end
3
7
  function M.check() if M.callback then M.callback() end end
4
8
  return M
@@ -0,0 +1,154 @@
1
+ local parse = require('codeoutline.parse')
2
+ local paths = require('codeoutline.path')
3
+ local text = require('codeoutline.text')
4
+ local M = { MAX_BYTES = 1500000 }
5
+ local Doc = {}
6
+ Doc.__index = Doc
7
+
8
+ function M.path(uri)
9
+ assert(type(uri) == 'string' and not uri:find('[%z\r\n]'), 'invalid document URI')
10
+ local host, path = uri:match('^file://([^/?#]*)(/[^?#]*)$')
11
+ if not path then return nil end
12
+ assert(not path:gsub('%%(%x%x)', ''):find('%%'), 'invalid URI escape')
13
+ path = path:gsub('%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end)
14
+ assert(not path:find('[%z\r\n]'), 'invalid file path')
15
+ if host ~= '' and host:lower() ~= 'localhost' then path = '//' .. host .. path
16
+ else path = path:match('^/(%a:.*)$') or path end
17
+ path = paths.normalize(path)
18
+ local prefix, rest
19
+ if path:match('^%a:/') then prefix, rest = path:sub(1, 3), path:sub(4)
20
+ elseif path:sub(1, 2) == '//' then prefix, rest = '//', path:sub(3)
21
+ else prefix, rest = '/', path:sub(2) end
22
+ local parts = {}
23
+ for part in rest:gmatch('[^/]+') do
24
+ if part == '..' then assert(#parts > 0, 'path escapes its root'); parts[#parts] = nil
25
+ elseif part ~= '.' then parts[#parts + 1] = part end
26
+ end
27
+ return prefix .. table.concat(parts, '/')
28
+ end
29
+
30
+ function M.uri(path)
31
+ path = paths.normalize(path)
32
+ local host = ''
33
+ if path:sub(1, 2) == '//' then host, path = assert(path:match('^//([^/]+)(/.*)$')) end
34
+ if path:match('^%a:/') then path = '/' .. path end
35
+ local escaped = path:gsub('[^%w%-%._~/:]', function(c) return string.format('%%%02X', c:byte()) end)
36
+ return 'file://' .. host .. escaped
37
+ end
38
+
39
+ function M.new(uri, source, version, language)
40
+ assert(type(source) == 'string' and #source <= M.MAX_BYTES, 'document exceeds size limit')
41
+ assert(text.valid(source) == source, 'document is not valid UTF-8')
42
+ if source:sub(1, 3) == '\239\187\191' then source = source:sub(4) end
43
+ local self = setmetatable({ uri = uri, path = M.path(uri), source = source,
44
+ version = version, language = language, lines = { 0 }, line_units = { 0 },
45
+ checkpoints = { offsets = { 0 }, units = { 0 } } }, Doc)
46
+ local p, units, checkpoint = 1, 0, 0
47
+ while p <= #source do
48
+ local b = source:byte(p)
49
+ if b == 13 then
50
+ if source:byte(p + 1) == 10 then p = p + 1; units = units + 1 end
51
+ units = units + 1
52
+ self.lines[#self.lines + 1] = p
53
+ self.line_units[#self.line_units + 1] = units
54
+ elseif b == 10 then
55
+ units = units + 1
56
+ self.lines[#self.lines + 1] = p
57
+ self.line_units[#self.line_units + 1] = units
58
+ else
59
+ local width = b < 128 and 1 or b < 224 and 2 or b < 240 and 3 or 4
60
+ p, units = p + width - 1, units + (width == 4 and 2 or 1)
61
+ end
62
+ -- Sparse prefix counts keep repeated positions on long lines bounded.
63
+ if p - checkpoint >= 512 then
64
+ local points = self.checkpoints
65
+ points.offsets[#points.offsets + 1], points.units[#points.units + 1] = p, units
66
+ checkpoint = p
67
+ end
68
+ p = p + 1
69
+ end
70
+ return self
71
+ end
72
+
73
+ function Doc:position(offset)
74
+ assert(type(offset) == 'number' and offset >= 0 and offset <= #self.source, 'invalid byte offset')
75
+ local lo, hi = 1, #self.lines
76
+ while lo < hi do
77
+ local mid = math.floor((lo + hi + 1) / 2)
78
+ if self.lines[mid] <= offset then lo = mid else hi = mid - 1 end
79
+ end
80
+ local line = lo
81
+ local next_line = self.lines[line + 1]
82
+ if next_line then
83
+ local last = next_line - 1
84
+ if self.source:byte(next_line) == 10 and self.source:byte(last) == 13 then last = last - 1 end
85
+ offset = math.min(offset, last)
86
+ end
87
+ local points = self.checkpoints
88
+ lo, hi = 1, #points.offsets
89
+ while lo < hi do
90
+ local mid = math.floor((lo + hi + 1) / 2)
91
+ if points.offsets[mid] <= offset then lo = mid else hi = mid - 1 end
92
+ end
93
+ local units, p = points.units[lo], points.offsets[lo] + 1
94
+ while p <= offset do
95
+ local b = self.source:byte(p)
96
+ local width = b < 128 and 1 or b < 224 and 2 or b < 240 and 3 or 4
97
+ assert(p + width - 1 <= offset, 'offset splits a UTF-8 character')
98
+ units, p = units + (width == 4 and 2 or 1), p + width
99
+ end
100
+ return { line = line - 1, character = units - self.line_units[line] }
101
+ end
102
+
103
+ function Doc:range(first, last)
104
+ return { start = self:position(first), ['end'] = self:position(last) }
105
+ end
106
+
107
+ -- Byte offset of an LSP position; lines and characters past the end clamp.
108
+ function Doc:offset(position)
109
+ assert(type(position) == 'table' and type(position.line) == 'number' and type(position.character) == 'number'
110
+ and position.line >= 0 and position.character >= 0, 'invalid position')
111
+ local line = position.line + 1
112
+ if line > #self.lines then return #self.source end
113
+ local p = self.lines[line]
114
+ local stop = self.lines[line + 1] and self.lines[line + 1] - 1 or #self.source
115
+ local units = 0
116
+ while p < stop and units < position.character do
117
+ local b = self.source:byte(p + 1)
118
+ if b == 10 or b == 13 then break end
119
+ local width = b < 128 and 1 or b < 224 and 2 or b < 240 and 3 or 4
120
+ p, units = p + width, units + (width == 4 and 2 or 1)
121
+ end
122
+ return p
123
+ end
124
+
125
+ local extensions = { javascript = 'js', typescript = 'ts', python = 'py', csharp = 'cs', rust = 'rs', cpp = 'cpp' }
126
+ local function parser_name(doc)
127
+ local name = doc.path or ('document.' .. (extensions[doc.language] or doc.language or 'txt'))
128
+ if not parse.language(name) and doc.language then name = 'document.' .. (extensions[doc.language] or doc.language) end
129
+ return name
130
+ end
131
+
132
+ function Doc:parse()
133
+ if self.parsed then return self.parsed end
134
+ self.parsed = parse.parse(parser_name(self), self.source) or { nodes = {} }
135
+ return self.parsed
136
+ end
137
+
138
+ -- Tokens are not retained: only cursor lookups outside indexed refs need them.
139
+ function Doc:tokens()
140
+ local _, tokens = parse.parse_tokens(parser_name(self), self.source)
141
+ return tokens
142
+ end
143
+
144
+ function M.read(path)
145
+ local f = io.open(path, 'rb')
146
+ if not f then return nil end
147
+ local raw = f:read(M.MAX_BYTES + 1); f:close()
148
+ if not raw or #raw > M.MAX_BYTES then return nil end
149
+ local source = xutils.to_utf8(raw)
150
+ if not source then return nil end
151
+ return M.new(M.uri(path), source)
152
+ end
153
+
154
+ return M
@@ -15,6 +15,7 @@
15
15
 
16
16
  local graph = require('codeoutline.graph')
17
17
  local text = require('codeoutline.text')
18
+ local control = require('codeoutline.control')
18
19
 
19
20
  local M = {}
20
21
 
@@ -125,7 +126,10 @@ end
125
126
  local function find_seeds(G, query)
126
127
  local cands = {} -- id -> score
127
128
  local files = {} -- file indices named in the query
129
+ local considered = 0
128
130
  local function consider(id, s)
131
+ considered = considered + 1
132
+ if considered % 128 == 0 then control.check() end
129
133
  if not cands[id] or cands[id] < s then cands[id] = s end
130
134
  end
131
135
  local list_terms = terms(query)
@@ -134,6 +138,7 @@ local function find_seeds(G, query)
134
138
  if t.kind ~= 'word' then has_code = true end
135
139
  end
136
140
  for _, t in ipairs(list_terms) do
141
+ control.check()
137
142
  if t.kind == 'word' then
138
143
  -- Plain words count only as exact names (no case folding or fuzzy
139
144
  -- matching, which is where English words pick up noise), and rank
@@ -143,13 +148,16 @@ local function find_seeds(G, query)
143
148
  end
144
149
  elseif t.kind == 'file' then
145
150
  local want = t.text:gsub('\\', '/')
146
- for f, rec in ipairs(G.files) do
151
+ for i, f in ipairs(G.file_order) do
152
+ if i % 128 == 0 then control.check() end
153
+ local rec = G.files[f]
147
154
  if rec.path == want or rec.path:sub(-(#want + 1)) == '/' .. want then files[#files + 1] = f end
148
155
  end
149
156
  elseif t.kind == 'qualified' then
150
157
  local last = t.text:match('([%w_]+)$')
151
158
  local first = t.text:match('^([%w_]+)')
152
- for _, id in ipairs(G.by_name[last] or {}) do
159
+ for i, id in ipairs(G.by_name[last] or {}) do
160
+ if i % 128 == 0 then control.check() end
153
161
  local q = G.nodes[id].qualified:gsub('[:]+', '.')
154
162
  if q == t.text or q:sub(-(#t.text + 1)) == '.' .. t.text then
155
163
  consider(id, seed_score(G, id, true) + 10)
@@ -172,8 +180,10 @@ local function find_seeds(G, query)
172
180
  elseif #t.text >= 4 then
173
181
  -- fuzzy: every query segment appears in the name's segments
174
182
  local want = segments(t.text)
175
- local hits = 0
183
+ local hits, visited = 0, 0
176
184
  for name, ids in pairs(G.by_name) do
185
+ visited = visited + 1
186
+ if visited % 128 == 0 then control.check() end
177
187
  local lname = name:lower()
178
188
  local ok = true
179
189
  for _, w in ipairs(want) do
@@ -190,10 +200,13 @@ local function find_seeds(G, query)
190
200
  end
191
201
  end
192
202
  local list = {}
193
- for id, s in pairs(cands) do list[#list + 1] = { id = id, s = s } end
203
+ for id, s in pairs(cands) do
204
+ if #list % 128 == 0 then control.check() end
205
+ list[#list + 1] = { id = id, s = s }
206
+ end
194
207
  table.sort(list, function(a, b)
195
208
  if a.s ~= b.s then return a.s > b.s end
196
- return a.id < b.id
209
+ return graph.before(G, a.id, b.id)
197
210
  end)
198
211
  return list, files
199
212
  end
@@ -204,8 +217,10 @@ local function path_between(G, a, b, depth)
204
217
  for _ = 1, depth do
205
218
  local next_frontier = {}
206
219
  for _, x in ipairs(frontier) do
220
+ control.check()
207
221
  local out = G.out[x] or {}
208
222
  for i = 1, #out, graph.EDGE do
223
+ if i % 128 == 1 then control.check() end
209
224
  local dst = out[i]
210
225
  if not prev[dst] then
211
226
  prev[dst] = x
@@ -286,10 +301,13 @@ local function label(G, id)
286
301
  return string.format('%s %s (%s)', node.kind, node.qualified, graph.where(G, id))
287
302
  end
288
303
 
289
- -- Run a query. Returns the text and a table with what was selected.
304
+ -- Run a query over symbol tables (graph.build). Returns the text and a table
305
+ -- with what was selected; relationships resolve per query.
290
306
  function M.run(G, idx, query, opts)
307
+ control.check()
291
308
  opts = opts or {}
292
309
  local budget = M.validate(query, opts)
310
+ G = graph.query(G, opts)
293
311
  file_cache = {}
294
312
  local ranked, named_files = find_seeds(G, query)
295
313
 
@@ -397,6 +415,8 @@ function M.run(G, idx, query, opts)
397
415
  by_file[f].outline = true
398
416
  end
399
417
 
418
+ if G.load_incoming then G:load_incoming(seeds) end
419
+
400
420
  -- Confirm every file whose source or symbol labels this query can emit.
401
421
  -- GBK reparsing invalidates node IDs, so rebuild and select again before
402
422
  -- producing any output. Unrelated project files remain speculative.
@@ -497,8 +517,13 @@ function M.run(G, idx, query, opts)
497
517
  if used + #block <= budget + 400 then emit(block) else omitted = true end
498
518
  end
499
519
 
520
+ if G.incomplete then
521
+ table.insert(parts, 1, 'Call-path expansion limit reached; relationships are incomplete.')
522
+ omitted = true
523
+ end
500
524
  local result, truncated = text.limit(table.concat(parts, '\n'), budget, omitted or #pointers > 0)
501
- return result, { seeds = seeds, spine = spine, shown = shown, flows = flows, truncated = truncated }
525
+ return result, { seeds = seeds, spine = spine, shown = shown, flows = flows, truncated = truncated,
526
+ relationships_incomplete = G.incomplete or false, query_stats = G.stats }
502
527
  end
503
528
 
504
529
  return M
@@ -1,12 +1,20 @@
1
1
  -- graph.lua — cross-file resolution over an index: global symbol tables,
2
- -- file dependencies (#include / import), and call edges between nodes.
2
+ -- file dependencies (#include / import), and call edges resolved per query.
3
3
  --
4
- -- local G = graph.build(idx)
4
+ -- local G = graph.build(idx) -- symbol and path tables, kept per file
5
5
  -- G.nodes[id] -> <node table> (shared with the file record)
6
6
  -- G.node_file[id] -> file index
7
7
  -- G.files[f] -> file record (path, language, nodes, refs, imports)
8
- -- G.by_name[name] -> { id, ... }
9
- -- G.out[id], G.inn[id] -> { other, line, kind, other, line, kind, ... }
8
+ -- G.file_order -> { f, ... } in path order
9
+ -- G.by_name[name] -> { id, ... } in path, then node order
10
+ -- G.by_owner[q] -> members of qualifier q (`Box` for `Box::area`)
11
+ -- local Q = graph.query(G)
12
+ -- Q.out[id], Q.inn[id] -> { other, line, kind, other, line, kind, ... }
13
+ --
14
+ -- Tables change one file at a time (add_file / remove_file). File and node
15
+ -- ids are never reused while the tables live, so removed files leave holes:
16
+ -- iterate file_order, and compare nodes with graph.before rather than by id.
17
+ -- Every ordered list matches what a fresh build of the same records gives.
10
18
  --
11
19
  -- Edge lists are flat, EDGE values per edge, and nodes carry no wrapper
12
20
  -- table: on large trees per-edge and per-node tables cost more than the
@@ -66,6 +74,16 @@ local function resolve_deps(G, f)
66
74
  for _, imp in ipairs(rec.imports) do
67
75
  local path = imp.path
68
76
  if C_FAMILY[rec.language] then
77
+ path = path:gsub('\\', '/')
78
+ if path:find('^%.%.?/') then
79
+ -- "../dir/x.h": relative to the including file first
80
+ local parts = {}
81
+ for segment in ((here ~= '' and here .. '/' or '') .. path):gmatch('[^/]+') do
82
+ if segment == '..' then parts[#parts] = nil
83
+ elseif segment ~= '.' then parts[#parts + 1] = segment end
84
+ end
85
+ add(G.file_index[table.concat(parts, '/')])
86
+ end
69
87
  -- "dir/x.h": files whose path ends with it, preferring the closest
70
88
  for _, g in ipairs(G.by_base[basename(path)] or {}) do
71
89
  local p = G.files[g].path
@@ -241,7 +259,11 @@ end
241
259
  -- unrelated, nil when invisible (a C static of another file).
242
260
  local function locality(G, f, cf, c, deps, paired)
243
261
  if cf == f then return 100 end
244
- if c.static then return nil end
262
+ if c.static then
263
+ -- `static inline` helpers in an included header serve every includer.
264
+ if deps[cf] and HEADER_EXT[G.files[cf].path:match('%.(%w+)$') or ''] then return 50 end
265
+ return nil
266
+ end
245
267
  if deps[cf] then return 50 end
246
268
  if paired[cf] then return 40 end
247
269
  if G.file_dir[cf] == G.file_dir[f] then return 20 end
@@ -282,10 +304,10 @@ local function is_decl(node) return node.kind == 'prototype' or node.decl end
282
304
  -- keep the hot loop allocation-free; only the first n entries are live.
283
305
  local scored_ids, scored_vals = {}, {}
284
306
 
285
- local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired)
307
+ -- accept overrides the node kinds the ref kind may bind to (navigation only).
308
+ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired, accept)
286
309
  if kind == 'annotation' then return nil end
287
- local accept = CALLABLE
288
- if kind == 'new' then accept = NEWABLE elseif kind == 'ref' then accept = VALUE end
310
+ accept = accept or (kind == 'new' and NEWABLE) or (kind == 'ref' and VALUE) or CALLABLE
289
311
  local cands = G.by_name[name]
290
312
  if not cands then return nil end
291
313
  -- Score first: score() rejects other languages and statics of other
@@ -332,81 +354,255 @@ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired)
332
354
  return ties
333
355
  end
334
356
 
357
+ -- Files compare by path; nodes by file path, then node order (ids ascend
358
+ -- within a file). Both are strict total orders over live entries.
359
+ local function file_before(G, a, b) return G.files[a].path < G.files[b].path end
360
+ local function node_before(G, a, b)
361
+ local fa, fb = G.node_file[a], G.node_file[b]
362
+ if fa == fb then return a < b end
363
+ return G.files[fa].path < G.files[fb].path
364
+ end
365
+ local function string_before(_, a, b) return a < b end
366
+ M.before = node_before
367
+
368
+ -- Position of v (or where it belongs) in an ordered list.
369
+ local function search(G, list, v, before)
370
+ local lo, hi = 1, #list + 1
371
+ while lo < hi do
372
+ local mid = math.floor((lo + hi) / 2)
373
+ if before(G, list[mid], v) then lo = mid + 1 else hi = mid end
374
+ end
375
+ return lo
376
+ end
377
+
378
+ -- Ordered insert; a build in path order appends after one comparison.
379
+ local function insert(G, t, key, v, before)
380
+ local list = t[key]
381
+ if not list then t[key] = { v }; return end
382
+ local n = #list
383
+ if before(G, list[n], v) then list[n + 1] = v; return end
384
+ table.insert(list, search(G, list, v, before), v)
385
+ end
386
+
387
+ local function remove(G, t, key, v, before)
388
+ local list = t[key]
389
+ if not list then return end
390
+ local i = search(G, list, v, before)
391
+ if list[i] ~= v then return end
392
+ table.remove(list, i)
393
+ if #list == 0 then t[key] = nil end
394
+ end
395
+
396
+ function M.new()
397
+ return { files = {}, file_index = {}, file_order = {}, file_dir = {}, dir_ids = {}, nodes = {}, node_file = {},
398
+ id_of = {}, by_name = {}, by_lname = {}, by_qualified = {}, by_owner = {}, by_base = {}, by_stem = {}, pkg_files = {},
399
+ children = {}, dir_files = {}, dir_by_base = {}, ns_files = {},
400
+ next_file = 0, next_node = 0, next_dir = 0, file_count = 0, node_count = 0 }
401
+ end
402
+
403
+ local function namespaces(rec)
404
+ local seen = {}
405
+ if rec.language == 'csharp' then
406
+ for _, node in ipairs(rec.nodes) do
407
+ if node.kind == 'namespace' then seen[node.qualified] = true end
408
+ end
409
+ end
410
+ return seen
411
+ end
412
+
413
+ -- Qualifier of a qualified name: `Box` for `Box::area`, `M` for `M:split` or `M.trim`.
414
+ -- by_owner lists members wherever they are defined (out-of-line, other files).
415
+ function M.owner(qualified)
416
+ return qualified:match('^(.+)::[^:]+$') or qualified:match('^(.+)[%.:][^%.:]+$')
417
+ end
418
+
419
+ -- Add a record under rel. No cancellation checkpoints: index commits call
420
+ -- this and must not stop halfway.
421
+ function M.add_file(G, rel, rec)
422
+ assert(not G.file_index[rel], 'file already in symbol tables: ' .. rel)
423
+ local f = G.next_file + 1
424
+ G.next_file = f
425
+ G.files[f], G.file_index[rel] = rec, f
426
+ G.file_order[#G.file_order + 1] = f
427
+ local order = G.file_order
428
+ if #order > 1 and not file_before(G, order[#order - 1], f) then
429
+ table.remove(order)
430
+ table.insert(order, search(G, order, f, file_before), f)
431
+ end
432
+ local dir = dirname(rel)
433
+ local did = G.dir_ids[dir]
434
+ if not did then G.next_dir = G.next_dir + 1; did = G.next_dir; G.dir_ids[dir] = did end
435
+ G.file_dir[f] = did
436
+ insert(G, G.by_base, basename(rel), f, file_before)
437
+ insert(G, G.by_stem, stem(rel), f, file_before)
438
+ if not G.dir_files[dir] then insert(G, G.dir_by_base, basename(dir), dir, string_before) end
439
+ insert(G, G.dir_files, dir, f, file_before)
440
+ if rec.package then insert(G, G.pkg_files, rec.package, f, file_before) end
441
+ for qualified in pairs(namespaces(rec)) do insert(G, G.ns_files, qualified, f, file_before) end
442
+ local ids = {}
443
+ G.id_of[f] = ids
444
+ for n, node in ipairs(rec.nodes) do
445
+ local id = G.next_node + 1
446
+ G.next_node = id
447
+ G.nodes[id], G.node_file[id] = node, f
448
+ ids[n] = id
449
+ insert(G, G.by_name, node.name, id, node_before)
450
+ insert(G, G.by_lname, node.name:lower(), id, node_before)
451
+ insert(G, G.by_qualified, node.qualified, id, node_before)
452
+ local owner = M.owner(node.qualified)
453
+ if owner then insert(G, G.by_owner, owner, id, node_before) end
454
+ if node.parent then push(G.children, ids[node.parent], id) end
455
+ end
456
+ G.file_count, G.node_count = G.file_count + 1, G.node_count + #rec.nodes
457
+ return f
458
+ end
459
+
460
+ function M.remove_file(G, rel)
461
+ local f = G.file_index[rel]
462
+ if not f then return end
463
+ local rec, ids = G.files[f], G.id_of[f]
464
+ for _, id in ipairs(ids) do
465
+ local node = G.nodes[id]
466
+ remove(G, G.by_name, node.name, id, node_before)
467
+ remove(G, G.by_lname, node.name:lower(), id, node_before)
468
+ remove(G, G.by_qualified, node.qualified, id, node_before)
469
+ local owner = M.owner(node.qualified)
470
+ if owner then remove(G, G.by_owner, owner, id, node_before) end
471
+ G.children[id] = nil
472
+ end
473
+ for _, id in ipairs(ids) do G.nodes[id], G.node_file[id] = nil, nil end
474
+ local dir = dirname(rel)
475
+ remove(G, G.by_base, basename(rel), f, file_before)
476
+ remove(G, G.by_stem, stem(rel), f, file_before)
477
+ remove(G, G.dir_files, dir, f, file_before)
478
+ if not G.dir_files[dir] then remove(G, G.dir_by_base, basename(dir), dir, string_before) end
479
+ if rec.package then remove(G, G.pkg_files, rec.package, f, file_before) end
480
+ for qualified in pairs(namespaces(rec)) do remove(G, G.ns_files, qualified, f, file_before) end
481
+ local order = G.file_order
482
+ table.remove(order, search(G, order, f, file_before))
483
+ G.files[f], G.file_index[rel], G.id_of[f], G.file_dir[f] = nil, nil, nil, nil
484
+ G.file_count, G.node_count = G.file_count - 1, G.node_count - #ids
485
+ end
486
+
487
+ -- Symbol tables for every record of idx. Checkpoints per file: a cancelled
488
+ -- build is simply discarded.
335
489
  function M.build(idx)
336
- local G = {
337
- files = {}, file_index = {}, file_dir = {}, nodes = {}, node_file = {}, id_of = {},
338
- by_name = {}, by_lname = {}, by_qualified = {}, by_base = {}, by_stem = {},
339
- pkg_files = {}, children = {}, out = {}, inn = {}, deps = {}, dir_files = {}, ns_files = {},
340
- generation = idx.generation,
341
- }
490
+ local G = M.new()
342
491
  local paths = {}
343
492
  for rel in pairs(idx.files) do paths[#paths + 1] = rel end
344
493
  table.sort(paths)
345
- -- Directory ids per file: placement scoring compares numbers instead of
346
- -- slicing two paths per candidate.
347
- local dir_ids = {}
348
- -- Checkpoints per file: LuaJIT does not run count hooks inside compiled
349
- -- loops, so cancellation relies on these (as in Index:refresh).
350
- for f, rel in ipairs(paths) do
494
+ for _, rel in ipairs(paths) do
351
495
  control.check()
352
- local rec = idx.files[rel]
353
- G.files[f] = rec
354
- G.file_index[rel] = f
355
- local dir = dirname(rel)
356
- local did = dir_ids[dir]
357
- if not did then did = f; dir_ids[dir] = did end
358
- G.file_dir[f] = did
359
- push(G.by_base, basename(rel), f)
360
- push(G.by_stem, stem(rel), f)
361
- push(G.dir_files, dirname(rel), f)
362
- if rec.package then push(G.pkg_files, rec.package, f) end
363
- local ids = {}
364
- G.id_of[f] = ids
365
- for n, node in ipairs(rec.nodes) do
366
- local id = #G.nodes + 1
367
- G.nodes[id], G.node_file[id] = node, f
368
- ids[n] = id
369
- push(G.by_name, node.name, id)
370
- push(G.by_lname, node.name:lower(), id)
371
- push(G.by_qualified, node.qualified, id)
372
- if node.kind == 'namespace' and rec.language == 'csharp' then
373
- local list = G.ns_files[node.qualified]
374
- if not list or list[#list] ~= f then push(G.ns_files, node.qualified, f) end
375
- end
376
- if node.parent then push(G.children, ids[node.parent], id) end
496
+ M.add_file(G, rel, idx.files[rel])
497
+ end
498
+ G.generation = idx.generation
499
+ return G
500
+ end
501
+
502
+ -- Query-owned edges: never attach these caches to the resident symbol tables.
503
+ M.MAX_EXPANDED = 20000
504
+ function M.query(symbols, opts)
505
+ opts = opts or {}
506
+ local limit = opts.max_expanded or M.MAX_EXPANDED
507
+ assert(type(limit) == 'number' and limit >= 1 and limit % 1 == 0 and limit < math.huge,
508
+ 'max_expanded must be a positive integer')
509
+ local Q = setmetatable({ out = {}, inn = {},
510
+ stats = { expanded = 0, incoming_scans = 0, resolved_refs = 0 }, incomplete = false }, { __index = symbols })
511
+ local dependencies, references = {}, {}
512
+ local function deps(f)
513
+ local entry = dependencies[f]
514
+ if not entry then
515
+ local direct, paired = resolve_deps(symbols, f)
516
+ entry = { direct, paired }; dependencies[f] = entry
377
517
  end
518
+ return entry[1], entry[2]
519
+ end
520
+ local function targets(f, i)
521
+ local rec, ids = symbols.files[f], symbols.id_of[f]
522
+ local refs = rec.refs
523
+ local direct, paired = deps(f)
524
+ Q.stats.resolved_refs = Q.stats.resolved_refs + 1
525
+ return resolve_ref(symbols, f, ids[refs.from[i]], refs.name[i], refs.kind[i],
526
+ refs.recv[i] or nil, direct, paired)
378
527
  end
379
- -- Directories indexed by their last path segment (Go import lookups).
380
- G.dir_by_base = {}
381
- for d in pairs(G.dir_files) do push(G.dir_by_base, basename(d), d) end
382
- for _, list in pairs(G.dir_by_base) do table.sort(list) end
383
- -- Edges.
384
- local edges = 0
385
- for f, rec in ipairs(G.files) do
528
+ setmetatable(Q.out, { __index = function(cache, id)
386
529
  control.check()
387
- local deps, paired = resolve_deps(G, f)
388
- G.deps[f] = deps
389
- local refs, ids = rec.refs, G.id_of[f]
390
- local names, froms, kinds, lines, recvs = refs.name, refs.from, refs.kind, refs.line, refs.recv
391
- for i = 1, #names do
392
- local from = froms[i]
393
- if from > 0 then
394
- local src, kind, line = ids[from], kinds[i], lines[i]
395
- local targets = resolve_ref(G, f, src, names[i], kind, recvs[i] or nil, deps, paired)
396
- if targets then
397
- for _, dst in ipairs(targets) do
398
- push3(G.out, src, dst, line, kind)
399
- push3(G.inn, dst, src, line, kind)
400
- edges = edges + 1
530
+ if Q.stats.expanded >= limit then Q.incomplete = true; return nil end
531
+ Q.stats.expanded = Q.stats.expanded + 1
532
+ local f = assert(symbols.node_file[id], 'unknown symbol')
533
+ local rec, ids = symbols.files[f], symbols.id_of[f]
534
+ local grouped = references[f]
535
+ if not grouped then
536
+ grouped = {}
537
+ for i, from in ipairs(rec.refs.from) do
538
+ if i % 128 == 0 then control.check() end
539
+ if from > 0 then push(grouped, ids[from], i) end
540
+ end
541
+ references[f] = grouped
542
+ end
543
+ local edges = {}
544
+ for _, i in ipairs(grouped[id] or {}) do
545
+ control.check()
546
+ for _, dst in ipairs(targets(f, i) or {}) do
547
+ edges[#edges + 1] = dst
548
+ edges[#edges + 1] = rec.refs.line[i]
549
+ edges[#edges + 1] = rec.refs.kind[i]
550
+ end
551
+ end
552
+ rawset(cache, id, edges)
553
+ return edges
554
+ end })
555
+ -- Resolve all requested callers with a single ordered multi-name scan.
556
+ function Q:load_incoming(ids)
557
+ local wanted, names, found = {}, {}, false
558
+ for _, id in ipairs(ids) do
559
+ if rawget(self.inn, id) == nil then
560
+ wanted[id], names[symbols.nodes[id].name], found = true, true, true
561
+ end
562
+ end
563
+ if not found then return end
564
+ local incoming = {}
565
+ for id in pairs(wanted) do incoming[id] = {} end
566
+ self.stats.incoming_scans = self.stats.incoming_scans + 1
567
+ -- Hot loop over every reference: numeric loops, hoisted fields, and the
568
+ -- selective name test first. Checkpoints run per file and per chunk.
569
+ local files = symbols.files
570
+ for _, f in ipairs(symbols.file_order) do
571
+ control.check()
572
+ local refs = files[f].refs
573
+ local ref_names, froms = refs.name, refs.from
574
+ local count = #ref_names
575
+ for first = 1, count, 4096 do
576
+ if first > 1 then control.check() end
577
+ for i = first, math.min(count, first + 4095) do
578
+ if names[ref_names[i]] and froms[i] > 0 then
579
+ for _, dst in ipairs(targets(f, i) or {}) do
580
+ if wanted[dst] then
581
+ push3(incoming, dst, symbols.id_of[f][froms[i]], refs.line[i], refs.kind[i])
582
+ end
583
+ end
401
584
  end
402
585
  end
403
586
  end
404
587
  end
588
+ -- Interrupted scans never publish partial caller lists.
589
+ for id, edges in pairs(incoming) do rawset(self.inn, id, edges) end
405
590
  end
406
- G.edge_count = edges
407
- return G
591
+ setmetatable(Q.inn, { __index = function(cache, id)
592
+ Q:load_incoming({ id })
593
+ return rawget(cache, id)
594
+ end })
595
+ return Q
408
596
  end
409
597
 
598
+ -- Navigation reuses the resolver over symbol tables or overlays of them.
599
+ M.CALLABLE = CALLABLE
600
+ M.dependencies = resolve_deps
601
+ M.resolve = resolve_ref
602
+ M.placement = locality
603
+ M.is_decl = is_decl
604
+ M.family = family
605
+
410
606
  -- Location string for a node: "path:line".
411
607
  function M.where(G, id)
412
608
  return G.files[G.node_file[id]].path .. ':' .. G.nodes[id].line