@codua/codeoutline-win32-x64 0.2.9 → 0.3.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 (33) hide show
  1. package/DISTRIBUTION.md +93 -8
  2. package/README.md +112 -2
  3. package/README.zh-CN.md +86 -2
  4. package/bin/xnet.exe +0 -0
  5. package/build-info.json +1 -1
  6. package/editors/codeoutline.vsix +0 -0
  7. package/editors/zed/extension.toml +15 -0
  8. package/editors/zed/extension.wasm +0 -0
  9. package/package.json +1 -1
  10. package/scripts/codeoutline/agent_config.lua +291 -0
  11. package/scripts/codeoutline/agent_install.lua +195 -0
  12. package/scripts/codeoutline/command.lua +110 -9
  13. package/scripts/codeoutline/completion.lua +66 -20
  14. package/scripts/codeoutline/cpp_types.lua +12 -3
  15. package/scripts/codeoutline/definition_candidates.lua +13 -7
  16. package/scripts/codeoutline/editor_install.lua +168 -0
  17. package/scripts/codeoutline/external_headers.lua +22 -9
  18. package/scripts/codeoutline/foreign.lua +23 -3
  19. package/scripts/codeoutline/idle_gc.lua +45 -0
  20. package/scripts/codeoutline/index.lua +3 -1
  21. package/scripts/codeoutline/index_worker.lua +39 -5
  22. package/scripts/codeoutline/install_prompt.lua +15 -0
  23. package/scripts/codeoutline/lang/cpp.lua +25 -14
  24. package/scripts/codeoutline/lsp.lua +44 -4
  25. package/scripts/codeoutline/lsp_trace.lua +57 -0
  26. package/scripts/codeoutline/navigation.lua +109 -11
  27. package/scripts/codeoutline/service.lua +38 -5
  28. package/scripts/codeoutline/type_references.lua +3 -2
  29. package/scripts/codeoutline/update_sequence.lua +1 -1
  30. package/scripts/codeoutline/version.lua +1 -1
  31. package/scripts/xupgate/client.lua +35 -18
  32. package/scripts/xupgate/common.lua +2 -2
  33. package/scripts/xupgate/http_client.lua +6 -1
@@ -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)
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,8 +70,10 @@ local function declarations(entry, name)
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
- and (node.kind == 'prototype' or node.kind == 'function') then
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
75
+ and (type_site and require('codeoutline.type_references').kinds[node.kind]
76
+ or not type_site and (node.kind == 'prototype' or node.kind == 'function')) then
74
77
  result[#result + 1] = { node = node, file = entry.file, source = entry.source,
75
78
  declaration = node.kind == 'prototype' or node.decl == true }
76
79
  end
@@ -78,14 +81,19 @@ local function declarations(entry, name)
78
81
  return result
79
82
  end
80
83
 
81
- function M.find(G, f, site, ctx, system_directories)
84
+ function M.find(G, f, site, ctx, system_directories, completion)
82
85
  local info = { files = 0, bytes = 0, truncated = false }
83
- if not site or site.recv or site.kind ~= 'call' or not require('codeoutline.definition_candidates').allowed(site) then return {}, info end
86
+ local type_site = site and site.kind == 'type' and site.reason == 'unresolved type'
87
+ and not site.recv and site.evidence and site.evidence.written == site.name
88
+ and not site.evidence.value_shadowed
89
+ if not site or site.recv or not type_site and (site.kind ~= 'call'
90
+ or not require('codeoutline.definition_candidates').allowed(site)) then return {}, info end
84
91
  local includes = require('codeoutline.cpp_includes')
85
92
  local headers, queued, projects, visited, project_queued = {}, {}, { f }, {}, { [f] = true }
86
93
  local defaults, started = nil, xtimer.now_ms()
87
94
  local function expired()
88
95
  control.check()
96
+ if completion and completion.expired() then info.truncated = true; return true end
89
97
  if xtimer.now_ms() - started >= M.TIME_MS then info.truncated = true; return true end
90
98
  return false
91
99
  end
@@ -158,10 +166,15 @@ function M.find(G, f, site, ctx, system_directories)
158
166
  local target = headers[hi]; hi = hi + 1
159
167
  local entry = read(target.file, info)
160
168
  if entry then
161
- local found = declarations(entry, site.name)
169
+ local found = declarations(entry, site.name, type_site, completion ~= nil)
162
170
  for _, hit in ipairs(found) do
163
- if hit.declaration then return { hit }, info end
164
- 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
165
178
  end
166
179
  for _, imp in ipairs(entry.imports) do add(locate(imp, target.roots, target.file), target.roots) end
167
180
  end
@@ -62,7 +62,9 @@ end
62
62
 
63
63
  function M.exports(rec, language, module)
64
64
  local exports = records[rec] or rec.foreign_exports
65
- return exports and exports[language] and exports[language][module]
65
+ local modules = exports and exports[language]
66
+ return modules and (modules[module] or language == 'lua' and type(module) == 'string'
67
+ and modules[module:gsub('%.', '_')])
66
68
  end
67
69
 
68
70
  function M.collect(rec, T)
@@ -71,9 +73,11 @@ end
71
73
 
72
74
  function M.modules(G, language, name, ctx)
73
75
  ctx = ctx or {}
76
+ if not name then return {} end
77
+ local loader = language == 'lua' and name:gsub('%.', '_') or name
74
78
  ctx.foreign = ctx.foreign or {}
75
79
  ctx.foreign_loaded = ctx.foreign_loaded or {}
76
- for _, f in ipairs(candidates(G, ctx)[name] or {}) do
80
+ for _, f in ipairs(candidates(G, ctx)[loader] or {}) do
77
81
  if not ctx.foreign_loaded[f] then
78
82
  local exports = load(G, f)
79
83
  -- Failed reads can be skipped within this query, but are never
@@ -89,12 +93,28 @@ function M.modules(G, language, name, ctx)
89
93
  end
90
94
  end
91
95
  end
92
- local files = (ctx.foreign[language] or {})[name] or {}
96
+ local modules = ctx.foreign[language] or {}
97
+ local files = modules[name] or modules[loader] or {}
93
98
  -- A long query may have evicted a previously used record.
94
99
  for _, f in ipairs(files) do load(G, f) end
95
100
  return files
96
101
  end
97
102
 
103
+ -- Lua's C loader replaces dots in a module name when naming the entry point.
104
+ -- The indexed entry alone is sufficient for navigating the require string;
105
+ -- member bindings still require the separately verified registration table.
106
+ function M.lua_entrypoints(G, name)
107
+ local result = {}
108
+ local symbol = 'luaopen_' .. name:gsub('%.', '_')
109
+ for _, id in ipairs(G.by_name[symbol] or {}) do
110
+ local node, f = G.nodes[id], G.node_file[id]
111
+ if node.kind == 'function' and not node.static and (G.files[f].language == 'c' or G.files[f].language == 'cpp') then
112
+ result[#result + 1] = id
113
+ end
114
+ end
115
+ return result
116
+ end
117
+
98
118
  -- Parameter labels are structured data, never reparsed as host-language code.
99
119
  function M.signature(node, language, name)
100
120
  local signatures = contracts[node] or node.foreign_signatures
@@ -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
@@ -225,24 +225,35 @@ local function cpp_decl(T, r, i, j, parent)
225
225
  if is(T, y, '=') and not eq and d == 0 and not kw(T, y - 1, 'operator') then eq = y end
226
226
  if is(T, y, '(') and T.m[y] then
227
227
  if not eq and d == 0 then
228
- local info = name_before_paren(T, y, i)
229
- if info and info.first > i - 1 and not ATTR[info.name] then
230
- local ctor = parent and r.nodes[parent].name == info.name
231
- if info.first == i and #info.quals == 0 and not info.name:find('^[~o]') and not ctor then
232
- common.add_calls(r, T, i, j) -- MACRO(args);
228
+ -- A function-pointer declarator is a field/variable, not a
229
+ -- function prototype: void (*callback)(int).
230
+ local close = T.m[y]
231
+ if is(T, y + 1, '*') and T.k[y + 2] == 'id' and close == y + 3
232
+ and is(T, close + 1, '(') and T.m[close + 1] then
233
+ names[#names + 1] = y + 2
234
+ y = T.m[close + 1] + 1
235
+ else
236
+ local info = name_before_paren(T, y, i)
237
+ if info and info.first > i - 1 and not ATTR[info.name] then
238
+ local ctor = parent and r.nodes[parent].name == info.name
239
+ if info.first == i and #info.quals == 0 and not info.name:find('^[~o]') and not ctor then
240
+ common.add_calls(r, T, i, j) -- MACRO(args);
241
+ return
242
+ end
243
+ common.add_node(r, T, {
244
+ kind = 'prototype', name = info.name, ni = info.ni, nj = info.nj, parent = parent,
245
+ ti = i, tj = j, sig = common.sig(T, i, T.m[y]),
246
+ quals = #info.quals > 0 and info.quals or nil,
247
+ static = first == 'static' or nil,
248
+ })
249
+ common.add_calls(r, T, T.m[y] + 1, j) -- default member initializers etc.
233
250
  return
234
251
  end
235
- common.add_node(r, T, {
236
- kind = 'prototype', name = info.name, ni = info.ni, nj = info.nj, parent = parent,
237
- ti = i, tj = j, sig = common.sig(T, i, T.m[y]),
238
- quals = #info.quals > 0 and info.quals or nil,
239
- static = first == 'static' or nil,
240
- })
241
- common.add_calls(r, T, T.m[y] + 1, j) -- default member initializers etc.
242
- return
252
+ y = T.m[y] + 1
243
253
  end
254
+ else
255
+ y = T.m[y] + 1
244
256
  end
245
- y = T.m[y] + 1
246
257
  elseif (is(T, y, '[') or is(T, y, '{')) and T.m[y] then
247
258
  if T.k[y - 1] == 'id' and is(T, y, '{') and not eq and d == 0 then names[#names + 1] = y - 1 end
248
259
  y = T.m[y] + 1
@@ -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