diff options
author | Mathias Fußenegger <mfussenegger@users.noreply.github.com> | 2021-09-27 21:57:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-27 21:57:28 +0200 |
commit | 63fde086d9e7936b21c51e145253ec69820b2112 (patch) | |
tree | fe42e09315181b66e32b5787508f6bd81e719c67 /runtime/lua/vim/lsp/handlers.lua | |
parent | 6736ee8be5ae9a3b004bd9033d7efffad74a03be (diff) | |
download | rneovim-63fde086d9e7936b21c51e145253ec69820b2112.tar.gz rneovim-63fde086d9e7936b21c51e145253ec69820b2112.tar.bz2 rneovim-63fde086d9e7936b21c51e145253ec69820b2112.zip |
feat(ui): add vim.ui.select and use in code actions (#15771)
Continuation of https://github.com/neovim/neovim/pull/15202
A plugin like telescope could override it with a fancy implementation
and then users would get the telescope-ui within each plugin that
utilizes the vim.ui.select function.
There are some plugins which override the `textDocument/codeAction`
handler solely to provide a different UI. With custom client commands and
soon codeAction resolve support, it becomes more difficult to implement
the handler right - so having a dedicated way to override the picking
function will be useful.
Diffstat (limited to 'runtime/lua/vim/lsp/handlers.lua')
-rw-r--r-- | runtime/lua/vim/lsp/handlers.lua | 70 |
1 files changed, 36 insertions, 34 deletions
diff --git a/runtime/lua/vim/lsp/handlers.lua b/runtime/lua/vim/lsp/handlers.lua index 624f8b5462..538bd2605b 100644 --- a/runtime/lua/vim/lsp/handlers.lua +++ b/runtime/lua/vim/lsp/handlers.lua @@ -116,42 +116,44 @@ M['textDocument/codeAction'] = function(_, result, ctx) return end - local option_strings = {"Code actions:"} - for i, action in ipairs(result) do - local title = action.title:gsub('\r\n', '\\r\\n') - title = title:gsub('\n', '\\n') - table.insert(option_strings, string.format("%d. %s", i, title)) - end - - local choice = vim.fn.inputlist(option_strings) - if choice < 1 or choice > #result then - return - end - local action = result[choice] - -- textDocument/codeAction can return either Command[] or CodeAction[] - -- - -- CodeAction - -- ... - -- edit?: WorkspaceEdit -- <- must be applied before command - -- command?: Command - -- - -- Command: - -- title: string - -- command: string - -- arguments?: any[] - -- - if action.edit then - util.apply_workspace_edit(action.edit) - end - if action.command then - local command = type(action.command) == 'table' and action.command or action - local fn = vim.lsp.commands[command.command] - if fn then - fn(command, ctx) - else - buf.execute_command(command) + ---@private + local function on_user_choice(action) + if not action then + return + end + -- textDocument/codeAction can return either Command[] or CodeAction[] + -- + -- CodeAction + -- ... + -- edit?: WorkspaceEdit -- <- must be applied before command + -- command?: Command + -- + -- Command: + -- title: string + -- command: string + -- arguments?: any[] + -- + if action.edit then + util.apply_workspace_edit(action.edit) + end + if action.command then + local command = type(action.command) == 'table' and action.command or action + local fn = vim.lsp.commands[command.command] + if fn then + fn(command, ctx) + else + buf.execute_command(command) + end end end + + vim.ui.select(result, { + prompt = 'Code actions:', + format_entry = function(action) + local title = action.title:gsub('\r\n', '\\r\\n') + return title:gsub('\n', '\\n') + end, + }, on_user_choice) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit |