Sometimes, I need to apply small patches to a plugin to fulfill a very niche use case or to fix something without waiting for the PR to reach upstream. However, when doing so, Lazy can't sync the repo because there are local changes.
This small plugin addresses this issue by automatically applying the patches through git commands (if possible) and reverting them before Lazy starts its lazy magic.
Before:
After:
return {
"polirritmico/lazy-local-patcher.nvim",
config = true,
ft = "lazy", -- for lazy loading
}
Create the patches
directory or the plugin will complain about the missing
dir: (default path)
$ mkdir ~/.config/nvim/patches
Here you could add your patches. Two considerations:
- Only one file per plugin.
- The name of the patch should match the repository name. (More precisely, the
directory name inside the Lazy root folder). e.g.:
nvim-treesitter.patch
Custom folders could be passed to the setup
function:
require("lazy-local-patcher").setup({
lazy_path = "/custom/root/lazy/path", -- directory where lazy install the plugins
patches_path = "/custom/patch/path", -- directory where diff patches files are stored
})
Lazy local patcher comes with the following defaults:
local defaults = {
lazy_path = vim.fn.stdpath("data") .. "/lazy", -- directory where lazy install the plugins
patches_path = vim.fn.stdpath("config") .. "/patches", -- directory where diff patches files are stored
}
Patches are applied using:
git -C <plugin_path_in_Lazy_root> apply --ignore-space-change <patch>
Example of patch creation:
cd .local/share/nvim/lazy/nvim-treesitter
nvim edit/some/file
git diff | tee ~/.config/nvim/patches/nvim-treesitter.patch
Now enter into Nvim and sync the plugin with Lazy.
You could use apply_all
or restore_all
functions to manually apply/restore
all patches inside the patches-path
folder:
:lua require("lazy-local-patcher").apply_all()
[patches: nvim-treesitter.patch] Applying patch...
[patches: nvim-treesitter.patch] Done
:lua require("lazy-local-patcher").restore_all()
[patches: nvim-treesitter.patch] Restoring plugin repository...
[patches: nvim-treesitter.patch] Done
While this plugin is primarily designed for my personal use and tailored to a very specific use case, suggestions, issues, or pull requests are very welcome.
Enjoy