ecosse3/nvim

Formatter not working properly

Charly-T opened this issue · 3 comments

Maybe its a misconfiguration on my side or a wierd config on my project, but formatting is not working as expected.

My project is using eslint for formatting with eslint-config-prettier to disable all the eslint rules that prettier can handle and eslint-plugin-prettier to format following the .prettierrc located on the root.

The prettier config sets useTabs to true, but when I try to format, it replaces tabs with spaces and then the linter complains about the spaces.

At the beginning I thought it was caused by the monorepo I'm using. Maybe is trying to find a .prettierrc close to the package package.json instead of the root folder, but afterwards I realized that is not working even on the js files on the root.

Any insights?

Hi, I was experiencing similar issue recently and it's probably due to some LSP/Eslint configuration. I use :EslintFixAll command in project with eslint and it works as expected.

Can you check if that works for you?

On my local env I've modified EcoVim format on save to choose eslint formatting if eslint lsp is active:

lua/lsp/functions.lua

function M.enable_format_on_save()
    local group = vim.api.nvim_create_augroup("format_on_save", { clear = false })
    vim.api.nvim_create_autocmd("BufWritePre", {
        callback = function()
            local root_dir = vim.fn.getcwd()  -- Adjust this if you have a more accurate way to find the project root, maybe via git?
            local eslintrc_json = root_dir .. "/.eslintrc.json"
            local eslintrc_js = root_dir .. "/.eslintrc.js"

            -- Check if eslint LSP is active
            local active_clients = vim.lsp.buf_get_clients()
            local eslint_is_active = false

            for _, client in ipairs(active_clients) do
                if client.name == "eslint" then
                    eslint_is_active = true
                    break
                end
            end

            if eslint_is_active and (vim.fn.filereadable(eslintrc_json) == 1 or vim.fn.filereadable(eslintrc_js) == 1) then
                vim.cmd("EslintFixAll")
            else
                vim.lsp.buf.format()
            end
        end,
        group = group,
    })
    require("notify")("Enabled format on save", "info", { title = "LSP", timeout = 2000 })
end

Please let me know if that works for you. I am trying to get vim.lsp.buf.format() to work with ESLint but no success for now.

Worked like a charm, although I also think that eslint should work as a formatter. I couldnt make It work either.

@Charly-T I will close it for now. I'm testing formatter & linter now and I'll make sure to make it work correctly with ESLint.