dmmulroy / ts-error-translator.nvim

MIT License
226 stars 5 forks source link

Question: Typescript-tools nvim integration #19

Closed andrew-t-james-core closed 6 months ago

andrew-t-james-core commented 6 months ago

Any insights on integrations for https://github.com/pmizio/typescript-tools.nvim. I tried overriding textDocument/publishDiagnostics as described in the documentation but unfortunately could not see the formatted error message. Curious if there is work to make this happen, happy to contribute if so.

For example this is th outpuf of a tsserver error diagnostics message before and after attempting to connect this plugin.

tsserver: Argument of type '{ completionUnit: CompletionUnits; completionAmmount: CompletionUnits; }' is not assignable to parameter of type '{ completionUnit?: CompletionUnits; }'.
  Object literal may only specify known properties, but 'completionAmmount' does not exist in type '{ completionUnit?: CompletionUnits; }'. Did you mean to write 'completionUnit'? [2345]
kohane27 commented 6 months ago

I was wondering the same thing too. The following doesn't work:

return {
  "pmizio/typescript-tools.nvim",
  config = function()
    require("typescript-tools").setup({
      -- disable formatting because using `prettier`
      on_attach = function(client, bufnr)
        client.server_capabilities.documentFormattingProvider = false
        client.server_capabilities.documentRangeFormattingProvider = false
      end,
      handlers = {
        ["textDocument/publishDiagnostics"] = require("ts-error-translator").lsp_publish_diagnostics_override(),
      },
    })
  end,
}
dmmulroy commented 6 months ago

Hi, thanks for report and sorry for the issue - just merged a change that you can see here that should help with this: https://github.com/dmmulroy/ts-error-translator.nvim/pull/18

Give it a try with this new api:

vim.lsp.handlers["textDocument/publishDiagnostics"] = function(err, result, ctx, config)
  require("ts-error-translator").translate_diagnostics(err, result, ctx, config)
  vim.lsp.diagnostic.on_publish_diagnostics(err, result, ctx, config)
end
andrew-t-james-core commented 6 months ago

thanks for taking a look unfortunately that didn't seem to resolve.

require("typescript-tools").setup({
  on_attach = function(client)
    client.server_capabilities.documentFormattingProvider = false
    client.server_capabilities.documentRangeFormattingProvider = false
    client.server_capabilities.documentFormattingProvider = false
    client.server_capabilities.documentRangeFormattingProvider = false
    client.server_capabilities["textDocument/publishDiagnostics"] = function(err, result, ctx, config)
      require("ts-error-translator").translate_diagnostics(err, result, ctx, config)
      vim.lsp.diagnostic.on_publish_diagnostics(err, result, ctx, config)
    end
  end,
  settings = {
    -- array of strings("fix_all"|"add_missing_imports"|"remove_unused"|
    -- "remove_unused_imports"|"organize_imports") -- or string "all"
    -- to include all supported code actions
    -- specify commands exposed as code_actions
    expose_as_code_action = { "fix_all", "add_missing_imports", "remove_unused" },
    -- spawn additional tsserver instance to calculate diagnostics on it
    separate_diagnostic_server = true,
    -- "change"|"insert_leave" determine when the client asks the server about diagnostic
    publish_diagnostic_on = "insert_leave",
    -- specify a list of plugins to load by tsserver, e.g., for support `styled-components`
    -- (see 💅 `styled-components` support section)
    tsserver_plugins = {},
    -- this value is passed to: https://nodejs.org/api/cli.html#--max-old-space-sizesize-in-megabytes
    -- memory limit in megabytes or "auto"(basically no limit)
    tsserver_max_memory = "auto",
    -- CodeLens
    -- WARNING: Experimental feature also in VSCode, because it might hit performance of server.
    -- possible values: ("off"|"all"|"implementations_only"|"references_only")
    code_lens = "off",
    -- by default code lenses are displayed on all referencable values and for some of you it can
    -- be too much this option reduce count of them by removing member references from lenses
    disable_member_code_lens = true,
    -- described below
    tsserver_format_options = {
      allowIncompleteCompletions = true,
      allowRenameOfImportPath = true,
    },
    tsserver_file_preferences = {
      includeCompletionsForModuleExports = true,
      includeInlayParameterNameHints = "all",
      includeInlayParameterNameHintsWhenArgumentMatchesName = true,
      includeInlayFunctionParameterTypeHints = true,
      includeInlayVariableTypeHints = true,
      includeInlayVariableTypeHintsWhenTypeMatchesName = true,
      includeInlayPropertyDeclarationTypeHints = true,
      includeInlayFunctionLikeReturnTypeHints = true,
      includeInlayEnumMemberValueHints = true,
      quotePreference = "auto",
    },
    handlers = {
      -- because I don't want to format on save via typescript-tools
      -- prefer other tools over typescript formatting
      ["textDocument/formatting"] = false,
      ["textDocument/rangeFormatting"] = false,
      ["textDocument/publishDiagnostics"] = function(err, result, ctx, config)
        require("ts-error-translator").translate_diagnostics(err, result, ctx, config)
        vim.lsp.diagnostic.on_publish_diagnostics(err, result, ctx, config)
      end,
    },
  },
})
dmmulroy commented 6 months ago

Could you see if #22 resolved this? It should hopefully Just Work™️ without extra config now as diagnostics will get passed onto it internally as a support LSP

filahf commented 6 months ago

I can confirm that it now works fine without any extra config!

kohane27 commented 6 months ago
return {
  "dmmulroy/ts-error-translator.nvim",
  config = true,
}

2024-02-27-20-52-

Awesome! Thank you!

andrew-t-james-core commented 6 months ago

Got closer this round, thanks for looking into this so quickly as well. Unfortunately couldn't override the existing lsp diagnostics and hit an error with the translation.

tsserver: Argument of type '{ completionUnit: CompletionUnits; completionAmmount: any; }' is not assignable to parameter of type '{ completionUnit?: CompletionUnits; }'.
  Object literal may only specify known properties, but 'completionAmmount' does not exist in type '{ completionUnit?: CompletionUnits; }'. Did you mean to write 'completionUnit'?

TypeScript Error Translation(s):
  • Something went wrong while translating your error. Please file an issue at https://github.com/dmmulroy/ts-error-translator.nvim and an example of the code that caused this error.
 [2345]
dmmulroy commented 6 months ago

This likely a separate bug related to #17 that I'm actively working on, so I hopefully soon will have fix up 🤞