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 <vim.lua:281>


Solution:

This is a follow-up to the work done in
6c03601e3a.
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.
This commit is contained in:
Chris Kipp 2021-09-24 03:20:10 +02:00 committed by GitHub
parent be93821647
commit 433bda405e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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