diff options
| author | Senghan Bright <senghan.bright@deltaprojects.com> | 2020-10-04 15:08:05 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-04 09:08:05 -0400 |
| commit | 054bd34498e3e6cfef8f8332fe8de4c0463914dd (patch) | |
| tree | 6a850906dbf3e440b527adf481c1f40320845524 /lua | |
| parent | ce66c1f78c87a2dcc8cb286d5536365b54d7e3ee (diff) | |
feat: builtin - lua package reloader (#132)
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/telescope/builtin.lua | 40 | ||||
| -rw-r--r-- | lua/telescope/make_entry.lua | 24 |
2 files changed, 64 insertions, 0 deletions
diff --git a/lua/telescope/builtin.lua b/lua/telescope/builtin.lua index 9f339a1..6d74e28 100644 --- a/lua/telescope/builtin.lua +++ b/lua/telescope/builtin.lua @@ -447,6 +447,46 @@ builtin.help_tags = function(opts) }):find() 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. -- vim.fn.setreg("+", "nnoremap $TODO :lua require('telescope.builtin').<whatever>()<CR>") -- TODO: Can we just do the names instead? diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index c62df79..8c598cd 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -331,4 +331,28 @@ function make_entry.gen_from_tagfile(opts) 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 |
