|
| 1 | +local cwd = vim.fn.getcwd() |
| 2 | +vim.opt.runtimepath:prepend(cwd) |
| 3 | + |
| 4 | +--[[ |
| 5 | +-- plugin name will be used to reload the loaded modules |
| 6 | +--]] |
| 7 | +local package_name = 'plugin_name' |
| 8 | + |
| 9 | +-- add the escape character to special characters |
| 10 | +local escape_pattern = function(text) |
| 11 | + return text:gsub('([^%w])', '%%%1') |
| 12 | +end |
| 13 | + |
| 14 | +-- unload loaded modules by the matching text |
| 15 | +local unload_packages = function() |
| 16 | + local esc_package_name = escape_pattern(package_name) |
| 17 | + |
| 18 | + for module_name, _ in pairs(package.loaded) do |
| 19 | + if string.find(module_name, esc_package_name) then |
| 20 | + package.loaded[module_name] = nil |
| 21 | + end |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 25 | +-- executes the run method in the package |
| 26 | +local run_action = function() |
| 27 | + vim.cmd.messages('clear') |
| 28 | + |
| 29 | + require(package_name).hello() |
| 30 | +end |
| 31 | + |
| 32 | +-- unload and run the function from the package |
| 33 | +function _G.Reload_and_run() |
| 34 | + unload_packages() |
| 35 | + run_action() |
| 36 | +end |
| 37 | + |
| 38 | +---@diagnostic disable-next-line: undefined-global |
| 39 | +local set_keymap = vim.api.nvim_set_keymap |
| 40 | + |
| 41 | +set_keymap('n', '<leader><leader>r', '<cmd>luafile dev/init.lua<cr>', {}) |
| 42 | +set_keymap('n', '<leader><leader>w', '<cmd>lua Reload_and_run()<cr>', {}) |
0 commit comments