feat: builtin - lua package reloader (#132)
This commit is contained in:
@@ -447,6 +447,46 @@ builtin.help_tags = function(opts)
|
|||||||
}):find()
|
}):find()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
builtin.reloader = function(opts)
|
||||||
|
opts = opts or {}
|
||||||
|
local package_list = vim.tbl_keys(package.loaded)
|
||||||
|
|
||||||
|
-- filter out packages we don't want and track the longest package name
|
||||||
|
opts.column_len = 0
|
||||||
|
for index, module_name in pairs(package_list) do
|
||||||
|
if type(require(module_name)) ~= 'table' or module_name:sub(1,1) == "_" or package.searchpath(module_name, package.path) == nil then
|
||||||
|
table.remove(package_list, index)
|
||||||
|
elseif #module_name > opts.column_len then
|
||||||
|
opts.column_len = #module_name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
pickers.new(opts, {
|
||||||
|
prompt = 'Packages',
|
||||||
|
finder = finders.new_table {
|
||||||
|
results = package_list,
|
||||||
|
entry_maker = make_entry.gen_from_packages(opts),
|
||||||
|
},
|
||||||
|
-- previewer = previewers.vim_buffer.new(opts),
|
||||||
|
sorter = sorters.get_generic_fuzzy_sorter(),
|
||||||
|
|
||||||
|
attach_mappings = function(prompt_bufnr, map)
|
||||||
|
local reload_package = function()
|
||||||
|
local selection = actions.get_selected_entry(prompt_bufnr)
|
||||||
|
|
||||||
|
actions.close(prompt_bufnr)
|
||||||
|
require('plenary.reload').reload_module(selection.value)
|
||||||
|
print(string.format("[%s] - module reloaded", selection.value))
|
||||||
|
end
|
||||||
|
|
||||||
|
map('i', '<CR>', reload_package)
|
||||||
|
map('n', '<CR>', reload_package)
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
}):find()
|
||||||
|
end
|
||||||
|
|
||||||
-- TODO: What the heck should we do for accepting this.
|
-- TODO: What the heck should we do for accepting this.
|
||||||
-- vim.fn.setreg("+", "nnoremap $TODO :lua require('telescope.builtin').<whatever>()<CR>")
|
-- vim.fn.setreg("+", "nnoremap $TODO :lua require('telescope.builtin').<whatever>()<CR>")
|
||||||
-- TODO: Can we just do the names instead?
|
-- TODO: Can we just do the names instead?
|
||||||
|
|||||||
@@ -331,4 +331,28 @@ function make_entry.gen_from_tagfile(opts)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function make_entry.gen_from_packages(opts)
|
||||||
|
opts = opts or {}
|
||||||
|
|
||||||
|
local make_display = function(module_name)
|
||||||
|
local path = package.searchpath(module_name, package.path) or ""
|
||||||
|
local display = string.format("%-" .. opts.column_len .. "s : %s", module_name, vim.fn.fnamemodify(path, ":~:."))
|
||||||
|
|
||||||
|
return display
|
||||||
|
end
|
||||||
|
|
||||||
|
return function(module_name)
|
||||||
|
local entry = {
|
||||||
|
valid = module_name ~= "",
|
||||||
|
entry_type = make_entry.types.GENERIC,
|
||||||
|
|
||||||
|
value = module_name,
|
||||||
|
ordinal = module_name,
|
||||||
|
}
|
||||||
|
entry.display = make_display(module_name)
|
||||||
|
|
||||||
|
return entry
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return make_entry
|
return make_entry
|
||||||
|
|||||||
Reference in New Issue
Block a user