8000 mini.nvim/lua/mini/extra.lua at main · PeterCardenas/mini.nvim · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2198 lines (1932 loc) · 86.1 KB

extra.lua

File metadata and controls

2198 lines (1932 loc) · 86.1 KB
end
for _, item in ipairs(items) do
--stylua: ignore
item.text = string.format(
'%s │ %s│%d│%d│%s',
H.ensure_text_width(item.highlighter, highlighter_width),
item.buf_name, item.lnum, item.col, item.line
)
item.buf_name, item.line = nil, nil
end
local show = function(buf_id, items_to_show, query)
pick.default_show(buf_id, items_to_show, query)
H.pick_clear_namespace(buf_id, H.ns_id.pickers)
for i, item in ipairs(items_to_show) do
local end_col = string.len(item.highlighter)
local extmark_opts = { hl_group = item.hl_group, end_row = i - 1, end_col = end_col, priority = 1 }
vim.api.nvim_buf_set_extmark(buf_id, H.ns_id.pickers, i - 1, 0, extmark_opts)
end
end
local name = string.format('Mini.hipatterns matches (%s)', scope)
return H.pick_start(items, { source = { name = name, show = show } }, opts)
end
--- Neovim history picker
---
--- Pick from output of |:history|. Use `<C-e>` to edit current match in
--- Command line.
---
--- Notes:
--- - Has no preview.
--- - Choosing action depends on scope:
--- - For "cmd" / ":" scopes, the command is executed.
--- - For "search" / "/" / "?" scopes, search is redone.
--- - For other scopes nothing is done (but chosen item is still returned).
--- - `<C-e>` only works for "cmd" / ":" / "search" / "/" / "?" scopes.
---
--- Examples:
---
--- - Command history: `MiniExtra.pickers.history({ scope = ':' })`
--- - Search history: `:Pick history scope='/'`
---
---@param local_opts __extra_pickers_local_opts
--- Possible fields:
--- - <scope> `(string)` - any allowed {name} flag of |:history| command.
--- Note: word abbreviations are not allowed. Default: "all".
---@param opts __extra_pickers_opts
---
---@return __extra_pickers_return
MiniExtra.pickers.history = function(local_opts, opts)
local pick = H.validate_pick('history')
local_opts = vim.tbl_deep_extend('force', { scope = 'all' }, local_opts or {})
local allowed_scope = { 'all', 'cmd', 'search', 'expr', 'input', 'debug', ':', '/', '?', '=', '@', '>' }
local scope = H.pick_validate_scope(local_opts, allowed_scope, 'history')
--stylua: ignore
local type_ids = {
cmd = ':', search = '/', expr = '=', input = '@', debug = '>',
[':'] = ':', ['/'] = '/', ['='] = '=', ['@'] = '@', ['>'] = '>',
['?'] = '?',
}
-- Construct items
local items = {}
local names = scope == 'all' and { 'cmd', 'search', 'expr', 'input', 'debug' } or { scope }
for _, cur_name in ipairs(names) do
local id = type_ids[cur_name]
for i = 1, vim.fn.histnr(cur_name) do
local hist_entry = vim.fn.histget(cur_name, -i)
if hist_entry == '' then break end
table.insert(items, string.format('%s %s', id, hist_entry))
end
end
-- Define source
local preview = H.pick_make_no_preview('history')
local choose = function(item)
if not (type(item) == 'string' and vim.fn.mode() == 'n') then return end
local id, entry = item:match('^(.) (.*)$')
if id == ':' or id == '/' or id == '?' then
vim.schedule(function() vim.fn.feedkeys(id .. entry .. '\r', 'nxt') end)
end
end
local edit_command = function()
local cur_match = MiniPick.get_picker_matches().current
local cur_scope, cur_item = cur_match:match('^(.) (.*)$')
if not (cur_scope == ':' or cur_scope == '/' or cur_scope == '?') then return end
vim.schedule(function() vim.api.nvim_input(cur_scope .. cur_item) end)
return true
end
local mappings = { edit_command = { char = '<C-e>', func = edit_command } }
opts = vim.tbl_deep_extend('force', opts or {}, { mappings = mappings })
local default_source = { name = string.format('History (%s)', scope), preview = preview, choose = choose }
return H.pick_start(items, { source = default_source }, opts)
end
--- Highlight groups picker
---
--- Pick and preview highlight groups.
--- Notes:
--- - Item line is colored with same highlight group it represents.
--- - Preview shows highlight's definition (as in |:highlight| with {group-name}).
--- - Choosing places highlight definition in Command line to update and apply.
0