@codua/codeoutline-win32-x64 0.1.8 → 0.2.0
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.
- package/README.md +32 -5
- package/README.zh-CN.md +32 -5
- package/THIRD_PARTY.md +3 -3
- package/bin/xnet.exe +0 -0
- package/build-info.json +1 -1
- package/package.json +1 -1
- package/scripts/codeoutline/cli.lua +2 -2
- package/scripts/codeoutline/command.lua +9 -1
- package/scripts/codeoutline/common.lua +36 -5
- package/scripts/codeoutline/completion.lua +913 -0
- package/scripts/codeoutline/control.lua +5 -1
- package/scripts/codeoutline/daemon.lua +4 -2
- package/scripts/codeoutline/documents.lua +154 -0
- package/scripts/codeoutline/explore.lua +32 -7
- package/scripts/codeoutline/graph.lua +264 -68
- package/scripts/codeoutline/index.lua +168 -48
- package/scripts/codeoutline/index_worker.lua +108 -17
- package/scripts/codeoutline/lang/c.lua +22 -12
- package/scripts/codeoutline/lang/cpp.lua +13 -12
- package/scripts/codeoutline/lang/csharp.lua +21 -21
- package/scripts/codeoutline/lang/go.lua +10 -10
- package/scripts/codeoutline/lang/java.lua +9 -9
- package/scripts/codeoutline/lang/js.lua +15 -14
- package/scripts/codeoutline/lang/lua.lua +9 -6
- package/scripts/codeoutline/lang/python.lua +6 -6
- package/scripts/codeoutline/lang/rust.lua +23 -15
- package/scripts/codeoutline/lsp.lua +574 -0
- package/scripts/codeoutline/main.lua +28 -5
- package/scripts/codeoutline/navigation.lua +934 -0
- package/scripts/codeoutline/parse.lua +8 -2
- package/scripts/codeoutline/parse_pool.lua +189 -0
- package/scripts/codeoutline/parse_worker.lua +24 -0
- package/scripts/codeoutline/service.lua +82 -31
- package/scripts/codeoutline/update_sequence.lua +1 -1
- package/scripts/codeoutline/updater.lua +1 -1
- package/scripts/codeoutline/version.lua +1 -1
- package/scripts/xupgate/client.lua +8 -0
- package/licenses/rpmalloc.txt +0 -12
|
@@ -0,0 +1,913 @@
|
|
|
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 }
|
|
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' 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' then
|
|
315
|
+
if (kw(T, i, 'let') or kw(T, i, 'const') or kw(T, i, 'var')) 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, ',') do e = e + 1 end
|
|
320
|
+
type = span(T, k + 1, e - 1); k = e
|
|
321
|
+
end
|
|
322
|
+
add(i + 1, type, is(T, k, '=') and k + 1 or nil)
|
|
323
|
+
end
|
|
324
|
+
elseif language == 'go' then
|
|
325
|
+
if T.k[i] == 'id' and (i == a or not (T.k[i - 1] == 'op' and MEMBER[text_of(T, i - 1)])) then
|
|
326
|
+
local k, names = i, {}
|
|
327
|
+
while T.k[k] == 'id' do
|
|
328
|
+
names[#names + 1] = k
|
|
329
|
+
if is(T, k + 1, ',') then k = k + 2 else k = k + 1; break end
|
|
330
|
+
end
|
|
331
|
+
if is(T, k, ':=') then
|
|
332
|
+
for n, tok in ipairs(names) do add(tok, nil, n == 1 and k + 1 or nil) end
|
|
333
|
+
i = k
|
|
334
|
+
end
|
|
335
|
+
elseif kw(T, i, 'var') and T.k[i + 1] == 'id' then
|
|
336
|
+
local e = i + 2
|
|
337
|
+
while e <= b and not is(T, e, '=') and T.k[e] ~= 'nl' and not is(T, e, ';') do e = e + 1 end
|
|
338
|
+
add(i + 1, span(T, i + 2, e - 1), is(T, e, '=') and e + 1 or nil)
|
|
339
|
+
end
|
|
340
|
+
elseif language == 'rust' then
|
|
341
|
+
if kw(T, i, 'let') then
|
|
342
|
+
local k = kw(T, i + 1, 'mut') and i + 2 or i + 1
|
|
343
|
+
if T.k[k] == 'id' then
|
|
344
|
+
local type, e = nil, k + 1
|
|
345
|
+
if is(T, e, ':') then
|
|
346
|
+
local t = e + 1
|
|
347
|
+
while t <= b and not is(T, t, '=') and not is(T, t, ';') do t = t + 1 end
|
|
348
|
+
type = span(T, e + 1, t - 1); e = t
|
|
349
|
+
end
|
|
350
|
+
add(k, type, is(T, e, '=') and e + 1 or nil)
|
|
351
|
+
end
|
|
352
|
+
elseif kw(T, i, 'for') and T.k[i + 1] == 'id' then add(i + 1) end
|
|
353
|
+
else
|
|
354
|
+
-- C family, Java, C#: `Type name =|;|,|:` after a statement boundary.
|
|
355
|
+
local nx = text_of(T, i + 1)
|
|
356
|
+
if T.k[i] == 'id' and T.k[i + 1] == 'op' and (nx == '=' or nx == ';' or nx == ',' or nx == ':' or nx == '[') then
|
|
357
|
+
local k = i - 1
|
|
358
|
+
while k >= a and (T.k[k] == 'id' or (T.k[k] == 'kw' and not ({ ['return'] = 1, ['case'] = 1,
|
|
359
|
+
['goto'] = 1, ['else'] = 1, ['throw'] = 1, ['delete'] = 1, ['typedef'] = 1 })[text_of(T, k)])
|
|
360
|
+
or is(T, k, '*') or is(T, k, '&') or is(T, k, '::') or is(T, k, '<') or is(T, k, '>')
|
|
361
|
+
or is(T, k, '[') or is(T, k, ']')) do
|
|
362
|
+
k = k - 1
|
|
363
|
+
end
|
|
364
|
+
local boundary = k < a or is(T, k, ';') or is(T, k, '{') or is(T, k, '}') or is(T, k, '(')
|
|
365
|
+
or T.k[k] == 'dir'
|
|
366
|
+
local type = boundary and span(T, k + 1, i - 1)
|
|
367
|
+
if type and type:find('[%w_]') then
|
|
368
|
+
local init = nx == '=' and i + 2 or nil
|
|
369
|
+
if type == 'auto' or type == 'var' or type == 'const auto' then type = nil end
|
|
370
|
+
add(i, type, init)
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
end
|
|
374
|
+
i = i + 1
|
|
375
|
+
end
|
|
376
|
+
end
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
-- Imports usable as module receivers, from the current document's tokens.
|
|
380
|
+
local function module_aliases(T, language)
|
|
381
|
+
local out = {}
|
|
382
|
+
for i = 1, T.n do
|
|
383
|
+
if language == 'lua' and kw(T, i, 'local') and T.k[i + 1] == 'id' and is(T, i + 2, '=') then
|
|
384
|
+
local value = initializer(T, i + 3, math.min(T.n, i + 6))
|
|
385
|
+
if value.module then out[text_of(T, i + 1)] = { kind = 'require', path = value.module } end
|
|
386
|
+
elseif language == 'python' and kw(T, i, 'import') then
|
|
387
|
+
local from, k = nil, i - 1
|
|
388
|
+
while k >= 1 and T.k[k] ~= 'nl' and not kw(T, k, 'from') do k = k - 1 end
|
|
389
|
+
if kw(T, k, 'from') then from = (span(T, k + 1, i - 1) or ''):gsub('%s+', '') end
|
|
390
|
+
local j = i + 1
|
|
391
|
+
while j <= T.n and T.k[j] ~= 'nl' do
|
|
392
|
+
if T.k[j] == 'id' then
|
|
393
|
+
local first = j
|
|
394
|
+
while is(T, j + 1, '.') and T.k[j + 2] == 'id' do j = j + 2 end
|
|
395
|
+
local path = span(T, first, j):gsub('%s+', '')
|
|
396
|
+
local alias = path
|
|
397
|
+
if kw(T, j + 1, 'as') and T.k[j + 2] == 'id' then alias = text_of(T, j + 2); j = j + 2 end
|
|
398
|
+
if from then out[alias] = { kind = 'import', path = from, names = { path }, member = path }
|
|
399
|
+
elseif alias ~= path or not path:find('.', 1, true) then out[alias] = { kind = 'import', path = path } end
|
|
400
|
+
end
|
|
401
|
+
j = j + 1
|
|
402
|
+
end
|
|
403
|
+
elseif (language == 'javascript' or language == 'typescript') then
|
|
404
|
+
if kw(T, i, 'import') and is(T, i + 1, '*') and T.k[i + 3] == 'id' and T.k[i + 5] == 'str' then
|
|
405
|
+
out[text_of(T, i + 3)] = { kind = 'import', path = text_of(T, i + 5):sub(2, -2) }
|
|
406
|
+
elseif kw(T, i, 'import') and T.k[i + 1] == 'id' and T.k[i + 3] == 'str' then
|
|
407
|
+
out[text_of(T, i + 1)] = { kind = 'import', path = text_of(T, i + 3):sub(2, -2) }
|
|
408
|
+
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
|
|
409
|
+
local value = initializer(T, i + 3, math.min(T.n, i + 6))
|
|
410
|
+
if value.module then out[text_of(T, i + 1)] = { kind = 'import', path = value.module } end
|
|
411
|
+
end
|
|
412
|
+
elseif language == 'go' and kw(T, i, 'import') then
|
|
413
|
+
local first, last = i + 1, i + 2
|
|
414
|
+
if is(T, i + 1, '(') and T.m[i + 1] then first, last = i + 2, T.m[i + 1] - 1 end
|
|
415
|
+
for j = first, last do
|
|
416
|
+
if T.k[j] == 'str' then
|
|
417
|
+
local path = text_of(T, j):sub(2, -2)
|
|
418
|
+
local alias = (j > first and T.k[j - 1] == 'id') and text_of(T, j - 1) or path:match('([^/]+)$')
|
|
419
|
+
out[alias] = { kind = 'import', path = path }
|
|
420
|
+
end
|
|
421
|
+
end
|
|
422
|
+
end
|
|
423
|
+
end
|
|
424
|
+
return out
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
-- Files an import resolves to, through the graph's dependency rules.
|
|
428
|
+
local import_files
|
|
429
|
+
function import_files(V, f, alias)
|
|
430
|
+
if alias.member and alias.path:sub(1, 1) ~= '.' then
|
|
431
|
+
-- `from pkg import mod`: try the submodule first.
|
|
432
|
+
local files = import_files(V, f, { kind = alias.kind, path = alias.path .. '.' .. alias.member })
|
|
433
|
+
if #files > 0 then return files end
|
|
434
|
+
end
|
|
435
|
+
local files = navigation.import_files(V, f, { path = alias.path, kind = alias.kind, names = alias.names })
|
|
436
|
+
if alias.member then
|
|
437
|
+
-- `from pkg import name`: a module file named like the import, else a symbol.
|
|
438
|
+
local modules = {}
|
|
439
|
+
for _, g in ipairs(files) do
|
|
440
|
+
local stem = V.files[g].path:match('([^/]+)%.[^./]+$')
|
|
441
|
+
if stem == alias.member or V.files[g].path:match('([^/]+)/__init__%.py$') == alias.member then modules[#modules + 1] = g end
|
|
442
|
+
end
|
|
443
|
+
return modules
|
|
444
|
+
end
|
|
445
|
+
return files
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
local Completion = {}
|
|
449
|
+
Completion.__index = Completion
|
|
450
|
+
|
|
451
|
+
-- Type nodes named name, as seen from file f.
|
|
452
|
+
function Completion:types(name, f)
|
|
453
|
+
if not name then return {} end
|
|
454
|
+
f = f or self.f
|
|
455
|
+
local accept = self.language == 'lua' and LUA_TYPES or TYPES
|
|
456
|
+
local direct, paired = navigation.deps(self.V, f)
|
|
457
|
+
local found = graph.resolve(self.V, f, nil, name, 'call', nil, direct, paired, accept) or {}
|
|
458
|
+
if #found == 0 then
|
|
459
|
+
local family = graph.family(self.V.files[f].language)
|
|
460
|
+
for _, id in ipairs(self.V.by_name[name] or {}) do
|
|
461
|
+
local node = self.V.nodes[id]
|
|
462
|
+
if accept[node.kind] and graph.family(self.V.files[self.V.node_file[id]].language) == family then
|
|
463
|
+
found[#found + 1] = id
|
|
464
|
+
if #found >= 3 then break end
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
end
|
|
468
|
+
-- Partial declarations: impl blocks, forward declarations, C# partial classes.
|
|
469
|
+
local set, list = {}, {}
|
|
470
|
+
for _, id in ipairs(found) do
|
|
471
|
+
local q = self.V.nodes[id].qualified
|
|
472
|
+
local family = graph.family(self.V.files[self.V.node_file[id]].language)
|
|
473
|
+
for _, cid in ipairs(self.V.by_name[name] or {}) do
|
|
474
|
+
local c = self.V.nodes[cid]
|
|
475
|
+
if not set[cid] and accept[c.kind] and c.qualified == q
|
|
476
|
+
and graph.family(self.V.files[self.V.node_file[cid]].language) == family then
|
|
477
|
+
set[cid] = true; list[#list + 1] = cid
|
|
478
|
+
end
|
|
479
|
+
end
|
|
480
|
+
end
|
|
481
|
+
return list
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
function Completion:expired()
|
|
485
|
+
if os.clock() > self.deadline then self.incomplete = true; return true end
|
|
486
|
+
return false
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
-- Member entries { name, kind, node, file } of the type ids, with bases.
|
|
490
|
+
function Completion:members(ids, depth, seen)
|
|
491
|
+
depth, seen = depth or 0, seen or {}
|
|
492
|
+
local V, out = self.V, {}
|
|
493
|
+
local function add(name, kind, node, f)
|
|
494
|
+
out[#out + 1] = { name = name, kind = kind, node = node, file = f }
|
|
495
|
+
end
|
|
496
|
+
for _, id in ipairs(ids) do
|
|
497
|
+
if seen[id] or self:expired() then break end
|
|
498
|
+
seen[id] = true
|
|
499
|
+
control.check()
|
|
500
|
+
local f = V.node_file[id]
|
|
501
|
+
local n = id - V.id_of[f][1] + 1
|
|
502
|
+
local node = V.nodes[id]
|
|
503
|
+
local rec, T = full(V, f)
|
|
504
|
+
local nodes = rec and rec.nodes or V.files[f].nodes
|
|
505
|
+
local target = nodes[n]
|
|
506
|
+
if not (target and target.qualified == node.qualified) then nodes, target = V.files[f].nodes, node end
|
|
507
|
+
local language = V.files[f].language
|
|
508
|
+
for _, c in ipairs(nodes) do
|
|
509
|
+
if c.parent == n then add(c.name, c.kind, c, f) end
|
|
510
|
+
end
|
|
511
|
+
-- Out-of-line members anywhere: `Box::area`, Go methods, Lua `M.f` / `M:f`.
|
|
512
|
+
for _, entry in ipairs(self:owned(node.qualified, graph.family(language))) do out[#out + 1] = entry end
|
|
513
|
+
-- Attributes assigned in methods (Python, JS) or on Lua tables.
|
|
514
|
+
if T and (language == 'python' or language == 'javascript' or language == 'typescript' or language == 'lua') then
|
|
515
|
+
local first, last = 1, T.n
|
|
516
|
+
if target.start_byte and language ~= 'lua' then
|
|
517
|
+
first, last = nil, nil
|
|
518
|
+
for i = 1, T.n do
|
|
519
|
+
if T.s[i] - 1 >= target.start_byte and not first then first = i end
|
|
520
|
+
if T.e[i] <= target.end_byte then last = i end
|
|
521
|
+
end
|
|
522
|
+
end
|
|
523
|
+
local receivers = language == 'lua' and { [node.name] = true, self = true } or { self = true, this = true }
|
|
524
|
+
for i = first or 1, (last or 0) - 3 do
|
|
525
|
+
if T.k[i] == 'id' or T.k[i] == 'kw' then
|
|
526
|
+
if receivers[text_of(T, i)] and T.k[i + 1] == 'op' and (text_of(T, i + 1) == '.')
|
|
527
|
+
and T.k[i + 2] == 'id' and is(T, i + 3, '=') then
|
|
528
|
+
add(text_of(T, i + 2), 'field', { name = text_of(T, i + 2), kind = 'field' }, f)
|
|
529
|
+
end
|
|
530
|
+
end
|
|
531
|
+
end
|
|
532
|
+
end
|
|
533
|
+
-- Base classes.
|
|
534
|
+
if depth < 3 and node.bases then
|
|
535
|
+
for _, base in ipairs(node.bases) do
|
|
536
|
+
local base_ids = self:types(normalize(base), f)
|
|
537
|
+
for _, entry in ipairs(self:members(base_ids, depth + 1, seen)) do out[#out + 1] = entry end
|
|
538
|
+
end
|
|
539
|
+
end
|
|
540
|
+
end
|
|
541
|
+
return out
|
|
542
|
+
end
|
|
543
|
+
|
|
544
|
+
-- Members recorded under qualifier q (by_owner), with open documents
|
|
545
|
+
-- replacing their files' resident nodes.
|
|
546
|
+
function Completion:owned(q, family)
|
|
547
|
+
local V, out = self.V, {}
|
|
548
|
+
for _, id in ipairs(V.by_owner[q] or {}) do
|
|
549
|
+
local g = V.node_file[id]
|
|
550
|
+
if not V.drafts[g] and graph.family(V.files[g].language) == family then
|
|
551
|
+
local node = V.nodes[id]
|
|
552
|
+
out[#out + 1] = { name = node.name, kind = node.kind, node = node, file = g }
|
|
553
|
+
end
|
|
554
|
+
end
|
|
555
|
+
for g, _ in pairs(V.drafts) do
|
|
556
|
+
if graph.family(V.files[g].language) == family then
|
|
557
|
+
for _, node in ipairs(V.files[g].nodes) do
|
|
558
|
+
if graph.owner(node.qualified) == q then
|
|
559
|
+
out[#out + 1] = { name = node.name, kind = node.kind, node = node, file = g }
|
|
560
|
+
end
|
|
561
|
+
end
|
|
562
|
+
end
|
|
563
|
+
end
|
|
564
|
+
return out
|
|
565
|
+
end
|
|
566
|
+
|
|
567
|
+
-- Fields assigned to name in the current file (`name.x = ...`), and the keys
|
|
568
|
+
-- of a table/object literal it is initialized with.
|
|
569
|
+
function Completion:assigned(name)
|
|
570
|
+
local T, out = self.T, {}
|
|
571
|
+
for i = 1, T.n - 3 do
|
|
572
|
+
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, '=')
|
|
573
|
+
and not (i > 1 and T.k[i - 1] == 'op' and MEMBER[text_of(T, i - 1)]) then
|
|
574
|
+
out[#out + 1] = { name = text_of(T, i + 2), kind = 'field' }
|
|
575
|
+
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
|
|
576
|
+
local depth = 0
|
|
577
|
+
for k = i + 3, T.m[i + 2] - 1 do
|
|
578
|
+
if T.k[k] == 'op' and (text_of(T, k) == '{' or text_of(T, k) == '(' or text_of(T, k) == '[') then
|
|
579
|
+
depth = depth + 1
|
|
580
|
+
elseif T.k[k] == 'op' and (text_of(T, k) == '}' or text_of(T, k) == ')' or text_of(T, k) == ']') then
|
|
581
|
+
depth = depth - 1
|
|
582
|
+
elseif depth == 0 and T.k[k] == 'id' and (is(T, k + 1, '=') or is(T, k + 1, ':'))
|
|
583
|
+
and (is(T, k - 1, '{') or is(T, k - 1, ',') or is(T, k - 1, ';')) then
|
|
584
|
+
out[#out + 1] = { name = text_of(T, k), kind = 'field' }
|
|
585
|
+
end
|
|
586
|
+
end
|
|
587
|
+
end
|
|
588
|
+
end
|
|
589
|
+
return out
|
|
590
|
+
end
|
|
591
|
+
|
|
592
|
+
-- Top-level members of module files.
|
|
593
|
+
function Completion:module_members(files)
|
|
594
|
+
local out = {}
|
|
595
|
+
for _, g in ipairs(files) do
|
|
596
|
+
if self:expired() then break end
|
|
597
|
+
local rec = self.V.files[g]
|
|
598
|
+
local language = rec.language
|
|
599
|
+
local exported
|
|
600
|
+
if language == 'lua' then
|
|
601
|
+
-- The table a Lua module returns names its members.
|
|
602
|
+
local _, T = full(self.V, g)
|
|
603
|
+
if T then
|
|
604
|
+
for i = T.n - 1, 1, -1 do
|
|
605
|
+
if kw(T, i, 'return') and T.k[i + 1] == 'id' then exported = text_of(T, i + 1); break end
|
|
606
|
+
end
|
|
607
|
+
end
|
|
608
|
+
end
|
|
609
|
+
for _, node in ipairs(rec.nodes) do
|
|
610
|
+
if exported then
|
|
611
|
+
local member = node.qualified:match('^' .. exported:gsub('%p', '%%%0') .. '[%.:]([%w_]+)$')
|
|
612
|
+
if member then out[#out + 1] = { name = member, kind = node.kind, node = node, file = g } end
|
|
613
|
+
elseif not node.parent and not node.qualified:find('[%.:]') and (language ~= 'go' or node.name:match('^%u')) then
|
|
614
|
+
out[#out + 1] = { name = node.name, kind = node.kind, node = node, file = g }
|
|
615
|
+
end
|
|
616
|
+
end
|
|
617
|
+
if exported then
|
|
618
|
+
local ids = self.V.id_of[g]
|
|
619
|
+
for n, node in ipairs(rec.nodes) do
|
|
620
|
+
if node.name == exported and not node.parent then
|
|
621
|
+
for _, entry in ipairs(self:members({ ids[n] })) do out[#out + 1] = entry end
|
|
622
|
+
break
|
|
623
|
+
end
|
|
624
|
+
end
|
|
625
|
+
end
|
|
626
|
+
end
|
|
627
|
+
return out
|
|
628
|
+
end
|
|
629
|
+
|
|
630
|
+
-- Innermost function-like node and enclosing type of the cursor, in the full record.
|
|
631
|
+
function Completion:scope()
|
|
632
|
+
local fn, owner
|
|
633
|
+
local best
|
|
634
|
+
for n, node in ipairs(self.rec.nodes) do
|
|
635
|
+
if node.start_byte and node.start_byte <= self.offset and self.offset <= node.end_byte then
|
|
636
|
+
if CALLABLE[node.kind] and (not best or node.end_byte - node.start_byte < best) then
|
|
637
|
+
fn, best = n, node.end_byte - node.start_byte
|
|
638
|
+
end
|
|
639
|
+
end
|
|
640
|
+
end
|
|
641
|
+
local p = fn and self.rec.nodes[fn].parent
|
|
642
|
+
while p do
|
|
643
|
+
if TYPES[self.rec.nodes[p].kind] then owner = p; break end
|
|
644
|
+
p = self.rec.nodes[p].parent
|
|
645
|
+
end
|
|
646
|
+
if not owner then
|
|
647
|
+
local start = fn or 0
|
|
648
|
+
local best_type
|
|
649
|
+
for n, node in ipairs(self.rec.nodes) do
|
|
650
|
+
if TYPES[node.kind] and node.start_byte and node.start_byte <= self.offset and self.offset <= node.end_byte
|
|
651
|
+
and (not best_type or node.end_byte - node.start_byte < best_type) and n ~= start then
|
|
652
|
+
owner, best_type = n, node.end_byte - node.start_byte
|
|
653
|
+
end
|
|
654
|
+
end
|
|
655
|
+
end
|
|
656
|
+
return fn, owner
|
|
657
|
+
end
|
|
658
|
+
|
|
659
|
+
-- Type ids of the enclosing class for self/this.
|
|
660
|
+
function Completion:self_types()
|
|
661
|
+
local fn, owner = self.fn, self.owner
|
|
662
|
+
if owner then return { self.V.id_of[self.f][owner] } end
|
|
663
|
+
if not fn then return {} end
|
|
664
|
+
local node = self.rec.nodes[fn]
|
|
665
|
+
local prefix = node.qualified:match('^(.*)::[%w_]+$') or node.qualified:match('^(.*)[%.:][%w_]+$')
|
|
666
|
+
if not prefix and self.language == 'go' then
|
|
667
|
+
prefix = (node.sig or ''):match('^func%s*%(%s*[%w_]*%s*%*?%s*([%w_]+)')
|
|
668
|
+
end
|
|
669
|
+
return prefix and self:types(prefix:match('([%w_]+)$')) or {}
|
|
670
|
+
end
|
|
671
|
+
|
|
672
|
+
-- Locals and parameters visible at the cursor.
|
|
673
|
+
function Completion:locals()
|
|
674
|
+
if self.locals_cache then return self.locals_cache end
|
|
675
|
+
local T, rec, out = self.T, self.rec, {}
|
|
676
|
+
local fn = self.fn and rec.nodes[self.fn]
|
|
677
|
+
local a, b = 1, 0
|
|
678
|
+
for i = 1, T.n do
|
|
679
|
+
if T.s[i] - 1 < self.offset then b = i end
|
|
680
|
+
if fn and T.s[i] - 1 < fn.start_byte then a = i + 1 end
|
|
681
|
+
end
|
|
682
|
+
-- Nested functions that end before the cursor keep their locals.
|
|
683
|
+
local skip = {}
|
|
684
|
+
for n, node in ipairs(rec.nodes) do
|
|
685
|
+
if n ~= self.fn and CALLABLE[node.kind] and node.start_byte and node.end_byte <= self.offset
|
|
686
|
+
and (not fn or node.start_byte >= fn.start_byte) then
|
|
687
|
+
local first, last
|
|
688
|
+
for i = a, b do
|
|
689
|
+
if not first and T.s[i] - 1 >= node.start_byte then first = i end
|
|
690
|
+
if T.e[i] <= node.end_byte then last = i end
|
|
691
|
+
end
|
|
692
|
+
if first and last then skip[#skip + 1] = { first, last } end
|
|
693
|
+
end
|
|
694
|
+
end
|
|
695
|
+
if fn then
|
|
696
|
+
local open
|
|
697
|
+
for i = a, b do
|
|
698
|
+
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
|
|
699
|
+
end
|
|
700
|
+
if self.language == 'go' and is(T, a + 1, '(') and T.m[a + 1] then
|
|
701
|
+
parameters(T, a + 2, T.m[a + 1] - 1, 'go', out)
|
|
702
|
+
end
|
|
703
|
+
if open then
|
|
704
|
+
parameters(T, open + 1, T.m[open] - 1, self.language, out)
|
|
705
|
+
a = T.m[open] + 1
|
|
706
|
+
end
|
|
707
|
+
for _, param in ipairs(out) do param.kind = 'parameter' end
|
|
708
|
+
end
|
|
709
|
+
declarations(T, a, b, self.language, skip, out)
|
|
710
|
+
self.locals_cache = out
|
|
711
|
+
return out
|
|
712
|
+
end
|
|
713
|
+
|
|
714
|
+
local function find_local(locals, name)
|
|
715
|
+
local hit
|
|
716
|
+
for _, entry in ipairs(locals) do if entry.name == name then hit = entry end end
|
|
717
|
+
return hit
|
|
718
|
+
end
|
|
719
|
+
|
|
720
|
+
-- A receiver: { types = ids } or { modules = files }.
|
|
721
|
+
function Completion:receiver(item, depth)
|
|
722
|
+
local name = item.name
|
|
723
|
+
if SELF[name] or (self.language == 'rust' and name == 'self') then
|
|
724
|
+
local ids = self:self_types()
|
|
725
|
+
if #ids > 0 then return { types = ids } end
|
|
726
|
+
end
|
|
727
|
+
local entry = find_local(self:locals(), name)
|
|
728
|
+
if entry then
|
|
729
|
+
if entry.module then
|
|
730
|
+
return { modules = import_files(self.V, self.f, { kind = 'require', path = entry.module }) }
|
|
731
|
+
elseif entry.type then
|
|
732
|
+
local ids = self:types(entry.type)
|
|
733
|
+
if #ids > 0 then return { types = ids } end
|
|
734
|
+
elseif entry.chain and depth < M.MAX_DEPTH then
|
|
735
|
+
return self:chain(entry.chain, depth + 1)
|
|
736
|
+
end
|
|
737
|
+
if not DYNAMIC[self.language] then return nil end
|
|
738
|
+
-- An untyped local: only this file's assignments describe it.
|
|
739
|
+
if not self.aliases[name] then return { owner = name, local_only = true } end
|
|
740
|
+
end
|
|
741
|
+
local alias = self.aliases[name]
|
|
742
|
+
if alias then
|
|
743
|
+
local files = import_files(self.V, self.f, alias)
|
|
744
|
+
if #files > 0 then return { modules = files } end
|
|
745
|
+
end
|
|
746
|
+
local ids = self:types(name)
|
|
747
|
+
if #ids > 0 then return { types = ids, static = true } end
|
|
748
|
+
-- Dynamic languages: a global table with members defined elsewhere, or a
|
|
749
|
+
-- local table whose fields this file assigns.
|
|
750
|
+
if DYNAMIC[self.language] then return { owner = name } end
|
|
751
|
+
return nil
|
|
752
|
+
end
|
|
753
|
+
|
|
754
|
+
function Completion:entries(target)
|
|
755
|
+
if not target then return {} end
|
|
756
|
+
if target.modules then return self:module_members(target.modules) end
|
|
757
|
+
if target.owner then
|
|
758
|
+
local out = target.local_only and {} or self:owned(target.owner, graph.family(self.language))
|
|
759
|
+
for _, entry in ipairs(self:assigned(target.owner)) do out[#out + 1] = entry end
|
|
760
|
+
return out
|
|
761
|
+
end
|
|
762
|
+
return self:members(target.types)
|
|
763
|
+
end
|
|
764
|
+
|
|
765
|
+
-- Type of a receiver chain, element by element.
|
|
766
|
+
function Completion:chain(chain, depth)
|
|
767
|
+
depth = depth or 0
|
|
768
|
+
local target = self:receiver(chain[1], depth)
|
|
769
|
+
for k = 2, #chain do
|
|
770
|
+
if not target or self:expired() then return nil end
|
|
771
|
+
local element, next_ids = chain[k], {}
|
|
772
|
+
for _, entry in ipairs(self:entries(target)) do
|
|
773
|
+
if entry.name == element.name then
|
|
774
|
+
local node = entry.node
|
|
775
|
+
if TYPES[node.kind] then
|
|
776
|
+
for _, id in ipairs(self:types(node.name, entry.file)) do next_ids[#next_ids + 1] = id end
|
|
777
|
+
else
|
|
778
|
+
local type = declared(node, self.V.files[entry.file].language)
|
|
779
|
+
if not type and element.call and FACTORY[element.name] and target.types then
|
|
780
|
+
for _, id in ipairs(target.types) do next_ids[#next_ids + 1] = id end
|
|
781
|
+
elseif type then
|
|
782
|
+
for _, id in ipairs(self:types(type, entry.file)) do next_ids[#next_ids + 1] = id end
|
|
783
|
+
end
|
|
784
|
+
end
|
|
785
|
+
end
|
|
786
|
+
end
|
|
787
|
+
target = #next_ids > 0 and { types = next_ids } or nil
|
|
788
|
+
end
|
|
789
|
+
return target
|
|
790
|
+
end
|
|
791
|
+
|
|
792
|
+
-- Receiver chain ending at member operator token op.
|
|
793
|
+
local function receiver_chain(T, op, member)
|
|
794
|
+
local chain, i = {}, op - 1
|
|
795
|
+
while i >= 1 and #chain < M.MAX_DEPTH do
|
|
796
|
+
local call = false
|
|
797
|
+
if is(T, i, ')') and T.m[i] then call = true; i = T.m[i] - 1 end
|
|
798
|
+
if is(T, i, '>') and T.m[i] then i = T.m[i] - 1 end
|
|
799
|
+
if not (T.k[i] == 'id' or (T.k[i] == 'kw' and SELF[text_of(T, i)])) then
|
|
800
|
+
if call or #chain == 0 then return nil end
|
|
801
|
+
break
|
|
802
|
+
end
|
|
803
|
+
table.insert(chain, 1, { name = text_of(T, i), call = call })
|
|
804
|
+
local p = i - 1
|
|
805
|
+
if p >= 1 and T.k[p] == 'op' and member[text_of(T, p)] then i = p - 1 else break end
|
|
806
|
+
end
|
|
807
|
+
return #chain > 0 and chain or nil
|
|
808
|
+
end
|
|
809
|
+
|
|
810
|
+
local function item_for(entry, language)
|
|
811
|
+
local node = entry.node
|
|
812
|
+
return { label = entry.name, kind = ITEM[entry.kind] or 6,
|
|
813
|
+
detail = node and (node.sig or (node.type and (node.type .. ' ' .. entry.name))) or entry.detail }
|
|
814
|
+
end
|
|
815
|
+
|
|
816
|
+
-- Completion items at the cursor of req.uri.
|
|
817
|
+
function M.complete(req)
|
|
818
|
+
local doc = navigation.request_doc(req)
|
|
819
|
+
if not doc then return { items = {} } end
|
|
820
|
+
local V = navigation.view(req.session, req.root)
|
|
821
|
+
local f = V.add(doc)
|
|
822
|
+
if not f then return { items = {} } end
|
|
823
|
+
local self = setmetatable({ V = V, f = f, doc = doc, deadline = os.clock() + M.BUDGET_SECONDS,
|
|
824
|
+
incomplete = false }, Completion)
|
|
825
|
+
self.rec, self.T = full(V, f)
|
|
826
|
+
if not self.rec or not self.T then return { items = {} } end
|
|
827
|
+
self.language = self.rec.language
|
|
828
|
+
self.offset = doc:offset(req.position)
|
|
829
|
+
local T, offset = self.T, self.offset
|
|
830
|
+
local t
|
|
831
|
+
for i = 1, T.n do if T.s[i] - 1 < offset then t = i else break end end
|
|
832
|
+
if t and T.k[t] == 'str' and offset < T.e[t] then return { items = {} } end
|
|
833
|
+
local member = self.language == 'lua' and LUA_MEMBER or MEMBER
|
|
834
|
+
local op = t
|
|
835
|
+
if t and (T.k[t] == 'id' or T.k[t] == 'kw') and T.e[t] >= offset then op = t - 1 end
|
|
836
|
+
local is_member = op and op >= 1 and T.k[op] == 'op' and member[text_of(T, op)] and T.e[op] <= offset
|
|
837
|
+
if req.trigger and not is_member then return { items = {} } end
|
|
838
|
+
self.fn, self.owner = self:scope()
|
|
839
|
+
self.aliases = module_aliases(T, self.language)
|
|
840
|
+
local entries = {}
|
|
841
|
+
if is_member then
|
|
842
|
+
local chain = receiver_chain(T, op, member)
|
|
843
|
+
local target = chain and self:chain(chain)
|
|
844
|
+
entries = self:entries(target)
|
|
845
|
+
if self.language == 'lua' and text_of(T, op) == ':' then
|
|
846
|
+
local methods = {}
|
|
847
|
+
for _, entry in ipairs(entries) do if CALLABLE[entry.kind] then methods[#methods + 1] = entry end end
|
|
848
|
+
entries = methods
|
|
849
|
+
end
|
|
850
|
+
else
|
|
851
|
+
for _, entry in ipairs(self:locals()) do
|
|
852
|
+
entries[#entries + 1] = { name = entry.name, kind = 'variable',
|
|
853
|
+
detail = entry.type and (entry.type .. ' ' .. entry.name) or nil }
|
|
854
|
+
end
|
|
855
|
+
for alias in pairs(self.aliases) do entries[#entries + 1] = { name = alias, kind = 'namespace', detail = 'module' } end
|
|
856
|
+
for _, node in ipairs(self.rec.nodes) do
|
|
857
|
+
if not node.parent and not node.qualified:find('[%.:]') then
|
|
858
|
+
entries[#entries + 1] = { name = node.name, kind = node.kind, node = node, file = f }
|
|
859
|
+
end
|
|
860
|
+
end
|
|
861
|
+
if self.language == 'c' or self.language == 'cpp' then
|
|
862
|
+
-- Use actual includes (including transitive headers), not paired
|
|
863
|
+
-- implementation files, whose private symbols are not visible here.
|
|
864
|
+
local included = navigation.deps(V, f)
|
|
865
|
+
local files = {}
|
|
866
|
+
for g in pairs(included) do files[#files + 1] = g end
|
|
867
|
+
table.sort(files, function(a, b) return V.files[a].path < V.files[b].path end)
|
|
868
|
+
for _, g in ipairs(files) do
|
|
869
|
+
if self:expired() then break end
|
|
870
|
+
for _, node in ipairs(V.files[g].nodes) do
|
|
871
|
+
if not node.parent and not node.qualified:find('[%.:]') then
|
|
872
|
+
entries[#entries + 1] = { name = node.name, kind = node.kind, node = node, file = g }
|
|
873
|
+
end
|
|
874
|
+
end
|
|
875
|
+
end
|
|
876
|
+
end
|
|
877
|
+
if self.owner or self.fn then
|
|
878
|
+
local ids = self:self_types()
|
|
879
|
+
for _, entry in ipairs(self:members(ids)) do entries[#entries + 1] = entry end
|
|
880
|
+
end
|
|
881
|
+
end
|
|
882
|
+
-- Preserve editor-side fuzzy filtering, but keep prefix matches ahead of
|
|
883
|
+
-- unrelated items when a large include tree exceeds the response limit.
|
|
884
|
+
local prefix = t and T.k[t] == 'id' and T.e[t] >= offset
|
|
885
|
+
and doc.source:sub(T.s[t], offset):lower() or ''
|
|
886
|
+
if prefix ~= '' and #entries > M.MAX_ITEMS then
|
|
887
|
+
local ordered = {}
|
|
888
|
+
for _, entry in ipairs(entries) do
|
|
889
|
+
if entry.name:sub(1, #prefix):lower() == prefix then ordered[#ordered + 1] = entry end
|
|
890
|
+
end
|
|
891
|
+
for _, entry in ipairs(entries) do
|
|
892
|
+
if entry.name:sub(1, #prefix):lower() ~= prefix then ordered[#ordered + 1] = entry end
|
|
893
|
+
end
|
|
894
|
+
entries = ordered
|
|
895
|
+
end
|
|
896
|
+
local items, seen = {}, {}
|
|
897
|
+
for _, entry in ipairs(entries) do
|
|
898
|
+
if not seen[entry.name] then
|
|
899
|
+
seen[entry.name] = true
|
|
900
|
+
if #items >= M.MAX_ITEMS then self.incomplete = true; break end
|
|
901
|
+
items[#items + 1] = item_for(entry, self.language)
|
|
902
|
+
end
|
|
903
|
+
end
|
|
904
|
+
return { items = items, incomplete = self.incomplete }
|
|
905
|
+
end
|
|
906
|
+
|
|
907
|
+
navigation.register('lsp_completion', function(req)
|
|
908
|
+
local result = M.complete(req)
|
|
909
|
+
return { isIncomplete = result.incomplete or false, items = result.items }, false
|
|
910
|
+
end)
|
|
911
|
+
|
|
912
|
+
M.normalize, M.declared = normalize, declared
|
|
913
|
+
return M
|