NvChad / NvChad

Blazing fast Neovim config providing solid defaults and a beautiful UI, enhancing your neovim experience.
https://nvchad.com/
GNU General Public License v3.0
24.75k stars 2.13k forks source link

Setup prettier/eslint installed inside every node project #2812

Closed caneta closed 6 months ago

caneta commented 6 months ago

I'm used to work with prettier/eslint installed per project instead the ones installed in the editor itself, because:

  1. every project could have a different prettier/eslint version
  2. every project could have a different prettier/eslint configuration
  3. the configuration is shared into the project repo to assure that every dev uses the same version/conf

Now, how to tell NVChad to do that?

I'm not sure on what to configure to achieve this: Mason? Conform? I need to install something else?

Is there any config snippet suggested?

Thanks

siduck commented 6 months ago

mason shouldnt be used here, because it downloads stuff globally for neovim and puts in a folder

i think you will achieve it by npm and for eslint, use lspconfig and for prettier, use conform. And make sure prettier/eslint isnt installed globally

caneta commented 5 months ago

No way to make project prettier work: I've got some hint here, but cannot find the executable: image

My current config inside lua/configs/conform.lua is the following:

local options = {
  formatters = {
    prettier = {
      command = "./node_modules/.bin/prettier",
    },
  },

  formatters_by_ft = {
    lua = { "stylua" },
    css = { "prettier" },
    scss = { "prettier" },
    html = { "prettier" },
    javascript = { "prettier" },
    javascriptreact = { "prettier" },
    typescript = { "prettier" },
    typescriptreact = { "prettier" },
  },

  format_on_save = {
    -- These options will be passed to conform.format()
    timeout_ms = 500,
    lsp_fallback = true,
  },
}

require("conform").setup(options)

Any suggestion?

siduck commented 5 months ago

@caneta i dont use prettier so cant help sorry! you could ask in conform repo

siduck commented 5 months ago

or r/neovim

caneta commented 5 months ago

I solved with some changes to NvChad/starter; here's a minimal example with prettier for css files:

lua/plugins/init.lua

{
  "stevearc/conform.nvim",
  event = "BufWritePre", -- uncommented
  cmd = "ConformInfo", -- otherwise ConformInfo don't show up until first <leader>fm
  config = function()
    require "configs.conform"
  end,
},

lua/configs/conform.lua

local options = {
  ft_parsers = { -- added
    css = "css",
  },

  formatters_by_ft = {
    css = { "prettier" }, --uncommented
  },

  format_on_save = { -- uncommented and changed
    -- These options will be passed to conform.format()
    timeout_ms = 999999, -- non way to make it work otherwise!
    lsp_fallback = true,
  },
}

lua/mappings.lua

-- overwriting the same lines in NvChad mapping file, in order to add timeout_ms
-- even here no way to make it work without it
map("n", "<leader>fm", function()
  require("conform").format({ timeout_ms = 999999, lsp_fallback = true })
end, { desc = "custom format files" })

I do not know why but the Conform async options (default to false) seems not to work properly here...

siduck commented 5 months ago

@caneta if it works the weird way then the plugin probably has a bug, you should make an issue there!

caneta commented 5 months ago

No bugs Conform side, the author just clarified me how to configure it better: https://github.com/stevearc/conform.nvim/issues/401

In the end I make it work with the following:

lua/plugins/init.lua

{
  "stevearc/conform.nvim",
  event = "BufWritePre",
  cmd = "ConformInfo",
  config = function()
    require "configs.conform"
  end,
},

lua/configs/conform.lua

local options = {
  formatters_by_ft = {
    css = { "prettier" },
  },

  format_after_save = {
    lsp_fallback = true,
  },
}

lua/mappings.lua

-- overwriting the same lines in NvChad mapping file, in order to add `async = true`
map("n", "<leader>fm", function()
  require("conform").format({ async = true, lsp_fallback = true })
end, { desc = "custom format files" })

Maybe the last one could be added to the core mappings of NvChad!

siduck commented 5 months ago

are there any cons of async?

caneta commented 5 months ago

Nope, as far as I know...