@codua/codeoutline-win32-x64 0.1.9 → 0.2.1

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 (40) hide show
  1. package/README.md +38 -6
  2. package/README.zh-CN.md +36 -6
  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 +45 -5
  10. package/scripts/codeoutline/completion.lua +936 -0
  11. package/scripts/codeoutline/control.lua +5 -1
  12. package/scripts/codeoutline/documents.lua +154 -0
  13. package/scripts/codeoutline/explore.lua +35 -8
  14. package/scripts/codeoutline/graph.lua +321 -102
  15. package/scripts/codeoutline/index.lua +170 -50
  16. package/scripts/codeoutline/index_worker.lua +108 -17
  17. package/scripts/codeoutline/indirect.lua +201 -0
  18. package/scripts/codeoutline/js_evidence.lua +175 -0
  19. package/scripts/codeoutline/js_resolution.lua +201 -0
  20. package/scripts/codeoutline/lang/c.lua +22 -12
  21. package/scripts/codeoutline/lang/cpp.lua +13 -12
  22. package/scripts/codeoutline/lang/csharp.lua +21 -21
  23. package/scripts/codeoutline/lang/go.lua +10 -10
  24. package/scripts/codeoutline/lang/java.lua +9 -9
  25. package/scripts/codeoutline/lang/js.lua +15 -14
  26. package/scripts/codeoutline/lang/kotlin.lua +384 -0
  27. package/scripts/codeoutline/lang/lua.lua +11 -6
  28. package/scripts/codeoutline/lang/python.lua +6 -6
  29. package/scripts/codeoutline/lang/rust.lua +23 -15
  30. package/scripts/codeoutline/lsp.lua +574 -0
  31. package/scripts/codeoutline/lua_bindings.lua +222 -0
  32. package/scripts/codeoutline/main.lua +28 -5
  33. package/scripts/codeoutline/navigation.lua +1046 -0
  34. package/scripts/codeoutline/parse.lua +9 -2
  35. package/scripts/codeoutline/parse_pool.lua +189 -0
  36. package/scripts/codeoutline/parse_worker.lua +24 -0
  37. package/scripts/codeoutline/service.lua +82 -31
  38. package/scripts/codeoutline/update_sequence.lua +1 -1
  39. package/scripts/codeoutline/version.lua +1 -1
  40. package/licenses/rpmalloc.txt +0 -12
@@ -17,20 +17,23 @@
17
17
  local ci = require('codeoutline.parse')
18
18
  local paths = require('codeoutline.path')
19
19
  local control = require('codeoutline.control')
20
+ local graph = require('codeoutline.graph')
21
+ local pool = require('codeoutline.parse_pool')
20
22
 
21
23
  local M = {}
24
+ local INDEX_PARSE = { ranges = false }
22
25
 
23
26
  -- Cache version: a checksum of the parser sources, so any parser change
24
27
  -- rebuilds stale caches instead of silently reusing old parse results.
25
28
  M.VERSION = (function()
26
29
  local parts = {}
27
- for _, m in ipairs({ 'xscan', 'text', 'common', 'parse', 'lang.c', 'lang.cpp', 'lang.lua', 'lang.python',
28
- 'lang.java', 'lang.go', 'lang.js', 'lang.csharp', 'lang.rust' }) do
30
+ for _, m in ipairs({ 'xscan', 'text', 'common', 'indirect', 'js_evidence', 'lua_bindings', 'parse', 'lang.c', 'lang.cpp', 'lang.lua', 'lang.python',
31
+ 'lang.java', 'lang.kotlin', 'lang.go', 'lang.js', 'lang.csharp', 'lang.rust' }) do
29
32
  local file = package.searchpath and package.searchpath('codeoutline.' .. m, package.path)
30
33
  local h = file and io.open(file, 'rb')
31
34
  if h then parts[#parts + 1] = h:read('a'); h:close() end
32
35
  end
33
- return 'p3-lazy-encoding-' .. xutils.sha256_hex(table.concat(parts))
36
+ return 'p5-compact-nodes-' .. xutils.sha256_hex(table.concat(parts))
34
37
  end)()
35
38
  -- Generated amalgamations (sqlite3.c, minified bundles) cost more to index
36
39
  -- than they give back; skip files above this size unless configured.
@@ -39,6 +42,12 @@ M.MAX_FILE_BYTES = 1500000
39
42
  -- few percent of level 6 at under half the time; inflating is cheap next to
40
43
  -- unpacking, and the checksum then covers the smaller compressed bytes.
41
44
  M.CACHE_COMPRESSION_LEVEL = 1
45
+ -- Below this many files to read, a full pass parses in place: posting and
46
+ -- unpacking results would cost more than it saves.
47
+ M.PARALLEL_MIN_FILES = 32
48
+ -- Bytes a file costs beyond its size (open, stat, record) when the parse pool
49
+ -- balances threads by size.
50
+ M.FILE_WEIGHT = 4096
42
51
 
43
52
  local Index = {}
44
53
  Index.__index = Index
@@ -101,7 +110,7 @@ function M.open(root, opts)
101
110
  max_bytes = max_bytes,
102
111
  lister = opts.lister, -- 'rg' | 'walk' | nil (rg when it runs here)
103
112
  files = {}, -- rel path -> record
104
- generation = 0, -- bumped on every change; graph caches key on it
113
+ generation = 0, -- bumped on every committed change
105
114
  }, Index)
106
115
  self.cache_loaded = not opts.rebuild and self:load() or false
107
116
  return self
@@ -124,13 +133,15 @@ function Index:load()
124
133
  assert(t.version == M.VERSION and t.root == paths.key(self.root) and type(t.files) == 'table')
125
134
  local records = {}
126
135
  for _, rec in ipairs(t.files) do
136
+ control.check()
127
137
  assert(type(rec) == 'table' and type(rec.path) == 'string')
128
138
  assert(not rec.path:find('[\\:%z]') and rec.path:sub(1, 1) ~= '/')
129
139
  for part in rec.path:gmatch('[^/]+') do assert(part ~= '..' and part ~= '.') end
130
140
  assert(type(rec.nodes) == 'table' and type(rec.refs) == 'table' and type(rec.imports) == 'table')
131
141
  assert(type(rec.size) == 'number' and type(rec.language) == 'string')
132
142
  assert(rec.encoding == nil or rec.encoding == 'utf-8' or rec.encoding == 'utf-8-bom' or rec.encoding == 'gbk')
133
- for _, n in ipairs(rec.nodes) do
143
+ for i, n in ipairs(rec.nodes) do
144
+ if i % 128 == 0 then control.check() end
134
145
  assert(type(n.name) == 'string' and type(n.qualified) == 'string' and type(n.kind) == 'string')
135
146
  assert(type(n.line) == 'number' and type(n.end_line) == 'number' and n.line >= 1 and n.end_line >= n.line)
136
147
  end
@@ -140,6 +151,7 @@ function Index:load()
140
151
  local nrefs = #refs.name
141
152
  assert(#refs.from == nrefs and #refs.kind == nrefs and #refs.line == nrefs and #refs.recv == nrefs)
142
153
  for i = 1, nrefs do
154
+ if i % 128 == 0 then control.check() end
143
155
  assert(type(refs.name[i]) == 'string' and type(refs.from[i]) == 'number'
144
156
  and type(refs.kind[i]) == 'string' and type(refs.line[i]) == 'number')
145
157
  local recv = refs.recv[i]
@@ -150,7 +162,10 @@ function Index:load()
150
162
  end
151
163
  return records
152
164
  end)
153
- if not ok then self.cache_error = 'invalid or stale cache; rebuilding'; return false end
165
+ if not ok then
166
+ if control.is_interrupted(files) then error(files, 0) end
167
+ self.cache_error = 'invalid or stale cache; rebuilding'; return false
168
+ end
154
169
  self.files = files
155
170
  self.generation = self.generation + 1
156
171
  self.saved_generation = self.generation
@@ -193,9 +208,35 @@ function Index:save()
193
208
  return true
194
209
  end
195
210
 
211
+ -- One file's change as a unit: the record, the symbol tables derived from
212
+ -- the records, and the generation. Nothing inside checks for cancellation,
213
+ -- so an interrupted refresh leaves every file either old or new. A failed
214
+ -- table update drops the tables; the next symbols() call rebuilds them.
215
+ function Index:commit(rel, rec)
216
+ local old = self.files[rel]
217
+ self.files[rel] = rec
218
+ self.generation = self.generation + 1
219
+ local G = self.symbol_tables
220
+ if G then
221
+ local ok = pcall(function()
222
+ if old then graph.remove_file(G, rel) end
223
+ if rec then graph.add_file(G, rel, rec) end
224
+ end)
225
+ if ok then G.generation = self.generation else self.symbol_tables = nil end
226
+ end
227
+ end
228
+
229
+ -- Symbol tables over the records, built on first use and then kept current
230
+ -- by commit(). Hosts that never resolve names do not pay for them.
231
+ function Index:symbols()
232
+ if not self.symbol_tables then self.symbol_tables = graph.build(self) end
233
+ return self.symbol_tables
234
+ end
235
+
196
236
  -- Only query-selected files are decoded. This cache lasts for one query and
197
237
  -- holds the exact snapshot used for both reparsing and source rendering.
198
238
  function Index:query_source(rel)
239
+ control.check()
199
240
  self.query_sources = self.query_sources or {}
200
241
  if self.query_sources[rel] then return self.query_sources[rel], false end
201
242
  local rec = assert(self.files[rel], 'source is not indexed: ' .. rel)
@@ -221,13 +262,15 @@ function Index:query_source(rel)
221
262
  local changed = false
222
263
  if not rec.encoding then
223
264
  if encoding == 'gbk' then
224
- local parsed = assert(ci.parse(rel, src), 'cannot parse decoded source: ' .. rel)
265
+ local parsed = assert(ci.parse(rel, src, INDEX_PARSE), 'cannot parse decoded source: ' .. rel)
225
266
  parsed.size, parsed.crc, parsed.mtime, parsed.checked = rec.size, rec.crc, rec.mtime, rec.checked
226
- self.files[rel], rec = parsed, parsed
227
- self.generation = self.generation + 1
267
+ parsed.encoding = encoding
268
+ self:commit(rel, parsed)
228
269
  changed = true
270
+ else
271
+ -- Metadata only: names and positions are unchanged.
272
+ rec.encoding = encoding
229
273
  end
230
- rec.encoding = encoding
231
274
  self.encoding_dirty = true
232
275
  end
233
276
  self.query_sources[rel] = src
@@ -243,6 +286,7 @@ local function rg_available()
243
286
  has_rg = false
244
287
  if io.popen then
245
288
  local ok, p = pcall(io.popen, 'rg --version' .. (package.config:sub(1, 1) == '\\' and ' 2>nul' or ' 2>/dev/null'))
289
+ if not ok and control.is_interrupted(p) then error(p, 0) end
246
290
  if ok and p then
247
291
  has_rg = (p:read('a') or ''):find('ripgrep', 1, true) ~= nil
248
292
  p:close()
@@ -388,10 +432,39 @@ local function new_stats(mode)
388
432
  return { parsed = 0, unchanged = 0, removed = 0, skipped = 0, failed = 0, mode = mode }
389
433
  end
390
434
 
391
- -- Bring one listed file in line with the tree, given its stat fields (nil
392
- -- when the runtime has no stat). Returns false when it no longer belongs in
393
- -- the index: gone, too large, unreadable or unparsable.
394
- function Index:sync_file(rel, st, stats)
435
+ -- Read one file and parse it unless its size and checksum match the known
436
+ -- record. Touches no index state, so a parse thread can run it too. `checked`
437
+ -- is taken before the read: content read after that second is current.
438
+ function M.load_file(abs, rel, max_bytes, old_size, old_crc)
439
+ local checked = os.time()
440
+ local src = slurp(abs)
441
+ if not src or #src > max_bytes then return { status = 'skipped' } end
442
+ local crc = checksum(src)
443
+ if old_size == #src and old_crc == crc then
444
+ return { status = 'unchanged', checked = checked }
445
+ end
446
+ local ok, r = pcall(ci.parse, rel, src, INDEX_PARSE)
447
+ if not ok and control.is_interrupted(r) then error(r, 0) end
448
+ -- A parser exception must not permanently hide a GBK file.
449
+ -- Successful speculative parsing does not scan encoding here.
450
+ if not ok or not r then
451
+ if xutils.to_utf8 then
452
+ local decoded, encoding = xutils.to_utf8(src)
453
+ if decoded then
454
+ ok, r = pcall(ci.parse, rel, decoded, INDEX_PARSE)
455
+ if not ok and control.is_interrupted(r) then error(r, 0) end
456
+ if ok and r then r.encoding = encoding end
457
+ end
458
+ end
459
+ end
460
+ if not (ok and r) then return { status = 'failed' } end
461
+ r.size, r.crc, r.checked = #src, crc, checked
462
+ return { status = 'parsed', rec = r }
463
+ end
464
+
465
+ -- Settle one listed file from its stat fields alone (nil when the runtime
466
+ -- has no stat): true or false as sync_file returns, nil when it must be read.
467
+ function Index:check_stat(rel, st, stats)
395
468
  local rec = self.files[rel]
396
469
  if st and (st.exists == false or st.type ~= 'file') then
397
470
  return false
@@ -406,42 +479,47 @@ function Index:sync_file(rel, st, stats)
406
479
  stats.unchanged = stats.unchanged + 1
407
480
  return true
408
481
  end
409
- local src = slurp(self:abs(rel))
410
- if not src or #src > self.max_bytes then
482
+ return nil
483
+ end
484
+
485
+ -- Commit what load_file found. Returns sync_file's answer.
486
+ function Index:apply_load(rel, st, res, stats)
487
+ if res.status == 'skipped' then
411
488
  stats.skipped = stats.skipped + 1
412
489
  return false
413
- end
414
- local crc = checksum(src)
415
- if rec and rec.size == #src and rec.crc == crc then
490
+ elseif res.status == 'failed' then
491
+ stats.failed = stats.failed + 1
492
+ return false
493
+ elseif res.status == 'unchanged' then
494
+ local rec = self.files[rel]
416
495
  rec.mtime = st and st.mtime or rec.mtime
417
- rec.checked = os.time()
496
+ rec.checked = res.checked
418
497
  stats.unchanged = stats.unchanged + 1
419
498
  return true
420
499
  end
421
- local ok, r = pcall(ci.parse, rel, src)
422
- control.check()
423
- -- A parser exception must not permanently hide a GBK file.
424
- -- Successful speculative parsing does not scan encoding here.
425
- if not ok or not r then
426
- if xutils.to_utf8 then
427
- local decoded, encoding = xutils.to_utf8(src)
428
- if decoded then
429
- ok, r = pcall(ci.parse, rel, decoded)
430
- if ok and r then r.encoding = encoding end
431
- end
432
- end
433
- end
434
- if not (ok and r) then
435
- stats.failed = stats.failed + 1
436
- return false
437
- end
438
- r.size, r.crc, r.mtime = #src, crc, st and st.mtime or nil
439
- r.checked = os.time()
440
- self.files[rel] = r
500
+ local r = res.rec
501
+ r.mtime = st and st.mtime or nil
502
+ self:commit(rel, r)
441
503
  stats.parsed = stats.parsed + 1
442
504
  return true
443
505
  end
444
506
 
507
+ function Index:read_file(rel, st, stats)
508
+ local rec = self.files[rel]
509
+ local res = M.load_file(self:abs(rel), rel, self.max_bytes, rec and rec.size, rec and rec.crc)
510
+ control.check()
511
+ return self:apply_load(rel, st, res, stats)
512
+ end
513
+
514
+ -- Bring one listed file in line with the tree, given its stat fields (nil
515
+ -- when the runtime has no stat). Returns false when it no longer belongs in
516
+ -- the index: gone, too large, unreadable or unparsable.
517
+ function Index:sync_file(rel, st, stats)
518
+ local settled = self:check_stat(rel, st, stats)
519
+ if settled ~= nil then return settled end
520
+ return self:read_file(rel, st, stats)
521
+ end
522
+
445
523
  -- Start change notification so later refreshes visit only what changed.
446
524
  -- Call it before the next refresh: that one stays a full pass, and anything
447
525
  -- changing while it runs is reported to the pass after. Returns false when
@@ -512,11 +590,11 @@ function Index:refresh_changed(changed, structural)
512
590
  local known = self.files[rel] ~= nil
513
591
  if listed and listed[rel] or (not listed and known) then
514
592
  if not self:sync_file(rel, has_stat and xutils.stat(self:abs(rel)) or nil, stats) and known then
515
- self.files[rel] = nil
593
+ self:commit(rel, nil)
516
594
  stats.removed = stats.removed + 1
517
595
  end
518
596
  elseif known then
519
- self.files[rel] = nil -- no longer listed
597
+ self:commit(rel, nil) -- no longer listed
520
598
  stats.removed = stats.removed + 1
521
599
  end
522
600
  end
@@ -530,7 +608,20 @@ end
530
608
  -- With a watcher (see Index:watch) only reported paths are visited; an
531
609
  -- overflow or an ignore-file change falls back to a full pass, and a failed
532
610
  -- watcher is reopened before one.
533
- function Index:refresh()
611
+ function Index:refresh(opts)
612
+ opts = opts or {}
613
+ if opts.paths then
614
+ local changed = {}
615
+ for _, path in ipairs(opts.paths) do
616
+ if paths.contains(self.root, path) then
617
+ local prefix = self.root:sub(-1) == '/' and self.root or self.root .. '/'
618
+ local rel = path:sub(#prefix + 1)
619
+ -- Unindexed/ignored files are admitted by watcher enumeration.
620
+ if self.files[rel] then changed[rel] = true end
621
+ end
622
+ end
623
+ return self:refresh_changed(changed, false)
624
+ end
534
625
  if self.watcher and self.watch_ready then
535
626
  local paths, structural, overflow = self.watcher:read()
536
627
  local changed = paths and not overflow and relevant(paths)
@@ -539,7 +630,6 @@ function Index:refresh()
539
630
  -- drained, so a pass that fails midway leaves a full one next.
540
631
  self.watch_ready = false
541
632
  local stats = self:refresh_changed(changed, structural)
542
- if stats.parsed > 0 or stats.removed > 0 then self.generation = self.generation + 1 end
543
633
  self.watch_ready = true
544
634
  return stats
545
635
  end
@@ -552,27 +642,57 @@ function Index:refresh()
552
642
  end
553
643
  if self.watching and not self.watcher and os.time() >= (self.watch_retry or 0) then self:open_watcher() end
554
644
  if self.watcher then self.watch_ready = false end
645
+ if not self.watcher and opts.full_interval and self.next_full and os.time() < self.next_full then
646
+ return new_stats('idle')
647
+ end
555
648
  local stats = self:refresh_all()
649
+ self.next_full = os.time() + (opts.full_interval or 0)
556
650
  if self.watcher then self.watch_ready = true end
557
651
  return stats
558
652
  end
559
653
 
654
+ -- Files commit one at a time. An interrupted pass leaves next_full unset
655
+ -- (and a watcher not ready), so the next refresh is a full pass again.
656
+ -- Files a stat cannot settle are read and parsed by the parse pool when the
657
+ -- host started one and this pass may wait for it; otherwise serially.
560
658
  function Index:refresh_all()
561
659
  local stats = new_stats('full')
660
+ self.next_full = nil
562
661
  local seen = {}
563
662
  local list, meta = self:list_files()
564
663
  if not meta and xutils and xutils.stat then meta = self:stat_listing(list) end
664
+ local reads = {}
565
665
  for _, rel in ipairs(list) do
566
666
  control.check()
567
- if self:sync_file(rel, meta and meta[rel], stats) then seen[rel] = true end
667
+ local settled = self:check_stat(rel, meta and meta[rel], stats)
668
+ if settled == nil then reads[#reads + 1] = rel
669
+ elseif settled then seen[rel] = true end
568
670
  end
569
- for rel in pairs(self.files) do
570
- if not seen[rel] then
571
- self.files[rel] = nil
572
- stats.removed = stats.removed + 1
671
+ if #reads >= M.PARALLEL_MIN_FILES and pool.ready() then
672
+ local jobs = {}
673
+ for i, rel in ipairs(reads) do
674
+ local rec, st = self.files[rel], meta and meta[rel]
675
+ jobs[i] = { rel = rel, abs = self:abs(rel), size = rec and rec.size, crc = rec and rec.crc,
676
+ weight = (st and st.size or rec and rec.size or 0) + M.FILE_WEIGHT }
677
+ end
678
+ pool.run(jobs, self.max_bytes, function(rel, res)
679
+ control.check()
680
+ if self:apply_load(rel, meta and meta[rel], res, stats) then seen[rel] = true end
681
+ end)
682
+ else
683
+ for _, rel in ipairs(reads) do
684
+ control.check()
685
+ if self:read_file(rel, meta and meta[rel], stats) then seen[rel] = true end
573
686
  end
574
687
  end
575
- if stats.parsed > 0 or stats.removed > 0 then self.generation = self.generation + 1 end
688
+ local gone = {}
689
+ for rel in pairs(self.files) do
690
+ if not seen[rel] then gone[#gone + 1] = rel end
691
+ end
692
+ for _, rel in ipairs(gone) do
693
+ self:commit(rel, nil)
694
+ stats.removed = stats.removed + 1
695
+ end
576
696
  return stats
577
697
  end
578
698
 
@@ -4,13 +4,60 @@ package.path = scripts .. '/?.lua;' .. package.path
4
4
  local service = require('codeoutline.service')
5
5
  local paths = require('codeoutline.path')
6
6
  local control = require('codeoutline.control')
7
+ local documents = require('codeoutline.documents')
8
+ local lsp = require('codeoutline.lsp')
9
+ local navigation = require('codeoutline.navigation')
10
+ local pool = require('codeoutline.parse_pool')
11
+ require('codeoutline.completion') -- registers lsp_completion
7
12
  service.configure({ watch = true })
8
13
  local shared = assert(xshared.dict('codeoutline_control'))
9
14
 
10
15
  local function execute(req)
16
+ if navigation.handles(req.method) then return navigation.execute(req) end
17
+ if req.method == 'lsp_close' then navigation.close(req.session); return true end
18
+ if req.method == 'lsp_symbols' then
19
+ local result = {}
20
+ if req.limit <= 0 then return result end
21
+ local needle = req.query:lower()
22
+ for _, root in ipairs(req.roots) do
23
+ local idx = service.resident(root)
24
+ local files = {}
25
+ for path in pairs(idx.files) do files[#files + 1] = path end
26
+ table.sort(files)
27
+ for _, rel in ipairs(files) do
28
+ control.check()
29
+ local rec, matched = idx.files[rel], false
30
+ local path = idx:abs(rel)
31
+ if not req.exclude[paths.key(path)] then
32
+ for i, node in ipairs(rec.nodes) do
33
+ if i % 128 == 0 then control.check() end
34
+ if node.qualified:lower():find(needle, 1, true) then matched = true; break end
35
+ end
36
+ end
37
+ if matched then
38
+ local canonical = xutils.realpath(path)
39
+ local doc = canonical and paths.contains(root, paths.normalize(canonical)) and documents.read(path)
40
+ if doc then
41
+ for _, symbol in ipairs(lsp.document_symbols(doc, false)) do
42
+ local name = symbol.containerName and (symbol.containerName .. '.' .. symbol.name) or symbol.name
43
+ if name:lower():find(needle, 1, true) then
44
+ if #result >= req.limit then return result end
45
+ result[#result + 1] = symbol
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ return result
53
+ end
11
54
  local root = assert(paths.canonical(req.projectPath))
12
55
  assert(paths.allowed(req.allowedRoots, root), 'project path is outside allowed roots')
13
- if req.method == 'explore' then
56
+ if req.method == 'lsp_refresh' then
57
+ service.configure({ max_projects = lsp.MAX_ROOTS })
58
+ service.refresh(root, { full_interval = 45, paths = req.paths })
59
+ return true
60
+ elseif req.method == 'explore' then
14
61
  local text, info = service.explore(root, req.query, { budget = req.budget })
15
62
  return { text = text, truncated = info.truncated or false, refresh = info.refresh }
16
63
  elseif req.method == 'status' then return service.status(root)
@@ -18,23 +65,67 @@ local function execute(req)
18
65
  else error('unknown operation') end
19
66
  end
20
67
 
68
+ -- Execute serially until per-file commits allow safe short-job interleaving.
69
+ -- Each job runs in a coroutine so a full refresh can wait for the parse
70
+ -- threads; jobs arriving meanwhile queue behind it.
71
+ local queue, active = {}, nil
72
+
73
+ local function run(id, req)
74
+ -- Draft text applies even to a cancelled request: the session counts it as sent.
75
+ if req.session and req.open then
76
+ local synced, err = pcall(navigation.sync, req)
77
+ if not synced then io.stderr:write('[codeoutline lsp] ', tostring(err), '\n') end
78
+ end
79
+ control.callback = function()
80
+ if shared:get('shutdown') or shared:get('cancel:' .. id) then error(control.cancelled(), 0) end
81
+ if xtimer.now_ms() > req.deadline then error(control.deadline(), 0) end
82
+ end
83
+ local ok, result = pcall(function() control.check(); return execute(req) end)
84
+ control.callback = nil
85
+ if not ok then result = tostring(result) end
86
+ shared:delete('cancel:' .. id)
87
+ assert(xthread.post(1, 'worker_result', id, ok, result))
88
+ end
89
+
90
+ local function resume(...)
91
+ local ok, err = coroutine.resume(active, ...)
92
+ if not ok then io.stderr:write('[codeoutline] index job failed: ', tostring(err), '\n') end
93
+ if coroutine.status(active) == 'dead' then active = nil; pool.bind(nil) end
94
+ end
95
+
96
+ local function pump()
97
+ while not active and #queue > 0 do
98
+ local item = table.remove(queue, 1)
99
+ active = coroutine.create(run)
100
+ pool.bind(active)
101
+ resume(item[1], item[2])
102
+ end
103
+ end
104
+
21
105
  return {
22
- __init = function() assert(xthread.post(1, 'worker_ready')) end,
23
- __update = function() service.sweep() end,
24
- __thread_handle = function(_, op, id, req)
25
- if op ~= 'run' then return end
26
- control.callback = function()
27
- if shared:get('shutdown') or shared:get('cancel:' .. id) then error('Request cancelled', 0) end
28
- if xtimer.now_ms() > req.deadline then error('Indexing deadline exceeded', 0) end
106
+ __init = function()
107
+ assert(xnet.init())
108
+ local threads = tonumber(os.getenv('CODEOUTLINE_INDEX_THREADS') or '') or pool.THREADS
109
+ pool.configure(scripts .. '/codeoutline/parse_worker.lua', math.max(0, math.floor(threads)))
110
+ assert(xthread.post(1, 'worker_ready'))
111
+ end,
112
+ -- A suspended job still holds its project: sweeping waits for it. Idle
113
+ -- ticks also collect garbage, a step at a time (see service.collect).
114
+ __update = function()
115
+ pool.sweep()
116
+ if not active then service.sweep(); service.collect() end
117
+ end,
118
+ __thread_handle = function(_, op, id, req, failure)
119
+ if op == 'run' then
120
+ queue[#queue + 1] = { id, req }
121
+ pump()
122
+ elseif op == 'parsed' then
123
+ -- (token, results, failure); stale tokens are dropped by the pool.
124
+ if pool.receive(id) and active and pool.waiting() then resume(id, req, failure); pump() end
29
125
  end
30
- -- Lua graph/parse loops are interruptible. Native scanning is bounded
31
- -- by max_file_bytes; explicit checkpoints also cover protected parses.
32
- debug.sethook(control.check, '', 20000)
33
- local ok, result = pcall(function() control.check(); return execute(req) end)
34
- debug.sethook()
35
- control.callback = nil
36
- if not ok then pcall(service.forget, req.projectPath) end
37
- shared:delete('cancel:' .. id)
38
- assert(xthread.post(1, 'worker_result', id, ok, result))
126
+ end,
127
+ __uninit = function()
128
+ pool.stop()
129
+ xnet.uninit()
39
130
  end,
40
131
  }