dmmulroy / ts-error-translator.nvim

MIT License
219 stars 4 forks source link

Can't make it work with lsp-zero #27

Closed mBaratta96 closed 4 months ago

mBaratta96 commented 4 months ago

Hello, I'm trying to integrate this plugin with my tsserver configuration in lsp-zero, but somehow the plugin doesn't show the translated diagnostics. What happens, once I open a tsx file, is that no diagnostic are shown (not even the ones of tsserver) . If I run the :LspRestart command, I see the diagnostics of tsserver only.

I use lazy.nvim and nvim 0.9.5.

My config:

{
        "dmmulroy/ts-error-translator.nvim",
                ft = {"typescript","typescriptreact"},
        config = function()
            require("ts-error-translator").setup()
        end,
    },

and my lsp-zero config:

{
        "neovim/nvim-lspconfig",
        cmd = { "LspInfo", "LspInstall", "LspStart" },
        event = { "BufReadPre", "BufNewFile" },
        dependencies = {
            { "hrsh7th/cmp-nvim-lsp" },
            { "williamboman/mason-lspconfig.nvim" },
            { "nvimtools/none-ls.nvim" },
            { "williamboman/mason.nvim" },
        },
        config = function()
            local lsp_zero = require("lsp-zero")
            local null_ls = require("confs.null-fs")

            null_ls.setLsp(lsp_zero)

            --lsp_confs.sign_columns()
            lsp_zero.on_attach(function(client, bufnr)
                -- see :help lsp-zero-keybindings
                -- to learn the available actions
                lsp_zero.default_keymaps({ buffer = bufnr, preserve_mappings = false })
                require("navigator.lspclient.mapping").setup({ bufnr = bufnr, client = client })
                vim.api.nvim_create_autocmd("CursorHold", {
                    buffer = bufnr,
                    callback = function()
                        local opts = {
                            focusable = false,
                            close_events = { "BufLeave", "CursorMoved", "InsertEnter", "FocusLost" },
                            border = "rounded",
                            source = "always",
                            prefix = " ",
                            scope = "cursor",
                        }
                        vim.diagnostic.open_float(nil, opts)
                    end,
                })
            end)
            lsp_zero.format_on_save({
                format_opts = {
                    async = false,
                    timeout_ns = 10000,
                },
                servers = {
                    ["ruff_lsp"] = { "python" },
                },
            })
            vim.keymap.set("n", "<leader>M", ":Mason<CR>", { noremap = true })
            require("mason").setup({})
            local lsp_server_conifg = require("confs.lspconfig")
            local handlers = table.merge_functions({ lsp_zero.default_setup }, lsp_server_conifg)

            require("mason-lspconfig").setup({
                ensure_installed = { "tsserver", "rust_analyzer" },
                handlers = handlers,
            })
        end,
    }

This is the result, after I run the LspRestart command:

ts

mBaratta96 commented 4 months ago

Forgot to specify that the table local handlers = table.merge_functions({ lsp_zero.default_setup }, lsp_server_conifg) contains the following setup for tsserver:

require("lspconfig").tsserver.setup({
        on_attach = function(client)
            client.server_capabilities.documentFormattingProvider = false
        end,
    })
dmmulroy commented 4 months ago

Thanks for opening the issue, and sorry you're running into this problem. Could you upload a small repo with a small neovim config using lsp-zero that this is reproducible 🙏

mBaratta96 commented 4 months ago

As I was preparing the repo, I noticed that the error doesn't occur if I remove the following line in the lsp-zero configuration:

require("navigator.lspclient.mapping").setup({ bufnr = bufnr, client = client })

After a bit of investigation, I think that navigator.lua overrides the diagnostics of your plugin. The following configuration fixes the problem:

{
        "dmmulroy/ts-error-translator.nvim",
        ft = { "typescript", "typescriptreact" },
        config = function()
            require("ts-error-translator").setup({ auto_override_publish_diagnostics = false })
        end,
}

{
        "neovim/nvim-lspconfig",
        cmd = { "LspInfo", "LspInstall", "LspStart" },
        event = { "BufReadPre", "BufNewFile" },
        dependencies = {
            { "hrsh7th/cmp-nvim-lsp" },
            { "williamboman/mason-lspconfig.nvim" },
            { "nvimtools/none-ls.nvim" },
            { "williamboman/mason.nvim" },
        },
        config = function()
            local lsp_zero = require("lsp-zero")
            local null_ls = require("confs.null-fs")

            null_ls.setLsp(lsp_zero)

            --lsp_confs.sign_columns()
            lsp_zero.on_attach(function(client, bufnr)
                -- see :help lsp-zero-keybindings
                -- to learn the available actions
                lsp_zero.default_keymaps({ buffer = bufnr, preserve_mappings = false })
                require("navigator.lspclient.mapping").setup({ bufnr = bufnr, client = client })
                -- CHANGE HERE
                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
                vim.api.nvim_create_autocmd("CursorHold", {
                    buffer = bufnr,
                    callback = function()
                        local opts = {
                            focusable = false,
                            close_events = { "BufLeave", "CursorMoved", "InsertEnter", "FocusLost" },
                            border = "rounded",
                            source = "always",
                            prefix = " ",
                            scope = "cursor",
                        }
                        vim.diagnostic.open_float(nil, opts)
                    end,
                })
            end)
            vim.lsp.set_log_level("debug")
            lsp_zero.format_on_save({
                format_opts = {
                    async = false,
                    timeout_ns = 10000,
                },
                servers = {
                    ["ruff_lsp"] = { "python" },
                },
            })
            vim.keymap.set("n", "<leader>M", ":Mason<CR>", { noremap = true })
            require("mason").setup({})
            local lsp_server_conifg = require("confs.lspconfig")
            local handlers = table.merge_functions({ lsp_zero.default_setup }, lsp_server_conifg)

            require("mason-lspconfig").setup({
                ensure_installed = { "tsserver", "rust_analyzer" },
                handlers = handlers,
            })
        end,
}

Thank you very much for your help. Excellent plugin.