diff --git a/lua/outline/hover.lua b/lua/outline/hover.lua index 39c5051..b43aaa0 100644 --- a/lua/outline/hover.lua +++ b/lua/outline/hover.lua @@ -1,6 +1,5 @@ local cfg = require('outline.config') local outline = require('outline') -local util = vim.lsp.util local M = {} @@ -15,7 +14,6 @@ local function get_hover_params(node, winnr) } end --- handler yoinked from the default implementation function M.show_hover() local current_line = vim.api.nvim_win_get_cursor(outline.current.view.win)[1] local node = outline.current.flats[current_line] @@ -26,18 +24,16 @@ function M.show_hover() hover_params.bufnr, 'textDocument/hover', hover_params, - ---@diagnostic disable-next-line: param-type-mismatch function(_, result, _, config) if not (result and result.contents) then return end - local markdown_lines = util.convert_input_to_markdown_lines(result.contents) - markdown_lines = util.trim_empty_lines(markdown_lines) + local markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents) + markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines) if vim.tbl_isempty(markdown_lines) then return end - -- FIXME - local bufnr, winnr = util.open_floating_preview(markdown_lines, 'markdown', config) + local bufnr, winnr = vim.lsp.util.open_floating_preview(markdown_lines, 'markdown', config) vim.api.nvim_win_set_option(winnr, 'winhighlight', cfg.o.preview_window.winhl) end ) diff --git a/lua/outline/providers/init.lua b/lua/outline/providers/init.lua index c059e10..fb38a5b 100644 --- a/lua/outline/providers/init.lua +++ b/lua/outline/providers/init.lua @@ -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 diff --git a/lua/outline/providers/nvim-lsp.lua b/lua/outline/providers/nvim-lsp.lua index f0c27a5..95b03e7 100644 --- a/lua/outline/providers/nvim-lsp.lua +++ b/lua/outline/providers/nvim-lsp.lua @@ -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 diff --git a/lua/outline/types/outline.lua b/lua/outline/types/outline.lua index 1f029bd..9e90cac 100644 --- a/lua/outline/types/outline.lua +++ b/lua/outline/types/outline.lua @@ -67,8 +67,8 @@ ---@field supports_buffer fun(bufnr:integer):boolean ---@field request_symbols fun(on_symbols:fun(symbols?:outline.ProviderSymbol[], opts:table?), opts:table?) ---@field hover_info? fun(bufnr:integer, params:table, on_info:function) ----@field rename_symbol? fun(sidebar:outline.Sidebar) ----@field code_actions? fun(sidebar:outline.Sidebar) +---@field rename_symbol? fun(sidebar:outline.Sidebar):boolean +---@field code_actions? fun(sidebar:outline.Sidebar):boolean -- HELP