lukas-reineke / lsp-format.nvim

A wrapper around Neovims native LSP formatting.
559 stars 27 forks source link

`Format sync` is returning async #84

Closed TechIsCool closed 4 months ago

TechIsCool commented 4 months ago

After a lot of trail and error I determined that the suggested command is returning async vim.cmd [[cabbrev wq execute "Format sync" <bar> wq]]

When this command is used it causes issues in a couple places

  1. When you search for wq find it breaks because it auto expands
  2. When you use wq on the last buffer nvim quits before the returned command

To determine the async problem I built a quick and dirty mills time r

function epoch_ms()
    local s, ns = vim.loop.gettimeofday()
    return s * 1000 + math.floor(ns / 1000)
end

Then ran a bunch of commands to understand what happened

:execute "lua print(epoch_ms())" | Format sync | execute "lua print(epoch_ms())"
1712103251379
1712103251383
:execute "lua print(epoch_ms())" | Format | execute "lua print(epoch_ms())"
1712103301293
1712103301295
:execute "lua print(epoch_ms())" | execute "lua print(vim.lsp.buf.format())" | execute "lua print(epoch_ms())"
1712103328159
1712103328221

Currently I have defined the following which does work but it feels very much like I am bypassing lsp-format and just using the built-ins.

vim.api.nvim_create_autocmd("BufWritePre", {
callback = function()
    require("lsp-format").format({ sync = true })
    -- vim.lsp.buf.format(). -- bypasses lsp-format
end
})

Any help would be wonderful as I do like using ZZ and wq to format before exit

lukas-reineke commented 4 months ago

I need to write proper tests for this...

Obvious bug, should be fixed now.