From 433bda405e1fa82db23660a73f275cac5ecca95e Mon Sep 17 00:00:00 2001 From: Chris Kipp Date: Fri, 24 Sep 2021 03:20:10 +0200 Subject: fix(lsp): guard textDocument/codeAction command logic #15769 Problem: Error executing vim.schedule lua callback: ...ovim/HEAD-aba3979/share/nvim/runtime/lua/vim/lsp/buf.lua:502: command: expected string, got nil stack traceback: ...ovim/HEAD-aba3979/share/nvim/runtime/lua/vim/lsp/buf.lua:502: in function 'execute_command' ...HEAD-aba3979/share/nvim/runtime/lua/vim/lsp/handlers.lua:151: in function <...HEAD-aba3979/share/nvim/runtime/lua/vim/lsp/handlers.lua:113> ...ovim/HEAD-aba3979/share/nvim/runtime/lua/vim/lsp/buf.lua:465: in function 'callback' ...r/neovim/HEAD-aba3979/share/nvim/runtime/lua/vim/lsp.lua:1325: in function 'handler' ...r/neovim/HEAD-aba3979/share/nvim/runtime/lua/vim/lsp.lua:899: in function 'cb' vim.lua:281: in function Solution: This is a follow-up to the work done in https://github.com/neovim/neovim/commit/6c03601e3adb4c3c4d47f148df8df20401b88677. There are valid situations where a `textDocument/codeAction` is returned without a command, since a command in optional. For example from Metals, the Scala language server when you get a code action to add a missing import, it looks like this: ```json Result: [ { "title": "Import \u0027Instant\u0027 from package \u0027java.time\u0027", "kind": "quickfix", "diagnostics": [ { "range": { "start": { "line": 6, "character": 10 }, "end": { "line": 6, "character": 17 } }, "severity": 1, "source": "bloop", "message": "not found: value Instant" } ], "edit": { "changes": { "file:///Users/ckipp/Documents/scala-workspace/sanity/src/main/scala/Thing.scala": [ { "range": { "start": { "line": 6, "character": 10 }, "end": { "line": 6, "character": 17 } }, "newText": "Instant" }, { "range": { "start": { "line": 1, "character": 0 }, "end": { "line": 1, "character": 0 } }, "newText": "\nimport java.time.Instant\n" } ] } } } ] ``` This change just wraps the logic that grabs the command in a conditional to skip it if there is no command. --- runtime/lua/vim/lsp/handlers.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'runtime/lua/vim/lsp/handlers.lua') diff --git a/runtime/lua/vim/lsp/handlers.lua b/runtime/lua/vim/lsp/handlers.lua index c2f2b870f7..624f8b5462 100644 --- a/runtime/lua/vim/lsp/handlers.lua +++ b/runtime/lua/vim/lsp/handlers.lua @@ -143,12 +143,14 @@ M['textDocument/codeAction'] = function(_, result, ctx) if action.edit then util.apply_workspace_edit(action.edit) end - 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) + 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 -- cgit