MunifTanjim / prettier.nvim

Prettier plugin for Neovim's built-in LSP client.
MIT License
289 stars 9 forks source link

[Need help] Format on Save #17

Closed remy-poirier closed 2 years ago

remy-poirier commented 2 years ago

Hello, I am a front end developer and I want to try developing with NeoVim, I followed a tutorial, and I'm almost done, in reality the last thing I need to achieve is prettier config.

I think it only misses a really few configurations, because when I run :Prettier in a file, it formats the file correctly.

Here are some of my config files:

null-ls.rc.lua

local null_ls = require("null-ls")

null_ls.setup({
  on_attach = function(client, bufnr)
    if client.server_capabilities.documentFormattingProvider then
      vim.cmd("nnoremap <silent><buffer> <Leader>f :lua vim.lsp.buf.formatting()<CR>")

      -- format on save
      vim.cmd("autocmd BufWritePost <buffer> lua vim.lsp.buf.formatting()")
    end

    if client.server_capabilities.documentRangeFormattingProvider then
      vim.cmd("xnoremap <silent><buffer> <Leader>f :lua vim.lsp.buf.range_formatting({})<CR>")
    end
  end,
})

prettier.rc.lua

local prettier = require("prettier")

prettier.setup({
  bin = 'prettier', -- or `'prettierd'` (v0.22+)
  filetypes = {
    "css",
    "graphql",
    "html",
    "javascript",
    "javascriptreact",
    "json",
    "less",
    "markdown",
    "scss",
    "typescript",
    "typescriptreact",
    "yaml",
  }
})

I also put my lspconfig.rc.lua file in case it can be useful:

local status, nvim_lsp = pcall(require, 'lspconfig')
if (not status) then return end

local protocol = require('vim.lsp.protocol')

local on_attach = function(client, bufnr)
  -- formatting
  if client.server_capabilities.documentFormattingProvider then
    vim.api.nvim_command [[augroup Format]]
    vim.api.nvim_command [[autocmd! * <buffer>]]
    vim.api.nvim_command [[autocmd BufWritePre <buffer> lua vim.lsp.buf.format({bufnr = bufnr })]]
    vim.api.nvim_command [[augroup END]]
  end
end

nvim_lsp.tsserver.setup {
  on_attach = on_attach,
  filetypes = { "typescript", "typescriptreact", "typescript.tsx" },
  cmd = { "typescript-language-server", "--stdio" }
}

nvim_lsp.sumneko_lua.setup {
  on_attach = on_attach,
  settings = {
    Lua = {
      diagnostics = {
        -- Get the language server to recognize the 'vim' global
        globals = { 'vim' }
      },

      workspace = {
        -- Make the server aware of Neovim runtime files
        library = vim.api.nvim_get_runtime_file("", true),
        checkThirdParty = false
      }
    }
  }
}

Do you see something weird in my config files ? Really the last part I'm missing is to add :Prettier on save :D

MunifTanjim commented 2 years ago

The issue is that multiple LSP servers are capable to format the same filetype (e.g. both null-ls with prettier.nvim, and tsserver can format js/ts files). So in your case, tsserver is probably winning and null-ls (with prettier.nvim) is not formatting the file on save.

What you need is a script like this: https://github.com/MunifTanjim/dotfiles/blob/b8269d77fc8cc7a9693e8d95908ef299aa1550f6/private_dot_config/nvim/lua/config/lsp/custom/format.lua

And instead of using vim.lsp.buf.formatting or vim.lsp.buf.format, use that custom format function to set up formatting for the files.

remy-poirier commented 2 years ago

OK thanks, I will look at this !