feat: Update outline on rename symbol

Also added ability to show provider status from code_action and
rename_symbol
This commit is contained in:
hedy
2023-11-26 20:44:42 +08:00
parent c1064c69fe
commit 8c13999acc
4 changed files with 77 additions and 43 deletions

View File

@@ -27,9 +27,13 @@ end
---@param args any[]
function M.action(sidebar, method, args)
if not sidebar.provider or not sidebar.provider[method] then
require('outline.utils').echo('No supported providers to perform this action.')
return
end
return sidebar.provider[method](unpack(args))
local ok = sidebar.provider[method](unpack(args))
if not ok then
require('outline.utils').echo('The provider could not perform this action successfully.')
end
end
return M

View File

@@ -15,7 +15,7 @@ function M.get_status()
return { 'client: ' .. M.client.name }
end
function M.hover_info(bufnr, params, on_info)
local function get_appropriate_client(bufnr, capability)
local clients = vim.lsp.get_active_clients({ bufnr = bufnr })
local use_client
@@ -23,7 +23,7 @@ function M.hover_info(bufnr, params, on_info)
if config.is_client_blacklisted(client) then
goto continue
else
if client.server_capabilities.hoverProvider then
if client.server_capabilities[capability] then
use_client = client
M.client = client
break
@@ -31,39 +31,16 @@ function M.hover_info(bufnr, params, on_info)
end
::continue::
end
if not use_client then
on_info(nil, {
contents = {
kind = 'markdown',
content = { 'No extra information availaible' },
},
})
return
end
use_client.request('textDocument/hover', params, on_info, bufnr)
return use_client
end
---@return boolean
function M.supports_buffer(bufnr)
local clients = vim.lsp.get_active_clients({ bufnr = bufnr })
local ret = false
for _, client in ipairs(clients) do
if config.is_client_blacklisted(client) then
goto continue
else
if client.server_capabilities.documentSymbolProvider then
M.client = client
ret = true
break
end
end
::continue::
local client = get_appropriate_client(bufnr, 'documentSymbolProvider')
if not client then
return false
end
return ret
return true
end
---@param response outline.ProviderSymbol[]
@@ -95,17 +72,74 @@ end
-- No good way to update outline when LSP action complete for now
---@param sidebar outline.Sidebar
---@return boolean success
function M.code_actions(sidebar)
local client = get_appropriate_client(sidebar.code.buf, 'codeActionProvider')
if not client then
return false
end
-- NOTE: Unfortunately the code_action function provided by neovim does a
-- lot, yet it doesn't let us filter clients. Since handling of code_actions
-- is beyond the scope of outline.nvim itself, we will not respect
-- blacklist_clients for code actions for now. Code actions feature would not
-- actually be included if I were to write this plugin from scratch. However
-- we still keep it because many people are moving here from
-- symbols-outline.nvim, which happened to implement this feature.
sidebar:wrap_goto_location(function()
vim.lsp.buf.code_action()
end)
return true
end
---Synchronously request rename from LSP
---@param sidebar outline.Sidebar
---@return boolean success
function M.rename_symbol(sidebar)
sidebar:wrap_goto_location(function()
vim.lsp.buf.rename()
end)
local client = get_appropriate_client(sidebar.code.buf, 'renameProvider')
if not client then
return false
end
local node = sidebar:_current_node()
-- Using fn.input so it's synchronous
local new_name = vim.fn.input({ prompt = 'New Name: ', default = node.name })
if not new_name or new_name == '' or new_name == node.name then
return true
end
local params = {
textDocument = { uri = 'file://' .. vim.api.nvim_buf_get_name(sidebar.code.buf) },
position = { line = node.line, character = node.character },
bufnr = sidebar.code.buf,
newName = new_name,
}
local timeout = 2000
local status, err = client.request_sync('textDocument/rename', params, timeout, sidebar.code.buf)
if status == nil or status.err or err or status.result == nil then
return false
end
vim.lsp.util.apply_workspace_edit(status.result, client.offset_encoding)
node.name = new_name
sidebar:_update_lines(false)
return true
end
function M.hover_info(bufnr, params, on_info)
local use_client = get_appropriate_client(bufnr, 'hoverProvider')
if not use_client then
on_info(nil, {
contents = {
kind = 'markdown',
content = { 'No extra information availaible' },
},
})
return
end
use_client.request('textDocument/hover', params, on_info, bufnr)
end
return M