Skip to content

Instantly share code, notes, and snippets.

@windwp
Created April 18, 2021 06:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save windwp/c2b91ab679a66efeb489a359291c2719 to your computer and use it in GitHub Desktop.
Save windwp/c2b91ab679a66efeb489a359291c2719 to your computer and use it in GitHub Desktop.
livereload for plugin development on lua
#Neovim Trick
Live Reload on plugin development and init.lua
# Demo
# How
* [ ] code
``` lua
if _G._require == nil then
if _G.__is_dev then
_G._require = require
_G.require = function(path)
if string.find(path, '^spectre[^_]*$') ~= nil then
plenary.reload_module(path)
end
return _G._require(path)
end
end
end
```
* [ ] use state when reload
``` lua
if _G.__is_dev then
_G.__spectre_state = _G.__spectre_state or state
state = _G.__spectre_state
end
```
* [ ] limited
You can't require("B") from A file
then in file B you require("A").
It will make a loop require
# automatic do it with nvim-projectconfig
# Use it in your init.lua
create a import function it will wrap require
function
``` lua
_G.import = function (path)
if _G.__is_dev then
return R(path)
else
local check, detail = pcall(require,path)
if check then
return detail
else
print("Import module error: " .. path)
error(debug.traceback(detail))
end
end
end
-- https://github.com/nvim-lua/plenary.nvim/blob/master/lua/plenary/reload.lua
local function reload_module(module_name, starts_with_only)
-- TODO: Might need to handle cpath / compiled lua packages? Not sure.
local matcher
if not starts_with_only then
matcher = function(pack)
return string.find(pack, module_name, 1, true)
end
else
matcher = function(pack)
return string.find(pack, '^' .. module_name)
end
end
for pack, _ in pairs(package.loaded) do
if matcher(pack) then
package.loaded[pack] = nil
end
end
end
_G.R = function(name)
reload_module(name)
return require(name)
end
--import('yourdotfile')
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment