@codua/codeoutline-win32-x64 0.2.4 → 0.2.9

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 (77) hide show
  1. package/README.md +205 -3
  2. package/README.zh-CN.md +40 -2
  3. package/bin/xnet.exe +0 -0
  4. package/build-info.json +1 -1
  5. package/package.json +1 -1
  6. package/scripts/codeoutline/affected.lua +145 -0
  7. package/scripts/codeoutline/automatic_events.lua +202 -0
  8. package/scripts/codeoutline/cache_repairs.lua +47 -0
  9. package/scripts/codeoutline/callers.lua +56 -16
  10. package/scripts/codeoutline/cargo_toml.lua +125 -0
  11. package/scripts/codeoutline/command.lua +26 -4
  12. package/scripts/codeoutline/common.lua +65 -9
  13. package/scripts/codeoutline/completion.lua +221 -65
  14. package/scripts/codeoutline/coverage.lua +63 -0
  15. package/scripts/codeoutline/cpp_bindings.lua +151 -0
  16. package/scripts/codeoutline/cpp_fragments.lua +143 -0
  17. package/scripts/codeoutline/cpp_implementations.lua +340 -0
  18. package/scripts/codeoutline/cpp_includes.lua +156 -0
  19. package/scripts/codeoutline/cpp_macros.lua +279 -0
  20. package/scripts/codeoutline/cpp_receivers.lua +342 -0
  21. package/scripts/codeoutline/cpp_types.lua +456 -0
  22. package/scripts/codeoutline/csharp_bindings.lua +498 -0
  23. package/scripts/codeoutline/definition_candidates.lua +180 -0
  24. package/scripts/codeoutline/dispatch_aliases.lua +479 -0
  25. package/scripts/codeoutline/documents.lua +32 -9
  26. package/scripts/codeoutline/explore.lua +274 -119
  27. package/scripts/codeoutline/external_headers.lua +171 -0
  28. package/scripts/codeoutline/foreign.lua +79 -14
  29. package/scripts/codeoutline/foreign_lua.lua +146 -19
  30. package/scripts/codeoutline/function_values.lua +69 -0
  31. package/scripts/codeoutline/go_methods.lua +931 -0
  32. package/scripts/codeoutline/go_modules.lua +329 -0
  33. package/scripts/codeoutline/graph.lua +346 -63
  34. package/scripts/codeoutline/implementations.lua +403 -0
  35. package/scripts/codeoutline/index.lua +165 -27
  36. package/scripts/codeoutline/index_worker.lua +11 -4
  37. package/scripts/codeoutline/indirect.lua +116 -40
  38. package/scripts/codeoutline/js_evidence.lua +32 -3
  39. package/scripts/codeoutline/js_resolution.lua +72 -10
  40. package/scripts/codeoutline/js_workspace.lua +325 -0
  41. package/scripts/codeoutline/lang/c.lua +15 -2
  42. package/scripts/codeoutline/lang/cpp.lua +7 -4
  43. package/scripts/codeoutline/lang/csharp.lua +15 -3
  44. package/scripts/codeoutline/lang/js.lua +56 -20
  45. package/scripts/codeoutline/lang/rust.lua +9 -1
  46. package/scripts/codeoutline/language_detect.lua +100 -0
  47. package/scripts/codeoutline/lsp.lua +173 -23
  48. package/scripts/codeoutline/lua_bindings.lua +228 -54
  49. package/scripts/codeoutline/lua_callback_loops.lua +210 -0
  50. package/scripts/codeoutline/lua_literal.lua +38 -0
  51. package/scripts/codeoutline/main.lua +32 -7
  52. package/scripts/codeoutline/mcp.lua +52 -9
  53. package/scripts/codeoutline/navigation.lua +593 -147
  54. package/scripts/codeoutline/on_demand.lua +93 -0
  55. package/scripts/codeoutline/ordinary_bindings.lua +396 -0
  56. package/scripts/codeoutline/parse.lua +18 -2
  57. package/scripts/codeoutline/parse_pool.lua +1 -1
  58. package/scripts/codeoutline/parse_worker.lua +1 -1
  59. package/scripts/codeoutline/python_bindings.lua +112 -0
  60. package/scripts/codeoutline/query_filters.lua +65 -0
  61. package/scripts/codeoutline/query_report.lua +135 -8
  62. package/scripts/codeoutline/resolution.lua +23 -0
  63. package/scripts/codeoutline/rules.lua +127 -0
  64. package/scripts/codeoutline/rust_packages.lua +203 -0
  65. package/scripts/codeoutline/rust_resolution.lua +301 -0
  66. package/scripts/codeoutline/search_ranking.lua +70 -0
  67. package/scripts/codeoutline/service.lua +47 -7
  68. package/scripts/codeoutline/shared_events.lua +369 -0
  69. package/scripts/codeoutline/static_dispatch.lua +569 -0
  70. package/scripts/codeoutline/text.lua +3 -2
  71. package/scripts/codeoutline/ts_implementations.lua +81 -0
  72. package/scripts/codeoutline/type_bases.lua +61 -0
  73. package/scripts/codeoutline/type_hierarchy.lua +324 -0
  74. package/scripts/codeoutline/type_references.lua +290 -0
  75. package/scripts/codeoutline/type_uses.lua +260 -0
  76. package/scripts/codeoutline/update_sequence.lua +1 -1
  77. package/scripts/codeoutline/version.lua +1 -1
@@ -0,0 +1,100 @@
1
+ -- Conservative, query-only language selection for unknown filenames.
2
+ local M = { MAX_SAMPLE = 65536 }
3
+ local aliases = { ['c++'] = 'cpp', ['c#'] = 'csharp', javascriptreact = 'javascript',
4
+ typescriptreact = 'typescript', jsx = 'javascript', tsx = 'typescript', ets = 'arkts' }
5
+ local supported = { c = true, cpp = true, lua = true, python = true, java = true,
6
+ kotlin = true, go = true, javascript = true, typescript = true, arkts = true,
7
+ csharp = true, rust = true }
8
+
9
+ -- Ignore strings and comments so embedded examples cannot choose a parser.
10
+ local function code(source)
11
+ local parts, i, first = {}, 1, 1
12
+ local function mask(last)
13
+ parts[#parts + 1] = source:sub(first, i - 1)
14
+ parts[#parts + 1] = source:sub(i, last):gsub('[^\r\n]', ' ')
15
+ i, first = last + 1, last + 1
16
+ end
17
+ while i <= #source do
18
+ if i % 4096 == 0 then require('codeoutline.control').check() end
19
+ local pair, c = source:sub(i, i + 1), source:sub(i, i)
20
+ local lua_comment = pair == '--' and source:match('^%-%-(%[=*%[)', i)
21
+ if lua_comment then
22
+ local ending = lua_comment:gsub('%[', ']')
23
+ local close = source:find(ending, i + 2 + #lua_comment, true)
24
+ mask(close and close + #ending - 1 or #source)
25
+ elseif pair == '/*' then
26
+ local close = source:find('*/', i + 2, true); mask(close and close + 1 or #source)
27
+ elseif pair == '//' or pair == '--' then
28
+ mask((source:find('\n', i + 2, true) or (#source + 1)) - 1)
29
+ elseif c == '#' and not source:match('^#%s*include%f[%W]', i)
30
+ and not source:match('^#%s*define%f[%W]', i) then
31
+ mask((source:find('\n', i + 1, true) or (#source + 1)) - 1)
32
+ elseif c == '"' or c == "'" or c == '`' then
33
+ local triple = source:sub(i, i + 2) == string.rep(c, 3)
34
+ local j = i + (triple and 3 or 1)
35
+ local ending = triple and string.rep(c, 3) or c
36
+ while j <= #source do
37
+ if source:sub(j, j) == '\\' then j = j + 2
38
+ elseif source:sub(j, j + #ending - 1) == ending then j = j + #ending; break
39
+ else j = j + 1 end
40
+ end
41
+ mask(math.min(#source, j - 1))
42
+ elseif c == '[' and source:match('^%[=*%[', i) then
43
+ local open = source:match('^(%[=*%[)', i)
44
+ local ending = open:gsub('%[', ']')
45
+ local close = source:find(ending, i + #open, true)
46
+ mask(close and close + #ending - 1 or #source)
47
+ else i = i + 1 end
48
+ end
49
+ parts[#parts + 1] = source:sub(first)
50
+ return '\n' .. table.concat(parts)
51
+ end
52
+
53
+ function M.resolve(source, hint, detect)
54
+ local language = type(hint) == 'string' and hint:lower()
55
+ language = aliases[language] or language
56
+ if supported[language] then return language, 'editor' end
57
+ if not detect or source:find('%z') then return nil end
58
+ local sample = source:sub(1, M.MAX_SAMPLE)
59
+ local shebang = sample:match('^#!([^\r\n]+)')
60
+ if shebang then
61
+ if shebang:find('python', 1, true) then return 'python', 'syntax' end
62
+ if shebang:find('lua', 1, true) then return 'lua', 'syntax' end
63
+ if shebang:find('node', 1, true) then return 'javascript', 'syntax' end
64
+ return nil
65
+ end
66
+ if sample:find('```', 1, true) or sample:lower():find('<!doctype', 1, true)
67
+ or sample:lower():find('<html', 1, true) then return nil end
68
+ local s, choices = code(sample), {}
69
+ local function add(lang, yes) if yes then choices[lang] = true end end
70
+ add('lua', s:find('%f[%w_]local%s+function%s+[%w_]+%s*%(')
71
+ or s:find('%f[%w_]local%s+[%w_]+%s*=%s*function%s*%(') and s:find('%f[%w_]end%f[%W]')
72
+ or s:find('%f[%w_]function%s+[%w_%.:]+%s*%(') and s:find('%f[%w_]end%f[%W]'))
73
+ add('python', s:find('\n%s*def%s+[%w_]+%s*%([^\n]-%)%s*[:%-]')
74
+ or s:find('\n%s*class%s+[%w_]+[^\n]-:%s*\n'))
75
+ add('rust', s:find('%f[%w_]fn%s+[%w_]+%s*[<(]') and s:find('{', 1, true))
76
+ add('go', s:find('\n%s*package%s+[%w_]+') and s:find('%f[%w_]func%s+[%w_(]'))
77
+ add('kotlin', s:find('%f[%w_]fun%s+[%w_]+%s*[<(]'))
78
+ add('java', (s:find('%f[%w_]package%s+[%w_%.]+%s*;') or s:find('%f[%w_]import%s+java%.'))
79
+ and (s:find('%f[%w_]class%s+') or s:find('%f[%w_]interface%s+')))
80
+ add('csharp', s:find('%f[%w_]using%s+System[%s%.;]')
81
+ and (s:find('%f[%w_]class%s+') or s:find('%f[%w_]namespace%s+')))
82
+ local js = s:find('%f[%w_]function%s+[%w_$]+%s*%([^)]*%)%s*{')
83
+ or s:find('%f[%w_]const%s+[%w_$]+[^\n]-=>')
84
+ or s:find('%f[%w_]export%s+default%f[%W]')
85
+ local ts = s:find('%f[%w_]interface%s+[%w_$]+%s*{') and not choices.java
86
+ and (s:find('%f[%w_]export%s+interface%f[%W]') or s:find(':%s*[%w_$]+'))
87
+ or js and s:find(':%s*number%f[%W]')
88
+ if s:find('@Component%f[%W]') and s:find('%f[%w_]struct%s+') then choices.arkts = true
89
+ elseif ts then choices.typescript = true
90
+ elseif js then choices.javascript = true end
91
+ local cpp = s:find('%f[%w_]std%s*::') or s:find('%f[%w_]template%s*<')
92
+ or s:find('%f[%w_]public%s*:') or s:find('%f[%w_]extern%s+%s*{')
93
+ local c = s:find('#%s*include%f[%W]') or s:find('%f[%w_]typedef%s+struct%f[%W]')
94
+ if cpp then choices.cpp = true elseif c then choices.c = true end
95
+ local found
96
+ for lang in pairs(choices) do if found then return nil end; found = lang end
97
+ return found, found and 'syntax' or nil
98
+ end
99
+
100
+ return M
@@ -3,7 +3,7 @@ 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 }
6
+ MAX_QUERY = 4096, MAX_SYMBOLS = 200, POLL_MS = 1000, WARM_DELAY_MS = 150 }
7
7
  local null = xutils.json_null
8
8
  local function array(t) return setmetatable(t or {}, xutils.json_array_mt) end
9
9
  local function object(t) return type(t) == 'table' and getmetatable(t) ~= xutils.json_array_mt end
@@ -81,9 +81,9 @@ end
81
81
 
82
82
  -- initializationOptions.features may disable any of these, e.g. in assistant mode
83
83
  -- next to another language server; disabled features are not advertised.
84
- M.FEATURES = { 'documentSymbol', 'workspaceSymbol', 'definition', 'hover', 'references', 'callHierarchy', 'completion', 'signatureHelp' }
84
+ M.FEATURES = { 'documentSymbol', 'workspaceSymbol', 'definition', 'implementation', 'hover', 'references', 'callHierarchy', 'completion', 'signatureHelp' }
85
85
  local feature_of = { ['textDocument/documentSymbol'] = 'documentSymbol', ['workspace/symbol'] = 'workspaceSymbol',
86
- ['textDocument/definition'] = 'definition', ['textDocument/hover'] = 'hover',
86
+ ['textDocument/definition'] = 'definition', ['textDocument/implementation'] = 'implementation', ['textDocument/hover'] = 'hover',
87
87
  ['textDocument/references'] = 'references', ['textDocument/prepareCallHierarchy'] = 'callHierarchy',
88
88
  ['callHierarchy/incomingCalls'] = 'callHierarchy', ['callHierarchy/outgoingCalls'] = 'callHierarchy',
89
89
  ['textDocument/completion'] = 'completion', ['textDocument/signatureHelp'] = 'signatureHelp' }
@@ -96,6 +96,8 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
96
96
  document_bytes = 0, document_count = 0, session = config.session or ('lsp-' .. sessions),
97
97
  synced = {}, features = {} }
98
98
  local progress, server_pending, next_id, next_poll = {}, {}, 0, 0
99
+ local navigation_flights, navigation_epoch = {}, 0
100
+ local warm_pending, warm_active, warm_done, warm_pump, focused_uri
99
101
  local handlers = {}
100
102
  local function error_response(id, code, message)
101
103
  emit({ jsonrpc = '2.0', id = id == nil and null or id, error = { code = code, message = message } })
@@ -168,6 +170,34 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
168
170
  local path = documents.path(uri)
169
171
  return path and server_path(path)
170
172
  end
173
+ local function cancel_warm()
174
+ local job = warm_active
175
+ if not job then return end
176
+ warm_active = nil
177
+ cancel(job.id)
178
+ for uri, version in pairs(job.sent or {}) do
179
+ if self.synced[uri] == version then self.synced[uri] = nil end
180
+ end
181
+ end
182
+ local function navigation_enabled()
183
+ for name, value in pairs(self.features) do
184
+ if value and name ~= 'documentSymbol' and name ~= 'workspaceSymbol' then return true end
185
+ end
186
+ return false
187
+ end
188
+ local function schedule_warm(uri, focused, delay)
189
+ if not navigation_enabled() then return end
190
+ if focused then focused_uri = uri end
191
+ if focused_uri and self.documents[focused_uri] and focused_uri ~= uri then return end
192
+ local doc = self.documents[uri]
193
+ if not doc then return end
194
+ for _, job in pairs({ warm_done, warm_active, warm_pending }) do
195
+ if job.uri == uri and job.version == doc.version and job.epoch == navigation_epoch then return end
196
+ end
197
+ if warm_active then cancel_warm() end
198
+ warm_pending = { uri = uri, version = doc.version, epoch = navigation_epoch,
199
+ due = xtimer.now_ms() + (delay or M.WARM_DELAY_MS) }
200
+ end
171
201
  local function scan(root, hints)
172
202
  local existing = self.scans[root]
173
203
  if existing then
@@ -178,7 +208,7 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
178
208
  self.scans[root] = job
179
209
  if not self.ready[root] then self:begin_index(root) end
180
210
  local id, err = submit({ method = 'lsp_refresh', projectPath = root, allowedRoots = self.roots,
181
- paths = hints }, function(ok, result)
211
+ paths = hints, prepare_navigation = navigation_enabled() }, function(ok, result)
182
212
  if self.scans[root] ~= job then return end
183
213
  self.scans[root] = nil
184
214
  self.ready[root] = ok or nil
@@ -194,7 +224,9 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
194
224
  if not id then self.scans[root] = nil; self:end_index(root, err) end
195
225
  end
196
226
  function self:poll()
197
- if self.state ~= 'ready' or xtimer.now_ms() < next_poll then return end
227
+ if self.state ~= 'ready' then return end
228
+ if warm_pump then warm_pump() end
229
+ if warm_active or next(self.pending) or xtimer.now_ms() < next_poll then return end
198
230
  next_poll = xtimer.now_ms() + M.POLL_MS
199
231
  for _, root in ipairs(self.roots) do scan(root) end
200
232
  end
@@ -238,20 +270,30 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
238
270
  if doc then doc.uri = uri end
239
271
  return doc
240
272
  end
241
- local function cancel_pending(id)
273
+ local function cancel_pending(id, silent)
242
274
  local job = self.pending[id]
243
275
  if job then
244
276
  self.pending[id] = nil
245
- cancel(job.id)
246
- error_response(id, -32800, 'Request cancelled')
277
+ if job.flight then
278
+ local flight = job.flight
279
+ flight.waiters[id] = nil
280
+ if not next(flight.waiters) then
281
+ if navigation_flights[flight.key] == flight then navigation_flights[flight.key] = nil end
282
+ cancel(flight.id)
283
+ end
284
+ else cancel(job.id) end
285
+ if not silent then error_response(id, -32800, 'Request cancelled') end
247
286
  end
248
287
  end
249
288
  function self:close()
250
289
  if self.state == 'closed' then return end
251
290
  self.state = 'closed'
252
- for id in pairs(self.pending) do cancel_pending(id) end
291
+ warm_pending = nil; cancel_warm()
292
+ -- The transport may already be disconnected. Cancel worker jobs without
293
+ -- writing responses into a pipe the editor has stopped reading.
294
+ for id in pairs(self.pending) do cancel_pending(id, true) end
253
295
  for _, job in pairs(self.scans) do cancel(job.id) end
254
- if next(self.synced) then submit({ method = 'lsp_close', session = self.session }, function() end) end
296
+ if next(self.synced) or self.worker_session then submit({ method = 'lsp_close', session = self.session }, function() end) end
255
297
  self.scans, self.documents, self.ready = {}, {}, {}
256
298
  progress, server_pending = {}, {}
257
299
  end
@@ -288,7 +330,7 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
288
330
  reply(id, { capabilities = { positionEncoding = 'utf-16',
289
331
  textDocumentSync = { openClose = true, change = 1, save = { includeText = false } },
290
332
  documentSymbolProvider = f.documentSymbol or nil, workspaceSymbolProvider = f.workspaceSymbol or nil,
291
- definitionProvider = f.definition or nil, hoverProvider = f.hover or nil,
333
+ definitionProvider = f.definition or nil, implementationProvider = f.implementation or nil, hoverProvider = f.hover or nil,
292
334
  referencesProvider = f.references or nil, callHierarchyProvider = f.callHierarchy or nil,
293
335
  completionProvider = f.completion and { triggerCharacters = { '.', ':', '>' }, resolveProvider = false } or nil,
294
336
  signatureHelpProvider = f.signatureHelp and { triggerCharacters = { '(', ',' }, retriggerCharacters = { ')' } } or nil,
@@ -300,6 +342,7 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
300
342
  end
301
343
  handlers.shutdown = function(id)
302
344
  self.state = 'shutdown'
345
+ warm_pending = nil; cancel_warm()
303
346
  for request_id in pairs(self.pending) do cancel_pending(request_id) end
304
347
  for _, job in pairs(self.scans) do cancel(job.id) end
305
348
  self.scans = {}
@@ -313,6 +356,7 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
313
356
  assert(self.document_count < M.MAX_DOCUMENTS and self.document_bytes + #doc.source <= M.MAX_DRAFT_BYTES, 'open document limit reached')
314
357
  self.documents[td.uri] = doc
315
358
  self.document_count, self.document_bytes = self.document_count + 1, self.document_bytes + #doc.source
359
+ schedule_warm(td.uri)
316
360
  end
317
361
  handlers['textDocument/didChange'] = function(_, params)
318
362
  local td = assert(params.textDocument, 'textDocument required')
@@ -328,9 +372,13 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
328
372
  assert(self.document_bytes - #doc.source + #updated.source <= M.MAX_DRAFT_BYTES, 'open document limit reached')
329
373
  self.documents[td.uri] = updated
330
374
  self.document_bytes = self.document_bytes - #doc.source + #updated.source
375
+ schedule_warm(td.uri, false, 500)
331
376
  end
332
377
  handlers['textDocument/didClose'] = function(_, params)
333
378
  local uri = params.textDocument.uri
379
+ if warm_pending and warm_pending.uri == uri then warm_pending = nil end
380
+ if warm_active and warm_active.uri == uri then cancel_warm() end
381
+ if focused_uri == uri then focused_uri = nil end
334
382
  local doc = self.documents[uri]
335
383
  if doc then
336
384
  self.document_count, self.document_bytes = self.document_count - 1, self.document_bytes - #doc.source
@@ -359,6 +407,14 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
359
407
  assert(id ~= nil, 'symbol query must be a request')
360
408
  local doc = document(params)
361
409
  reply(id, doc and M.document_symbols(doc, self.hierarchical) or array())
410
+ schedule_warm(params.textDocument.uri)
411
+ end
412
+ handlers['codeoutline/visibleDocument'] = function(id, params)
413
+ assert(id == nil, 'visibleDocument must be a notification')
414
+ local td = params.textDocument
415
+ assert(object(td) and type(td.uri) == 'string', 'document URI required')
416
+ local doc = self.documents[td.uri]
417
+ if doc and (td.version == nil or td.version == doc.version) then schedule_warm(td.uri, true) end
362
418
  end
363
419
  -- Callback transports remain usable by embedders/tests. Production LSP
364
420
  -- supplies an RPC transport; both expose the same yielding interface.
@@ -375,16 +431,47 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
375
431
  return outcome, value
376
432
  end
377
433
  -- Each query owns a coroutine; cancelled/replaced requests discard replies.
378
- local function request(id, req, started)
434
+ local function request(id, req, started, key)
435
+ cancel_warm()
436
+ if warm_pending and req.uri == server_uri(warm_pending.uri) then warm_pending = nil end
379
437
  local count = 0
380
438
  for _ in pairs(self.pending) do count = count + 1 end
381
439
  assert(count < M.MAX_PENDING, 'too many pending requests')
382
440
  local job = { coroutine = coroutine.running() }
383
441
  self.pending[id] = job
384
- local called, ok, result = pcall(await_worker, req, function(job_id)
385
- job.id = job_id
386
- if started then started() end
387
- end)
442
+ local called, ok, result
443
+ local flight = key and navigation_flights[key]
444
+ if flight then
445
+ job.flight, flight.waiters[id] = flight, job
446
+ coroutine.yield()
447
+ called, ok, result = true, job.ok, job.result
448
+ else
449
+ if key then
450
+ flight = { key = key, waiters = { [id] = job } }
451
+ job.flight, navigation_flights[key] = flight, flight
452
+ end
453
+ called, ok, result = pcall(await_worker, req, function(job_id)
454
+ job.id = job_id
455
+ if flight then flight.id = job_id end
456
+ if started then started() end
457
+ end)
458
+ if flight then
459
+ if navigation_flights[key] == flight then navigation_flights[key] = nil end
460
+ local waiters = flight.waiters
461
+ flight.waiters = {}
462
+ for waiter_id, waiter in pairs(waiters) do
463
+ if waiter ~= job and self.pending[waiter_id] == waiter then
464
+ if called then waiter.ok, waiter.result = ok, result
465
+ else waiter.ok, waiter.result = false, ok end
466
+ local resumed, err = coroutine.resume(waiter.coroutine)
467
+ if not resumed then
468
+ self.pending[waiter_id] = nil
469
+ error_response(waiter_id, -32603, tostring(err))
470
+ end
471
+ end
472
+ end
473
+ end
474
+ end
388
475
  if not called then result, ok = ok, false end
389
476
  -- Even a cancelled request may fail before its drafts reach the worker.
390
477
  if not ok and req.changed then self.synced = {} end
@@ -428,14 +515,14 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
428
515
  end
429
516
  -- Navigation runs in the worker against the session's drafts: every request
430
517
  -- carries the open set and the text of versions the worker has not seen.
431
- local function navigate(id, req, finish)
432
- assert(id ~= nil, 'navigation must be a request')
518
+ local function drafts(req, only)
519
+ self.worker_session = true
433
520
  local open, changed, sent = {}, {}, {}
434
521
  for uri, doc in pairs(self.documents) do
435
522
  if doc.path and root_for(server_path(doc.path)) then
436
523
  local key = server_uri(uri)
437
524
  open[key] = doc.version
438
- if self.synced[uri] ~= doc.version then
525
+ if self.synced[uri] ~= doc.version and (not only or uri == only) then
439
526
  changed[key] = { source = doc.source, version = doc.version, language = doc.language }
440
527
  sent[uri] = doc.version
441
528
  end
@@ -443,16 +530,54 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
443
530
  end
444
531
  req.session, req.open, req.changed = self.session, open, changed
445
532
  req.includePaths = self.include_paths
533
+ return sent
534
+ end
535
+ warm_pump = function()
536
+ if not warm_pending or warm_active or next(self.pending) or next(self.scans)
537
+ or xtimer.now_ms() < warm_pending.due then return end
538
+ local uri = warm_pending.uri
539
+ local doc = self.documents[uri]
540
+ local root = doc and doc.path and root_for(server_path(doc.path))
541
+ if not root then warm_pending = nil; return end
542
+ if not self.ready[root] then return end
543
+ warm_pending = nil
544
+ if #doc.source > 1048576 then return end
545
+ local req = { method = 'lsp_warm', root = root, uri = server_uri(uri) }
546
+ local job = { uri = uri, version = doc.version, epoch = navigation_epoch, sent = drafts(req, uri) }
547
+ warm_active = job
548
+ local id = submit(req, function(ok)
549
+ if warm_active ~= job then return end
550
+ job.failed = not ok
551
+ if not ok then cancel_warm()
552
+ else
553
+ for changed, version in pairs(job.sent) do self.synced[changed] = version end
554
+ warm_done, warm_active, job.sent = job, nil, nil
555
+ end
556
+ end)
557
+ job.id = id
558
+ if id and not job.failed then
559
+ for changed, version in pairs(job.sent or {}) do self.synced[changed] = version end
560
+ elseif warm_active == job then warm_active = nil end
561
+ end
562
+ local function navigate(id, req, finish, key)
563
+ assert(id ~= nil, 'navigation must be a request')
564
+ cancel_warm()
565
+ local sent = drafts(req)
446
566
  local value, ok = request(id, req, function()
447
567
  -- Record submission before yielding so subsequent requests send only
448
568
  -- newer drafts. The worker applies drafts even to cancelled jobs.
449
569
  for uri, version in pairs(sent) do self.synced[uri] = version end
450
- end)
570
+ end, key)
451
571
  if not ok then return end
452
572
  if value.error then error_response(id, -32803, value.error); return end
453
573
  if value.incomplete then
574
+ local message = req.method == 'lsp_definition'
575
+ and 'CodeOutline: definition lookup reached a candidate, source or visibility limit; some definitions may be omitted'
576
+ or req.method == 'lsp_references'
577
+ and 'CodeOutline: results were truncated; too many matching references'
578
+ or 'CodeOutline: lookup reached a query limit; some results may be omitted'
454
579
  emit({ jsonrpc = '2.0', method = 'window/showMessage', params = { type = 2,
455
- message = 'CodeOutline: results were truncated; too many matching references' } })
580
+ message = message } })
456
581
  end
457
582
  reply(id, to_client(finish(value.result)))
458
583
  end
@@ -472,11 +597,31 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
472
597
  if not root then reply(id, empty); return end
473
598
  local req = { method = method, root = root, uri = server_uri(params.textDocument.uri), position = params.position }
474
599
  for key, value in pairs(extra or {}) do req[key] = value end
475
- navigate(id, req, finish)
600
+ local key
601
+ if method ~= 'lsp_signature' and method ~= 'lsp_completion' then
602
+ local doc = self.documents[params.textDocument.uri]
603
+ local point = tostring(params.position.line) .. ':' .. tostring(params.position.character)
604
+ if doc then
605
+ local offset = doc:offset(params.position)
606
+ -- Cursor motion over one identifier asks the same read-only
607
+ -- question. Only share work while it is in flight; document
608
+ -- notifications invalidate the key, and results are not cached.
609
+ local first, last = offset, offset
610
+ while first > 0 and offset - first < 1024 and doc.source:sub(first, first):match('[%w_]') do first = first - 1 end
611
+ while last < #doc.source and last - offset < 1024 and doc.source:sub(last + 1, last + 1):match('[%w_]') do last = last + 1 end
612
+ if last > first then point = tostring(first) .. ':' .. tostring(last) end
613
+ end
614
+ key = table.concat({ method, root, req.uri, tostring(navigation_epoch), point,
615
+ tostring(req.includeDeclaration == true) }, '\0')
616
+ end
617
+ navigate(id, req, finish, key)
476
618
  end
477
619
  handlers['textDocument/definition'] = function(id, params)
478
620
  at(id, params, 'lsp_definition', array(), list)
479
621
  end
622
+ handlers['textDocument/implementation'] = function(id, params)
623
+ at(id, params, 'lsp_implementation', array(), list)
624
+ end
480
625
  handlers['textDocument/hover'] = function(id, params)
481
626
  at(id, params, 'lsp_hover', nil, function(value) return value or nil end)
482
627
  end
@@ -520,7 +665,7 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
520
665
  end
521
666
  handlers['callHierarchy/incomingCalls'] = hierarchy('lsp_incoming')
522
667
  handlers['callHierarchy/outgoingCalls'] = hierarchy('lsp_outgoing')
523
- local navigation = { ['textDocument/definition'] = true, ['textDocument/hover'] = true, ['textDocument/signatureHelp'] = true,
668
+ local navigation = { ['textDocument/definition'] = true, ['textDocument/implementation'] = true, ['textDocument/hover'] = true, ['textDocument/signatureHelp'] = true,
524
669
  ['textDocument/references'] = true, ['textDocument/prepareCallHierarchy'] = true,
525
670
  ['callHierarchy/incomingCalls'] = true, ['callHierarchy/outgoingCalls'] = true }
526
671
  local function dispatch(msg)
@@ -531,6 +676,11 @@ function M.new(config, emit, submit, cancel, stop, await_worker)
531
676
  end
532
677
  if self.state == 'closed' then return end
533
678
  if method == '$/cancelRequest' then cancel_pending(params.id); return end
679
+ if method == 'textDocument/didOpen' or method == 'textDocument/didChange'
680
+ or method == 'textDocument/didClose' or method == 'textDocument/didSave'
681
+ or method == 'workspace/didChangeWatchedFiles' or method == 'workspace/didChangeWorkspaceFolders' then
682
+ navigation_epoch = navigation_epoch + 1
683
+ end
534
684
  if self.pending[id] then error_response(id, -32600, 'Request ID is already pending'); return end
535
685
  if method == 'initialize' then handlers.initialize(id, params); return end
536
686
  if self.state == 'new' then if id ~= nil then error_response(id, -32002, 'Server not initialized') end; return end