1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
local actions = require('telescope.actions')
local action_state = require('telescope.actions.state')
local finders = require('telescope.finders')
local make_entry = require('telescope.make_entry')
local pickers = require('telescope.pickers')
local entry_display = require('telescope.pickers.entry_display')
local utils = require('telescope.utils')
local a = require('plenary.async_lib')
local async, await = a.async, a.await
local channel = a.util.channel
local conf = require('telescope.config').values
local lsp = {}
lsp.references = function(opts)
opts.shorten_path = utils.get_default(opts.shorten_path, true)
local params = vim.lsp.util.make_position_params()
params.context = { includeDeclaration = true }
local results_lsp, err = vim.lsp.buf_request_sync(0, "textDocument/references", params, opts.timeout or 10000)
if err then
vim.api.nvim_err_writeln("Error when finding references: " .. err)
return
end
local locations = {}
for _, server_results in pairs(results_lsp) do
if server_results.result then
vim.list_extend(locations, vim.lsp.util.locations_to_items(server_results.result) or {})
end
end
if vim.tbl_isempty(locations) then
return
end
pickers.new(opts, {
prompt_title = 'LSP References',
finder = finders.new_table {
results = locations,
entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts),
},
previewer = conf.qflist_previewer(opts),
sorter = conf.generic_sorter(opts),
}):find()
end
local function list_or_jump(action, title, opts)
opts = opts or {}
local params = vim.lsp.util.make_position_params()
local result, err = vim.lsp.buf_request_sync(0, action, params, opts.timeout or 10000)
if err then
vim.api.nvim_err_writeln("Error when executing " .. action .. " : " .. err)
return
end
local flattened_results = {}
for _, server_results in pairs(result) do
if server_results.result then
vim.list_extend(flattened_results, server_results.result)
end
end
if #flattened_results == 0 then
return
elseif #flattened_results == 1 then
vim.lsp.util.jump_to_location(flattened_results[1])
else
local locations = vim.lsp.util.locations_to_items(flattened_results)
pickers.new(opts, {
prompt_title = title,
finder = finders.new_table {
results = locations,
entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts),
},
previewer = conf.qflist_previewer(opts),
sorter = conf.generic_sorter(opts),
}):find()
end
end
lsp.definitions = function(opts)
return list_or_jump("textDocument/definition", 'LSP Definitions', opts)
end
lsp.implementations = function(opts)
return list_or_jump("textDocument/implementation", 'LSP Implementations', opts)
end
lsp.document_symbols = function(opts)
local params = vim.lsp.util.make_position_params()
local results_lsp, err = vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params, opts.timeout or 10000)
if err then
vim.api.nvim_err_writeln("Error when finding document symbols: " .. err)
return
end
if not results_lsp or vim.tbl_isempty(results_lsp) then
print("No results from textDocument/documentSymbol")
return
end
local locations = {}
for _, server_results in pairs(results_lsp) do
vim.list_extend(locations, vim.lsp.util.symbols_to_items(server_results.result, 0) or {})
end
locations = utils.filter_symbols(locations, opts)
if locations == nil then
-- error message already printed in `utils.filter_symbols`
return
end
if vim.tbl_isempty(locations) then
return
end
opts.ignore_filename = opts.ignore_filename or true
pickers.new(opts, {
prompt_title = 'LSP Document Symbols',
finder = finders.new_table {
results = locations,
entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts)
},
previewer = conf.qflist_previewer(opts),
sorter = conf.prefilter_sorter{
tag = "symbol_type",
sorter = conf.generic_sorter(opts)
}
}):find()
end
lsp.code_actions = function(opts)
local params = opts.params or vim.lsp.util.make_range_params()
params.context = {
diagnostics = vim.lsp.diagnostic.get_line_diagnostics()
}
local results_lsp, err = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, opts.timeout or 10000)
if err then
print("ERROR: " .. err)
return
end
if not results_lsp or vim.tbl_isempty(results_lsp) then
print("No results from textDocument/codeAction")
return
end
local idx = 1
local results = {}
local widths = {
idx = 0,
command_title = 0,
client_name = 0,
}
for client_id, response in pairs(results_lsp) do
if response.result then
local client = vim.lsp.get_client_by_id(client_id)
for _, result in pairs(response.result) do
local entry = {
idx = idx,
command_title = result.title,
client_name = client and client.name or "",
command = result,
}
for key, value in pairs(widths) do
widths[key] = math.max(value, utils.strdisplaywidth(entry[key]))
end
table.insert(results, entry)
idx = idx + 1
end
end
end
if #results == 0 then
print("No code actions available")
return
end
local displayer = entry_display.create {
separator = " ",
items = {
{ width = widths.idx + 1 }, -- +1 for ":" suffix
{ width = widths.command_title },
{ width = widths.client_name },
},
}
local function make_display(entry)
return displayer {
{entry.idx .. ":", "TelescopePromptPrefix"},
{entry.command_title},
{entry.client_name, "TelescopeResultsComment"},
}
end
pickers.new(opts, {
prompt_title = 'LSP Code Actions',
finder = finders.new_table {
results = results,
entry_maker = function(line)
return {
valid = line ~= nil,
value = line.command,
ordinal = line.idx .. line.command_title,
command_title = line.command_title,
idx = line.idx,
client_name = line.client_name,
display = make_display,
}
end
},
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
local val = selection.value
if val.edit or type(val.command) == "table" then
if val.edit then
vim.lsp.util.apply_workspace_edit(val.edit)
end
if type(val.command) == "table" then
vim.lsp.buf.execute_command(val.command)
end
else
vim.lsp.buf.execute_command(val)
end
end)
return true
end,
sorter = conf.generic_sorter(opts),
}):find()
end
lsp.range_code_actions = function(opts)
opts.params = vim.lsp.util.make_given_range_params()
lsp.code_actions(opts)
end
lsp.workspace_symbols = function(opts)
opts.shorten_path = utils.get_default(opts.shorten_path, true)
local params = {query = opts.query or ''}
local results_lsp, err = vim.lsp.buf_request_sync(0, "workspace/symbol", params, opts.timeout or 10000)
if err then
vim.api.nvim_err_writeln("Error when finding workspace symbols: " .. err)
return
end
local locations = {}
if results_lsp and not vim.tbl_isempty(results_lsp) then
for _, server_results in pairs(results_lsp) do
-- Some LSPs (like Clangd and intelephense) might return { { result = {} } }, so make sure we have result
if server_results and server_results.result and not vim.tbl_isempty(server_results.result) then
vim.list_extend(locations, vim.lsp.util.symbols_to_items(server_results.result, 0) or {})
end
end
end
locations = utils.filter_symbols(locations, opts)
if locations == nil then
-- error message already printed in `utils.filter_symbols`
return
end
if vim.tbl_isempty(locations) then
print("No results from workspace/symbol. Maybe try a different query: " ..
"Telescope lsp_workspace_symbols query=example")
return
end
opts.ignore_filename = utils.get_default(opts.ignore_filename, false)
opts.hide_filename = utils.get_default(opts.hide_filename, false)
pickers.new(opts, {
prompt_title = 'LSP Workspace Symbols',
finder = finders.new_table {
results = locations,
entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts)
},
previewer = conf.qflist_previewer(opts),
sorter = conf.prefilter_sorter{
tag = "symbol_type",
sorter = conf.generic_sorter(opts)
}
}):find()
end
local function get_workspace_symbols_requester(bufnr)
local cancel = function() end
return async(function(prompt)
local tx, rx = channel.oneshot()
cancel()
_, cancel = vim.lsp.buf_request(bufnr, "workspace/symbol", {query = prompt}, tx)
local err, _, results_lsp = await(rx())
assert(not err, err)
local locations = vim.lsp.util.symbols_to_items(results_lsp or {}, bufnr) or {}
return locations
end)
end
lsp.dynamic_workspace_symbols = function(opts)
local curr_bufnr = vim.api.nvim_get_current_buf()
pickers.new(opts, {
prompt_title = 'LSP Dynamic Workspace Symbols',
finder = finders.new_dynamic {
entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts),
fn = get_workspace_symbols_requester(curr_bufnr),
},
previewer = conf.qflist_previewer(opts),
sorter = conf.generic_sorter()
}):find()
end
lsp.diagnostics = function(opts)
local locations = utils.diagnostics_to_tbl(opts)
if vim.tbl_isempty(locations) then
print('No diagnostics found')
return
end
opts.hide_filename = utils.get_default(opts.hide_filename, true)
pickers.new(opts, {
prompt_title = 'LSP Document Diagnostics',
finder = finders.new_table {
results = locations,
entry_maker = opts.entry_maker or make_entry.gen_from_lsp_diagnostics(opts)
},
previewer = conf.qflist_previewer(opts),
sorter = conf.prefilter_sorter{
tag = "type",
sorter = conf.generic_sorter(opts)
}
}):find()
end
lsp.workspace_diagnostics = function(opts)
opts = utils.get_default(opts, {})
opts.hide_filename = utils.get_default(opts.hide_filename, false)
opts.prompt_title = 'LSP Workspace Diagnostics'
opts.get_all = true
lsp.diagnostics(opts)
end
local function check_capabilities(feature)
local clients = vim.lsp.buf_get_clients(0)
local supported_client = false
for _, client in pairs(clients) do
supported_client = client.resolved_capabilities[feature]
if supported_client then break end
end
if supported_client then
return true
else
if #clients == 0 then
print("LSP: no client attached")
else
print("LSP: server does not support " .. feature)
end
return false
end
end
local feature_map = {
["code_actions"] = "code_action",
["document_symbols"] = "document_symbol",
["references"] = "find_references",
["definitions"] = "goto_definition",
["implementations"] = "implementation",
["workspace_symbols"] = "workspace_symbol",
}
local function apply_checks(mod)
for k, v in pairs(mod) do
mod[k] = function(opts)
opts = opts or {}
local feature_name = feature_map[k]
if feature_name and not check_capabilities(feature_name) then
return
end
v(opts)
end
end
return mod
end
return apply_checks(lsp)
|