@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
@@ -0,0 +1,936 @@
1
+ -- completion.lua — LSP completion inside the INDEX worker.
2
+ --
3
+ -- Without a member operator the items are the current file's locals,
4
+ -- parameters, definitions and imports, C/C++ included declarations, plus members of the
5
+ -- enclosing class. After `.`, `->`, `::`, `?.` or Lua `:` the receiver chain
6
+ -- is typed from `self`/`this`, declared types of locals, parameters, fields
7
+ -- and return types (read from signatures), initializers (`new T`, `T(`,
8
+ -- `T{`, `T::new(`), imports and static names, then its members are listed:
9
+ -- children, out-of-line definitions, assigned attributes and base classes.
10
+ -- Items are unsorted and unfiltered by case; the editor filters them. Type
11
+ -- files are reparsed on demand within a time budget; running out of it, or
12
+ -- of items, marks the list incomplete.
13
+ local graph = require('codeoutline.graph')
14
+ local parse = require('codeoutline.parse')
15
+ local navigation = require('codeoutline.navigation')
16
+ local control = require('codeoutline.control')
17
+ local M = { MAX_ITEMS = 500, BUDGET_SECONDS = 0.3, MAX_FILE_BYTES = 524288, MAX_DEPTH = 6, CACHE_FILES = 64 }
18
+
19
+ local ITEM = { ['function'] = 3, method = 2, constructor = 4, destructor = 4, field = 5, variable = 6,
20
+ property = 10, class = 7, struct = 22, union = 22, record = 22, interface = 8, enum = 13, enum_member = 20,
21
+ constant = 21, namespace = 9, impl = 9, macro = 3, prototype = 3, typedef = 7, annotation_type = 8 }
22
+ local TYPES = { class = true, struct = true, union = true, interface = true, enum = true, record = true,
23
+ typedef = true, namespace = true, impl = true, annotation_type = true }
24
+ local LUA_TYPES = setmetatable({ variable = true }, { __index = TYPES })
25
+ local CALLABLE = { ['function'] = true, method = true, constructor = true, prototype = true, macro = true, destructor = true }
26
+ local MEMBER = { ['.'] = true, ['->'] = true, ['::'] = true, ['?.'] = true }
27
+ local LUA_MEMBER = { ['.'] = true, [':'] = true }
28
+ local SELF = { self = true, this = true, cls = true }
29
+ -- Methods conventionally returning their receiver's type when no type is declared.
30
+ local FACTORY = { new = true, create = true, make = true, of = true, instance = true, getInstance = true,
31
+ get_instance = true, New = true, Create = true, Instance = true }
32
+ local MODIFIERS = {}
33
+ for word in ([[static const constexpr volatile virtual inline extern explicit friend public private protected
34
+ internal final abstract override sealed readonly async unsafe new partial synchronized native transient
35
+ struct class enum union typename mut dyn impl ref out in params let var auto def function func pub]]):gmatch('%S+') do
36
+ MODIFIERS[word] = true
37
+ end
38
+ local WRAPPERS = {}
39
+ for word in ([[Option Box Rc Arc RefCell Cell Mutex RwLock Weak unique_ptr shared_ptr weak_ptr Ptr Ref
40
+ Optional Nullable Promise Future Lazy]]):gmatch('%S+') do WRAPPERS[word] = true end
41
+ local TYPE_AFTER = { go = true, rust = true, typescript = true, javascript = true, python = true, kotlin = true }
42
+ local DYNAMIC = { lua = true, python = true, javascript = true, typescript = true }
43
+
44
+ -- Full parses of type files, keyed by the index record they correspond to.
45
+ local cache, cache_order = setmetatable({}, { __mode = 'k' }), {}
46
+
47
+ local function text_of(T, i) return T.s[i] and T:text(i) or '' end
48
+ local function is(T, i, s) return T.k[i] == 'op' and text_of(T, i) == s end
49
+ local function kw(T, i, s) return T.k[i] == 'kw' and text_of(T, i) == s end
50
+ -- Token texts of [a, b] joined by spaces (enough for declared types).
51
+ local function span(T, a, b)
52
+ if a > b then return nil end
53
+ local parts = {}
54
+ for i = a, b do parts[#parts + 1] = text_of(T, i) end
55
+ return table.concat(parts, ' ')
56
+ end
57
+
58
+ -- Type name for a declared type text: modifiers, pointers, references,
59
+ -- arrays, namespaces and well-known wrappers removed.
60
+ local function normalize(text)
61
+ if type(text) ~= 'string' then return nil end
62
+ text = text:gsub('^%s+', ''):gsub('%s+$', '')
63
+ for _ = 1, 8 do
64
+ local first, rest = text:match('^([%a_]+)%s+(.+)$')
65
+ if first and MODIFIERS[first] then text = rest else break end
66
+ end
67
+ text = text:gsub('^[%*&%[%]]+', ''):gsub('[%*&%?!%s]+$', ''):gsub('%[%]$', '')
68
+ text = text:gsub('^%[%d*%]', ''):gsub('^map%b[]', '')
69
+ local outer, inner = text:match('^([%w_:%.]+)%s*<(.*)>$')
70
+ if outer then
71
+ local last = outer:match('([%w_]+)$')
72
+ if WRAPPERS[last] then
73
+ local depth, arg = 0, ''
74
+ for c in inner:gmatch('.') do
75
+ if c == '<' then depth = depth + 1 elseif c == '>' then depth = depth - 1 end
76
+ if c == ',' and depth == 0 then break end
77
+ arg = arg .. c
78
+ end
79
+ return normalize(arg)
80
+ end
81
+ return last
82
+ end
83
+ return text:match('([%a_][%w_]*)$')
84
+ end
85
+
86
+ local function strip_name(prefix, name)
87
+ -- `Box::name` in an out-of-line definition, or a trailing `*`/`&`.
88
+ prefix = prefix:gsub('[%w_]+%s*::%s*$', '')
89
+ return prefix
90
+ end
91
+
92
+ -- Declared type of a node from its signature (or the full-parse `type`).
93
+ local function declared(node, language)
94
+ if node.type then return normalize(node.type) end
95
+ local sig, name = node.sig, node.name
96
+ if not sig then return nil end
97
+ if node.kind == 'constructor' then return nil end
98
+ local callable = CALLABLE[node.kind]
99
+ if callable then
100
+ if language == 'rust' or language == 'python' then
101
+ local after = sig:match('%->%s*([^:{]+)')
102
+ return after and normalize(after)
103
+ elseif language == 'go' then
104
+ local at = sig:find(name .. '%s*%(')
105
+ if not at then return nil end
106
+ local rest = sig:sub(at + #name)
107
+ local params = rest:match('^%s*%b()')
108
+ if not params then return nil end
109
+ rest = rest:sub(#params + 1):gsub('^%s+', '')
110
+ if rest:sub(1, 1) == '(' then rest = rest:match('^%((.-)[,%)]') or '' end
111
+ return normalize(rest)
112
+ elseif language == 'typescript' or language == 'javascript' or language == 'kotlin' then
113
+ local after = sig:match('%)%s*:%s*([^{=]+)$')
114
+ return after and normalize(after)
115
+ elseif language == 'lua' then
116
+ return nil
117
+ end
118
+ local at = sig:find('[%w_]*' .. name .. '%s*%(')
119
+ if not at then return nil end
120
+ return normalize(strip_name(sig:sub(1, at - 1), name))
121
+ end
122
+ if language == 'go' then
123
+ return normalize(sig:match('^%s*' .. name .. '%s+(.+)$'))
124
+ elseif TYPE_AFTER[language] then
125
+ local after = sig:match(name .. '%s*%??%s*:%s*([^=]+)')
126
+ return after and normalize(after)
127
+ elseif language == 'lua' then
128
+ return nil
129
+ end
130
+ local at
131
+ for position in sig:gmatch('()' .. name .. '%f[^%w_]') do at = position end
132
+ if not at or at == 1 then return nil end
133
+ return normalize(sig:sub(1, at - 1))
134
+ end
135
+
136
+ -- Full record and tokens of file f: the overlay document, else a cached
137
+ -- reparse of the indexed file (nil when too large or unreadable).
138
+ local function full(V, f)
139
+ local draft = V.drafts[f]
140
+ if draft then
141
+ draft.completion = draft.completion or {}
142
+ local entry = draft.completion
143
+ if not entry.rec then entry.rec, entry.tokens = parse.parse_tokens(draft.path or V.files[f].path, draft.source) end
144
+ return entry.rec, entry.tokens, draft
145
+ end
146
+ local key = V.files[f]
147
+ local entry = cache[key]
148
+ if entry == nil then
149
+ if (key.size or 0) > M.MAX_FILE_BYTES then cache[key] = false; return nil end
150
+ local doc = navigation.source_doc(V, f)
151
+ entry = false
152
+ if doc then
153
+ local rec, tokens = parse.parse_tokens(key.path, doc.source)
154
+ entry = rec and { rec = rec, tokens = tokens } or false
155
+ end
156
+ cache[key] = entry
157
+ cache_order[#cache_order + 1] = key
158
+ if #cache_order > M.CACHE_FILES then cache[table.remove(cache_order, 1)] = nil end
159
+ end
160
+ if not entry then return nil end
161
+ return entry.rec, entry.tokens
162
+ end
163
+
164
+ -- Parameters inside parentheses [a, b] of a function header.
165
+ local function parameters(T, a, b, language, out)
166
+ local segments, start, depth = {}, a, 0
167
+ for i = a, b + 1 do
168
+ local closing = i == b + 1
169
+ if not closing then
170
+ local s = text_of(T, i)
171
+ if T.k[i] == 'op' and (s == '(' or s == '[' or s == '{' or s == '<') then depth = depth + 1
172
+ elseif T.k[i] == 'op' and (s == ')' or s == ']' or s == '}' or s == '>') then depth = depth - 1 end
173
+ end
174
+ if closing or (depth == 0 and is(T, i, ',')) then
175
+ if start <= i - 1 then segments[#segments + 1] = { start, i - 1 } end
176
+ start = i + 1
177
+ end
178
+ end
179
+ local pending = {}
180
+ for _, seg in ipairs(segments) do
181
+ local x, y = seg[1], seg[2]
182
+ local eq = x
183
+ while eq <= y and not is(T, eq, '=') do eq = eq + 1 end
184
+ y = eq - 1
185
+ if language == 'go' then
186
+ local names, k = {}, x
187
+ if T.k[k] == 'id' then names[1] = text_of(T, k); k = k + 1 end
188
+ local type = span(T, k, y)
189
+ if type then
190
+ for _, name in ipairs(pending) do out[#out + 1] = { name = name, type = normalize(type) } end
191
+ pending = {}
192
+ for _, name in ipairs(names) do out[#out + 1] = { name = name, type = normalize(type) } end
193
+ else
194
+ for _, name in ipairs(names) do pending[#pending + 1] = name end
195
+ end
196
+ elseif TYPE_AFTER[language] or language == 'lua' then
197
+ local k = x
198
+ while k <= y and T.k[k] ~= 'id' and not kw(T, k, 'self') and not kw(T, k, 'this') do k = k + 1 end
199
+ if k <= y then
200
+ local colon = k + 1
201
+ while colon <= y and not is(T, colon, ':') do colon = colon + 1 end
202
+ out[#out + 1] = { name = text_of(T, k), type = colon < y and normalize(span(T, colon + 1, y)) or nil }
203
+ end
204
+ else
205
+ local name
206
+ for k = y, x, -1 do if T.k[k] == 'id' then name = k; break end end
207
+ if name and name > x then out[#out + 1] = { name = text_of(T, name), type = normalize(span(T, x, name - 1)) } end
208
+ end
209
+ end
210
+ for _, name in ipairs(pending) do out[#out + 1] = { name = name } end
211
+ end
212
+
213
+ -- Initializer starting at token j: a type name, a module path, or a call chain.
214
+ local function initializer(T, j, stop)
215
+ if not j or j > stop then return {} end
216
+ if kw(T, j, 'new') or (T.k[j] == 'id' and text_of(T, j) == 'new') then
217
+ local k, last = j + 1, nil
218
+ while k <= stop and (T.k[k] == 'id' or is(T, k, '::') or is(T, k, '.')) do
219
+ if T.k[k] == 'id' then last = text_of(T, k) end
220
+ k = k + 1
221
+ end
222
+ return { type = last }
223
+ end
224
+ if is(T, j, '&') then j = j + 1 end
225
+ if T.k[j] == 'id' and text_of(T, j) == 'require' then
226
+ local k = is(T, j + 1, '(') and j + 2 or j + 1
227
+ if T.k[k] == 'str' then return { module = text_of(T, k):gsub('^[%a]*["\'%[=]+', ''):gsub('["\'%]=]+$', '') } end
228
+ end
229
+ local chain, k = {}, j
230
+ while k <= stop and (T.k[k] == 'id' or (T.k[k] == 'kw' and SELF[text_of(T, k)])) do
231
+ local item = { name = text_of(T, k) }
232
+ chain[#chain + 1] = item
233
+ k = k + 1
234
+ if is(T, k, '<') and T.m[k] then k = T.m[k] + 1 end
235
+ if is(T, k, '(') and T.m[k] then item.call = true; k = T.m[k] + 1
236
+ elseif is(T, k, '{') and #chain == 1 then return { type = item.name } end
237
+ if k <= stop and T.k[k] == 'op' and (MEMBER[text_of(T, k)] or text_of(T, k) == ':') then k = k + 1 else break end
238
+ end
239
+ if #chain == 1 and chain[1].call and chain[1].name:match('^%u') then return { type = chain[1].name } end
240
+ if #chain > 0 then return { chain = chain } end
241
+ return {}
242
+ end
243
+
244
+ -- Local declarations in tokens [a, b], skipping excluded ranges.
245
+ local function declarations(T, a, b, language, skip, out)
246
+ local function skipped(i)
247
+ for _, r in ipairs(skip) do if i >= r[1] and i <= r[2] then return r[2] end end
248
+ end
249
+ local function statement_end(j)
250
+ local depth = 0
251
+ for k = j, b do
252
+ local s = text_of(T, k)
253
+ if T.k[k] == 'op' and (s == '(' or s == '[' or s == '{') then depth = depth + 1
254
+ elseif T.k[k] == 'op' and (s == ')' or s == ']' or s == '}') then
255
+ if depth == 0 then return k - 1 end
256
+ depth = depth - 1
257
+ elseif depth == 0 and (s == ';' or T.k[k] == 'nl') then return k - 1 end
258
+ end
259
+ return b
260
+ end
261
+ local function add(name_tok, type, init)
262
+ local entry = { name = text_of(T, name_tok), type = normalize(type), at = name_tok }
263
+ if init then
264
+ local stop = statement_end(init)
265
+ local value = initializer(T, init, stop)
266
+ entry.type = entry.type or normalize(value.type)
267
+ entry.module, entry.chain = value.module, value.chain
268
+ end
269
+ out[#out + 1] = entry
270
+ end
271
+ local i = a
272
+ while i <= b do
273
+ local jump = skipped(i)
274
+ if jump then i = jump + 1
275
+ else
276
+ local s = text_of(T, i)
277
+ if language == 'lua' then
278
+ if kw(T, i, 'local') then
279
+ if kw(T, i + 1, 'function') and T.k[i + 2] == 'id' then add(i + 2)
280
+ else
281
+ local names, k = {}, i + 1
282
+ while T.k[k] == 'id' do
283
+ names[#names + 1] = k
284
+ if is(T, k + 1, ',') then k = k + 2 else k = k + 1; break end
285
+ end
286
+ local init = is(T, k, '=') and k + 1 or nil
287
+ for n, tok in ipairs(names) do add(tok, nil, n == 1 and init or nil) end
288
+ end
289
+ elseif kw(T, i, 'for') then
290
+ local k = i + 1
291
+ while T.k[k] == 'id' do add(k); if is(T, k + 1, ',') then k = k + 2 else break end end
292
+ end
293
+ elseif language == 'python' then
294
+ local starts = i == a or T.k[i - 1] == 'nl' or T.k[i - 1] == 'indent' or T.k[i - 1] == 'dedent'
295
+ if starts and T.k[i] == 'id' then
296
+ local k, names = i, {}
297
+ while T.k[k] == 'id' do
298
+ names[#names + 1] = k
299
+ if is(T, k + 1, ',') then k = k + 2 else k = k + 1; break end
300
+ end
301
+ local type
302
+ if is(T, k, ':') then
303
+ local e = k + 1
304
+ while e <= b and not is(T, e, '=') and T.k[e] ~= 'nl' do e = e + 1 end
305
+ type = span(T, k + 1, e - 1); k = e
306
+ end
307
+ if is(T, k, '=') then
308
+ for n, tok in ipairs(names) do add(tok, type, n == 1 and k + 1 or nil) end
309
+ end
310
+ elseif kw(T, i, 'for') then
311
+ local k = i + 1
312
+ while T.k[k] == 'id' do add(k); if is(T, k + 1, ',') then k = k + 2 else break end end
313
+ elseif kw(T, i, 'as') and T.k[i + 1] == 'id' then add(i + 1) end
314
+ elseif language == 'javascript' or language == 'typescript' or language == 'kotlin' then
315
+ if (kw(T, i, 'let') or kw(T, i, 'const') or kw(T, i, 'var') or kw(T, i, 'val')) and T.k[i + 1] == 'id' then
316
+ local k, type = i + 2, nil
317
+ if is(T, k, ':') then
318
+ local e = k + 1
319
+ while e <= b and not is(T, e, '=') and not is(T, e, ';') and not is(T, e, ',')
320
+ and not (language == 'kotlin' and e > k + 1 and T:line(e) > T:eline(e - 1)) do e = e + 1 end
321
+ type = span(T, k + 1, e - 1); k = e
322
+ end
323
+ add(i + 1, type, is(T, k, '=') and k + 1 or nil)
324
+ end
325
+ elseif language == 'go' then
326
+ if T.k[i] == 'id' and (i == a or not (T.k[i - 1] == 'op' and MEMBER[text_of(T, i - 1)])) then
327
+ local k, names = i, {}
328
+ while T.k[k] == 'id' do
329
+ names[#names + 1] = k
330
+ if is(T, k + 1, ',') then k = k + 2 else k = k + 1; break end
331
+ end
332
+ if is(T, k, ':=') then
333
+ for n, tok in ipairs(names) do add(tok, nil, n == 1 and k + 1 or nil) end
334
+ i = k
335
+ end
336
+ elseif kw(T, i, 'var') and T.k[i + 1] == 'id' then
337
+ local e = i + 2
338
+ while e <= b and not is(T, e, '=') and T.k[e] ~= 'nl' and not is(T, e, ';') do e = e + 1 end
339
+ add(i + 1, span(T, i + 2, e - 1), is(T, e, '=') and e + 1 or nil)
340
+ end
341
+ elseif language == 'rust' then
342
+ if kw(T, i, 'let') then
343
+ local k = kw(T, i + 1, 'mut') and i + 2 or i + 1
344
+ if T.k[k] == 'id' then
345
+ local type, e = nil, k + 1
346
+ if is(T, e, ':') then
347
+ local t = e + 1
348
+ while t <= b and not is(T, t, '=') and not is(T, t, ';') do t = t + 1 end
349
+ type = span(T, e + 1, t - 1); e = t
350
+ end
351
+ add(k, type, is(T, e, '=') and e + 1 or nil)
352
+ end
353
+ elseif kw(T, i, 'for') and T.k[i + 1] == 'id' then add(i + 1) end
354
+ else
355
+ -- C family, Java, C#: `Type name =|;|,|:` after a statement boundary.
356
+ local nx = text_of(T, i + 1)
357
+ if T.k[i] == 'id' and T.k[i + 1] == 'op' and (nx == '=' or nx == ';' or nx == ',' or nx == ':' or nx == '[') then
358
+ local k = i - 1
359
+ while k >= a and (T.k[k] == 'id' or (T.k[k] == 'kw' and not ({ ['return'] = 1, ['case'] = 1,
360
+ ['goto'] = 1, ['else'] = 1, ['throw'] = 1, ['delete'] = 1, ['typedef'] = 1 })[text_of(T, k)])
361
+ or is(T, k, '*') or is(T, k, '&') or is(T, k, '::') or is(T, k, '<') or is(T, k, '>')
362
+ or is(T, k, '[') or is(T, k, ']')) do
363
+ k = k - 1
364
+ end
365
+ local boundary = k < a or is(T, k, ';') or is(T, k, '{') or is(T, k, '}') or is(T, k, '(')
366
+ or T.k[k] == 'dir'
367
+ local type = boundary and span(T, k + 1, i - 1)
368
+ if type and type:find('[%w_]') then
369
+ local init = nx == '=' and i + 2 or nil
370
+ if type == 'auto' or type == 'var' or type == 'const auto' then type = nil end
371
+ add(i, type, init)
372
+ end
373
+ end
374
+ end
375
+ i = i + 1
376
+ end
377
+ end
378
+ end
379
+
380
+ -- Imports usable as module receivers, from the current document's tokens.
381
+ local function module_aliases(T, language)
382
+ local out = {}
383
+ for i = 1, T.n do
384
+ if language == 'lua' and kw(T, i, 'local') and T.k[i + 1] == 'id' and is(T, i + 2, '=') then
385
+ local value = initializer(T, i + 3, math.min(T.n, i + 6))
386
+ if value.module then out[text_of(T, i + 1)] = { kind = 'require', path = value.module } end
387
+ elseif language == 'python' and kw(T, i, 'import') then
388
+ local from, k = nil, i - 1
389
+ while k >= 1 and T.k[k] ~= 'nl' and not kw(T, k, 'from') do k = k - 1 end
390
+ if kw(T, k, 'from') then from = (span(T, k + 1, i - 1) or ''):gsub('%s+', '') end
391
+ local j = i + 1
392
+ while j <= T.n and T.k[j] ~= 'nl' do
393
+ if T.k[j] == 'id' then
394
+ local first = j
395
+ while is(T, j + 1, '.') and T.k[j + 2] == 'id' do j = j + 2 end
396
+ local path = span(T, first, j):gsub('%s+', '')
397
+ local alias = path
398
+ if kw(T, j + 1, 'as') and T.k[j + 2] == 'id' then alias = text_of(T, j + 2); j = j + 2 end
399
+ if from then out[alias] = { kind = 'import', path = from, names = { path }, member = path }
400
+ elseif alias ~= path or not path:find('.', 1, true) then out[alias] = { kind = 'import', path = path } end
401
+ end
402
+ j = j + 1
403
+ end
404
+ elseif (language == 'javascript' or language == 'typescript') then
405
+ if kw(T, i, 'import') and is(T, i + 1, '*') and T.k[i + 3] == 'id' and T.k[i + 5] == 'str' then
406
+ out[text_of(T, i + 3)] = { kind = 'import', path = text_of(T, i + 5):sub(2, -2) }
407
+ elseif kw(T, i, 'import') and T.k[i + 1] == 'id' and T.k[i + 3] == 'str' then
408
+ out[text_of(T, i + 1)] = { kind = 'import', path = text_of(T, i + 3):sub(2, -2) }
409
+ elseif (kw(T, i, 'const') or kw(T, i, 'let') or kw(T, i, 'var')) and T.k[i + 1] == 'id' and is(T, i + 2, '=') then
410
+ local value = initializer(T, i + 3, math.min(T.n, i + 6))
411
+ if value.module then out[text_of(T, i + 1)] = { kind = 'import', path = value.module } end
412
+ end
413
+ elseif language == 'go' and kw(T, i, 'import') then
414
+ local first, last = i + 1, i + 2
415
+ if is(T, i + 1, '(') and T.m[i + 1] then first, last = i + 2, T.m[i + 1] - 1 end
416
+ for j = first, last do
417
+ if T.k[j] == 'str' then
418
+ local path = text_of(T, j):sub(2, -2)
419
+ local alias = (j > first and T.k[j - 1] == 'id') and text_of(T, j - 1) or path:match('([^/]+)$')
420
+ out[alias] = { kind = 'import', path = path }
421
+ end
422
+ end
423
+ end
424
+ end
425
+ return out
426
+ end
427
+
428
+ -- Files an import resolves to, through the graph's dependency rules.
429
+ local import_files
430
+ function import_files(V, f, alias)
431
+ if alias.member and alias.path:sub(1, 1) ~= '.' then
432
+ -- `from pkg import mod`: try the submodule first.
433
+ local files = import_files(V, f, { kind = alias.kind, path = alias.path .. '.' .. alias.member })
434
+ if #files > 0 then return files end
435
+ end
436
+ local files = navigation.import_files(V, f, { path = alias.path, kind = alias.kind, names = alias.names })
437
+ if alias.member then
438
+ -- `from pkg import name`: a module file named like the import, else a symbol.
439
+ local modules = {}
440
+ for _, g in ipairs(files) do
441
+ local stem = V.files[g].path:match('([^/]+)%.[^./]+$')
442
+ if stem == alias.member or V.files[g].path:match('([^/]+)/__init__%.py$') == alias.member then modules[#modules + 1] = g end
443
+ end
444
+ return modules
445
+ end
446
+ return files
447
+ end
448
+
449
+ local Completion = {}
450
+ Completion.__index = Completion
451
+
452
+ -- Type nodes named name, as seen from file f.
453
+ function Completion:types(name, f)
454
+ if not name then return {} end
455
+ f = f or self.f
456
+ local accept = self.language == 'lua' and LUA_TYPES or TYPES
457
+ local direct, paired = navigation.deps(self.V, f)
458
+ local found = graph.resolve(self.V, f, nil, name, 'call', nil, direct, paired, accept) or {}
459
+ if #found == 0 then
460
+ local family = graph.family(self.V.files[f].language)
461
+ for _, id in ipairs(self.V.by_name[name] or {}) do
462
+ local node = self.V.nodes[id]
463
+ if accept[node.kind] and graph.family(self.V.files[self.V.node_file[id]].language) == family then
464
+ found[#found + 1] = id
465
+ if #found >= 3 then break end
466
+ end
467
+ end
468
+ end
469
+ -- Partial declarations: impl blocks, forward declarations, C# partial classes.
470
+ local set, list = {}, {}
471
+ for _, id in ipairs(found) do
472
+ local q = self.V.nodes[id].qualified
473
+ local family = graph.family(self.V.files[self.V.node_file[id]].language)
474
+ for _, cid in ipairs(self.V.by_name[self.V.nodes[id].name] or {}) do
475
+ local c = self.V.nodes[cid]
476
+ if not set[cid] and accept[c.kind] and c.qualified == q
477
+ and graph.family(self.V.files[self.V.node_file[cid]].language) == family then
478
+ set[cid] = true; list[#list + 1] = cid
479
+ end
480
+ end
481
+ end
482
+ return list
483
+ end
484
+
485
+ function Completion:expired()
486
+ if os.clock() > self.deadline then self.incomplete = true; return true end
487
+ return false
488
+ end
489
+
490
+ -- Member entries { name, kind, node, file } of the type ids, with bases.
491
+ function Completion:members(ids, depth, seen)
492
+ depth, seen = depth or 0, seen or {}
493
+ local V, out = self.V, {}
494
+ local function add(name, kind, node, f)
495
+ out[#out + 1] = { name = name, kind = kind, node = node, file = f }
496
+ end
497
+ for _, id in ipairs(ids) do
498
+ if seen[id] or self:expired() then break end
499
+ seen[id] = true
500
+ control.check()
501
+ local f = V.node_file[id]
502
+ local n = id - V.id_of[f][1] + 1
503
+ local node = V.nodes[id]
504
+ local rec, T = full(V, f)
505
+ local nodes = rec and rec.nodes or V.files[f].nodes
506
+ local target = nodes[n]
507
+ if not (target and target.qualified == node.qualified) then nodes, target = V.files[f].nodes, node end
508
+ local language = V.files[f].language
509
+ for _, c in ipairs(nodes) do
510
+ if c.parent == n then add(c.name, c.kind, c, f) end
511
+ end
512
+ -- Out-of-line members anywhere: `Box::area`, Go methods, Lua `M.f` / `M:f`.
513
+ for _, entry in ipairs(self:owned(node.qualified, graph.family(language))) do out[#out + 1] = entry end
514
+ -- Attributes assigned in methods (Python, JS) or on Lua tables.
515
+ if T and (language == 'python' or language == 'javascript' or language == 'typescript' or language == 'lua') then
516
+ local first, last = 1, T.n
517
+ if target.start_byte and language ~= 'lua' then
518
+ first, last = nil, nil
519
+ for i = 1, T.n do
520
+ if T.s[i] - 1 >= target.start_byte and not first then first = i end
521
+ if T.e[i] <= target.end_byte then last = i end
522
+ end
523
+ end
524
+ local receivers = language == 'lua' and { [node.name] = true, self = true } or { self = true, this = true }
525
+ for i = first or 1, (last or 0) - 3 do
526
+ if T.k[i] == 'id' or T.k[i] == 'kw' then
527
+ if receivers[text_of(T, i)] and T.k[i + 1] == 'op' and (text_of(T, i + 1) == '.')
528
+ and T.k[i + 2] == 'id' and is(T, i + 3, '=') then
529
+ add(text_of(T, i + 2), 'field', { name = text_of(T, i + 2), kind = 'field' }, f)
530
+ end
531
+ end
532
+ end
533
+ end
534
+ -- Base classes.
535
+ if depth < 3 and node.bases then
536
+ for _, base in ipairs(node.bases) do
537
+ local base_ids = self:types(normalize(base), f)
538
+ for _, entry in ipairs(self:members(base_ids, depth + 1, seen)) do out[#out + 1] = entry end
539
+ end
540
+ end
541
+ end
542
+ return out
543
+ end
544
+
545
+ -- Members recorded under qualifier q (by_owner), with open documents
546
+ -- replacing their files' resident nodes.
547
+ function Completion:owned(q, family)
548
+ local V, out = self.V, {}
549
+ for _, id in ipairs(V.by_owner[q] or {}) do
550
+ local g = V.node_file[id]
551
+ if not V.drafts[g] and graph.family(V.files[g].language) == family then
552
+ local node = V.nodes[id]
553
+ out[#out + 1] = { name = node.name, kind = node.kind, node = node, file = g }
554
+ end
555
+ end
556
+ for g, _ in pairs(V.drafts) do
557
+ if graph.family(V.files[g].language) == family then
558
+ for _, node in ipairs(V.files[g].nodes) do
559
+ if graph.owner(node.qualified) == q then
560
+ out[#out + 1] = { name = node.name, kind = node.kind, node = node, file = g }
561
+ end
562
+ end
563
+ end
564
+ end
565
+ return out
566
+ end
567
+
568
+ -- Fields assigned to name in the current file (`name.x = ...`), and the keys
569
+ -- of a table/object literal it is initialized with.
570
+ function Completion:assigned(name)
571
+ local T, out = self.T, {}
572
+ for i = 1, T.n - 3 do
573
+ if T.k[i] == 'id' and text_of(T, i) == name and is(T, i + 1, '.') and T.k[i + 2] == 'id' and is(T, i + 3, '=')
574
+ and not (i > 1 and T.k[i - 1] == 'op' and MEMBER[text_of(T, i - 1)]) then
575
+ out[#out + 1] = { name = text_of(T, i + 2), kind = 'field' }
576
+ elseif T.k[i] == 'id' and text_of(T, i) == name and is(T, i + 1, '=') and is(T, i + 2, '{') and T.m[i + 2] then
577
+ local depth = 0
578
+ for k = i + 3, T.m[i + 2] - 1 do
579
+ if T.k[k] == 'op' and (text_of(T, k) == '{' or text_of(T, k) == '(' or text_of(T, k) == '[') then
580
+ depth = depth + 1
581
+ elseif T.k[k] == 'op' and (text_of(T, k) == '}' or text_of(T, k) == ')' or text_of(T, k) == ']') then
582
+ depth = depth - 1
583
+ elseif depth == 0 and T.k[k] == 'id' and (is(T, k + 1, '=') or is(T, k + 1, ':'))
584
+ and (is(T, k - 1, '{') or is(T, k - 1, ',') or is(T, k - 1, ';')) then
585
+ out[#out + 1] = { name = text_of(T, k), kind = 'field' }
586
+ end
587
+ end
588
+ end
589
+ end
590
+ return out
591
+ end
592
+
593
+ -- Top-level members of module files.
594
+ function Completion:module_members(files)
595
+ local out = {}
596
+ for _, g in ipairs(files) do
597
+ if self:expired() then break end
598
+ local rec = self.V.files[g]
599
+ local language = rec.language
600
+ local exported
601
+ if language == 'lua' then
602
+ -- The table a Lua module returns names its members.
603
+ local _, T = full(self.V, g)
604
+ if T then
605
+ for i = T.n - 1, 1, -1 do
606
+ if kw(T, i, 'return') and T.k[i + 1] == 'id' then exported = text_of(T, i + 1); break end
607
+ end
608
+ end
609
+ end
610
+ for _, node in ipairs(rec.nodes) do
611
+ if exported then
612
+ local member = node.qualified:match('^' .. exported:gsub('%p', '%%%0') .. '[%.:]([%w_]+)$')
613
+ if member then out[#out + 1] = { name = member, kind = node.kind, node = node, file = g } end
614
+ elseif not node.parent and not node.qualified:find('[%.:]') and (language ~= 'go' or node.name:match('^%u')) then
615
+ out[#out + 1] = { name = node.name, kind = node.kind, node = node, file = g }
616
+ end
617
+ end
618
+ if exported then
619
+ local ids = self.V.id_of[g]
620
+ for n, node in ipairs(rec.nodes) do
621
+ if node.name == exported and not node.parent then
622
+ for _, entry in ipairs(self:members({ ids[n] })) do out[#out + 1] = entry end
623
+ break
624
+ end
625
+ end
626
+ end
627
+ end
628
+ return out
629
+ end
630
+
631
+ -- Innermost function-like node and enclosing type of the cursor, in the full record.
632
+ function Completion:scope()
633
+ local fn, owner
634
+ local best
635
+ for n, node in ipairs(self.rec.nodes) do
636
+ if node.start_byte and node.start_byte <= self.offset and self.offset <= node.end_byte then
637
+ if CALLABLE[node.kind] and (not best or node.end_byte - node.start_byte < best) then
638
+ fn, best = n, node.end_byte - node.start_byte
639
+ end
640
+ end
641
+ end
642
+ local p = fn and self.rec.nodes[fn].parent
643
+ while p do
644
+ if TYPES[self.rec.nodes[p].kind] then owner = p; break end
645
+ p = self.rec.nodes[p].parent
646
+ end
647
+ if not owner then
648
+ local start = fn or 0
649
+ local best_type
650
+ for n, node in ipairs(self.rec.nodes) do
651
+ if TYPES[node.kind] and node.start_byte and node.start_byte <= self.offset and self.offset <= node.end_byte
652
+ and (not best_type or node.end_byte - node.start_byte < best_type) and n ~= start then
653
+ owner, best_type = n, node.end_byte - node.start_byte
654
+ end
655
+ end
656
+ end
657
+ return fn, owner
658
+ end
659
+
660
+ -- Type ids of the enclosing class for self/this.
661
+ function Completion:self_types()
662
+ local fn, owner = self.fn, self.owner
663
+ if owner then return { self.V.id_of[self.f][owner] } end
664
+ if not fn then return {} end
665
+ local node = self.rec.nodes[fn]
666
+ if self.language == 'kotlin' and node.receiver then return self:types(normalize(node.receiver)) end
667
+ local prefix = node.qualified:match('^(.*)::[%w_]+$') or node.qualified:match('^(.*)[%.:][%w_]+$')
668
+ if not prefix and self.language == 'go' then
669
+ prefix = (node.sig or ''):match('^func%s*%(%s*[%w_]*%s*%*?%s*([%w_]+)')
670
+ end
671
+ return prefix and self:types(prefix:match('([%w_]+)$')) or {}
672
+ end
673
+
674
+ -- Locals and parameters visible at the cursor.
675
+ function Completion:locals()
676
+ if self.locals_cache then return self.locals_cache end
677
+ local T, rec, out = self.T, self.rec, {}
678
+ local fn = self.fn and rec.nodes[self.fn]
679
+ local a, b = 1, 0
680
+ for i = 1, T.n do
681
+ if T.s[i] - 1 < self.offset then b = i end
682
+ if fn and T.s[i] - 1 < fn.start_byte then a = i + 1 end
683
+ end
684
+ -- Nested functions that end before the cursor keep their locals.
685
+ local skip = {}
686
+ for n, node in ipairs(rec.nodes) do
687
+ if n ~= self.fn and CALLABLE[node.kind] and node.start_byte and node.end_byte <= self.offset
688
+ and (not fn or node.start_byte >= fn.start_byte) then
689
+ local first, last
690
+ for i = a, b do
691
+ if not first and T.s[i] - 1 >= node.start_byte then first = i end
692
+ if T.e[i] <= node.end_byte then last = i end
693
+ end
694
+ if first and last then skip[#skip + 1] = { first, last } end
695
+ end
696
+ end
697
+ if fn then
698
+ local open
699
+ for i = a, b do
700
+ if is(T, i, '(') and T.m[i] and T.s[i] - 1 >= (fn.name_end or fn.start_byte) then open = i; break end
701
+ end
702
+ if self.language == 'go' and is(T, a + 1, '(') and T.m[a + 1] then
703
+ parameters(T, a + 2, T.m[a + 1] - 1, 'go', out)
704
+ end
705
+ if open then
706
+ parameters(T, open + 1, T.m[open] - 1, self.language, out)
707
+ a = T.m[open] + 1
708
+ end
709
+ for _, param in ipairs(out) do param.kind = 'parameter' end
710
+ end
711
+ declarations(T, a, b, self.language, skip, out)
712
+ self.locals_cache = out
713
+ return out
714
+ end
715
+
716
+ local function find_local(locals, name)
717
+ local hit
718
+ for _, entry in ipairs(locals) do if entry.name == name then hit = entry end end
719
+ return hit
720
+ end
721
+
722
+ -- A receiver: { types = ids } or { modules = files }.
723
+ function Completion:receiver(item, depth)
724
+ local name = item.name
725
+ if SELF[name] or (self.language == 'rust' and name == 'self') then
726
+ local ids = self:self_types()
727
+ if #ids > 0 then return { types = ids } end
728
+ end
729
+ local entry = find_local(self:locals(), name)
730
+ if entry then
731
+ if entry.module then
732
+ return { modules = import_files(self.V, self.f, { kind = 'require', path = entry.module }) }
733
+ elseif entry.type then
734
+ local ids = self:types(entry.type)
735
+ if #ids > 0 then return { types = ids } end
736
+ elseif entry.chain and depth < M.MAX_DEPTH then
737
+ return self:chain(entry.chain, depth + 1)
738
+ end
739
+ if not DYNAMIC[self.language] then return nil end
740
+ -- An untyped local: only this file's assignments describe it.
741
+ if not self.aliases[name] then return { owner = name, local_only = true } end
742
+ end
743
+ local alias = self.aliases[name]
744
+ if alias then
745
+ local files = import_files(self.V, self.f, alias)
746
+ if #files > 0 then return { modules = files } end
747
+ end
748
+ local ids = self:types(name)
749
+ if #ids > 0 then return { types = ids, static = true } end
750
+ -- Dynamic languages: a global table with members defined elsewhere, or a
751
+ -- local table whose fields this file assigns.
752
+ if DYNAMIC[self.language] then return { owner = name } end
753
+ return nil
754
+ end
755
+
756
+ function Completion:entries(target)
757
+ if not target then return {} end
758
+ if target.modules then return self:module_members(target.modules) end
759
+ if target.owner then
760
+ local out = target.local_only and {} or self:owned(target.owner, graph.family(self.language))
761
+ for _, entry in ipairs(self:assigned(target.owner)) do out[#out + 1] = entry end
762
+ return out
763
+ end
764
+ return self:members(target.types)
765
+ end
766
+
767
+ -- Type of a receiver chain, element by element.
768
+ function Completion:chain(chain, depth)
769
+ depth = depth or 0
770
+ local target = self:receiver(chain[1], depth)
771
+ for k = 2, #chain do
772
+ if not target or self:expired() then return nil end
773
+ local element, next_ids = chain[k], {}
774
+ for _, entry in ipairs(self:entries(target)) do
775
+ if entry.name == element.name then
776
+ local node = entry.node
777
+ if TYPES[node.kind] then
778
+ for _, id in ipairs(self:types(node.name, entry.file)) do next_ids[#next_ids + 1] = id end
779
+ else
780
+ local type = declared(node, self.V.files[entry.file].language)
781
+ if not type and element.call and FACTORY[element.name] and target.types then
782
+ for _, id in ipairs(target.types) do next_ids[#next_ids + 1] = id end
783
+ elseif type then
784
+ for _, id in ipairs(self:types(type, entry.file)) do next_ids[#next_ids + 1] = id end
785
+ end
786
+ end
787
+ end
788
+ end
789
+ target = #next_ids > 0 and { types = next_ids } or nil
790
+ end
791
+ return target
792
+ end
793
+
794
+ -- Receiver chain ending at member operator token op.
795
+ local function receiver_chain(T, op, member)
796
+ local chain, i = {}, op - 1
797
+ while i >= 1 and #chain < M.MAX_DEPTH do
798
+ local call = false
799
+ if is(T, i, ')') and T.m[i] then call = true; i = T.m[i] - 1 end
800
+ if is(T, i, '>') and T.m[i] then i = T.m[i] - 1 end
801
+ if not (T.k[i] == 'id' or (T.k[i] == 'kw' and SELF[text_of(T, i)])) then
802
+ if call or #chain == 0 then return nil end
803
+ break
804
+ end
805
+ table.insert(chain, 1, { name = text_of(T, i), call = call })
806
+ local p = i - 1
807
+ if p >= 1 and T.k[p] == 'op' and member[text_of(T, p)] then i = p - 1 else break end
808
+ end
809
+ return #chain > 0 and chain or nil
810
+ end
811
+
812
+ local function item_for(entry, language)
813
+ local node = entry.node
814
+ return { label = entry.name, kind = ITEM[entry.kind] or 6,
815
+ detail = node and (node.sig or (node.type and (node.type .. ' ' .. entry.name))) or entry.detail }
816
+ end
817
+
818
+ -- Completion items at the cursor of req.uri.
819
+ function M.complete(req)
820
+ local doc = navigation.request_doc(req)
821
+ if not doc then return { items = {} } end
822
+ local V = navigation.view(req.session, req.root)
823
+ local f = V.add(doc)
824
+ if not f then return { items = {} } end
825
+ local self = setmetatable({ V = V, f = f, doc = doc, deadline = os.clock() + M.BUDGET_SECONDS,
826
+ incomplete = false }, Completion)
827
+ self.rec, self.T = full(V, f)
828
+ if not self.rec or not self.T then return { items = {} } end
829
+ self.language = self.rec.language
830
+ self.offset = doc:offset(req.position)
831
+ local T, offset = self.T, self.offset
832
+ local t
833
+ for i = 1, T.n do if T.s[i] - 1 < offset then t = i else break end end
834
+ if t and T.k[t] == 'str' and offset < T.e[t] then return { items = {} } end
835
+ local member = self.language == 'lua' and LUA_MEMBER or MEMBER
836
+ local op = t
837
+ if t and (T.k[t] == 'id' or T.k[t] == 'kw') and T.e[t] >= offset then op = t - 1 end
838
+ local is_member = op and op >= 1 and T.k[op] == 'op' and member[text_of(T, op)] and T.e[op] <= offset
839
+ if req.trigger and not is_member then return { items = {} } end
840
+ self.fn, self.owner = self:scope()
841
+ self.aliases = module_aliases(T, self.language)
842
+ local entries = {}
843
+ if is_member then
844
+ local chain = receiver_chain(T, op, member)
845
+ local target = chain and self:chain(chain)
846
+ entries = self:entries(target)
847
+ if self.language == 'lua' and text_of(T, op) == ':' then
848
+ local methods = {}
849
+ for _, entry in ipairs(entries) do if CALLABLE[entry.kind] then methods[#methods + 1] = entry end end
850
+ entries = methods
851
+ end
852
+ else
853
+ for _, entry in ipairs(self:locals()) do
854
+ entries[#entries + 1] = { name = entry.name, kind = 'variable',
855
+ detail = entry.type and (entry.type .. ' ' .. entry.name) or nil }
856
+ end
857
+ for alias in pairs(self.aliases) do entries[#entries + 1] = { name = alias, kind = 'namespace', detail = 'module' } end
858
+ for _, node in ipairs(self.rec.nodes) do
859
+ if not node.parent and (self.language == 'kotlin' or not node.qualified:find('[%.:]')) then
860
+ entries[#entries + 1] = { name = node.name, kind = node.kind, node = node, file = f }
861
+ end
862
+ end
863
+ if self.language == 'kotlin' then
864
+ local direct = navigation.deps(V, f)
865
+ for g in pairs(direct) do
866
+ if self:expired() then break end
867
+ local rec = V.files[g]
868
+ if rec.language == 'kotlin' then
869
+ for _, node in ipairs(rec.nodes) do
870
+ local visible = not node.parent and rec.package == self.rec.package and node.name or nil
871
+ for _, imp in ipairs(self.rec.imports) do
872
+ if node.qualified == imp.path then visible = imp.alias or node.name
873
+ elseif imp.path:sub(-2) == '.*' and graph.owner(node.qualified) == imp.path:sub(1, -3) then
874
+ visible = node.name
875
+ end
876
+ end
877
+ if visible and not node.static then
878
+ entries[#entries + 1] = { name = visible, kind = node.kind, node = node, file = g }
879
+ end
880
+ end
881
+ end
882
+ end
883
+ end
884
+ if self.language == 'c' or self.language == 'cpp' then
885
+ -- Use actual includes (including transitive headers), not paired
886
+ -- implementation files, whose private symbols are not visible here.
887
+ local included = navigation.deps(V, f)
888
+ local files = {}
889
+ for g in pairs(included) do files[#files + 1] = g end
890
+ table.sort(files, function(a, b) return V.files[a].path < V.files[b].path end)
891
+ for _, g in ipairs(files) do
892
+ if self:expired() then break end
893
+ for _, node in ipairs(V.files[g].nodes) do
894
+ if not node.parent and not node.qualified:find('[%.:]') then
895
+ entries[#entries + 1] = { name = node.name, kind = node.kind, node = node, file = g }
896
+ end
897
+ end
898
+ end
899
+ end
900
+ if self.owner or self.fn then
901
+ local ids = self:self_types()
902
+ for _, entry in ipairs(self:members(ids)) do entries[#entries + 1] = entry end
903
+ end
904
+ end
905
+ -- Preserve editor-side fuzzy filtering, but keep prefix matches ahead of
906
+ -- unrelated items when a large include tree exceeds the response limit.
907
+ local prefix = t and T.k[t] == 'id' and T.e[t] >= offset
908
+ and doc.source:sub(T.s[t], offset):lower() or ''
909
+ if prefix ~= '' and #entries > M.MAX_ITEMS then
910
+ local ordered = {}
911
+ for _, entry in ipairs(entries) do
912
+ if entry.name:sub(1, #prefix):lower() == prefix then ordered[#ordered + 1] = entry end
913
+ end
914
+ for _, entry in ipairs(entries) do
915
+ if entry.name:sub(1, #prefix):lower() ~= prefix then ordered[#ordered + 1] = entry end
916
+ end
917
+ entries = ordered
918
+ end
919
+ local items, seen = {}, {}
920
+ for _, entry in ipairs(entries) do
921
+ if not seen[entry.name] then
922
+ seen[entry.name] = true
923
+ if #items >= M.MAX_ITEMS then self.incomplete = true; break end
924
+ items[#items + 1] = item_for(entry, self.language)
925
+ end
926
+ end
927
+ return { items = items, incomplete = self.incomplete }
928
+ end
929
+
930
+ navigation.register('lsp_completion', function(req)
931
+ local result = M.complete(req)
932
+ return { isIncomplete = result.incomplete or false, items = result.items }, false
933
+ end)
934
+
935
+ M.normalize, M.declared = normalize, declared
936
+ return M