diff options
| author | Senghan Bright <senghan.bright@deltaprojects.com> | 2020-12-02 00:27:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-02 00:27:54 +0100 |
| commit | 8546fdf610584c367f0f1e105aedec370d71bf54 (patch) | |
| tree | 6d57d1fd4882655b64bb26b363f4667ce56458b9 /lua/telescope/builtin/internal.lua | |
| parent | b1d436ce9247b13b83590652d905e45749a0bdd5 (diff) | |
new builtin - autocommands (#302)
* feat: new builtin - Autocommands finder
* fix: remove decorators to avoid confusion.
* make preview split same hl-group as border
* use highlight instead of marker character for preview selection hl
Diffstat (limited to 'lua/telescope/builtin/internal.lua')
| -rw-r--r-- | lua/telescope/builtin/internal.lua | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index c50e04b..73db64d 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -569,6 +569,96 @@ internal.highlights = function(opts) }):find() end +internal.autocommands = function(opts) + local autocmd_table = {} + + local pattern = {} + pattern.BUFFER = "<buffer=%d+>" + pattern.EVENT = "[%a]+" + pattern.GROUP = "[%a%d_:]+" + pattern.INDENT = "^%s%s%s%s" -- match indentation of 4 spaces + + local event, group, ft_pat, cmd, source_file, source_lnum + local current_event, current_group, current_ft + + local cmd_output = vim.fn.execute("verb autocmd *", "silent") + for line in cmd_output:gmatch("[^\r\n]+") do + -- capture group and event + group, event = line:match("^(" .. pattern.GROUP .. ")%s+(" .. pattern.EVENT .. ")") + -- ..or just an event + if event == nil then + event = line:match("^(" .. pattern.EVENT .. ")") + end + + if event then + group = group or "<anonymous>" + if event ~= current_event or group ~= current_group then + current_event = event + current_group = group + end + goto line_parsed + end + + -- non event/group lines + ft_pat = line:match(pattern.INDENT .. "(%S+)") + if ft_pat then + if ft_pat:match("^%d+") then + ft_pat = "<buffer=" .. ft_pat .. ">" + end + current_ft = ft_pat + + -- is there a command on the same line? + cmd = line:match(pattern.INDENT .. "%S+%s+(.+)") + + goto line_parsed + end + + if current_ft and cmd == nil then + -- trim leading spaces + cmd = line:gsub("^%s+", "") + goto line_parsed + end + + if current_ft and cmd then + source_file, source_lnum = line:match("Last set from (.*) line (.*)") + if source_file then + local autocmd = {} + autocmd.event = current_event + autocmd.group = current_group + autocmd.ft_pattern = current_ft + autocmd.command = cmd + autocmd.source_file = source_file + autocmd.source_lnum = source_lnum + table.insert(autocmd_table, autocmd) + + cmd = nil + end + end + + ::line_parsed:: + end + + -- print(vim.inspect(autocmd_table)) + + pickers.new(opts,{ + prompt_title = 'autocommands', + finder = finders.new_table { + results = autocmd_table, + entry_maker = make_entry.gen_from_autocommands(opts), + }, + previewer = previewers.autocommands.new(opts), + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions._goto_file_selection:replace(function(_, cmd) + local selection = actions.get_selected_entry() + actions.close(prompt_bufnr) + vim.cmd(cmd .. ' ' .. selection.value) + end) + + return true + end + }):find() +end local function apply_checks(mod) for k, v in pairs(mod) do |
