nvimdev / guard.nvim

async fast minimalist plugin make format easy in neovim
MIT License
448 stars 24 forks source link

Format on save not working - Manual formatting works #148

Closed BennyThadikaran closed 5 months ago

BennyThadikaran commented 5 months ago

Running :GuardFmt works but saving the file does not run formatter.

Running :LspInfo shows python as detected filetype and lsp (pyright) has been loaded. Also made sure guard.nvim and guard-collection has been loaded.

Nvim version

NVIM v0.10.0-dev-2384+g848fc8ede Build type: RelWithDebInfo LuaJIT 2.1.1707061634

Below is my basic config using Lazy as my plugin manager.

return {
    "nvimdev/guard.nvim",
    event = "LspAttach",

    dependencies = {
        "nvimdev/guard-collection",
    },
    config = function()
        local ft = require("guard.filetype")

        ft("python"):fmt({
            cmd = "black",
            args = { "--quiet", "-l", "80", "-" },
            stdin = true,
        })

        require("guard").setup({
            fmt_on_save = true,
            lsp_as_default_formatter = true,
        })
    end,
}
xiaoshihou514 commented 5 months ago

Cannot repro with the following:

vim.opt.rtp:append("/tmp/guard.nvim")
vim.opt.rtp:append("/tmp/guard-collection.nvim")
local ft = require("guard.filetype")

ft("python"):fmt({
    cmd = "black",
    args = { "--quiet", "-l", "80", "-" },
    stdin = true,
})

require("guard").setup({
    fmt_on_save = true,
    lsp_as_default_formatter = true,
})
cd /tmp
git clone https://github.com/nvimdev/guard.nvim
git clone https://github.com/nvimdev/guard-collection
vi -u test.lua repro.py
print(      "foo") # gets formatted on :w

Could you show the output of :au BufWritePre? @BennyThadikaran

BennyThadikaran commented 5 months ago

Thanks for your time. Below is the requested output.

:au BufWritePre                                                                                                              
--- Autocommands ---                                                                                                                   
lsp_c_1_b_1_save  BufWritePre                                                                                                          
    <buffer=1>                                                                                                                         
              <Lua 201: /tmp/.mount_nvim.aKXqU8Q/usr/share/nvim/runtime/lua/vim/lsp.lua:793> [vim.lsp: textDocument/willSave]

I removed most of my plugins to make sure they weren't interfering with Guard.nvim. Still no good. :GuardFmt works though. Screenshot from 2024-05-25 21-24-44

xiaoshihou514 commented 5 months ago

Ah, I believe I know the issue. Guard registers a FileType autocmd after you call setup(), but since you are using LspAttach it likely happened after you entered the python file and hence it did not get properly triggered. Since you are not using lsp formatting (for python at least), why not just set the event to be VimEnter or UIEnter? Keep in mind that almost all plugins are not expected to do anything before you call setup(), which arguably is not the best design but it's what we get :)

BennyThadikaran commented 4 months ago

Removed the event option and set lazy = false to disable lazy loading. All working perfectly now. Thank you!

Your setup was simple and very intuitive.