Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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