OlegGulevskyy / better-ts-errors.nvim

MIT License
109 stars 5 forks source link

Possible to implement within lsp handler? #9

Open adamsitar opened 9 months ago

adamsitar commented 9 months ago

Describe the problem

For example, the plugin davidosomething/format-ts-errors.nvim does something similar, but rather than triggering a new buffer/window with a shortcut, they instruct the user to overwrite the existing "textDocument/publishDiagnostics" LSP handler, in order to format the actual LSP error hover. I think having an option to use existing formatting code in this way would be great.

OlegGulevskyy commented 9 months ago

I see what you mean, but I am not sure how useful this is or why. If I understand it right, with such approach you wouldn't be able to have a highlighted variable, for example. Yes we can break the message into multiple lines + format some objects or types into prettified code, but that's it. It would look like exactly what is looks in the repo mentioned.

adamsitar commented 9 months ago

I see what you mean, but I am not sure how useful this is or why. If I understand it right, with such approach you wouldn't be able to have a highlighted variable, for example. Yes we can break the message into multiple lines + format some objects or types into prettified code, but that's it. It would look like exactly what is looks in the repo mentioned.

No no no that's not what I meant, your formatting code is better (highlighting etc) than theirs, but theirs directly overwrites the default LSP handlers when you go to error, meaning you don't have to open the error message in a new buffer/window.

Here's the LSP setup, using the format-ts-errors plugin:

local lspconfig = require("lspconfig")
lspconfig.tsserver.setup({
  handlers = {
    ["textDocument/publishDiagnostics"] = function(
      _,
      result,
      ctx,
      config
    )
      if result.diagnostics == nil then
        return
      end

      -- ignore some tsserver diagnostics
      local idx = 1
      while idx <= #result.diagnostics do
        local entry = result.diagnostics[idx]

        local formatter = require('format-ts-errors')[entry.code]
        entry.message = formatter and formatter(entry.message) or entry.message

        -- codes: https://github.com/microsoft/TypeScript/blob/main/src/compiler/diagnosticMessages.json
        if entry.code == 80001 then
          -- { message = "File is a CommonJS module; it may be converted to an ES module.", }
          table.remove(result.diagnostics, idx)
        else
          idx = idx + 1
        end
      end

      vim.lsp.diagnostic.on_publish_diagnostics(
        _,
        result,
        ctx,
        config
      )
    end,
  },
}))

How could I use your formatting code inside the overwrite for the LSP handler?

Sorry, I'm not very good at lua and don't know how the LSP works internally, but I'm guessing that these two lines are handling the formatting of the error by overwriting the entry

local formatter = require('format-ts-errors')[entry.code]
entry.message = formatter and formatter(entry.message) or entry.message

So I guess my question is, how could I use your better formatter instead of theirs here?

Btw thanks for taking the time and replying to my issue :heart: