chore: cleanup autocmd builtin (#1947)

This commit is contained in:
Simon Hauser
2022-06-12 15:09:41 +02:00
committed by Simon Hauser
parent 838c32d6a8
commit 77e2b8ceea
3 changed files with 46 additions and 96 deletions

View File

@@ -1170,95 +1170,44 @@ internal.highlights = function(opts)
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 inner_loop = function(line)
-- 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
return
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+(.+)")
return
end
if current_ft and cmd == nil then
-- trim leading spaces
cmd = line:gsub("^%s+", "")
return
end
if current_ft and cmd then
source_file = line:match "Last set from (.*) line %d*$" or line:match "Last set from (.*)$"
source_lnum = line:match "line (%d*)$" or "1"
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
end
local cmd_output = vim.fn.execute("verb autocmd *", "silent")
for line in cmd_output:gmatch "[^\r\n]+" do
inner_loop(line)
end
local autocmds = vim.api.nvim_get_autocmds {}
table.sort(autocmds, function(lhs, rhs)
return lhs.event < rhs.event
end)
pickers.new(opts, {
prompt_title = "autocommands",
finder = finders.new_table {
results = autocmd_table,
results = autocmds,
entry_maker = opts.entry_maker or make_entry.gen_from_autocommands(opts),
},
previewer = previewers.autocommands.new(opts),
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr)
action_set.select:replace(function(_, type)
action_set.select:replace_if(function()
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "builtin.autocommands"
return
return false
end
local val = selection.value
local output = vim.fn.execute(
"verb autocmd " .. val.group_name .. " " .. val.event .. " " .. val.pattern,
"silent"
)
for line in output:gmatch "[^\r\n]+" do
local source_file = line:match "Last set from (.*) line %d*$" or line:match "Last set from (.*)$"
if source_file and source_file ~= "Lua" then
selection.filename = source_file
local source_lnum = line:match "line (%d*)$" or "1"
selection.lnum = tonumber(source_lnum)
selection.col = 1
return false
end
end
return true
end, function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
vim.cmd(action_state.select_key_to_edit_key(type) .. " " .. selection.value)
print("You selected autocmd: " .. vim.inspect(selection.value))
end)
return true