nvimdev / lspsaga.nvim

improve neovim lsp experience
MIT License
3.42k stars 286 forks source link

Lightbulb sends ill-formated code action requests #1478

Closed gregorias closed 1 month ago

gregorias commented 1 month ago

Describe the bug

Since https://github.com/nvimdev/lspsaga.nvim/commit/bbe032ff1c40e9b695f8a5de9c779ee558dddf4f, Lspsaga sends ill-formatted code action requests causing errors in some language servers, e.g., Haskell Language Server:

LSP[hls] Error condition, please check your setup and/or the [issue tracker](https://github.com/haskell/haskell-language-server/issues): 
LSP: incoming message parse error:
Error in $.params.context.diagnostics[0]: key "range" not found
when processing
{
  "jsonrpc": "2.0",
  "method": "textDocument/codeAction",
  "id": 31,
  "params": {
    "textDocument": {
      "uri": "file:///Users/grzesiek/Code/findata/transcoder/test/Test/Transcoder/CharlesSchwab.hs"
    },
    "context": {
      "diagnostics": [
        {
          "lnum": 19,
          "bufnr": 1,
          "namespace": 37,
          "end_col": 29,
          "end_lnum": 34,
          "col": 37,
          "severity": 1,
          "source": "typecheck",
          "message": "• \"Could not parse :\\n2:68:\\n  |\\n2 |   REDACTED",
          "user_data": {
            "lsp": []
          }
        }
      ]
    },
    "range": {
      "start": {
        "line": 21,
        "character": 0
      },
      "end": {
        "line": 21,
        "character": 0
      }
    }
  }
}

The issue is that range is missing from params.context.diagnostics. It’s a bug, because the protocol requires it.

The problem stems from the plugin copying the table from vim.diagnostic.get verbatim. vim.diagnostic.get promises to return vim.Diagnostic, which is not a valid LSP Diagnostic.

A fix could be restructuring the return value of vim.diagnostic.get into a valid format.

Steps to reproduce

I believe the description describes the cause and how to fix it well.

Expected behavior

I believe the description describes the cause and how to fix it well.

Neovim version (nvim -v)

0.10.1

lspsaga commit

bbe032ff1c40e9b695f8a5de9c779ee558dddf4f

Terminal name/version

kitty

CharlesEkkel commented 1 month ago

FYI @glepnir I'm still seeing this issue in the latest commit a8b24b2:

image

LSP[haskell-tools.nvim] Error condition, please check your setup and/or the [issue tracker](https://github.com/haskell/haskell-language-server/issues): LSP: incoming message parse error: Error in $.params.context.diagnostics[0]: key "range" not found when processing {"params":{"range":{"end":{"line":11,"character":0},"start":{"line":11,"character":0}},"textDocument":{"uri":"file:\/\/\/home\/me\/ projects\/myproject\/src\/MyLib.hs"},"context":{"diagnostics":[{"code":"-Wunused-matches"}]}},"method":"textDocument\/codeAction","jsonrpc ":"2.0","id":17} Press ENTER or type command to continue

And I've verified that this issue disappears when I use a commit before bbe032f (fix: deprecate and add use_nerd)

glepnir commented 1 month ago

what output of vim.diagnostic.get

CharlesEkkel commented 1 month ago

Yeah sure, when I run :lua print(vim.inspect(vim.diagnostics.get())) in a haskell file where I'm seeing this issue (and without changing that cursor position) I see this:

{ { _tags = { unnecessary = true }, bufnr = 1, code = "-Wunused-matches", col = 12, end_col = 15, end_lnum = 12, lnum = 12, message = "Defined but not used: ‘env’", namespace = 33, severity = 2, source = "typecheck", user_data = { lsp = { code = "-Wunused-matches" } } }, { _tags = { unnecessary = true }, bufnr = 11, code = "-Wunused-imports", col = 0, end_col = 29, end_lnum = 15, lnum = 15, message = "The import of ‘Data.Foldable’ is redundant\n except perhaps to import instances from ‘Data.Foldable’\nTo import instances alone, use: import Data.Foldable()", namespace = 33, severity = 2, source = "typecheck", user_data = { lsp = { code = "-Wunused-imports" } } } }

glepnir commented 1 month ago

I have update a check of range . does it works now ?

CharlesEkkel commented 1 month ago

Oh and this is what I get from :lua print(vim.inspect(vim.lsp.diagnostics.get_line_diagnostics())), in case that helps.

{ { code = "-Wunused-matches", message = "Defined but not used: ‘env’", range = { ["end"] = { character = 15, line = 12 }, start = { character = 12, line = 12 } }, severity = 2, source = "typecheck", tags = { 1 } } }

But I just saw your comment! Thanks, yes that has made the error go away! Thank you @glepnir.

CharlesEkkel commented 1 month ago

Might also be worth checking if lnum = row + 1 is correct here, I was seeing the error pop up when I reached the line before the code with the warning.

Could be a coincidence, but perhaps vim.diagnostics.get considers row numbers to start from 0? I tried checking the docs but they don't say.

glepnir commented 1 month ago

update