xvzc / chezmoi.nvim

Chezmoi plugin for neovim
MIT License
73 stars 3 forks source link

Treat all files in `chezmoi source-path` as Chezmoi files #2

Closed cweagans closed 5 months ago

cweagans commented 7 months ago

Thanks for putting this together! I'm probably gonna start using this pretty regularly.

Would you be interested in a pull request to automatically treat anything in chezmoi source-path as a Chezmoi file? That way, I can chezmoi cd, then nvim ., start editing, and have stuff applied automatically. Right now, it only applies changes if I :ChezmoiEdit ~/path/to/my/file.

xvzc commented 7 months ago

Ah, yeah i've also thought about it, but i think we should just provide the callback function so that users can set it properly with nvim_create_autocmd. What do you think about this?

cweagans commented 7 months ago

That'd work. I suppose I don't care how automatic it is - just that there is a way to automatically sync whenever I edit stuff in my chezmoi dir.

xvzc commented 7 months ago

Can i do this for you then? I have been doing some refactoring and i think i can add this feature too.

cweagans commented 7 months ago

Sure! Thank you! Glad to test whenever!! :)

xvzc commented 7 months ago

I think it is almost done. Currently testing on refactor branch, and my configuration is now like below

require("chezmoi").setup({
    edit = {
        watch = true,
        force = true,
    },
    notification = {
        on_open = false,
        on_save = true,
        on_watch = true,
    },
})

vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
    pattern = { os.getenv("HOME") .. "/.local/share/chezmoi/*" },
    callback = function()
    -- invoke with vim.schedule() for better startup time
    vim.schedule(require("chezmoi.commands.__edit").watch)
    end,
})

Please test the feature on the refactor branch if you ever get a chance.

cweagans commented 7 months ago

It took me a bit to get back to this -- thanks for your patience! I tried this out in https://github.com/cweagans/dotfiles/commit/d67217254c2cd587e2d333dfe4cf97735da78cfb and it worked really really well! This is exactly what I had in mind and it's flexible enough for other people to define their own workflows too.

I think my only feedback would be to maybe have something in the plugin that runs chezmoi source-path in the background to get the Chezmoi source. That way, the autocommand could look something like this instead:

vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
    pattern = { require("chezmoi").source_path },
    callback = function()
    -- invoke with vim.schedule() for better startup time
    vim.schedule(require("chezmoi.commands.__edit").watch)
    end,
})
xvzc commented 7 months ago

Good point. I've actually also tried it, but i thought assigning the result of chezmoi.source_path to the autocmd pattern required more lines of code. Furthermore, it will make our neovim take slightly more time during the startup since chezmoi.source_path returns a list of exact filenames(absolute paths) rather than a pattern strings. It's true that we have to at least check if the file is really a chezmoi managed file, in that case we can do some additional check in watch() function like below; In this commit, i've used chezmoi.status.

https://github.com/xvzc/chezmoi.nvim/blob/4fbd33d7510e4a9697fa8b60181b9066b19399a1/lua/chezmoi/commands/__edit.lua#L44-L58

Please let me know if you got any suggestion or better idea!

xvzc commented 6 months ago

@cweagans Do you mind if i close this issue if you don't have any opinion?

cweagans commented 5 months ago

Not at all!