stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
3.15k stars 161 forks source link

Question/Bug: I can't get conform to format my code properly with eslint/prettier. #291

Closed Saveljeffjonatan closed 8 months ago

Saveljeffjonatan commented 9 months ago

Neovim version (nvim -v)

0.9.5

Operating system/version

Macos Sonoma 14.3

Add the debug logs

Log file

Log file: /Users/quacksensei/.local/state/nvim/conform.log 19:16:12[DEBUG] Running LSP formatter on /Users/quacksensei/git/work/Phoenix/src/pages/Appointments/Appointments.tsx 19:16:14[DEBUG] Running LSP formatter on /Users/quacksensei/git/work/Phoenix/src/pages/Appointments/Appointments.tsx 19:16:15[DEBUG] Running LSP formatter on /Users/quacksensei/git/work/Phoenix/src/pages/Appointments/Appointments.tsx 19:16:16[DEBUG] Running LSP formatter on /Users/quacksensei/git/work/Phoenix/src/pages/Appointments/Appointments.tsx 19:17:18[DEBUG] Running LSP formatter on /Users/quacksensei/git/work/Phoenix/src/pages/Appointments/Appointments.tsx 19:17:28[DEBUG] Running LSP formatter on /Users/quacksensei/git/work/Phoenix/src/pages/Appointments/Appointments.tsx 19:18:17[DEBUG] Running LSP formatter on /Users/quacksensei/git/work/Phoenix/src/pages/Appointments/Appointments.tsx 19:18:18[DEBUG] Running LSP formatter on /Users/quacksensei/git/work/Phoenix/src/pages/Appointments/Appointments.tsx

Formatters for this buffer: LSP: tsserver trimwhitespace ready () /usr/bin/awk codespell unavailable: Command not found

Other formatters: gofmt ready (go) /usr/local/go/bin/gofmt goimports unavailable: Command not found prettier ready (javascript, typescript) /Users/quacksensei/git/work/Phoenix/node_modules/.bin/prettier prettierd unavailable: Command not found stylua ready (lua) /Users/quacksensei/.local/share/nvim/mason/bin/stylua

Describe the bug

Doesn't seem to listen to my eslint settings.

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

  1. Not sure, I could share my setup maybe?

Expected Behavior

Format according to prettier with eslint as linted format in mind

Minimal example file

No response

Minimal init.lua

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
    "stevearc/conform.nvim",
    config = function()
      require("conform").setup({
        log_level = vim.log.levels.DEBUG,
        -- add your config here
      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

Additional context

Screenshot 2024-02-07 at 19 24 33 Screenshot 2024-02-07 at 19 24 44

One is before save (proper syntax on my formatting) The one with errors is what happen when I do :w

Saveljeffjonatan commented 9 months ago

Update: might be related to how prettier formats the code. If I install Eslint LSP and do a EslintFixAll then the problem goes away and it formats correctly.

Saveljeffjonatan commented 8 months ago

Managed to solve this issue, turns our that I wasn't looking at typescriptreact but just typescript...

borasemiz commented 7 months ago

Managed to solve this issue, turns our that I wasn't looking at typescriptreact but just typescript...

For the documentation authors of this plugin: Is there any way to see what sort of file types are accepted? I mean, there are many ways to type "javascript":

With react too:

???

pkropachev commented 7 months ago

@Saveljeffjonatan, how did you solve your problem?

In my case I have two LSP: typescript-language-server (ts server) and client-lsp (eslint). In ConformInfo I see that tsserver LSP is used. So by pressing <leader>cf code formatting not in according to .eslintrc. But in case use EslintFixAll formatting works correctly.

scr1

I finally just set empty list of formatters for typescript and javascript. After that formatting works correct.

scr2

Saveljeffjonatan commented 7 months ago

I solved it by also adding typescriptreact and javascriptreact like this:

return {
  "stevearc/conform.nvim",
  event = { "BufReadPre", "BufNewFile" },
  config = function()
    local conf = require("conform")

    conf.setup({
      log_level = vim.log.levels.DEBUG,
      formatters_by_ft = {
        lua = { "stylua" },
        typescript = { { "prettierd", "prettier" } },
        typescriptreact = { { "prettierd", "prettier" } },
        javascript = { { "prettierd", "prettier" } },
        javascriptreact = { { "prettierd", "prettier" } },
        go = { "goimports", "gofmt" },

        ["*"] = { "codespell" },
        -- Use the "_" filetype to run formatters on filetypes that don't
        -- have other formatters configured.
        ["_"] = { "trim_whitespace" },
      },
    })

    vim.api.nvim_create_autocmd("BufWritePre", {
      pattern = "*",
      callback = function(args)
        require("conform").format({ bufnr = args.buf, lsp_fallback = true })
      end,
    })
  end,
}