LoginSignup
1
0

Neovimでprettierのフォーマットをかけると謎の差分が出る問題

Last updated at Posted at 2024-03-14

問題

Neovimでnune-lsを通してprettierでフォーマットを書ける設定を書いた

map("n", "<C-f>", "<CMD>lua vim.lsp.buf.format({ async = false })<CR><CMD>w<CR>", opts)

しかしCLIでprettierコマンドを使った時と謎の差分が出てしまっていた
スクリーンショット 2024-03-08 13.44.15.png
.prettierrcが読み込まれていないか,Neovimで実行されるprettierとCLIで実行されるprettierのバージョンが同じか等を調査したが解決できなかった

原因

Masonでインストールしていたtypescript-language-serverがprettierのフォーマット後に実行を行っていたのが原因だった

解決策

typescript-language-serverのフォーマット機能をオフにするだけです
自分の環境ではMasonを使っているのでmason-lspconfigで設定を行っています

  {
    "williamboman/mason-lspconfig.nvim",
    config = function()
      local lspconfig = require("lspconfig")
      require("mason-lspconfig").setup_handlers({
        tsserver = function()
          lspconfig.tsserver.setup({
            on_attach = function(client, bufnr)
              client.server_capabilities.documentFormattingProvider = false
              client.server_capabilities.documentRangeFormattingProvider = false
            end,
          })
        end,
      })
    end,
  },

終わりに

原因に気づくまでに時間がかかったので同じように困っている人の助けになれば幸いです

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0