fix: support multiple clients in lsp code actions (#722)
* fix: support multiple clients in lsp code actions * no goto * reduce diff a bit * use displayer, also include lsp client name for each entry * review comments
This commit is contained in:
@@ -15,7 +15,7 @@ git.files = function(opts)
|
|||||||
local show_untracked = utils.get_default(opts.show_untracked, true)
|
local show_untracked = utils.get_default(opts.show_untracked, true)
|
||||||
local recurse_submodules = utils.get_default(opts.recurse_submodules, false)
|
local recurse_submodules = utils.get_default(opts.recurse_submodules, false)
|
||||||
if show_untracked and recurse_submodules then
|
if show_untracked and recurse_submodules then
|
||||||
error("Git does not suppurt both --others and --recurse-submodules")
|
error("Git does not support both --others and --recurse-submodules")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- By creating the entry maker after the cwd options,
|
-- By creating the entry maker after the cwd options,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ local action_state = require('telescope.actions.state')
|
|||||||
local finders = require('telescope.finders')
|
local finders = require('telescope.finders')
|
||||||
local make_entry = require('telescope.make_entry')
|
local make_entry = require('telescope.make_entry')
|
||||||
local pickers = require('telescope.pickers')
|
local pickers = require('telescope.pickers')
|
||||||
|
local entry_display = require('telescope.pickers.entry_display')
|
||||||
local utils = require('telescope.utils')
|
local utils = require('telescope.utils')
|
||||||
local a = require('plenary.async_lib')
|
local a = require('plenary.async_lib')
|
||||||
local async, await = a.async, a.await
|
local async, await = a.async, a.await
|
||||||
@@ -131,20 +132,56 @@ lsp.code_actions = function(opts)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local _, response = next(results_lsp)
|
local idx = 1
|
||||||
if not response then
|
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")
|
print("No code actions available")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local results = response.result
|
local displayer = entry_display.create {
|
||||||
if not results or #results == 0 then
|
separator = " ",
|
||||||
print("No code actions available")
|
items = {
|
||||||
return
|
{ width = widths.idx + 1 }, -- +1 for ":" suffix
|
||||||
end
|
{ width = widths.command_title },
|
||||||
|
{ width = widths.client_name },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
for i,x in ipairs(results) do
|
local function make_display(entry)
|
||||||
x.idx = i
|
return displayer {
|
||||||
|
{entry.idx .. ":", "TelescopePromptPrefix"},
|
||||||
|
{entry.command_title},
|
||||||
|
{entry.client_name, "TelescopeResultsComment"},
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
pickers.new(opts, {
|
pickers.new(opts, {
|
||||||
@@ -154,9 +191,12 @@ lsp.code_actions = function(opts)
|
|||||||
entry_maker = function(line)
|
entry_maker = function(line)
|
||||||
return {
|
return {
|
||||||
valid = line ~= nil,
|
valid = line ~= nil,
|
||||||
value = line,
|
value = line.command,
|
||||||
ordinal = line.idx .. line.title,
|
ordinal = line.idx .. line.command_title,
|
||||||
display = line.idx .. ': ' .. line.title
|
command_title = line.command_title,
|
||||||
|
idx = line.idx,
|
||||||
|
client_name = line.client_name,
|
||||||
|
display = make_display,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -259,13 +259,14 @@ utils.strdisplaywidth = (function()
|
|||||||
|
|
||||||
return function(str, col)
|
return function(str, col)
|
||||||
local startcol = col or 0
|
local startcol = col or 0
|
||||||
|
str = tostring(str)
|
||||||
local s = ffi.new('char[?]', #str + 1)
|
local s = ffi.new('char[?]', #str + 1)
|
||||||
ffi.copy(s, str)
|
ffi.copy(s, str)
|
||||||
return ffi.C.linetabsize_col(startcol, s) - startcol
|
return ffi.C.linetabsize_col(startcol, s) - startcol
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
return function(str, col)
|
return function(str, col)
|
||||||
return #str - (col or 0)
|
return #(tostring(str)) - (col or 0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)()
|
end)()
|
||||||
|
|||||||
Reference in New Issue
Block a user