From 8546fdf610584c367f0f1e105aedec370d71bf54 Mon Sep 17 00:00:00 2001 From: Senghan Bright Date: Wed, 2 Dec 2020 00:27:54 +0100 Subject: 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 --- lua/telescope/previewers.lua | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'lua/telescope/previewers.lua') diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua index 81664ec..9c63836 100644 --- a/lua/telescope/previewers.lua +++ b/lua/telescope/previewers.lua @@ -770,6 +770,51 @@ previewers.man = defaulter(function(_) } end) +previewers.autocommands = defaulter(function(_) +return previewers.new_buffer_previewer { + setup = function() + return {} + end, + + teardown = function(self) + if self.state and self.state.hl_id then + pcall(vim.fn.matchdelete, self.state.hl_id, self.state.hl_win) + self.state.hl_id = nil + end + end, + + preview_fn = function(self, entry, status) + local results = vim.tbl_filter( + function (x) return x.group == entry.group end, + status.picker.finder.results + ) + local display = {} + table.insert(display, string.format(" augroup: %s - [ %d entries ]", entry.group, #results)) + -- TODO: calculate banner width/string in setup() + -- TODO: get column characters to be the same HL group as border + table.insert(display, string.rep("─", vim.fn.getwininfo(status.preview_win)[1].width)) + + local selected_row + for idx, item in ipairs(results) do + if item == entry then + selected_row = idx + end + table.insert(display, + string.format(" %-14sā–%-08s %s", item.event, item.ft_pattern, item.command) + ) + end + + with_preview_window(status, nil, function() + -- TODO: set filetype in setup() + vim.api.nvim_buf_set_option(status.preview_bufnr, "filetype", "vim") + vim.api.nvim_buf_set_lines(status.preview_bufnr, 0, -1, false, display) + vim.api.nvim_buf_add_highlight(status.preview_bufnr, 0, "TelescopeBorder", 1, 0, -1) + vim.api.nvim_buf_add_highlight(status.preview_bufnr, 0, "TelescopeSelection", selected_row + 1, 0, -1) + end) + end, +} +end, {}) + previewers.display_content = defaulter(function(_) return previewers.new_buffer_previewer { preview_fn = function(self, entry, status) -- cgit v1.2.3