@codua/codeoutline-win32-x64 0.3.0 → 0.3.2

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.
@@ -53,9 +53,10 @@ local function read(file, info)
53
53
  end
54
54
  return entry
55
55
  end
56
- local function declarations(entry, name, type_site)
56
+ local function declarations(entry, name, type_site, prefix)
57
57
  local escaped = name:gsub('([^%w])', '%%%1')
58
- if not entry.source:find('%f[%w_$]' .. escaped .. '%f[^%w_$]') then return {} end
58
+ local source = prefix and entry.source:lower() or entry.source
59
+ if not source:find('%f[%w_$]' .. escaped .. (prefix and '' or '%f[^%w_$]')) then return {} end
59
60
  if not entry.nodes then
60
61
  local rec = require('codeoutline.parse').parse(entry.file, entry.source, { type_uses = false, analysis = false })
61
62
  entry.nodes = rec and rec.nodes or {}
@@ -69,7 +70,8 @@ local function declarations(entry, name, type_site)
69
70
  end
70
71
  local result = {}
71
72
  for _, node in ipairs(entry.nodes) do
72
- if node.name == name and node.qualified == name and not node.parent and node.name_start
73
+ if (prefix and node.name:sub(1, #name):lower() == name or not prefix and node.name == name)
74
+ and node.qualified == node.name and not node.parent and node.name_start
73
75
  and (type_site and require('codeoutline.type_references').kinds[node.kind]
74
76
  or not type_site and (node.kind == 'prototype' or node.kind == 'function')) then
75
77
  result[#result + 1] = { node = node, file = entry.file, source = entry.source,
@@ -79,7 +81,7 @@ local function declarations(entry, name, type_site)
79
81
  return result
80
82
  end
81
83
 
82
- function M.find(G, f, site, ctx, system_directories)
84
+ function M.find(G, f, site, ctx, system_directories, completion)
83
85
  local info = { files = 0, bytes = 0, truncated = false }
84
86
  local type_site = site and site.kind == 'type' and site.reason == 'unresolved type'
85
87
  and not site.recv and site.evidence and site.evidence.written == site.name
@@ -91,6 +93,7 @@ function M.find(G, f, site, ctx, system_directories)
91
93
  local defaults, started = nil, xtimer.now_ms()
92
94
  local function expired()
93
95
  control.check()
96
+ if completion and completion.expired() then info.truncated = true; return true end
94
97
  if xtimer.now_ms() - started >= M.TIME_MS then info.truncated = true; return true end
95
98
  return false
96
99
  end
@@ -163,10 +166,15 @@ function M.find(G, f, site, ctx, system_directories)
163
166
  local target = headers[hi]; hi = hi + 1
164
167
  local entry = read(target.file, info)
165
168
  if entry then
166
- local found = declarations(entry, site.name, type_site)
169
+ local found = declarations(entry, site.name, type_site, completion ~= nil)
167
170
  for _, hit in ipairs(found) do
168
- if hit.declaration then return { hit }, info end
169
- if #best < 8 then best[#best + 1] = hit end
171
+ if completion then
172
+ if #best >= completion.limit then info.truncated = true; return best, info end
173
+ best[#best + 1] = hit
174
+ else
175
+ if hit.declaration then return { hit }, info end
176
+ if #best < 8 then best[#best + 1] = hit end
177
+ end
170
178
  end
171
179
  for _, imp in ipairs(entry.imports) do add(locate(imp, target.roots, target.file), target.roots) end
172
180
  end
@@ -0,0 +1,45 @@
1
+ -- One pending native timer per worker. Timer callbacks run on the same Lua
2
+ -- state: they advance one GC step only while foreground work is absent.
3
+ -- The owning worker initializes xtimer in __init before scheduling requests.
4
+ local M = {}
5
+
6
+ function M.new(opts)
7
+ local timer = opts.timer or xtimer
8
+ local handle, wanted, start_cycle, closed = nil, false, false, false
9
+ local generation = 0
10
+ local arm
11
+ local function cancel()
12
+ generation = generation + 1
13
+ if handle then timer.del(handle); handle = nil end
14
+ end
15
+ arm = function()
16
+ cancel()
17
+ if closed or not wanted then return end
18
+ local token = generation
19
+ handle = timer.add(opts.delay_ms, function()
20
+ if closed or token ~= generation then return end
21
+ handle = nil
22
+ -- A suspended foreground coroutine also owns its resident state.
23
+ -- Its eventual reply will rearm us; do not poll while it waits.
24
+ if opts.busy() then return end
25
+ if start_cycle then opts.request(); start_cycle = false end
26
+ wanted = opts.step()
27
+ if wanted then arm() end
28
+ end, 1)
29
+ end
30
+ local self = {}
31
+ function self.schedule()
32
+ if closed then return end
33
+ wanted, start_cycle = true, true
34
+ arm()
35
+ end
36
+ function self.suspend() cancel() end
37
+ function self.pending() return wanted end
38
+ function self.close()
39
+ closed, wanted, start_cycle = true, false, false
40
+ cancel()
41
+ end
42
+ return self
43
+ end
44
+
45
+ return M
@@ -112,9 +112,11 @@ function M.open(root, opts)
112
112
  local max_bytes = opts.max_file_bytes or M.MAX_FILE_BYTES
113
113
  assert(type(max_bytes) == 'number' and max_bytes >= 1 and max_bytes < math.huge and max_bytes % 1 == 0,
114
114
  'max_file_bytes must be a positive integer')
115
+ local cache_override = os.getenv('CODEOUTLINE_MEMORY_CACHE')
116
+ if cache_override == '' then cache_override = nil end
115
117
  local self = setmetatable({
116
118
  root = norm(root),
117
- cache_path = opts.cache_path and norm(opts.cache_path) or cache_path_for(root),
119
+ cache_path = cache_override and norm(cache_override) or (opts.cache_path and norm(opts.cache_path) or cache_path_for(root)),
118
120
  max_bytes = max_bytes,
119
121
  lister = opts.lister, -- 'rg' | 'walk' | nil (rg when it runs here)
120
122
  files = {}, -- rel path -> record
@@ -55,10 +55,10 @@ local function execute(req)
55
55
  assert(paths.allowed(req.allowedRoots, root), 'project path is outside allowed roots')
56
56
  if req.method == 'lsp_refresh' then
57
57
  service.configure({ max_projects = lsp.MAX_ROOTS })
58
- local idx = service.refresh(root, { full_interval = 45, paths = req.paths })
59
- -- Ready navigation must not leave a whole-project symbol-table build
60
- -- on the first definition request. Symbol-only clients retain lazy data.
61
- if req.prepare_navigation then idx:symbols() end
58
+ -- Navigation startup includes symbol preparation in the same scoped GC
59
+ -- policy as cache loading. Symbol-only clients retain lazy preparation.
60
+ local refresh = req.prepare_navigation and service.get or service.refresh
61
+ refresh(root, { full_interval = 45, paths = req.paths })
62
62
  return true
63
63
  elseif req.method == 'explore' or req.method == 'callers' or req.method == 'type_hierarchy' or req.method == 'type_references' or req.method == 'affected' then
64
64
  local text, info = service[req.method](root, req.method == 'affected' and req.files or req.query, { budget = req.budget, depth = req.depth,
@@ -74,6 +74,23 @@ end
74
74
  -- Each job runs in a coroutine so a full refresh can wait for the parse
75
75
  -- threads; jobs arriving meanwhile queue behind it.
76
76
  local queue, active = {}, nil
77
+ -- Every external reply can release temporary analysis, including errors and
78
+ -- cancellation. Retain one pending timer and yield between collection steps.
79
+ local function gc_option(name, default, minimum)
80
+ local value = tonumber(os.getenv(name) or '')
81
+ if not value or value ~= value or math.abs(value) == math.huge then return default end
82
+ return math.max(minimum, math.floor(value))
83
+ end
84
+ local gc_every = gc_option('CODEOUTLINE_LSP_GC_EVERY', 1, 0)
85
+ local requests = 0
86
+ local gc_delay_ms = gc_option('CODEOUTLINE_LSP_GC_DELAY_MS', 1, 0)
87
+ local gc_step_kb = gc_option('CODEOUTLINE_LSP_GC_STEP_KB', 4096, 1)
88
+ local timer_gc = require('codeoutline.idle_gc').new({
89
+ delay_ms = gc_delay_ms,
90
+ busy = function() return active ~= nil or #queue > 0 end,
91
+ request = service.request_collection,
92
+ step = function() return service.collect(gc_step_kb) end,
93
+ })
77
94
 
78
95
  local function run(id, req, reply)
79
96
  if req.queue_until then assert(xthread.post(1, 'worker_started', id)) end
@@ -96,6 +113,12 @@ local function run(id, req, reply)
96
113
  else
97
114
  assert(xthread.post(1, 'worker_result', id, ok, result))
98
115
  end
116
+ requests = requests + 1
117
+ if gc_every > 0 and requests % gc_every == 0 then
118
+ timer_gc.schedule()
119
+ elseif timer_gc.pending() then
120
+ timer_gc.schedule()
121
+ end
99
122
  end
100
123
 
101
124
  local function resume(...)
@@ -116,6 +139,7 @@ end
116
139
  return {
117
140
  __init = function()
118
141
  assert(xnet.init())
142
+ xtimer.init()
119
143
  local threads = tonumber(os.getenv('CODEOUTLINE_INDEX_THREADS') or '') or pool.THREADS
120
144
  pool.configure(scripts .. '/codeoutline/parse_worker.lua', math.max(0, math.floor(threads)))
121
145
  assert(xthread.post(1, 'worker_ready'))
@@ -124,14 +148,23 @@ return {
124
148
  -- ticks also collect garbage, a step at a time (see service.collect).
125
149
  __update = function()
126
150
  pool.sweep()
127
- if not active then service.sweep(); service.collect() end
151
+ if not active and #queue == 0 then
152
+ service.sweep()
153
+ -- Timer callbacks own pending cycles. Retain idle collection for
154
+ -- allocations/dropped projects that happen after a timer finishes.
155
+ if not timer_gc.pending() then
156
+ service.collect()
157
+ end
158
+ end
128
159
  end,
129
160
  __thread_handle = function(router, op, id, req, failure, rpc_request)
130
161
  if type(router) == 'string' and req == 'run_rpc' then
131
162
  -- RPC wire: router, coroutine id, session key, method, job id, request.
163
+ timer_gc.suspend()
132
164
  queue[#queue + 1] = { failure, rpc_request, { router = router, co = op, sk = id } }
133
165
  pump()
134
166
  elseif op == 'run' then
167
+ timer_gc.suspend()
135
168
  queue[#queue + 1] = { id, req }
136
169
  pump()
137
170
  elseif op == 'parsed' then
@@ -140,6 +173,7 @@ return {
140
173
  end
141
174
  end,
142
175
  __uninit = function()
176
+ timer_gc.close()
143
177
  pool.stop()
144
178
  xnet.uninit()
145
179
  end,
@@ -0,0 +1,15 @@
1
+ -- Shared terminal confirmation for optional integrations. EOF skips the rest.
2
+ local M = {}
3
+ function M.confirm(message, default)
4
+ while true do
5
+ io.write(message, default and ' [Y/n]\n' or ' [y/N]\n'); io.stdout:flush()
6
+ local answer = io.read('*l')
7
+ if answer == nil then return nil end
8
+ answer = answer:lower():gsub('^%s+', ''):gsub('%s+$', '')
9
+ if answer == '' then return default end
10
+ if answer == 'y' or answer == 'yes' then return true end
11
+ if answer == 'n' or answer == 'no' then return false end
12
+ io.write('Please enter y or n, or press Enter for the default.\n'); io.stdout:flush()
13
+ end
14
+ end
15
+ return M
@@ -3,7 +3,8 @@ local paths = require('codeoutline.path')
3
3
  local text = require('codeoutline.text')
4
4
  local M = { MAX_HEADER = 16384, MAX_MESSAGE = 4194304, MAX_ROOTS = 16,
5
5
  MAX_DOCUMENTS = 128, MAX_DRAFT_BYTES = 33554432, MAX_PENDING = 64,
6
- MAX_QUERY = 4096, MAX_SYMBOLS = 200, POLL_MS = 1000, WARM_DELAY_MS = 150 }
6
+ MAX_QUERY = 4096, MAX_SYMBOLS = 200, POLL_MS = 1000, WARM_DELAY_MS = 150,
7
+ LIMIT_WARNING_MS = 10000 }
7
8
  local null = xutils.json_null
8
9
  local function array(t) return setmetatable(t or {}, xutils.json_array_mt) end
9
10
  local function object(t) return type(t) == 'table' and getmetatable(t) ~= xutils.json_array_mt end
@@ -95,8 +96,17 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
95
96
  local self = { state = 'new', documents = {}, roots = {}, pending = {}, scans = {}, ready = {},
96
97
  document_bytes = 0, document_count = 0, session = config.session or ('lsp-' .. sessions),
97
98
  synced = {}, features = {} }
99
+ local trace = require('codeoutline.lsp_trace').open(os.getenv('CODEOUTLINE_LSP_TRACE'), self.session)
100
+ if trace then
101
+ local transport_emit = emit
102
+ emit = function(msg) trace:message('send', msg); transport_emit(msg) end
103
+ trace:record('start', { source = debug.getinfo(1, 'S').source })
104
+ end
98
105
  local progress, server_pending, next_id, next_poll = {}, {}, 0, 0
99
106
  local navigation_flights, navigation_epoch = {}, 0
107
+ -- Clients also query definitions while restoring focus or showing links.
108
+ -- Preserve every incomplete report in the log without stacking popups.
109
+ local limit_warnings = {}
100
110
  local warm_pending, warm_active, warm_done, warm_pump, focused_uri
101
111
  local handlers = {}
102
112
  local function error_response(id, code, message)
@@ -272,6 +282,7 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
272
282
  end
273
283
  local function cancel_pending(id, silent)
274
284
  local job = self.pending[id]
285
+ if trace then trace:record('cancel', { id = id, pending = job ~= nil, silent = silent or false }) end
275
286
  if job then
276
287
  self.pending[id] = nil
277
288
  if job.flight then
@@ -285,6 +296,13 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
285
296
  if not silent then error_response(id, -32800, 'Request cancelled') end
286
297
  end
287
298
  end
299
+ local function cancel_completions(uri)
300
+ local ids = {}
301
+ for id, job in pairs(self.pending) do
302
+ if job.method == 'lsp_completion' and job.uri == uri then ids[#ids + 1] = id end
303
+ end
304
+ for _, id in ipairs(ids) do cancel_pending(id) end
305
+ end
288
306
  function self:close()
289
307
  if self.state == 'closed' then return end
290
308
  self.state = 'closed'
@@ -296,6 +314,7 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
296
314
  if next(self.synced) or self.worker_session then submit({ method = 'lsp_close', session = self.session }, function() end) end
297
315
  self.scans, self.documents, self.ready = {}, {}, {}
298
316
  progress, server_pending = {}, {}
317
+ if trace then trace:record('close'); trace:close() end
299
318
  end
300
319
  handlers.initialize = function(id, params)
301
320
  assert(self.state == 'new', 'server already initialized')
@@ -372,10 +391,12 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
372
391
  assert(self.document_bytes - #doc.source + #updated.source <= M.MAX_DRAFT_BYTES, 'open document limit reached')
373
392
  self.documents[td.uri] = updated
374
393
  self.document_bytes = self.document_bytes - #doc.source + #updated.source
394
+ cancel_completions(server_uri(td.uri))
375
395
  schedule_warm(td.uri, false, 500)
376
396
  end
377
397
  handlers['textDocument/didClose'] = function(_, params)
378
398
  local uri = params.textDocument.uri
399
+ cancel_completions(server_uri(uri))
379
400
  if warm_pending and warm_pending.uri == uri then warm_pending = nil end
380
401
  if warm_active and warm_active.uri == uri then cancel_warm() end
381
402
  if focused_uri == uri then focused_uri = nil end
@@ -433,11 +454,15 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
433
454
  -- Each query owns a coroutine; cancelled/replaced requests discard replies.
434
455
  local function request(id, req, started, key)
435
456
  cancel_warm()
457
+ if req.method == 'lsp_completion' then cancel_completions(req.uri) end
436
458
  if warm_pending and req.uri == server_uri(warm_pending.uri) then warm_pending = nil end
437
459
  local count = 0
438
460
  for _ in pairs(self.pending) do count = count + 1 end
439
461
  assert(count < M.MAX_PENDING, 'too many pending requests')
440
- local job = { coroutine = coroutine.running() }
462
+ local job = { coroutine = coroutine.running(), method = req.method, uri = req.uri }
463
+ local started_ms = trace and xtimer.now_ms()
464
+ if trace then trace:record('query', { id = id, method = req.method, uri = req.uri,
465
+ position = req.position, epoch = navigation_epoch, shared = key and navigation_flights[key] ~= nil or false }) end
441
466
  self.pending[id] = job
442
467
  local called, ok, result
443
468
  local flight = key and navigation_flights[key]
@@ -477,6 +502,9 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
477
502
  if not ok and req.changed then self.synced = {} end
478
503
  if self.pending[id] ~= job then return nil, false end
479
504
  self.pending[id] = nil
505
+ if trace then trace:record('query_done', { id = id, method = req.method,
506
+ ms = xtimer.now_ms() - started_ms, ok = ok,
507
+ incomplete = type(result) == 'table' and result.incomplete or false }) end
480
508
  if not ok then
481
509
  error_response(id, -32603, tostring(result))
482
510
  return nil, false
@@ -571,13 +599,24 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
571
599
  if not ok then return end
572
600
  if value.error then error_response(id, -32803, value.error); return end
573
601
  if value.incomplete then
602
+ if trace then trace:record('limit', { id = id, method = req.method, uri = req.uri,
603
+ position = req.position, epoch = navigation_epoch }) end
574
604
  local message = req.method == 'lsp_definition'
575
605
  and 'CodeOutline: definition lookup reached a candidate, source or visibility limit; some definitions may be omitted'
576
606
  or req.method == 'lsp_references'
577
607
  and 'CodeOutline: results were truncated; too many matching references'
578
608
  or 'CodeOutline: lookup reached a query limit; some results may be omitted'
579
- emit({ jsonrpc = '2.0', method = 'window/showMessage', params = { type = 2,
580
- message = message } })
609
+ local now, previous = xtimer.now_ms(), limit_warnings[message]
610
+ local show = not previous or now - previous >= M.LIMIT_WARNING_MS
611
+ if show then limit_warnings[message] = now end
612
+ local detail = message
613
+ if not show then
614
+ local position = req.position or {}
615
+ detail = message .. ' [' .. req.method .. ' ' .. (req.uri or '')
616
+ .. ':' .. tostring(position.line or 0) .. ':' .. tostring(position.character or 0) .. ']'
617
+ end
618
+ emit({ jsonrpc = '2.0', method = show and 'window/showMessage' or 'window/logMessage',
619
+ params = { type = 2, message = detail } })
581
620
  end
582
621
  reply(id, to_client(finish(value.result)))
583
622
  end
@@ -707,6 +746,7 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
707
746
  function self:accept(body)
708
747
  local ok, msg = pcall(xutils.json_unpack, body)
709
748
  if not ok or text.valid(body) ~= body then error_response(nil, -32700, 'Invalid JSON'); return end
749
+ if trace and object(msg) then trace:message('receive', msg) end
710
750
  if object(msg) and msg.jsonrpc == '2.0' and msg.method == nil and valid_id(msg.id)
711
751
  and ((msg.result ~= nil and msg.error == nil) or (msg.result == nil and object(msg.error))) then
712
752
  local job = server_pending[msg.id]
@@ -0,0 +1,57 @@
1
+ -- Opt-in local protocol metadata; document text and result bodies are omitted.
2
+ local M = {}
3
+
4
+ function M.open(path, session)
5
+ if not path or path == '' then return nil end
6
+ local file, err = io.open(path, 'ab')
7
+ if not file then
8
+ io.stderr:write('[codeoutline lsp trace] cannot open trace: ', tostring(err), '\n')
9
+ return nil
10
+ end
11
+ local self, sequence = {}, 0
12
+ function self:record(event, fields)
13
+ if not file then return end
14
+ sequence = sequence + 1
15
+ local row = { event = event, session = session, sequence = sequence,
16
+ utc = os.date('!%Y-%m-%dT%H:%M:%SZ'), ms = xtimer.now_ms(), fields = fields or {} }
17
+ local ok, failure = file:write(xutils.json_pack(row), '\n')
18
+ if ok then ok, failure = file:flush() end
19
+ if not ok then
20
+ io.stderr:write('[codeoutline lsp trace] write failed: ', tostring(failure), '\n')
21
+ file:close(); file = nil
22
+ end
23
+ end
24
+ function self:message(direction, msg)
25
+ local fields = { id = msg.id, method = msg.method }
26
+ local params = type(msg.params) == 'table' and msg.params or {}
27
+ local doc = type(params.textDocument) == 'table' and params.textDocument or {}
28
+ fields.uri, fields.version, fields.language = doc.uri, doc.version, doc.languageId
29
+ if type(params.position) == 'table' then
30
+ fields.position = { line = params.position.line, character = params.position.character }
31
+ end
32
+ if type(doc.text) == 'string' then fields.text_bytes = #doc.text end
33
+ if type(params.contentChanges) == 'table' then fields.changes = #params.contentChanges end
34
+ if msg.method == '$/cancelRequest' then fields.cancel_id = params.id end
35
+ if msg.method == 'initialize' then
36
+ fields.client = type(params.clientInfo) == 'table' and params.clientInfo.name or nil
37
+ fields.root_uri = params.rootUri
38
+ end
39
+ if msg.method == 'window/showMessage' or msg.method == 'window/logMessage' then
40
+ fields.message, fields.type = params.message, params.type
41
+ end
42
+ if type(msg.error) == 'table' then fields.error_code = msg.error.code end
43
+ if type(msg.result) == 'table' then
44
+ fields.result_count = #msg.result
45
+ fields.incomplete = msg.result.isIncomplete
46
+ local first = msg.result[1]
47
+ if type(first) == 'table' then fields.target_uri = first.uri or first.targetUri end
48
+ end
49
+ self:record(direction, fields)
50
+ end
51
+ function self:close()
52
+ if file then file:close(); file = nil end
53
+ end
54
+ return self
55
+ end
56
+
57
+ return M
@@ -752,6 +752,12 @@ local function probe_includes()
752
752
  return unique
753
753
  end
754
754
 
755
+ function M.external_completions(V, f, prefix, line, expired, limit)
756
+ return require('codeoutline.external_headers').find(V, f,
757
+ { name = prefix, kind = 'call', reason = 'no indexed target', line = line },
758
+ V.js_context, probe_includes, { expired = expired, limit = limit })
759
+ end
760
+
755
761
  -- Locations of the files an import on this line names: project files by the
756
762
  -- dependency rules, and C/C++ headers found in the include directories.
757
763
  local function import_targets(V, f, line)
@@ -1320,6 +1326,21 @@ function handlers.lsp_definition(req)
1320
1326
  return result, V.js_context.include_incomplete or V.js_context.workspace_incomplete or V.js_context.go_incomplete or V.js_context.go_methods_incomplete or V.js_context.cpp_receivers_incomplete or V.js_context.rust_incomplete
1321
1327
  end
1322
1328
 
1329
+ -- Display tags may be truncated before a heavily annotated SDK declaration's
1330
+ -- argument list. Recover only the selected function's bounded source span.
1331
+ local function external_signature(entry)
1332
+ local label, node = entry.node.sig, entry.node
1333
+ if label and not label:match('%.%.%.$') then return label end
1334
+ if not node.name_start then return label end
1335
+ local source = entry.source:sub(node.name_start + 1, node.name_start + 4096)
1336
+ local doc = documents.new(documents.uri(entry.file), source, 0)
1337
+ local T = doc:tokens()
1338
+ if T and T:text(1) == node.name and T:text(2) == '(' and T.m[2] then
1339
+ return source:sub(1, T.e[T.m[2]]):gsub('%s+', ' ')
1340
+ end
1341
+ return label
1342
+ end
1343
+
1323
1344
  -- Follow unmatched delimiters up to the cursor. Commas in nested calls,
1324
1345
  -- literals and indexing expressions do not advance the enclosing argument.
1325
1346
  function handlers.lsp_signature(req)
@@ -1378,19 +1399,16 @@ function handlers.lsp_signature(req)
1378
1399
  local query = {}
1379
1400
  for key, value in pairs(req) do query[key] = value end
1380
1401
  query.position = doc:position(T.s[name_token] - 1)
1381
- local V, _, c, targets = at(query)
1402
+ local V, _, c, targets, f = at(query)
1382
1403
  if not V or not c or c.decl then return false end
1383
1404
  local signatures, seen = {}, {}
1384
- for _, id in ipairs(targets) do
1385
- local node = V.nodes[id]
1386
- local foreign = require('codeoutline.foreign').signature(node, doc:parse().language, T:text(name_token))
1387
- local label = M.detail(V, V.node_file[id], node).sig
1405
+ local function add_signature(node, label, uri, foreign)
1388
1406
  if foreign and not seen[foreign.label] then
1389
1407
  foreign.activeParameter = #foreign.parameters > 0 and math.min(call.argument, #foreign.parameters - 1) or nil
1390
1408
  signatures[#signatures + 1] = foreign
1391
1409
  seen[foreign.label] = true
1392
1410
  elseif not foreign and REFERENCE[node.kind] and label and not seen[label] then
1393
- local sig_doc = documents.new(uri_of(V, V.node_file[id]), label, 0)
1411
+ local sig_doc = documents.new(uri, label, 0)
1394
1412
  local S = sig_doc:tokens()
1395
1413
  local opening
1396
1414
  for i = 1, S and S.n - 1 or 0 do
@@ -1418,6 +1436,20 @@ function handlers.lsp_signature(req)
1418
1436
  end
1419
1437
  end
1420
1438
  end
1439
+ for _, id in ipairs(targets) do
1440
+ local node = V.nodes[id]
1441
+ add_signature(node, M.detail(V, V.node_file[id], node).sig, uri_of(V, V.node_file[id]),
1442
+ require('codeoutline.foreign').signature(node, doc:parse().language, T:text(name_token)))
1443
+ end
1444
+ if #targets == 0 and C_FAMILY[doc:parse().language] then
1445
+ -- Share definition lookup's bounded, source-validated external-header
1446
+ -- cache. SDK declarations stay query-local and never enter the index.
1447
+ local entries = require('codeoutline.external_headers').find(V, f,
1448
+ c, V.js_context, probe_includes)
1449
+ for _, entry in ipairs(entries) do
1450
+ add_signature(entry.node, external_signature(entry), documents.uri(entry.file))
1451
+ end
1452
+ end
1421
1453
  if #signatures == 0 then return false end
1422
1454
  return { signatures = signatures, activeSignature = 0, activeParameter = signatures[1].activeParameter }
1423
1455
  end
@@ -4,6 +4,8 @@ local explore = require('codeoutline.explore')
4
4
  local paths = require('codeoutline.path')
5
5
  local control = require('codeoutline.control')
6
6
  local M = {}
7
+ -- Loading uses tighter GC thresholds; queries retain the caller's settings.
8
+ local startup_gc = os.getenv('CODEOUTLINE_MEMORY_POLICY') ~= 'default'
7
9
  local projects = {}
8
10
  local sequence = 0
9
11
  local limits = { max_projects = 8, idle_seconds = 900 }
@@ -73,12 +75,17 @@ end
73
75
  -- reaches a sixteenth of the live heap (at least GC_MIN_KB), or a project is
74
76
  -- dropped/replaced, and then advances one step per call until it completes.
75
77
  -- Dropped indexes also need a follow-up cycle to reclaim already-marked objects.
76
- function M.collect()
78
+ -- Request a cycle without doing marking work on the foreground request.
79
+ function M.request_collection()
80
+ gc.running = true
81
+ end
82
+
83
+ function M.collect(step_kb)
77
84
  if not gc.running then
78
85
  if collectgarbage('count') - gc.live < math.max(M.GC_MIN_KB, gc.live / 16) then return false end
79
86
  gc.running = true
80
87
  end
81
- if collectgarbage('step', M.GC_STEP_KB) then
88
+ if collectgarbage('step', step_kb or M.GC_STEP_KB) then
82
89
  if gc.again then gc.again = false
83
90
  else gc.running, gc.live = false, collectgarbage('count') end
84
91
  end
@@ -93,10 +100,9 @@ end
93
100
 
94
101
  -- Files commit one at a time, so an interrupted refresh needs no rollback:
95
102
  -- the index stays consistent and the next refresh resumes with a full pass.
96
- local function refresh(root, opts, with_symbols)
103
+ local function refresh_project(root, opts, with_symbols)
97
104
  opts = opts or {}
98
105
  local key, canonical = key_for(root)
99
- M.sweep()
100
106
  local p = projects[key]
101
107
  if p then
102
108
  for _, name in ipairs({ 'cache_path', 'max_file_bytes', 'lister' }) do
@@ -135,6 +141,24 @@ local function refresh(root, opts, with_symbols)
135
141
  return p.idx, with_symbols and p.idx:symbols() or nil, stats
136
142
  end
137
143
 
144
+ -- Keep transient cache decoding/building allocations below the resident heap's
145
+ -- normal GC threshold. Restore the caller's settings before serving queries,
146
+ -- including when opening, indexing, saving or cancellation raises an error.
147
+ local function refresh(root, opts, with_symbols)
148
+ local key = key_for(root)
149
+ M.sweep()
150
+ if not startup_gc or projects[key] then
151
+ return refresh_project(root, opts, with_symbols)
152
+ end
153
+ local pause = collectgarbage('setpause', 100)
154
+ local stepmul = collectgarbage('setstepmul', 500)
155
+ local ok, idx, symbols, stats = pcall(refresh_project, root, opts, with_symbols)
156
+ collectgarbage('setpause', pause)
157
+ collectgarbage('setstepmul', stepmul)
158
+ if not ok then error(idx, 0) end
159
+ return idx, symbols, stats
160
+ end
161
+
138
162
  -- LSP refreshes records; symbol tables are kept current only once built.
139
163
  function M.refresh(root, opts)
140
164
  local idx, _, stats = refresh(root, opts, false)
@@ -155,7 +179,7 @@ function M.resident(root)
155
179
  return p.idx, p.idx.symbol_tables
156
180
  end
157
181
 
158
- local function run_query(engine, root, query, opts)
182
+ local function execute_query(engine, root, query, opts)
159
183
  engine.validate(query, opts)
160
184
  local idx, _, stats = refresh(root, opts, true)
161
185
  idx.query_sources = {}
@@ -194,6 +218,15 @@ local function run_query(engine, root, query, opts)
194
218
  return result.text, result.info
195
219
  end
196
220
 
221
+ -- Agent work can spend longer reclaiming its temporary analysis. Keep this
222
+ -- outside execute_query's stack and leave LSP navigation on its idle GC path.
223
+ local function run_query(engine, root, query, opts)
224
+ local ok, text, info = pcall(execute_query, engine, root, query, opts)
225
+ if os.getenv('CODEOUTLINE_AGENT_GC') ~= '0' then collectgarbage('collect') end
226
+ if not ok then error(text, 0) end
227
+ return text, info
228
+ end
229
+
197
230
  function M.explore(root, query, opts)
198
231
  return run_query(explore, root, query, opts)
199
232
  end
@@ -1 +1 @@
1
- return 19
1
+ return 21
@@ -1 +1 @@
1
- return '0.3.0'
1
+ return '0.3.2'