jose-elias-alvarez / nvim-lsp-ts-utils

Utilities to improve the TypeScript development experience for Neovim's built-in LSP client.
The Unlicense
437 stars 18 forks source link

Formatting with `vim.lsp.buf.formatting_sync()` fails to write changes #32

Closed jacksonludwig closed 3 years ago

jacksonludwig commented 3 years ago

Using vim.lsp.buf.formatting_sync() on BufWritePost, it would be expected that the buffer would format and then save the changes.

However, I noticed that although the file is formatted correctly and neovim reports that the file is saved, reopening the file shows that the formatting changes were not actually saved. This did not occur with the earlier implementation of this plugin when it had a format_on_save option.

To reproduce, open a file that needs to be fixed by prettier, save it, and then do :e <filename> and observe that the format was not saved.

This is the config I used:

nvim_lsp.tsserver.setup {
  on_attach = function(client, bufnr)

    local ts_utils = require("nvim-lsp-ts-utils")

    -- defaults
    ts_utils.setup {
      debug = false,
      disable_commands = false,
      enable_import_on_completion = false,

      -- eslint
      eslint_enable_code_actions = true,
      eslint_enable_disable_comments = true,
      eslint_bin = "eslint_d",
      eslint_config_fallback = nil,

      -- eslint diagnostics
      eslint_enable_diagnostics = false,
      eslint_diagnostics_debounce = 250,

      -- formatting
      enable_formatting = true,
      formatter = "prettier",
      formatter_config_fallback = nil,

      -- parentheses completion
      complete_parens = false,
      signature_help_in_parens = false,

      -- update imports on file move
      update_imports_on_move = false,
      require_confirmation_on_move = false,
      watch_dir = nil,
    }

    -- required to enable ESLint code actions and formatting
    ts_utils.setup_client(client)

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

    vim.api.nvim_buf_set_keymap(bufnr, "n", "<space>tq", ":TSLspFixCurrent<CR>", {silent = true})
    vim.api.nvim_buf_set_keymap(bufnr, "n", "<space>tr", ":TSLspRenameFile<CR>", {silent = true})
    vim.api.nvim_buf_set_keymap(bufnr, "n", "<space>to", ":TSLspOrganize<CR>", {silent = true})
    vim.api.nvim_buf_set_keymap(bufnr, "n", "<space>ti", ":TSLspImportAll<CR>", {silent = true})
  end
}
jose-elias-alvarez commented 3 years ago

Have you installed and set up null-ls? Formatting and ESLint integrations now depend on it, and it requires a tiny bit of extra setup (see the formatting section of this project's README).

From your config, it seems possible that you're formatting the file with tsserver itself (the README also describes how to disable that). I was able to replicate the behavior you're describing by copy-pasting your setup and disabling null-ls, so I wonder if that's the issue.

jacksonludwig commented 3 years ago

Yep you're correct, with null-ls installed formatting does work. My mistake I did not realize the readme was updated. Thanks.

jose-elias-alvarez commented 3 years ago

No worries and thanks for the patience with the breaking change!