@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,934 @@
|
|
|
1
|
+
-- navigation.lua — LSP navigation inside the INDEX worker.
|
|
2
|
+
--
|
|
3
|
+
-- Resolution reuses the graph resolver over the resident symbol tables of one
|
|
4
|
+
-- workspace root, overlaid with the session's open documents (and the queried
|
|
5
|
+
-- file itself), so drafts hide the disk records of their files. Nothing here
|
|
6
|
+
-- is persisted: exact positions come from reparsing only the files a result
|
|
7
|
+
-- names, and callers are found by one scan of reference names per request.
|
|
8
|
+
local graph = require('codeoutline.graph')
|
|
9
|
+
local service = require('codeoutline.service')
|
|
10
|
+
local documents = require('codeoutline.documents')
|
|
11
|
+
local paths = require('codeoutline.path')
|
|
12
|
+
local control = require('codeoutline.control')
|
|
13
|
+
local lsp = require('codeoutline.lsp')
|
|
14
|
+
-- Reference scans stop at a result count or time budget and report truncation.
|
|
15
|
+
local M = { MAX_RESULTS = 1000, MAX_HOVER = 3, SCAN_SECONDS = 3, POSITION_SECONDS = 2, MAX_INCLUDES = 2000 }
|
|
16
|
+
|
|
17
|
+
local sessions = {}
|
|
18
|
+
local ANY = setmetatable({}, { __index = function() return true end })
|
|
19
|
+
-- Bare identifiers bind only nearby, except type names used across files.
|
|
20
|
+
local TYPES = { class = true, struct = true, union = true, interface = true, enum = true, record = true,
|
|
21
|
+
typedef = true, namespace = true, annotation_type = true }
|
|
22
|
+
-- references and call hierarchy cover callables; types need complete reference semantics.
|
|
23
|
+
local REFERENCE = { ['function'] = true, method = true, macro = true, constructor = true,
|
|
24
|
+
prototype = true, destructor = true }
|
|
25
|
+
local CALLS = { ['function'] = true, method = true, macro = true, constructor = true,
|
|
26
|
+
prototype = true, destructor = true, class = true, record = true }
|
|
27
|
+
local MEMBER = { ['.'] = true, ['->'] = true, ['::'] = true, ['?.'] = true }
|
|
28
|
+
local LUA_MEMBER = { ['.'] = true, [':'] = true }
|
|
29
|
+
local SELF = { self = true, this = true, cls = true }
|
|
30
|
+
local PROTOTYPE = { prototype = true }
|
|
31
|
+
local C_FAMILY = { c = true, cpp = true }
|
|
32
|
+
|
|
33
|
+
-- Session drafts arrive with each navigation request: the open set always,
|
|
34
|
+
-- text only for versions this worker has not seen.
|
|
35
|
+
function M.sync(req)
|
|
36
|
+
local store = sessions[req.session] or {}
|
|
37
|
+
sessions[req.session] = store
|
|
38
|
+
for uri in pairs(store) do
|
|
39
|
+
if not req.open[uri] then store[uri] = nil end
|
|
40
|
+
end
|
|
41
|
+
for uri, draft in pairs(req.changed or {}) do
|
|
42
|
+
store[uri] = documents.new(uri, draft.source, draft.version, draft.language)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
function M.close(session) sessions[session] = nil end
|
|
47
|
+
|
|
48
|
+
local function relative(root, path)
|
|
49
|
+
local real = xutils.realpath(path)
|
|
50
|
+
local full = real and paths.normalize(real) or path
|
|
51
|
+
if not paths.contains(root, full) or paths.key(full) == paths.key(root) then return nil end
|
|
52
|
+
return full:sub(#root + 2)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
-- The request's document: the session draft, else the file on disk.
|
|
56
|
+
local function request_doc(req)
|
|
57
|
+
local doc = (sessions[req.session] or {})[req.uri]
|
|
58
|
+
if doc then return doc end
|
|
59
|
+
local path = documents.path(req.uri)
|
|
60
|
+
local real = path and xutils.realpath(path)
|
|
61
|
+
if not real or not paths.contains(req.root, paths.normalize(real)) then return nil end
|
|
62
|
+
doc = documents.read(paths.normalize(real))
|
|
63
|
+
if doc then doc.uri = req.uri end
|
|
64
|
+
return doc
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
-- The index's maintained symbol tables with documents layered over their
|
|
68
|
+
-- files. Overlay files and nodes take ids after every resident one; masked
|
|
69
|
+
-- resident nodes drop out of name lists, which keep path and node order.
|
|
70
|
+
-- Nothing is written to the resident tables.
|
|
71
|
+
local function view(session, root)
|
|
72
|
+
local idx = service.resident(root)
|
|
73
|
+
local G = idx:symbols()
|
|
74
|
+
local V = setmetatable({ root = root, idx = idx, drafts = {}, disk = {}, dep_cache = {}, new_files = {},
|
|
75
|
+
next_file = G.next_file, next_node = G.next_node, next_dir = G.next_dir }, { __index = G })
|
|
76
|
+
for _, name in ipairs({ 'files', 'nodes', 'node_file', 'id_of', 'file_index', 'file_dir' }) do
|
|
77
|
+
V[name] = setmetatable({}, { __index = G[name] })
|
|
78
|
+
end
|
|
79
|
+
local masked, added = {}, {}
|
|
80
|
+
function V.add(doc)
|
|
81
|
+
local rel = doc.path and relative(root, doc.path)
|
|
82
|
+
local f = rel and V.file_index[rel]
|
|
83
|
+
if not rel or (f and V.drafts[f]) then return f end
|
|
84
|
+
local rec = doc:parse()
|
|
85
|
+
if not rec.language then return nil end
|
|
86
|
+
if f then masked[f] = true
|
|
87
|
+
else
|
|
88
|
+
-- An unsaved file resolves against others; no resident file depends on it.
|
|
89
|
+
V.next_file = V.next_file + 1
|
|
90
|
+
f = V.next_file
|
|
91
|
+
V.file_index[rel] = f
|
|
92
|
+
V.new_files[#V.new_files + 1] = f
|
|
93
|
+
local same = G.dir_ids[rel:match('^(.*)/[^/]*$') or '']
|
|
94
|
+
if not same then V.next_dir = V.next_dir + 1; same = V.next_dir end
|
|
95
|
+
V.file_dir[f] = same
|
|
96
|
+
end
|
|
97
|
+
local copy = {}
|
|
98
|
+
for key, value in pairs(rec) do copy[key] = value end
|
|
99
|
+
copy.path = rel
|
|
100
|
+
V.files[f], V.drafts[f] = copy, doc
|
|
101
|
+
local ids = {}
|
|
102
|
+
V.id_of[f] = ids
|
|
103
|
+
for n, node in ipairs(rec.nodes) do
|
|
104
|
+
V.next_node = V.next_node + 1
|
|
105
|
+
local id = V.next_node
|
|
106
|
+
V.nodes[id], V.node_file[id], ids[n] = node, f, id
|
|
107
|
+
local list = added[node.name]
|
|
108
|
+
if not list then list = {}; added[node.name] = list end
|
|
109
|
+
list[#list + 1] = id
|
|
110
|
+
end
|
|
111
|
+
return f
|
|
112
|
+
end
|
|
113
|
+
V.by_name = setmetatable({}, { __index = function(cache, name)
|
|
114
|
+
local list, extra = G.by_name[name] or {}, added[name]
|
|
115
|
+
if next(masked) then
|
|
116
|
+
local kept = {}
|
|
117
|
+
for _, id in ipairs(list) do
|
|
118
|
+
if not masked[G.node_file[id]] then kept[#kept + 1] = id end
|
|
119
|
+
end
|
|
120
|
+
list = kept
|
|
121
|
+
end
|
|
122
|
+
if extra then
|
|
123
|
+
local merged = {}
|
|
124
|
+
for _, id in ipairs(list) do merged[#merged + 1] = id end
|
|
125
|
+
for _, id in ipairs(extra) do merged[#merged + 1] = id end
|
|
126
|
+
table.sort(merged, function(a, b) return graph.before(V, a, b) end)
|
|
127
|
+
list = merged
|
|
128
|
+
end
|
|
129
|
+
rawset(cache, name, list)
|
|
130
|
+
return list
|
|
131
|
+
end })
|
|
132
|
+
local drafts = {}
|
|
133
|
+
for _, doc in pairs(sessions[session] or {}) do drafts[#drafts + 1] = doc end
|
|
134
|
+
table.sort(drafts, function(a, b) return a.uri < b.uri end)
|
|
135
|
+
for _, doc in ipairs(drafts) do V.add(doc) end
|
|
136
|
+
return V
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
-- Dependencies of file f. C and C++ see everything their includes include,
|
|
140
|
+
-- so headers are followed transitively (bounded), with the sources paired to
|
|
141
|
+
-- any of them.
|
|
142
|
+
local function deps(V, f)
|
|
143
|
+
local entry = V.dep_cache[f]
|
|
144
|
+
if not entry then
|
|
145
|
+
local direct, paired = graph.dependencies(V, f)
|
|
146
|
+
if C_FAMILY[V.files[f].language] then
|
|
147
|
+
local queue = {}
|
|
148
|
+
for g in pairs(direct) do queue[#queue + 1] = g end
|
|
149
|
+
local i = 1
|
|
150
|
+
while i <= #queue and #queue < M.MAX_INCLUDES do
|
|
151
|
+
local more, sources = graph.dependencies(V, queue[i])
|
|
152
|
+
for h in pairs(sources) do paired[h] = true end
|
|
153
|
+
for h in pairs(more) do
|
|
154
|
+
if not direct[h] and h ~= f then direct[h] = true; queue[#queue + 1] = h end
|
|
155
|
+
end
|
|
156
|
+
i = i + 1
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
entry = { direct, paired }
|
|
160
|
+
V.dep_cache[f] = entry
|
|
161
|
+
end
|
|
162
|
+
return entry[1], entry[2]
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
local function index_of(V, id)
|
|
166
|
+
local f = V.node_file[id]
|
|
167
|
+
return id - V.id_of[f][1] + 1, f
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
-- self.f() inside f, or M.f() inside M.f / Box::f() inside Box::f.
|
|
171
|
+
local function recursive(node, recv)
|
|
172
|
+
if not recv then return false end
|
|
173
|
+
if SELF[recv] then return true end
|
|
174
|
+
local q = node.qualified
|
|
175
|
+
for _, sep in ipairs({ '.', ':', '::' }) do
|
|
176
|
+
local tail = recv .. sep .. node.name
|
|
177
|
+
if q == tail or q:sub(-(#tail + 1)) == '.' .. tail then return true end
|
|
178
|
+
end
|
|
179
|
+
return false
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
-- Targets of one occurrence of name in file f, enclosed by node index from.
|
|
183
|
+
local function resolve_site(V, f, from, name, kind, recv)
|
|
184
|
+
local src = from > 0 and V.id_of[f][from] or nil
|
|
185
|
+
local direct, paired = deps(V, f)
|
|
186
|
+
local rkind, accept = kind, nil
|
|
187
|
+
if kind == 'symbol' then rkind, accept = 'call', ANY
|
|
188
|
+
elseif kind == 'member_symbol' then rkind, accept = 'member_call', ANY end
|
|
189
|
+
local targets = graph.resolve(V, f, src, name, rkind, recv or nil, direct, paired, accept) or {}
|
|
190
|
+
if kind == 'symbol' then
|
|
191
|
+
local kept = {}
|
|
192
|
+
for _, id in ipairs(targets) do
|
|
193
|
+
local node = V.nodes[id]
|
|
194
|
+
if TYPES[node.kind] or (graph.placement(V, f, V.node_file[id], node, direct, paired) or 0) > 0 then
|
|
195
|
+
kept[#kept + 1] = id
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
targets = kept
|
|
199
|
+
end
|
|
200
|
+
-- The resolver never binds a ref to its enclosing node, which hides recursion.
|
|
201
|
+
if #targets == 0 and src and V.nodes[src].name == name and (kind == 'call' or recursive(V.nodes[src], recv)) then
|
|
202
|
+
targets = { src }
|
|
203
|
+
end
|
|
204
|
+
-- Definitions too far apart to choose between: a visible prototype still
|
|
205
|
+
-- names the function, and going to its definition lists them all.
|
|
206
|
+
if #targets == 0 and (kind == 'call' or kind == 'ref') then
|
|
207
|
+
targets = graph.resolve(V, f, src, name, kind, recv or nil, direct, paired, PROTOTYPE) or {}
|
|
208
|
+
end
|
|
209
|
+
if C_FAMILY[V.files[f].language] then
|
|
210
|
+
-- C binds only what its includes make visible; a lone far definition
|
|
211
|
+
-- (another library's macro) is not the one a system header declares.
|
|
212
|
+
local kept = {}
|
|
213
|
+
for _, id in ipairs(targets) do
|
|
214
|
+
if id == src or (graph.placement(V, f, V.node_file[id], V.nodes[id], direct, paired) or 0) > 0 then
|
|
215
|
+
kept[#kept + 1] = id
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
targets = kept
|
|
219
|
+
end
|
|
220
|
+
return targets
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
-- What the cursor names: a declaration, an indexed reference, or another identifier.
|
|
224
|
+
local function find_cursor(doc, offset, strict)
|
|
225
|
+
local rec = doc:parse()
|
|
226
|
+
local function inside(a, b) return a <= offset and (offset < b or (not strict and offset == b)) end
|
|
227
|
+
for n, node in ipairs(rec.nodes) do
|
|
228
|
+
if node.name_start and inside(node.name_start, node.name_end) then
|
|
229
|
+
return { decl = n, name = node.name, first = node.name_start, last = node.name_end }
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
local refs = rec.refs
|
|
233
|
+
if refs and refs.start then
|
|
234
|
+
for i = 1, #refs.name do
|
|
235
|
+
local a = refs.start[i]
|
|
236
|
+
if a and inside(a, refs.stop[i]) then
|
|
237
|
+
return { from = refs.from[i], name = refs.name[i], kind = refs.kind[i], recv = refs.recv[i],
|
|
238
|
+
first = a, last = refs.stop[i] }
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
local function cursor(doc, offset)
|
|
245
|
+
local found = find_cursor(doc, offset, true) or find_cursor(doc, offset, false)
|
|
246
|
+
if found then return found end
|
|
247
|
+
local rec, T = doc:parse(), doc:tokens()
|
|
248
|
+
if not T then return nil end
|
|
249
|
+
local t
|
|
250
|
+
for i = 1, T.n do
|
|
251
|
+
local a = T.s[i] - 1
|
|
252
|
+
if a > offset then break end
|
|
253
|
+
if T.k[i] == 'id' and offset <= T.e[i] then t = i end
|
|
254
|
+
end
|
|
255
|
+
if not t then return nil end
|
|
256
|
+
local kind, recv = 'symbol', nil
|
|
257
|
+
local member = rec.language == 'lua' and LUA_MEMBER or MEMBER
|
|
258
|
+
if T.k[t + 1] == 'op' and T:text(t + 1) == '(' then kind = 'call' end
|
|
259
|
+
local p = t - 1
|
|
260
|
+
if p >= 1 and T.k[p] == 'op' and member[T:text(p)] then
|
|
261
|
+
kind = kind == 'call' and 'member_call' or 'member_symbol'
|
|
262
|
+
if T.k[p - 1] == 'id' or T.k[p - 1] == 'kw' then recv = T:text(p - 1) end
|
|
263
|
+
elseif p >= 1 and T.k[p] == 'kw' and T:text(p) == 'new' then
|
|
264
|
+
kind = 'new'
|
|
265
|
+
end
|
|
266
|
+
local from, span = 0, math.huge
|
|
267
|
+
for n, node in ipairs(rec.nodes) do
|
|
268
|
+
if node.start_byte and node.start_byte <= offset and offset < node.end_byte
|
|
269
|
+
and node.end_byte - node.start_byte < span then
|
|
270
|
+
from, span = n, node.end_byte - node.start_byte
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
return { from = from, name = T:text(t), kind = kind, recv = recv, first = T.s[t] - 1, last = T.e[t] }
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
-- Source document for file f: its overlay, else the disk file read once per request.
|
|
277
|
+
local function source_doc(V, f)
|
|
278
|
+
if V.drafts[f] then return V.drafts[f] end
|
|
279
|
+
local doc = V.disk[f]
|
|
280
|
+
if doc == nil then
|
|
281
|
+
doc = documents.read(V.idx:abs(V.files[f].path)) or false
|
|
282
|
+
V.disk[f] = doc
|
|
283
|
+
end
|
|
284
|
+
return doc or nil
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
local function uri_of(V, f)
|
|
288
|
+
local doc = source_doc(V, f)
|
|
289
|
+
return doc and doc.uri or documents.uri(V.idx:abs(V.files[f].path))
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
local function line_range(line)
|
|
293
|
+
local at = { line = math.max(line - 1, 0), character = 0 }
|
|
294
|
+
return { start = at, ['end'] = at }
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
-- Reparsed node for id, matched by index and verified by name, else by qualified name.
|
|
298
|
+
local function locate(V, id)
|
|
299
|
+
local n, f = index_of(V, id)
|
|
300
|
+
local node, doc = V.nodes[id], source_doc(V, f)
|
|
301
|
+
if not doc then return nil end
|
|
302
|
+
local nodes = doc:parse().nodes
|
|
303
|
+
local hit = nodes[n]
|
|
304
|
+
if hit and hit.name == node.name and hit.qualified == node.qualified and hit.name_start then return doc, hit end
|
|
305
|
+
hit = nil
|
|
306
|
+
for _, c in ipairs(nodes) do
|
|
307
|
+
if c.qualified == node.qualified and c.name_start
|
|
308
|
+
and (not hit or math.abs(c.line - node.line) < math.abs(hit.line - node.line)) then hit = c end
|
|
309
|
+
end
|
|
310
|
+
return doc, hit
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
local function location(V, id)
|
|
314
|
+
local doc, hit = locate(V, id)
|
|
315
|
+
local f = V.node_file[id]
|
|
316
|
+
if hit then return { uri = doc.uri, range = doc:range(hit.name_start, hit.name_end) }, doc, hit end
|
|
317
|
+
return { uri = uri_of(V, f), range = line_range(V.nodes[id].line) }
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
local function item(V, id)
|
|
321
|
+
local loc, doc, hit = location(V, id)
|
|
322
|
+
local node = V.nodes[id]
|
|
323
|
+
local n, f = index_of(V, id)
|
|
324
|
+
return { name = node.name, kind = lsp.symbol_kind(node), detail = node.sig or node.qualified, uri = loc.uri,
|
|
325
|
+
range = hit and doc:range(hit.start_byte, hit.end_byte) or loc.range, selectionRange = loc.range,
|
|
326
|
+
data = { root = V.root, path = V.files[f].path, index = n, name = node.name, qualified = node.qualified } }
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
-- File-scope code (Lua chunks, Python modules, JS top level) calls from the file itself.
|
|
330
|
+
local function file_item(V, f)
|
|
331
|
+
local rel = V.files[f].path
|
|
332
|
+
local start = line_range(1)
|
|
333
|
+
return { name = rel:match('[^/]*$'), kind = 1, detail = rel, uri = uri_of(V, f), range = start,
|
|
334
|
+
selectionRange = start, data = { root = V.root, path = rel, index = 0 } }
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
-- Item data to a node id (nil for a file item) and file index.
|
|
338
|
+
local function relocate(V, data)
|
|
339
|
+
local f = type(data) == 'table' and type(data.path) == 'string' and V.file_index[data.path]
|
|
340
|
+
if not f then return nil end
|
|
341
|
+
if data.index == 0 then return nil, f end
|
|
342
|
+
local ids = V.id_of[f]
|
|
343
|
+
local id = type(data.index) == 'number' and ids[data.index]
|
|
344
|
+
if id and V.nodes[id].name == data.name and V.nodes[id].qualified == data.qualified then return id, f end
|
|
345
|
+
for _, cid in ipairs(ids) do
|
|
346
|
+
if V.nodes[cid].qualified == data.qualified and V.nodes[cid].name == data.name then return cid, f end
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
-- Exact range of reference i of file f, from the overlay or a reparse of the
|
|
351
|
+
-- disk file. A file edited since indexing is matched by name and line.
|
|
352
|
+
local function site_range(V, f, i)
|
|
353
|
+
local doc = source_doc(V, f)
|
|
354
|
+
if not doc then return nil end
|
|
355
|
+
local refs, indexed = doc:parse().refs, V.files[f].refs
|
|
356
|
+
if not refs or not refs.start then return nil end
|
|
357
|
+
local j = i
|
|
358
|
+
if refs.name[i] ~= indexed.name[i] or refs.line[i] ~= indexed.line[i] then
|
|
359
|
+
j = nil
|
|
360
|
+
for k = 1, #refs.name do
|
|
361
|
+
if refs.name[k] == indexed.name[i] and refs.line[k] == indexed.line[i] then j = k; break end
|
|
362
|
+
end
|
|
363
|
+
if not j then return nil end
|
|
364
|
+
end
|
|
365
|
+
local a, b = refs.start[j], refs.stop[j]
|
|
366
|
+
if not a then
|
|
367
|
+
-- Macro bodies record the directive token; find the name on its line.
|
|
368
|
+
local line = refs.line[j]
|
|
369
|
+
local first = doc.lines[line]
|
|
370
|
+
if not first then return nil end
|
|
371
|
+
local last = doc.lines[line + 1] or #doc.source
|
|
372
|
+
local s = doc.source:find(refs.name[j], first + 1, true)
|
|
373
|
+
if not s or s > last then return nil end
|
|
374
|
+
a, b = s - 1, s - 1 + #refs.name[j]
|
|
375
|
+
end
|
|
376
|
+
return doc:range(a, b)
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
local function is_decl(node) return not not graph.is_decl(node) end
|
|
380
|
+
|
|
381
|
+
-- A declaration and its definitions (prototypes, in-class declarations) as one target.
|
|
382
|
+
local function expand(V, set)
|
|
383
|
+
local out = {}
|
|
384
|
+
for id in pairs(set) do
|
|
385
|
+
out[id] = true
|
|
386
|
+
local node = V.nodes[id]
|
|
387
|
+
local family = graph.family(V.files[V.node_file[id]].language)
|
|
388
|
+
for _, cid in ipairs(V.by_name[node.name]) do
|
|
389
|
+
local c = V.nodes[cid]
|
|
390
|
+
if c.qualified == node.qualified and is_decl(c) ~= is_decl(node)
|
|
391
|
+
and graph.family(V.files[V.node_file[cid]].language) == family then out[cid] = true end
|
|
392
|
+
end
|
|
393
|
+
end
|
|
394
|
+
return out
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
local function sorted(set)
|
|
398
|
+
local list = {}
|
|
399
|
+
for id in pairs(set) do list[#list + 1] = id end
|
|
400
|
+
table.sort(list)
|
|
401
|
+
return list
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
-- References to name that resolve into targets, as { f, i } in file order.
|
|
405
|
+
-- Hot names can match thousands of files; past the budget the scan stops.
|
|
406
|
+
local function scan(V, targets, name)
|
|
407
|
+
local hits, deadline = {}, os.clock() + M.SCAN_SECONDS
|
|
408
|
+
local files = {}
|
|
409
|
+
for i, f in ipairs(V.file_order) do files[i] = f end
|
|
410
|
+
for _, f in ipairs(V.new_files) do files[#files + 1] = f end
|
|
411
|
+
for _, f in ipairs(files) do
|
|
412
|
+
control.check()
|
|
413
|
+
local refs = V.files[f].refs
|
|
414
|
+
local names = refs.name
|
|
415
|
+
for i = 1, #names do
|
|
416
|
+
if i % 4096 == 0 then control.check() end
|
|
417
|
+
if names[i] == name then
|
|
418
|
+
if os.clock() > deadline then return hits, true end
|
|
419
|
+
for _, dst in ipairs(resolve_site(V, f, refs.from[i], name, refs.kind[i], refs.recv[i])) do
|
|
420
|
+
if targets[dst] then hits[#hits + 1] = { f, i }; break end
|
|
421
|
+
end
|
|
422
|
+
if #hits >= M.MAX_RESULTS then return hits, true end
|
|
423
|
+
end
|
|
424
|
+
end
|
|
425
|
+
end
|
|
426
|
+
return hits, false
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
-- Files one import record resolves to, through the graph's dependency
|
|
430
|
+
-- rules: a temporary record holding only that import.
|
|
431
|
+
function M.import_files(V, f, imp)
|
|
432
|
+
local rec = V.files[f]
|
|
433
|
+
local fake = { path = rec.language == 'go' and '\0/import.go' or rec.path, language = rec.language,
|
|
434
|
+
nodes = {}, imports = { imp } }
|
|
435
|
+
rawset(V.files, -1, fake)
|
|
436
|
+
local ok, direct = pcall(graph.dependencies, V, -1)
|
|
437
|
+
rawset(V.files, -1, nil)
|
|
438
|
+
local files = {}
|
|
439
|
+
if ok then for g in pairs(direct) do files[#files + 1] = g end end
|
|
440
|
+
table.sort(files, function(x, y) return V.files[x].path < V.files[y].path end)
|
|
441
|
+
return files
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
-- System include directories: client includePaths, INCLUDE/CPATH-style
|
|
445
|
+
-- variables, then the compiler's own list (cc -E -v) or, on Windows, the
|
|
446
|
+
-- newest Windows Kits and the MSVC found by vswhere. Probed once.
|
|
447
|
+
local probed_includes
|
|
448
|
+
local function existing(dir)
|
|
449
|
+
local st = dir and xutils.stat(dir)
|
|
450
|
+
return st and st.type == 'directory'
|
|
451
|
+
end
|
|
452
|
+
local function newest(dir)
|
|
453
|
+
local entries = existing(dir) and xutils.list_dir(dir, nil, true) or {}
|
|
454
|
+
local best
|
|
455
|
+
for _, e in ipairs(entries) do
|
|
456
|
+
local name = e.name or e
|
|
457
|
+
if name:match('^%d') and (not best or name > best) then best = name end
|
|
458
|
+
end
|
|
459
|
+
return best and (dir .. '/' .. best)
|
|
460
|
+
end
|
|
461
|
+
local function probe_includes()
|
|
462
|
+
if probed_includes then return probed_includes end
|
|
463
|
+
local dirs = {}
|
|
464
|
+
local function add(dir)
|
|
465
|
+
dir = dir and paths.normalize(dir)
|
|
466
|
+
if existing(dir) then dirs[#dirs + 1] = dir end
|
|
467
|
+
end
|
|
468
|
+
local windows = paths.windows
|
|
469
|
+
for _, name in ipairs({ 'INCLUDE', 'CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH' }) do
|
|
470
|
+
for dir in (os.getenv(name) or ''):gmatch(windows and '[^;]+' or '[^:]+') do add(dir) end
|
|
471
|
+
end
|
|
472
|
+
if windows then
|
|
473
|
+
local kits = newest('C:/Program Files (x86)/Windows Kits/10/Include')
|
|
474
|
+
if kits then
|
|
475
|
+
for _, sub in ipairs({ 'ucrt', 'um', 'shared', 'winrt' }) do add(kits .. '/' .. sub) end
|
|
476
|
+
end
|
|
477
|
+
local vswhere = 'C:/Program Files (x86)/Microsoft Visual Studio/Installer/vswhere.exe'
|
|
478
|
+
if xutils.stat(vswhere) and io.popen then
|
|
479
|
+
-- cmd strips one pair of quotes around a command with `(x86)` in it.
|
|
480
|
+
local command = '""' .. vswhere:gsub('/', '\\') .. '" -latest -products * -property installationPath 2>nul"'
|
|
481
|
+
local ok, pipe = pcall(io.popen, command)
|
|
482
|
+
local install = ok and pipe and pipe:read('*l')
|
|
483
|
+
if ok and pipe then pipe:close() end
|
|
484
|
+
local msvc = install and newest(paths.normalize(install) .. '/VC/Tools/MSVC')
|
|
485
|
+
if msvc then add(msvc .. '/include') end
|
|
486
|
+
end
|
|
487
|
+
elseif io.popen then
|
|
488
|
+
local ok, pipe = pcall(io.popen, 'cc -xc++ -E -v - </dev/null 2>&1')
|
|
489
|
+
local output = ok and pipe and pipe:read('*a') or ''
|
|
490
|
+
if ok and pipe then pipe:close() end
|
|
491
|
+
local list = output:match('#include <%.%.%.> search starts here:(.-)End of search list') or ''
|
|
492
|
+
for line in list:gmatch('[^\n]+') do add((line:gsub('^%s+', ''):gsub('%s+%(framework directory%)$', ''))) end
|
|
493
|
+
add('/usr/local/include'); add('/usr/include')
|
|
494
|
+
end
|
|
495
|
+
local seen, unique = {}, {}
|
|
496
|
+
for _, dir in ipairs(dirs) do
|
|
497
|
+
local key = paths.key(dir)
|
|
498
|
+
if not seen[key] then seen[key] = true; unique[#unique + 1] = dir end
|
|
499
|
+
end
|
|
500
|
+
probed_includes = unique
|
|
501
|
+
return unique
|
|
502
|
+
end
|
|
503
|
+
|
|
504
|
+
-- Locations of the files an import on this line names: project files by the
|
|
505
|
+
-- dependency rules, and C/C++ headers found in the include directories.
|
|
506
|
+
local function import_targets(V, f, line, include_paths)
|
|
507
|
+
local rec, result, seen = V.files[f], {}, {}
|
|
508
|
+
local function add(uri)
|
|
509
|
+
if not seen[uri] then seen[uri] = true; result[#result + 1] = { uri = uri, range = line_range(1) } end
|
|
510
|
+
end
|
|
511
|
+
for _, imp in ipairs(rec.imports or {}) do
|
|
512
|
+
if imp.line == line then
|
|
513
|
+
local files = imp.system and {} or M.import_files(V, f, imp)
|
|
514
|
+
for _, g in ipairs(files) do add(uri_of(V, g)) end
|
|
515
|
+
if #files == 0 and C_FAMILY[rec.language] then
|
|
516
|
+
local dirs = {}
|
|
517
|
+
if not imp.system then dirs[1] = (V.idx:abs(rec.path):match('^(.*)/[^/]*$')) end
|
|
518
|
+
for _, dir in ipairs(include_paths or {}) do dirs[#dirs + 1] = paths.normalize(dir) end
|
|
519
|
+
for _, dir in ipairs(probe_includes()) do dirs[#dirs + 1] = dir end
|
|
520
|
+
for _, dir in ipairs(dirs) do
|
|
521
|
+
local file = dir .. '/' .. imp.path:gsub('\\', '/')
|
|
522
|
+
local st = xutils.stat(file)
|
|
523
|
+
if st and st.type == 'file' then add(documents.uri(file)); break end
|
|
524
|
+
end
|
|
525
|
+
end
|
|
526
|
+
end
|
|
527
|
+
end
|
|
528
|
+
return result
|
|
529
|
+
end
|
|
530
|
+
|
|
531
|
+
-- Project-local declarative rules; read per request so configuration edits are live.
|
|
532
|
+
local function definition_rules(root)
|
|
533
|
+
local file = io.open(root .. '/.codeoutline.json', 'rb')
|
|
534
|
+
if not file then return {} end
|
|
535
|
+
local raw = file:read(65537); file:close()
|
|
536
|
+
assert(#raw <= 65536, '.codeoutline.json exceeds 64 KiB')
|
|
537
|
+
local ok, config = pcall(xutils.json_unpack, raw)
|
|
538
|
+
assert(ok and type(config) == 'table', 'invalid .codeoutline.json')
|
|
539
|
+
local rules = config.definitionRules or {}
|
|
540
|
+
assert(type(rules) == 'table' and #rules <= 32, 'definitionRules must contain at most 32 rules')
|
|
541
|
+
for _, rule in ipairs(rules) do
|
|
542
|
+
assert(type(rule) == 'table' and (rule.language == nil or rule.language == 'lua'),
|
|
543
|
+
'definitionRules currently support language lua')
|
|
544
|
+
for _, key in ipairs({ 'call', 'target' }) do
|
|
545
|
+
assert(type(rule[key]) == 'string' and rule[key]:match('^[%a_][%w_%.:]*$'),
|
|
546
|
+
'definitionRules.' .. key .. ' must be a qualified function name')
|
|
547
|
+
end
|
|
548
|
+
for _, key in ipairs({ 'argument', 'targetArgument' }) do
|
|
549
|
+
local n = rule[key]
|
|
550
|
+
assert(type(n) == 'number' and n >= 1 and n <= 32 and n % 1 == 0,
|
|
551
|
+
'definitionRules.' .. key .. ' must be an integer from 1 to 32')
|
|
552
|
+
end
|
|
553
|
+
end
|
|
554
|
+
return rules
|
|
555
|
+
end
|
|
556
|
+
|
|
557
|
+
-- Decode literals without running source code. Lua long strings and quoted escapes.
|
|
558
|
+
local function string_value(raw)
|
|
559
|
+
local eq, body = raw:match('^%[(=*)%[(.*)%]%1%]$')
|
|
560
|
+
if eq then return body:gsub('^\r?\n', '', 1) end
|
|
561
|
+
local quote = raw:sub(1, 1)
|
|
562
|
+
if (quote ~= '"' and quote ~= "'") or raw:sub(-1) ~= quote then return nil end
|
|
563
|
+
local escaped = { a = '\a', b = '\b', f = '\f', n = '\n', r = '\r', t = '\t', v = '\v',
|
|
564
|
+
['\\'] = '\\', ['"'] = '"', ["'"] = "'", ['\n'] = '\n' }
|
|
565
|
+
local out, i, stop = {}, 2, #raw - 1
|
|
566
|
+
while i <= stop do
|
|
567
|
+
local c = raw:sub(i, i)
|
|
568
|
+
if c ~= '\\' then out[#out + 1] = c; i = i + 1
|
|
569
|
+
else
|
|
570
|
+
c = raw:sub(i + 1, i + 1); i = i + 2
|
|
571
|
+
if escaped[c] then out[#out + 1] = escaped[c]
|
|
572
|
+
elseif c == 'z' then
|
|
573
|
+
while i <= stop and raw:sub(i, i):match('%s') do i = i + 1 end
|
|
574
|
+
elseif c == '\r' then
|
|
575
|
+
if raw:sub(i, i) == '\n' then i = i + 1 end
|
|
576
|
+
out[#out + 1] = '\n'
|
|
577
|
+
elseif c == 'x' then
|
|
578
|
+
local hex = raw:sub(i, i + 1)
|
|
579
|
+
if not hex:match('^%x%x$') then return nil end
|
|
580
|
+
out[#out + 1] = string.char(tonumber(hex, 16)); i = i + 2
|
|
581
|
+
elseif c:match('%d') then
|
|
582
|
+
local digits = (c .. raw:sub(i, stop)):match('^%d%d?%d?')
|
|
583
|
+
local n = tonumber(digits)
|
|
584
|
+
if n > 255 then return nil end
|
|
585
|
+
out[#out + 1] = string.char(n); i = i + #digits - 1
|
|
586
|
+
else return nil end
|
|
587
|
+
end
|
|
588
|
+
end
|
|
589
|
+
return table.concat(out)
|
|
590
|
+
end
|
|
591
|
+
|
|
592
|
+
local function call_name(T, i)
|
|
593
|
+
if T.k[i] ~= 'id' or T:text(i + 1) ~= '(' then return nil end
|
|
594
|
+
local name, first = T:text(i), i
|
|
595
|
+
while T.k[first - 2] == 'id' and (T:text(first - 1) == '.' or T:text(first - 1) == ':') do
|
|
596
|
+
name = T:text(first - 2) .. T:text(first - 1) .. name
|
|
597
|
+
first = first - 2
|
|
598
|
+
end
|
|
599
|
+
if T:text(first - 1) == 'function' then return nil end
|
|
600
|
+
return name
|
|
601
|
+
end
|
|
602
|
+
|
|
603
|
+
-- Find a single literal argument, skipping nested brackets and anonymous functions.
|
|
604
|
+
local function literal_argument(T, call, argument)
|
|
605
|
+
local open, number, first = call + 1, 1, call + 2
|
|
606
|
+
local close = T.m[open]
|
|
607
|
+
if not close then return nil end
|
|
608
|
+
local i = first
|
|
609
|
+
while i <= close do
|
|
610
|
+
local token = T:text(i)
|
|
611
|
+
if i == close or token == ',' then
|
|
612
|
+
if number == argument then
|
|
613
|
+
if i == first + 1 and T.k[first] == 'str' then
|
|
614
|
+
return string_value(T:text(first)), T.s[first] - 1, T.e[first]
|
|
615
|
+
end
|
|
616
|
+
return nil
|
|
617
|
+
end
|
|
618
|
+
number, first = number + 1, i + 1
|
|
619
|
+
elseif token == '(' or token == '[' or token == '{' then
|
|
620
|
+
if not T.m[i] then return nil end
|
|
621
|
+
i = T.m[i]
|
|
622
|
+
elseif T.k[i] == 'kw' and token == 'function' then
|
|
623
|
+
local depth = 1
|
|
624
|
+
while depth > 0 and i < close do
|
|
625
|
+
i = i + 1
|
|
626
|
+
local kw = T.k[i] == 'kw' and T:text(i)
|
|
627
|
+
if kw == 'function' or kw == 'if' or kw == 'do' or kw == 'repeat' then depth = depth + 1
|
|
628
|
+
elseif kw == 'end' or kw == 'until' then depth = depth - 1 end
|
|
629
|
+
end
|
|
630
|
+
if depth > 0 then return nil end
|
|
631
|
+
end
|
|
632
|
+
i = i + 1
|
|
633
|
+
end
|
|
634
|
+
end
|
|
635
|
+
|
|
636
|
+
local function configured_definition(V, doc, offset)
|
|
637
|
+
if doc:parse().language ~= 'lua' then return nil end
|
|
638
|
+
local rules = definition_rules(V.root)
|
|
639
|
+
if #rules == 0 then return nil end
|
|
640
|
+
local tokens = doc:tokens()
|
|
641
|
+
local matched = {}
|
|
642
|
+
for i = 1, tokens.n do
|
|
643
|
+
if i % 128 == 0 then control.check() end
|
|
644
|
+
local name = call_name(tokens, i)
|
|
645
|
+
for _, rule in ipairs(rules) do
|
|
646
|
+
if name == rule.call then
|
|
647
|
+
local value, first, last = literal_argument(tokens, i, rule.argument)
|
|
648
|
+
if value and first <= offset and offset < last then matched[#matched + 1] = { rule, value } end
|
|
649
|
+
end
|
|
650
|
+
end
|
|
651
|
+
end
|
|
652
|
+
if #matched == 0 then return nil end
|
|
653
|
+
local result, seen, files = {}, {}, {}
|
|
654
|
+
for _, f in ipairs(V.file_order) do files[#files + 1] = f end
|
|
655
|
+
for _, f in ipairs(V.new_files) do files[#files + 1] = f end
|
|
656
|
+
table.sort(files, function(a, b) return V.files[a].path < V.files[b].path end)
|
|
657
|
+
local deadline = os.clock() + M.SCAN_SECONDS
|
|
658
|
+
for _, f in ipairs(files) do
|
|
659
|
+
control.check()
|
|
660
|
+
if os.clock() > deadline then return result, true end
|
|
661
|
+
if V.files[f].language == 'lua' then
|
|
662
|
+
local target = source_doc(V, f)
|
|
663
|
+
if target then
|
|
664
|
+
local candidate = false
|
|
665
|
+
for _, match in ipairs(matched) do
|
|
666
|
+
local tail = match[1].target:match('([%w_]+)$')
|
|
667
|
+
if target.source:find(tail, 1, true) then candidate = true; break end
|
|
668
|
+
end
|
|
669
|
+
if candidate then
|
|
670
|
+
local T = target:tokens()
|
|
671
|
+
for i = 1, T.n do
|
|
672
|
+
if i % 128 == 0 then
|
|
673
|
+
control.check()
|
|
674
|
+
if os.clock() > deadline then return result, true end
|
|
675
|
+
end
|
|
676
|
+
local name = call_name(T, i)
|
|
677
|
+
for _, match in ipairs(matched) do
|
|
678
|
+
if name == match[1].target then
|
|
679
|
+
local value, first, last = literal_argument(T, i, match[1].targetArgument)
|
|
680
|
+
if value == match[2] then
|
|
681
|
+
local key = target.uri .. ':' .. first
|
|
682
|
+
if not seen[key] then
|
|
683
|
+
seen[key] = true
|
|
684
|
+
result[#result + 1] = { uri = target.uri, range = target:range(first, last) }
|
|
685
|
+
if #result >= M.MAX_RESULTS then return result, true end
|
|
686
|
+
end
|
|
687
|
+
end
|
|
688
|
+
end
|
|
689
|
+
end
|
|
690
|
+
end
|
|
691
|
+
end
|
|
692
|
+
end
|
|
693
|
+
end
|
|
694
|
+
end
|
|
695
|
+
return result, false
|
|
696
|
+
end
|
|
697
|
+
|
|
698
|
+
local function at(req)
|
|
699
|
+
local doc = request_doc(req)
|
|
700
|
+
if not doc then return nil end
|
|
701
|
+
local V = view(req.session, req.root)
|
|
702
|
+
local f = V.add(doc)
|
|
703
|
+
if not f then return nil end
|
|
704
|
+
local offset = doc:offset(req.position)
|
|
705
|
+
local c = cursor(doc, offset)
|
|
706
|
+
local targets = {}
|
|
707
|
+
if c and c.decl then targets = { V.id_of[f][c.decl] }
|
|
708
|
+
elseif c then targets = resolve_site(V, f, c.from, c.name, c.kind, c.recv) end
|
|
709
|
+
return V, doc, c, targets, f, offset
|
|
710
|
+
end
|
|
711
|
+
|
|
712
|
+
local handlers = {}
|
|
713
|
+
|
|
714
|
+
function handlers.lsp_definition(req)
|
|
715
|
+
local V, doc, c, targets, f, offset = at(req)
|
|
716
|
+
if not V then return false end
|
|
717
|
+
local configured, incomplete = configured_definition(V, doc, offset)
|
|
718
|
+
if configured then return configured, incomplete end
|
|
719
|
+
local line = doc:position(offset).line + 1
|
|
720
|
+
local importer = false
|
|
721
|
+
if c and not c.decl then
|
|
722
|
+
-- `require`/`dofile` themselves name the file they load, not a same-named project function.
|
|
723
|
+
for _, imp in ipairs(V.files[f].imports or {}) do
|
|
724
|
+
if imp.line == line and imp.kind == c.name then importer = true end
|
|
725
|
+
end
|
|
726
|
+
end
|
|
727
|
+
if #targets == 0 or importer then
|
|
728
|
+
-- An import line: `require "a.b"`, `dofile("x.lua")`, `#include <x.h>`, `from . import m`.
|
|
729
|
+
local files = import_targets(V, f, line, req.includePaths)
|
|
730
|
+
if #files > 0 or #targets == 0 then return files end
|
|
731
|
+
end
|
|
732
|
+
if c.decl and is_decl(V.nodes[targets[1]]) then
|
|
733
|
+
-- From a declaration, go to its definitions.
|
|
734
|
+
local defs = {}
|
|
735
|
+
for _, id in ipairs(sorted(expand(V, { [targets[1]] = true }))) do
|
|
736
|
+
if not is_decl(V.nodes[id]) then defs[#defs + 1] = id end
|
|
737
|
+
end
|
|
738
|
+
if #defs > 0 then targets = defs end
|
|
739
|
+
end
|
|
740
|
+
local result = {}
|
|
741
|
+
for _, id in ipairs(targets) do result[#result + 1] = (location(V, id)) end
|
|
742
|
+
return result
|
|
743
|
+
end
|
|
744
|
+
|
|
745
|
+
-- Follow unmatched delimiters up to the cursor. Commas in nested calls,
|
|
746
|
+
-- literals and indexing expressions do not advance the enclosing argument.
|
|
747
|
+
function handlers.lsp_signature(req)
|
|
748
|
+
local doc = request_doc(req)
|
|
749
|
+
if not doc then return false end
|
|
750
|
+
local T, offset = doc:tokens(), doc:offset(req.position)
|
|
751
|
+
if not T then return false end
|
|
752
|
+
local stack = {}
|
|
753
|
+
local close = { [')'] = '(', [']'] = '[', ['}'] = '{' }
|
|
754
|
+
for i = 1, T.n do
|
|
755
|
+
if T.s[i] - 1 >= offset then break end
|
|
756
|
+
if i % 128 == 0 then control.check() end
|
|
757
|
+
if T.k[i] == 'op' then
|
|
758
|
+
local s = T:text(i)
|
|
759
|
+
if s == '(' or s == '[' or s == '{' then
|
|
760
|
+
stack[#stack + 1] = { token = i, delimiter = s, argument = 0 }
|
|
761
|
+
elseif close[s] then
|
|
762
|
+
if stack[#stack] and stack[#stack].delimiter == close[s] then table.remove(stack) end
|
|
763
|
+
elseif s == ',' and stack[#stack] then
|
|
764
|
+
stack[#stack].argument = stack[#stack].argument + 1
|
|
765
|
+
end
|
|
766
|
+
end
|
|
767
|
+
end
|
|
768
|
+
local call
|
|
769
|
+
for i = #stack, 1, -1 do
|
|
770
|
+
local entry = stack[i]
|
|
771
|
+
if entry.delimiter == '(' and T.k[entry.token - 1] == 'id' then call = entry; break end
|
|
772
|
+
end
|
|
773
|
+
if not call then return false end
|
|
774
|
+
local name_token = call.token - 1
|
|
775
|
+
local query = {}
|
|
776
|
+
for key, value in pairs(req) do query[key] = value end
|
|
777
|
+
query.position = doc:position(T.s[name_token] - 1)
|
|
778
|
+
local V, _, c, targets = at(query)
|
|
779
|
+
if not V or not c or c.decl then return false end
|
|
780
|
+
local signatures, seen = {}, {}
|
|
781
|
+
for _, id in ipairs(targets) do
|
|
782
|
+
local node = V.nodes[id]
|
|
783
|
+
local label = node.sig
|
|
784
|
+
if REFERENCE[node.kind] and label and not seen[label] then
|
|
785
|
+
local sig_doc = documents.new(uri_of(V, V.node_file[id]), label, 0)
|
|
786
|
+
local S = sig_doc:tokens()
|
|
787
|
+
local opening
|
|
788
|
+
for i = 1, S and S.n - 1 or 0 do
|
|
789
|
+
if S:text(i) == node.name and S:text(i + 1) == '(' then opening = i + 1; break end
|
|
790
|
+
end
|
|
791
|
+
local ending = opening and S.m[opening]
|
|
792
|
+
if ending then
|
|
793
|
+
local parameters, start, i = {}, S.e[opening] + 1, opening + 1
|
|
794
|
+
while i <= ending do
|
|
795
|
+
local s = S:text(i)
|
|
796
|
+
if i == ending or s == ',' then
|
|
797
|
+
local parameter = label:sub(start, S.s[i] - 1):match('^%s*(.-)%s*$')
|
|
798
|
+
if parameter ~= '' and parameter ~= 'void' then parameters[#parameters + 1] = { label = parameter } end
|
|
799
|
+
start = S.e[i] + 1
|
|
800
|
+
elseif S.m[i] and S.m[i] > i then i = S.m[i] end
|
|
801
|
+
i = i + 1
|
|
802
|
+
end
|
|
803
|
+
signatures[#signatures + 1] = { label = label, parameters = parameters,
|
|
804
|
+
activeParameter = #parameters > 0 and math.min(call.argument, #parameters - 1) or nil }
|
|
805
|
+
seen[label] = true
|
|
806
|
+
end
|
|
807
|
+
end
|
|
808
|
+
end
|
|
809
|
+
if #signatures == 0 then return false end
|
|
810
|
+
return { signatures = signatures, activeSignature = 0, activeParameter = signatures[1].activeParameter }
|
|
811
|
+
end
|
|
812
|
+
|
|
813
|
+
function handlers.lsp_hover(req)
|
|
814
|
+
local V, doc, c, targets = at(req)
|
|
815
|
+
if not V or #targets == 0 then return false end
|
|
816
|
+
local parts = {}
|
|
817
|
+
for k, id in ipairs(targets) do
|
|
818
|
+
if k > M.MAX_HOVER then break end
|
|
819
|
+
local node, rec = V.nodes[id], V.files[V.node_file[id]]
|
|
820
|
+
parts[#parts + 1] = '```' .. (rec.language or '') .. '\n' .. (node.sig or node.qualified) .. '\n```'
|
|
821
|
+
parts[#parts + 1] = node.kind .. ' `' .. node.qualified .. '` — ' .. rec.path .. ':' .. node.line
|
|
822
|
+
end
|
|
823
|
+
return { contents = { kind = 'markdown', value = table.concat(parts, '\n\n') }, range = doc:range(c.first, c.last) }
|
|
824
|
+
end
|
|
825
|
+
|
|
826
|
+
function handlers.lsp_prepare_calls(req)
|
|
827
|
+
local V, _, _, targets = at(req)
|
|
828
|
+
if not V then return false end
|
|
829
|
+
local result = {}
|
|
830
|
+
for _, id in ipairs(targets) do
|
|
831
|
+
if CALLS[V.nodes[id].kind] then result[#result + 1] = item(V, id) end
|
|
832
|
+
end
|
|
833
|
+
return #result > 0 and result or false
|
|
834
|
+
end
|
|
835
|
+
|
|
836
|
+
local function positions_left(t0, incomplete)
|
|
837
|
+
return not incomplete and os.clock() - t0 <= M.POSITION_SECONDS
|
|
838
|
+
end
|
|
839
|
+
|
|
840
|
+
function handlers.lsp_incoming(req)
|
|
841
|
+
local V = view(req.session, req.root)
|
|
842
|
+
local id = relocate(V, req.data)
|
|
843
|
+
if not id then return {} end
|
|
844
|
+
local name = V.nodes[id].name
|
|
845
|
+
local hits, incomplete = scan(V, expand(V, { [id] = true }), name)
|
|
846
|
+
local result, groups, t0 = {}, {}, os.clock()
|
|
847
|
+
for _, hit in ipairs(hits) do
|
|
848
|
+
if not positions_left(t0, false) then incomplete = true; break end
|
|
849
|
+
local f, i = hit[1], hit[2]
|
|
850
|
+
local from = V.files[f].refs.from[i]
|
|
851
|
+
local key = f .. ':' .. from
|
|
852
|
+
local group = groups[key]
|
|
853
|
+
if not group then
|
|
854
|
+
group = { from = from > 0 and item(V, V.id_of[f][from]) or file_item(V, f), fromRanges = {} }
|
|
855
|
+
groups[key] = group
|
|
856
|
+
result[#result + 1] = group
|
|
857
|
+
end
|
|
858
|
+
local range = site_range(V, f, i)
|
|
859
|
+
if range then group.fromRanges[#group.fromRanges + 1] = range end
|
|
860
|
+
end
|
|
861
|
+
return result, incomplete
|
|
862
|
+
end
|
|
863
|
+
|
|
864
|
+
function handlers.lsp_outgoing(req)
|
|
865
|
+
local V = view(req.session, req.root)
|
|
866
|
+
local f = type(req.data) == 'table' and type(req.data.path) == 'string' and V.file_index[req.data.path]
|
|
867
|
+
if not f then return {} end
|
|
868
|
+
if not V.drafts[f] then
|
|
869
|
+
-- Reference positions of the calling file come from one exact reparse.
|
|
870
|
+
local doc = source_doc(V, f)
|
|
871
|
+
if doc then V.add(doc) end
|
|
872
|
+
end
|
|
873
|
+
local id
|
|
874
|
+
id, f = relocate(V, req.data)
|
|
875
|
+
if not f or (req.data.index ~= 0 and not id) then return {} end
|
|
876
|
+
local from = id and index_of(V, id) or 0
|
|
877
|
+
local refs = V.files[f].refs
|
|
878
|
+
local result, groups = {}, {}
|
|
879
|
+
for i = 1, #refs.name do
|
|
880
|
+
if i % 128 == 0 then control.check() end
|
|
881
|
+
if refs.from[i] == from then
|
|
882
|
+
for _, dst in ipairs(resolve_site(V, f, from, refs.name[i], refs.kind[i], refs.recv[i])) do
|
|
883
|
+
local group = groups[dst]
|
|
884
|
+
if not group then
|
|
885
|
+
group = { to = item(V, dst), fromRanges = {} }
|
|
886
|
+
groups[dst] = group
|
|
887
|
+
result[#result + 1] = group
|
|
888
|
+
end
|
|
889
|
+
local range = site_range(V, f, i)
|
|
890
|
+
if range then group.fromRanges[#group.fromRanges + 1] = range end
|
|
891
|
+
end
|
|
892
|
+
end
|
|
893
|
+
end
|
|
894
|
+
return result
|
|
895
|
+
end
|
|
896
|
+
|
|
897
|
+
function handlers.lsp_references(req)
|
|
898
|
+
local V, _, c, targets = at(req)
|
|
899
|
+
if not V or #targets == 0 then return {} end
|
|
900
|
+
local set = {}
|
|
901
|
+
for _, id in ipairs(targets) do
|
|
902
|
+
if not REFERENCE[V.nodes[id].kind] then
|
|
903
|
+
return { error = 'References currently support functions, methods, constructors and macros only' }
|
|
904
|
+
end
|
|
905
|
+
set[id] = true
|
|
906
|
+
end
|
|
907
|
+
set = expand(V, set)
|
|
908
|
+
local hits, incomplete = scan(V, set, c.name)
|
|
909
|
+
local result, t0 = {}, os.clock()
|
|
910
|
+
if req.includeDeclaration then
|
|
911
|
+
for _, id in ipairs(sorted(set)) do result[#result + 1] = (location(V, id)) end
|
|
912
|
+
end
|
|
913
|
+
for _, hit in ipairs(hits) do
|
|
914
|
+
if not positions_left(t0, false) then incomplete = true; break end
|
|
915
|
+
local range = site_range(V, hit[1], hit[2])
|
|
916
|
+
if range then result[#result + 1] = { uri = uri_of(V, hit[1]), range = range } end
|
|
917
|
+
end
|
|
918
|
+
return result, incomplete
|
|
919
|
+
end
|
|
920
|
+
|
|
921
|
+
-- Shared with completion.lua, which registers its own handler.
|
|
922
|
+
M.view, M.request_doc, M.deps, M.source_doc = view, request_doc, deps, source_doc
|
|
923
|
+
function M.register(method, handler) handlers[method] = handler end
|
|
924
|
+
|
|
925
|
+
function M.handles(method) return handlers[method] ~= nil end
|
|
926
|
+
|
|
927
|
+
-- Returns { result = value (false for null), incomplete = bool } or { error = message }.
|
|
928
|
+
function M.execute(req)
|
|
929
|
+
local result, incomplete = handlers[req.method](req)
|
|
930
|
+
if type(result) == 'table' and result.error then return result end
|
|
931
|
+
return { result = result, incomplete = incomplete or false }
|
|
932
|
+
end
|
|
933
|
+
|
|
934
|
+
return M
|