--- *mini.extra* Extra mini.nvim functionality --- --- MIT License Copyright (c) 2023 Evgeni Chasnovski --- Extra useful functionality which is not essential enough for other |mini.nvim| --- modules to include directly. --- --- Features: --- --- - Various pickers for |mini.pick|: --- - Built-in diagnostic (|MiniExtra.pickers.diagnostic()|). --- - File explorer (|MiniExtra.pickers.explorer()|). --- - Git branches/commits/files/hunks (|MiniExtra.pickers.git_hunks()|, etc.). --- - Command/search/input history (|MiniExtra.pickers.history()|). --- - LSP references/symbols/etc. (|MiniExtra.pickers.lsp()|). --- - Tree-sitter nodes (|MiniExtra.pickers.treesitter()|). --- - And much more. --- See |MiniExtra.pickers| for more. --- --- - Various textobject specifications for |mini.ai|. See |MiniExtra.gen_ai_spec|. --- --- - Various highlighters for |mini.hipatterns|. See |MiniExtra.gen_highlighter|. --- --- Notes: --- - This module requires only those |mini.nvim| modules which are needed for --- a particular functionality: |mini.pick| for pickers, etc. --- --- # Setup ~ --- --- This module needs a setup with `require('mini.extra').setup({})` (replace --- `{}` with your `config` table). It will create global Lua table `MiniExtra` --- which you can use for scripting or manually (with `:lua MiniExtra.*`). --- --- See |MiniExtra.config| for `config` structure and default values. --- --- This module doesn't have runtime options, so using `vim.b.miniextra_config` --- will have no effect here. --- --- # Comparisons ~ --- --- - [nvim-telescope/telescope.nvim](https://github.com/nvim-telescope/telescope.nvim): --- - With |MiniExtra.pickers|, |mini.pick| is reasonably on par when it comes --- to built-in pickers. --- --- - [ibhagwan/fzf-lua](https://github.com/ibhagwan/fzf-lua): --- - Same as `nvim-telescope/telescope.nvim`. ---@tag MiniExtra ---@diagnostic disable:undefined-field ---@diagnostic disable:discard-returns ---@diagnostic disable:unused-local ---@diagnostic disable:cast-local-type ---@diagnostic disable:undefined-doc-name ---@diagnostic disable:luadoc-miss-type-name ---@alias __extra_ai_spec_return function Function implementing |MiniAi-textobject-specification|. ---@alias __extra_pickers_local_opts table|nil Options defining behavior of this particular picker. ---@alias __extra_pickers_opts table|nil Options forwarded to |MiniPick.start()|. ---@alias __extra_pickers_return any Output of the called picker. ---@alias __extra_pickers_git_notes Notes: --- - Requires executable `git`. --- - Requires target path to be part of git repository. --- - Present for exploration and navigation purposes. Doing any Git operations --- is suggested to be done in a dedicated Git client and is not planned. ---@alias __extra_pickers_preserve_order - `(boolean)` - whether to preserve original order --- during query. Default: `false`. ---@alias __extra_pickers_git_path - `(string|nil)` - target path for Git operation (if required). Also --- used to find Git repository inside which to construct items. --- Default: `nil` for root of Git repository containing |current-directory|. -- Module definition ========================================================== local MiniExtra = {} local H = {} --- Module setup --- ---@param config table|nil Module config table. See |MiniExtra.config|. --- ---@usage >lua --- require('mini.extra').setup() -- use default config --- -- OR --- require('mini.extra').setup({}) -- replace {} with your config table --- < MiniExtra.setup = function(config) -- TODO: Remove after Neovim=0.9 support is dropped if vim.fn.has('nvim-0.10') == 0 then vim.notify( '(mini.extra) Neovim<0.10 is soft deprecated (module works but is not supported).' .. " It will be deprecated after the next 'mini.nvim' release (module might not work)." .. ' Please update your Neovim version.' ) end -- Export module _G.MiniExtra = MiniExtra -- Setup config config = H.setup_config(config) -- Apply config H.apply_config(config) end --stylua: ignore --- Defaults ~ ---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section) MiniExtra.config = {} --minidoc_afterlines_end --- |mini.ai| textobject specification generators --- --- This is a table with function elements. Call to actually get specification. --- --- Assumed to be used as part of |MiniAi.setup()|. Example: >lua --- --- local gen_ai_spec = require('mini.extra').gen_ai_spec --- require('mini.ai').setup({ --- custom_textobjects = { --- B = gen_ai_spec.buffer(), --- D = gen_ai_spec.diagnostic(), --- I = gen_ai_spec.indent(), --- L = gen_ai_spec.line(), --- N = gen_ai_spec.number(), --- }, --- }) --- < MiniExtra.gen_ai_spec = {} --- Current buffer textobject --- --- Notes: --- - `a` textobject selects all lines in a buffer. --- - `i` textobject selects all lines except blank lines at start and end. --- ---@return __extra_ai_spec_return MiniExtra.gen_ai_spec.buffer = function() return function(ai_type) local start_line, end_line = 1, vim.fn.line('$') if ai_type == 'i' then -- Skip first and last blank lines for `i` textobject local first_nonblank, last_nonblank = vim.fn.nextnonblank(start_line), vim.fn.prevnonblank(end_line) -- Do nothing for buffer with all blanks if first_nonblank == 0 or last_nonblank == 0 then return { from = { line = start_line, col = 1 } } end start_line, end_line = first_nonblank, last_nonblank end local to_col = math.max(vim.fn.getline(end_line):len(), 1) return { from = { line = start_line, col = 1 }, to = { line = end_line, col = to_col } } end end --- Current buffer diagnostic textobject --- --- Notes: --- - Both `a` and `i` textobjects return |vim.diagnostic.get()| output for the --- current buffer. It is modified to fit |MiniAi-textobject-specification|. --- ---@param severity any Which severity to use. Forwarded to |vim.diagnostic.get()|. --- Default: `nil` to use all diagnostic entries. --- ---@return __extra_ai_spec_return MiniExtra.gen_ai_spec.diagnostic = function(severity) return function(ai_type) local cur_diag = vim.diagnostic.get(0, { severity = severity }) local regions = {} for _, diag in ipairs(cur_diag) do local from = { line = diag.lnum + 1, col = diag.col + 1 } local to = { line = diag.end_lnum + 1, col = diag.end_col } if to.line == nil or to.col == nil then to = { line = diag.lnum + 1, col = diag.col + 1 } end table.insert(regions, { from = from, to = to }) end return regions end end --- Current buffer indent scopes textobject --- --- Indent scope is a set of consecutive lines with the following properties: --- - Lines above first and below last are non-blank. They are called borders. --- - There is at least one non-blank line in a set. --- - All non-blank lines between borders have strictly greater indent --- (perceived leading space respecting |'tabstop'|) than either of borders. --- --- Notes: --- - `a` textobject selects scope including borders. --- - `i` textobject selects the scope charwise. --- - Differences with |MiniIndentscope.textobject()|: --- - This textobject always treats blank lines on top and bottom of `i` --- textobject as part of it, while |mini.indentscope| can configure that. --- - This textobject can select non-covering scopes, while |mini.indentscope| --- can not (by design). --- - In this textobject scope computation is done only by "casting rays" from --- top to bottom and not in both ways as in |mini.indentscope|. --- This works in most common scenarios and doesn't work only if indent of --- of the bottom border is expected to be larger than the top. --- ---@return function Function implementing |MiniAi-textobject-specification|. --- It returns array of regions representing all indent scopes in the buffer --- ordered increasingly by the start line. MiniExtra.gen_ai_spec.indent = function() return H.ai_indent_spec end --- Current line textobject --- --- Notes: --- - `a` textobject selects whole line. --- - `i` textobject selects line after initial indent. --- ---@return __extra_ai_spec_return MiniExtra.gen_ai_spec.line = function() return function(ai_type, _, opts) local lnum_from = vim.fn.line('.') local lnum_to = lnum_from + opts.n_times - 1 local line_from, line_to = vim.fn.getline(lnum_from), vim.fn.getline(lnum_to) -- Ignore indentation for `i` textobject local col_from = ai_type == 'a' and 1 or (line_from:match('^(%s*)'):len() + 1) -- Don't select `\n` past the line to operate within a line local col_to = line_to:len() return { from = { line = lnum_from, col = col_from }, to = { line = lnum_to, col = col_to } } end end --- Number textobject --- --- Notes: --- - `a` textobject selects a whole number possibly preceded with "-" and --- possibly followed by decimal part (dot and digits). --- - `i` textobject selects consecutive digits. --- ---@return __extra_ai_spec_return MiniExtra.gen_ai_spec.number = function() local digits_pattern = '%f[%d]%d+%f[%D]' local find_a_number = function(line, init) -- First find consecutive digits local from, to = line:find(digits_pattern, init) if from == nil then return nil, nil end -- Make sure that these digits were not processed before. This can happen -- because 'miin.ai' does next with `init = from + 1`, meaning that -- "-12.34" was already matched, then it would try to match starting from -- "1": we want to avoid matching that right away and avoid matching "34" -- from this number. if from == init and line:sub(from - 1, from - 1) == '-' then init = to + 1 from, to = line:find(digits_pattern, init) end if from == nil then return nil, nil end if line:sub(from - 2):find('^%d%.') ~= nil then init = to + 1 from, to = line:find(digits_pattern, init) end if from == nil then return nil, nil end -- Match the whole number with minus and decimal part if line:sub(from - 1, from - 1) == '-' then from = from - 1 end local dec_part = line:sub(to + 1):match('^%.%d+()') if dec_part ~= nil then to = to + dec_part - 1 end return from, to end return function(ai_type) if ai_type == 'i' then return { digits_pattern } end return { find_a_number, { '^().*()$' } } end end --- |mini.hipatterns| highlighter generators --- --- This is a table with function elements. Call to actually get specification. --- --- Assumed to be used as part of |MiniHipatterns.setup()|. Example: >lua --- --- local hi_words = require('mini.extra').gen_highlighter.words --- require('mini.hipatterns').setup({ --- highlighters = { --- todo = hi_words({ 'TODO', 'Todo', 'todo' }, 'MiniHipatternsTodo'), --- }, --- }) --- < MiniExtra.gen_highlighter = {} --- Highlight words --- --- Notes: --- - Words should start and end with alphanumeric symbol (latin letter or digit). --- - Words will be highlighted only in full and not if part bigger word, i.e. --- there should not be alphanumeric symbol before and after it. --- ---@param words table Array of words to highlight. Will be matched as is, not --- as Lua pattern. ---@param group string|function Proper `group` field for `highlighter`. --- See |MiniHipatterns.config|. ---@param extmark_opts any Proper `extmark_opts` field for `highlighter`. --- See |MiniHipatterns.config|. MiniExtra.gen_highlighter.words = function(words, group, extmark_opts) if not H.islist(words) then H.error('`words` should be an array.') end if not (type(group) == 'string' or vim.is_callable(group)) then H.error('`group` should be string or callable.') end local pattern = vim.tbl_map(function(x) if type(x) ~= 'string' then H.error('All elements of `words` should be strings.') end return '%f[%w]()' .. vim.pesc(x) .. '()%f[%W]' end, words) return { pattern = pattern, group = group, extmark_opts = extmark_opts } end --- |mini.pick| pickers --- --- A table with |mini.pick| pickers (which is a hard dependency). --- Notes: --- - All have the same signature: --- - - optional table with options local to picker. --- - - optional table with options forwarded to |MiniPick.start()|. --- - All of them are automatically registered in |MiniPick.registry| inside --- both |MiniExtra.setup()| or |MiniPick.setup()| (only one is enough). --- - All use default versions of |MiniPick-source.preview|, |MiniPick-source.choose|, --- and |MiniPick-source.choose_marked| if not stated otherwise. --- Shown text and |MiniPick-source.show| are targeted to the picked items. --- --- Examples of usage: --- - As Lua code: `MiniExtra.pickers.buf_lines()`. --- - With |:Pick| command: `:Pick buf_lines scope='current'` --- Note: this requires calling |MiniExtra.setup()|. MiniExtra.pickers = {} --- Buffer lines picker --- --- Pick from buffer lines. Notes: --- - Loads all target buffers which are currently unloaded. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- - `(string)` - one of "all" (normal listed buffers) or "current". --- Default: "all". --- __extra_pickers_preserve_order ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.buf_lines = function(local_opts, opts) local pick = H.validate_pick('buf_lines') local_opts = vim.tbl_deep_extend('force', { scope = 'all', preserve_order = false }, local_opts or {}) local scope = H.pick_validate_scope(local_opts, { 'all', 'current' }, 'buf_lines') local is_scope_all = scope == 'all' -- Define non-blocking callable `items` because getting all lines from all -- buffers (plus loading them) may take visibly long time local buffers = {} if is_scope_all then for _, buf_id in ipairs(vim.api.nvim_list_bufs()) do if vim.bo[buf_id].buflisted and vim.bo[buf_id].buftype == '' then table.insert(buffers, buf_id) end end else buffers = { vim.api.nvim_get_current_buf() } end local poke_picker = pick.poke_is_picker_active local f = function() local items = {} for _, buf_id in ipairs(buffers) do if not poke_picker() then return end H.buf_ensure_loaded(buf_id) local buf_name = H.buf_get_name(buf_id) or '' local n_digits = math.floor(math.log10(vim.api.nvim_buf_line_count(buf_id))) + 1 local format_pattern = '%s%' .. n_digits .. 'd\0%s' for lnum, l in ipairs(vim.api.nvim_buf_get_lines(buf_id, 0, -1, false)) do local prefix = is_scope_all and (buf_name .. '\0') or '' table.insert(items, { text = format_pattern:format(prefix, lnum, l), bufnr = buf_id, lnum = lnum }) end end pick.set_picker_items(items) end local items = vim.schedule_wrap(coroutine.wrap(f)) local show = H.pick_get_config().source.show if is_scope_all and show == nil then show = H.show_with_icons end local match_opts = { preserve_order = local_opts.preserve_order } local match = function(stritems, inds, query) pick.default_match(stritems, inds, query, match_opts) end local default_source = { name = string.format('Buffer lines (%s)', scope), show = show, match = match } return H.pick_start(items, { source = default_source }, opts) end --- Color scheme picker --- --- Pick and apply color scheme. Preview temporarily applies item's color scheme --- and shows how selected highlight groups look. --- Canceling reverts to color scheme before picker start: --- - With |MiniColors-colorscheme:apply()| if |mini.colors| was available. --- - With |:colorscheme| if |g:colors_name| was available. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- - `(table)` - array of color scheme names to pick from. --- Default: all available color schemes. --- - `(table)` - array of highlight groups to show in preview --- window. Default: all defined highlight groups in alphabetical order. --- ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.colorschemes = function(local_opts, opts) local pick = H.validate_pick('colorschemes') local_opts = local_opts or {} -- Infer data to show local all_cs = vim.fn.getcompletion('', 'color') local items = local_opts.names or all_cs if not H.islist(items) then H.error('`names` should be array of color scheme names') end for _, item in ipairs(items) do if not vim.tbl_contains(all_cs, item) then H.error(vim.inspect(item) .. ' is not a color scheme name') end end local hl_groups = local_opts.preview_hl_groups if hl_groups ~= nil and not H.islist(hl_groups) then H.error('`preview_hl_groups` should be array') end -- Compute original color scheme to restore local bg_orig = vim.o.background local has_minicolors, minicolors = pcall(require, 'mini.colors') local cs_orig = has_minicolors and minicolors.get_colorscheme() or vim.g.colors_name local restore = function() vim.o.background = bg_orig if cs_orig == nil then return end if type(cs_orig) == 'string' then return vim.cmd('colorscheme ' .. cs_orig) end cs_orig:apply() -- Trigger to indicate actual color scheme application vim.api.nvim_exec_autocmds('ColorScheme', {}) end -- Define source local choose = function(item) vim.cmd('colorscheme ' .. item) end local choose_marked = function(items_to_mark) choose(items_to_mark[1] or '') end local needs_restore = false local preview = function(buf_id, item) needs_restore = true -- Ensure that previewed color scheme tries to use the initial background. -- This can be not the case if previous preview forced new background. vim.o.background = bg_orig vim.cmd('colorscheme ' .. item) H.preview_cs_hl_groups(buf_id, hl_groups) end local default_source = { name = 'Colorschemes', preview = preview, choose = choose, choose_marked = choose_marked } local result = H.pick_start(items, { source = default_source }, opts) if result == nil and needs_restore then restore() end return result end --- Neovim commands picker --- --- Pick from Neovim built-in (|Ex-command|) and |user-commands|. --- Notes: --- - Preview shows information about the command (if available). --- - Choosing either executes command (if reliably known that it doesn't need --- arguments) or populates Command line with the command. --- ---@param local_opts __extra_pickers_local_opts --- Not used at the moment. ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.commands = function(local_opts, opts) local pick = H.validate_pick('commands') local commands = vim.tbl_deep_extend('force', vim.api.nvim_get_commands({}), vim.api.nvim_buf_get_commands(0, {})) local preview = function(buf_id, item) local data = commands[item] local lines = data == nil and { string.format('No command data for `%s` is yet available.', item) } or vim.split(vim.inspect(data), '\n') H.set_buflines(buf_id, lines) end local choose = function(item) local data = commands[item] or {} -- If no arguments needed, execute immediately local keys = string.format(':%s%s', item, data.nargs == '0' and '\r' or ' ') vim.schedule(function() vim.fn.feedkeys(keys) end) end local items = vim.fn.getcompletion('', 'command') local default_opts = { source = { name = 'Commands', preview = preview, choose = choose } } return H.pick_start(items, default_opts, opts) end --- Built-in diagnostic picker --- --- Pick from |vim.diagnostic| using |vim.diagnostic.get()|. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- - `(table)` - options for |vim.diagnostic.get()|. Can be used --- to limit severity or namespace. Default: `{}`. --- - `(string)` - one of "all" (available) or "current" (buffer). --- Default: "all". --- - `(string)` - sort priority. One of "severity", "path", "none". --- Default: "severity". ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.diagnostic = function(local_opts, opts) local pick = H.validate_pick('diagnostic') local_opts = vim.tbl_deep_extend('force', { get_opts = {}, scope = 'all', sort_by = 'severity' }, local_opts or {}) local scope = H.pick_validate_scope(local_opts, { 'all', 'current' }, 'diagnostic') local sort_by = H.pick_validate_one_of('sort_by', local_opts, { 'severity', 'path', 'none' }, 'diagnostic') local plus_one = function(x) if x == nil then return nil end return x + 1 end local diag_buf_id if scope == 'current' then diag_buf_id = vim.api.nvim_get_current_buf() end local items = vim.deepcopy(vim.diagnostic.get(diag_buf_id, local_opts.get_opts)) -- Compute final path width local path_width = 0 for _, item in ipairs(items) do item.path = H.buf_get_name(item.bufnr) or '' item.severity = item.severity or 0 path_width = math.max(path_width, vim.fn.strchars(item.path)) end -- Sort local compare = H.diagnostic_make_compare(sort_by) if vim.is_callable(compare) then table.sort(items, compare) end -- Update items for _, item in ipairs(items) do local severity = vim.diagnostic.severity[item.severity] or ' ' local text = item.message:gsub('\n', ' ') item.text = string.format('%s │ %s │ %s', severity:sub(1, 1), H.ensure_text_width(item.path, path_width), text) item.lnum, item.col, item.end_lnum, item.end_col = plus_one(item.lnum), plus_one(item.col), plus_one(item.end_lnum), plus_one(item.end_col) end local hl_groups_ref = { [vim.diagnostic.severity.ERROR] = 'DiagnosticFloatingError', [vim.diagnostic.severity.WARN] = 'DiagnosticFloatingWarn', [vim.diagnostic.severity.INFO] = 'DiagnosticFloatingInfo', [vim.diagnostic.severity.HINT] = 'DiagnosticFloatingHint', } -- Define source 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 H.pick_highlight_line(buf_id, i, hl_groups_ref[item.severity], 199) end end local name = string.format('Diagnostic (%s)', scope) return H.pick_start(items, { source = { name = name, choose = H.choose_with_buflisted, show = show } }, opts) end --- File explorer picker --- --- Explore file system and open file. --- Notes: --- - Choosing a directory navigates inside it, changing picker's items and --- current working directory. --- - Query and preview work as usual (not only `move_next`/`move_prev` can be used). --- - Preview works for any item. --- --- Examples: --- --- - `MiniExtra.pickers.explorer()` --- - `:Pick explorer cwd='..'` - open explorer in parent directory. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- - `(string)` - initial directory to explore. Should be a valid --- directory path. Default: `nil` for |current-directory|. --- - `(function)` - callable predicate to filter items to show. --- Will be called for every item and should return `true` if it should be --- shown. Each item is a table with the following fields: --- - `(string)` - path type. One of "directory" or "file". --- - `(string)` - item path. --- - `(string)` - shown text (path's basename). --- - `(function)` - callable item sorter. Will be called with array --- of items (each element with structure as described above) and should --- return sorted array of items. ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.explorer = function(local_opts, opts) local pick = H.validate_pick('explorer') local_opts = vim.tbl_deep_extend('force', { cwd = nil, filter = nil, sort = nil }, local_opts or {}) local cwd = local_opts.cwd or vim.fn.getcwd() if vim.fn.isdirectory(cwd) == 0 then H.error('`local_opts.cwd` should be valid directory path.') end -- - Call twice "full path" to make sure that possible '..' are collapsed cwd = H.full_path(vim.fn.fnamemodify(cwd, ':p')) local filter = local_opts.filter or function() return true end if not vim.is_callable(filter) then H.error('`local_opts.filter` should be callable.') end local sort = local_opts.sort or H.explorer_default_sort if not vim.is_callable(sort) then H.error('`local_opts.sort` should be callable.') end -- Define source local choose = function(item) local path = item.path if vim.fn.filereadable(path) == 1 then return pick.default_choose(path) end if vim.fn.isdirectory(path) == 0 then return false end pick.set_picker_items(H.explorer_make_items(path, filter, sort)) pick.set_picker_opts({ source = { cwd = path } }) pick.set_picker_query({}) return true end local show = H.pick_get_config().source.show or H.show_with_icons local items = H.explorer_make_items(cwd, filter, sort) local source = { items = items, name = 'File explorer', cwd = cwd, show = show, choose = choose } opts = vim.tbl_deep_extend('force', { source = source }, opts or {}) return pick.start(opts) end --- Git branches picker --- --- Pick from Git branches using `git branch`. --- __extra_pickers_git_notes --- - On choose opens scratch buffer with branch's history. --- --- Examples: --- --- - `MiniExtra.pickers.git_branches({ scope = 'local' })` - local branches of --- the |current-directory| parent Git repository. --- - `:Pick git_branches path='%'` - all branches of the current file parent --- Git repository. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- __extra_pickers_git_path --- - `(string)` - branch scope to show. One of "all", "local", "remotes". --- Default: "all". ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.git_branches = function(local_opts, opts) local pick = H.validate_pick('git_branches') H.validate_git('git_branches') local_opts = vim.tbl_deep_extend('force', { path = nil, scope = 'all' }, local_opts or {}) local scope = H.pick_validate_scope(local_opts, { 'all', 'local', 'remotes' }, 'git_branches') -- Compute path to repo with target path (as it might differ from current) local path, path_type = H.git_normalize_path(local_opts.path, 'git_branches') local repo_dir = H.git_get_repo_dir(path, path_type, 'git_branches') -- Define source local preview = function(buf_id, item) local branch = item:match('^%*?%s*(%S+)') H.cli_show_output(buf_id, { 'git', '-C', repo_dir, 'log', branch, '--format=format:%h %s' }) end local choose = function(item) local win_target = (pick.get_picker_state().windows or {}).target if win_target == nil or not H.is_valid_win(win_target) then return end local buf_id = vim.api.nvim_create_buf(true, true) H.set_buf_name(buf_id, item:match('^%*?%s*(%S+)')) preview(buf_id, item) vim.api.nvim_win_set_buf(win_target, buf_id) end local command = { 'git', 'branch', '-v', '--no-color', '--list' } if scope == 'all' or scope == 'remotes' then table.insert(command, 3, '--' .. scope) end local name = string.format('Git branches (%s)', scope) local default_source = { name = name, cwd = repo_dir, preview = preview, choose = choose } opts = vim.tbl_deep_extend('force', { source = default_source }, opts or {}) return pick.builtin.cli({ command = command }, opts) end --- Git commits picker --- --- Pick from Git commits using `git log`. --- __extra_pickers_git_notes --- - On choose opens scratch buffer with commit's diff. --- --- Examples: --- --- - `MiniExtra.pickers.git_commits()` - all commits from parent Git --- repository of |current-directory|. --- - `MiniExtra.pickers.git_commits({ path = 'subdir' })` - commits affecting --- files from `subdir` subdirectory. --- - `:Pick git_commits path='%'` commits affecting current file. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- __extra_pickers_git_path ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.git_commits = function(local_opts, opts) local pick = H.validate_pick('git_commits') H.validate_git('git_commits') local_opts = vim.tbl_deep_extend('force', { path = nil }, local_opts or {}) -- Compute path to repo with target path (as it might differ from current) local path, path_type = H.git_normalize_path(local_opts.path, 'git_commits') local repo_dir = H.git_get_repo_dir(path, path_type, 'git_commits') if local_opts.path == nil then path = repo_dir end -- Define source local preview = function(buf_id, item) if type(item) ~= 'string' then return end local has_parser, parser = pcall(vim.treesitter.get_parser, buf_id, 'git', { error = false }) has_parser = has_parser and parser ~= nil if has_parser then has_parser = pcall(vim.treesitter.start, buf_id, 'git') end if not has_parser then vim.bo[buf_id].syntax = 'git' end H.cli_show_output(buf_id, { 'git', '-C', repo_dir, '--no-pager', 'show', item:match('^(%S+)') }) end local choose = function(item) local win_target = (pick.get_picker_state().windows or {}).target if win_target == nil or not H.is_valid_win(win_target) then return end local buf_id = vim.api.nvim_create_buf(true, true) H.set_buf_name(buf_id, item:match('^(%S+)')) preview(buf_id, item) -- Set filetype on opened buffer to trigger appropriate `FileType` event vim.bo[buf_id].filetype = 'git' vim.api.nvim_win_set_buf(win_target, buf_id) end local command = { 'git', 'log', [[--format=format:%h %s]], '--', path } local name = string.format('Git commits (%s)', local_opts.path == nil and 'all' or 'for path') local default_source = { name = name, cwd = repo_dir, preview = preview, choose = choose } opts = vim.tbl_deep_extend('force', { source = default_source }, opts or {}) return pick.builtin.cli({ command = command }, opts) end --- Git files picker --- --- Pick from Git files using `git ls-files` using standard ignore rules. --- __extra_pickers_git_notes --- --- Examples: --- --- - `MiniExtra.pickers.git_files({ scope = 'ignored' })` - ignored files from --- parent Git repository of |current-directory|. --- - `:Pick git_files path='subdir' scope='modified'` - files from `subdir` --- subdirectory which differ from Git index. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- __extra_pickers_git_path --- - `(string)` - files scope to show. One of: --- - "tracked" (`--cached` Git flag). --- - "modified" (`--modified` Git flag). --- - "untracked" (`--others` Git flag). --- - "ignored" (`--ignored` Git flag). --- - "deleted" (`--deleted` Git flag). --- Default: "tracked". ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.git_files = function(local_opts, opts) local pick = H.validate_pick('git_files') H.validate_git('git_files') local_opts = vim.tbl_deep_extend('force', { path = nil, scope = 'tracked' }, local_opts or {}) local allowed_scope = { 'tracked', 'modified', 'untracked', 'ignored', 'deleted' } local scope = H.pick_validate_scope(local_opts, allowed_scope, 'git_files') -- Compute path to repo with target path (as it might differ from current) local path, path_type = H.git_normalize_path(local_opts.path, 'git_files') local repo_dir = H.git_get_repo_dir(path, path_type, 'git_files') if local_opts.path == nil then path = repo_dir end local path_dir = path_type == 'directory' and path or vim.fn.fnamemodify(path, ':h') -- Define source local show = H.pick_get_config().source.show or H.show_with_icons local args = ({ tracked = { '--cached', '--exclude-standard' }, modified = { '--modified', '--exclude-standard' }, untracked = { '--others', '--exclude-standard' }, ignored = { '--others', '--ignored', '--exclude-standard' }, deleted = { '--deleted', '--exclude-standard' }, })[local_opts.scope] local command = vim.list_extend({ 'git', '-C', path_dir, '-c', 'core.quotepath=false', 'ls-files' }, args) local name = string.format('Git files (%s)', local_opts.scope) local default_source = { name = name, cwd = path_dir, show = show } opts = vim.tbl_deep_extend('force', { source = default_source }, opts or {}) return pick.builtin.cli({ command = command }, opts) end --- Git hunks picker --- --- Pick from Git hunks using `git diff`. --- __extra_pickers_git_notes --- - On choose navigates to hunk's first change. --- --- Examples: --- --- - `MiniExtra.pickers.git_hunks({ scope = 'staged' })` - staged hunks from --- parent Git repository of |current-directory|. --- - `:Pick git_hunks path='%' n_context=0` - hunks from current file computed --- with no context. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- - `(number)` - number of context lines to show in hunk's preview. --- Default: 3. --- __extra_pickers_git_path --- - `(string)` - hunks scope to show. One of "unstaged" or "staged". --- Default: "unstaged". ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.git_hunks = function(local_opts, opts) local pick = H.validate_pick('git_hunks') H.validate_git('git_hunks') local default_local_opts = { n_context = 3, path = nil, scope = 'unstaged' } local_opts = vim.tbl_deep_extend('force', default_local_opts, local_opts or {}) if not (type(local_opts.n_context) == 'number' and local_opts.n_context >= 0) then H.error('`n_context` option in `pickers.git_hunks` picker should be non-negative number.') end local n_context = math.floor(local_opts.n_context) local scope = H.pick_validate_scope(local_opts, { 'unstaged', 'staged' }, 'git_hunks') -- Compute path to repo with target path (as it might differ from current) local path, path_type = H.git_normalize_path(local_opts.path, 'git_hunks') local repo_dir = H.git_get_repo_dir(path, path_type, 'git_hunks') if local_opts.path == nil then path = repo_dir end -- Define source local show = H.pick_get_config().source.show or H.show_with_icons local preview = function(buf_id, item) local has_parser, parser = pcall(vim.treesitter.get_parser, buf_id, 'diff', { error = false }) has_parser = has_parser and parser ~= nil if has_parser then has_parser = pcall(vim.treesitter.start, buf_id, 'diff') end if not has_parser then vim.bo[buf_id].syntax = 'diff' end local lines = vim.deepcopy(item.header) vim.list_extend(lines, item.hunk) H.set_buflines(buf_id, lines) end local command = { 'git', 'diff', '--patch', '--unified=' .. n_context, '--color=never', '--', path } if scope == 'staged' then table.insert(command, 4, '--cached') end local postprocess = function(lines) return H.git_difflines_to_hunkitems(lines, n_context) end local name = string.format('Git hunks (%s %s)', scope, local_opts.path == nil and 'all' or 'for path') local default_source = { name = name, cwd = repo_dir, show = show, preview = preview } opts = vim.tbl_deep_extend('force', { source = default_source }, opts or {}) return pick.builtin.cli({ command = command, postprocess = postprocess }, opts) end --- Matches from |mini.hipatterns| picker --- --- Pick from |mini.hipatterns| matches using |MiniHipatterns.get_matches()|. --- Notes: --- - Requires |mini.hipatterns|. --- - Highlighter identifier is highlighted with its highlight group. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- - `(string)` - one of "all" (buffers with enabled |mini.hipatterns|) --- or "current" (buffer). Default: "all". --- - `(table|nil)` - highlighters for which to find matches. --- Forwarded to |MiniHipatterns.get_matches()|. Default: `nil`. ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.hipatterns = function(local_opts, opts) local pick = H.validate_pick('hipatterns') local has_hipatterns, hipatterns = pcall(require, 'mini.hipatterns') if not has_hipatterns then H.error([[`pickers.hipatterns` requires 'mini.hipatterns' which can not be found.]]) end local_opts = vim.tbl_deep_extend('force', { highlighters = nil, scope = 'all' }, local_opts or {}) if local_opts.highlighters ~= nil and not H.islist(local_opts.highlighters) then H.error('`local_opts.highlighters` should be an array of highlighter identifiers.') end local highlighters = local_opts.highlighters local scope = H.pick_validate_scope(local_opts, { 'all', 'current' }, 'hipatterns') -- Get items local buffers = scope == 'all' and hipatterns.get_enabled_buffers() or { vim.api.nvim_get_current_buf() } local items, highlighter_width = {}, 0 for _, buf_id in ipairs(buffers) do local lines = vim.api.nvim_buf_get_lines(buf_id, 0, -1, false) local buf_name = H.buf_get_name(buf_id) if buf_name == '' then buf_name = 'Buffer_' .. buf_id end for _, match in ipairs(hipatterns.get_matches(buf_id, highlighters)) do match.highlighter = tostring(match.highlighter) match.buf_name, match.line = buf_name, lines[match.lnum] table.insert(items, match) highlighter_width = math.max(highlighter_width, vim.fn.strchars(match.highlighter)) end 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 `` 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). --- - `` 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: --- - `(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:gsub('<', '')) end) return true end local mappings = { edit_command = { char = '', 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. --- ---@param local_opts __extra_pickers_local_opts --- Not used at the moment. ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.hl_groups = function(local_opts, opts) local pick = H.validate_pick('hl_groups') -- Construct items local group_data = vim.split(vim.api.nvim_exec('highlight', true), '\n') local items = {} for _, l in ipairs(group_data) do local group = l:match('^(%S+)') if group ~= nil then table.insert(items, group) end end local show = function(buf_id, items_to_show, query) H.set_buflines(buf_id, items_to_show) H.pick_clear_namespace(buf_id, H.ns_id.pickers) -- Highlight line with highlight group of its item for i = 1, #items_to_show do H.pick_highlight_line(buf_id, i, items_to_show[i], 300) end end -- Define source local preview = function(buf_id, item) local lines = vim.split(vim.api.nvim_exec('hi ' .. item, true), '\n') H.set_buflines(buf_id, lines) end local choose = function(item) local hl_def = vim.split(vim.api.nvim_exec('hi ' .. item, true), '\n')[1] hl_def = hl_def:gsub('^(%S+)%s+xxx%s+', '%1 ') vim.schedule(function() vim.fn.feedkeys(':hi ' .. hl_def, 'n') end) end local default_source = { name = 'Highlight groups', show = show, preview = preview, choose = choose } return H.pick_start(items, { source = default_source }, opts) end --- Neovim keymaps picker --- --- Pick and preview data about Neovim keymaps. --- Notes: --- - Item line contains data about keymap mode, whether it is buffer local, its --- left hand side, and inferred description. --- - Preview shows keymap data or callback source (if present and reachable). --- - Choosing emulates pressing the left hand side of the keymap. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- - `(string)` - modes to show. One of "all" or appropriate mode --- for |nvim_set_keymap()|. Default: "all". --- - `(string)` - scope to show. One of "all", "global", "buf". --- Default: "all". ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.keymaps = function(local_opts, opts) local pick = H.validate_pick('keymaps') local_opts = vim.tbl_deep_extend('force', { mode = 'all', scope = 'all' }, local_opts or {}) local mode = H.pick_validate_one_of('mode', local_opts, { 'all', 'n', 'x', 's', 'o', 'i', 'l', 'c', 't' }, 'keymaps') local scope = H.pick_validate_scope(local_opts, { 'all', 'global', 'buf' }, 'keymaps') -- Create items local items = {} local populate_modes = mode == 'all' and { 'n', 'x', 's', 'o', 'i', 'l', 'c', 't' } or { mode } local max_lhs_width = 0 local populate_items = function(source) for _, m in ipairs(populate_modes) do for _, maparg in ipairs(source(m)) do local desc = maparg.desc ~= nil and vim.inspect(maparg.desc) or maparg.rhs local lhs = vim.fn.keytrans(maparg.lhsraw or maparg.lhs) max_lhs_width = math.max(vim.fn.strchars(lhs), max_lhs_width) table.insert(items, { lhs = lhs, desc = desc, maparg = maparg }) end end end if scope == 'all' or scope == 'buf' then populate_items(function(m) return vim.api.nvim_buf_get_keymap(0, m) end) end if scope == 'all' or scope == 'global' then populate_items(vim.api.nvim_get_keymap) end for _, item in ipairs(items) do local buf_map_indicator = item.maparg.buffer == 0 and ' ' or '@' local lhs_text = H.ensure_text_width(item.lhs, max_lhs_width) item.text = string.format('%s %s │ %s │ %s', item.maparg.mode, buf_map_indicator, lhs_text, item.desc or '') end -- Define source local get_callback_pos = function(maparg) if type(maparg.callback) ~= 'function' then return nil, nil end local info = debug.getinfo(maparg.callback) local path = info.source:gsub('^@', '') if vim.fn.filereadable(path) == 0 then return nil, nil end return path, info.linedefined end local preview = function(buf_id, item) local path, lnum = get_callback_pos(item.maparg) if path ~= nil then item.path, item.lnum = path, lnum return pick.default_preview(buf_id, item) end local lines = vim.split(vim.inspect(item.maparg), '\n') H.set_buflines(buf_id, lines) end local choose = function(item) local keys = vim.api.nvim_replace_termcodes(item.maparg.lhs, true, true, true) -- Restore Visual mode (should be active previously at least once) if item.maparg.mode == 'x' then keys = 'gv' .. keys end vim.schedule(function() vim.api.nvim_input(keys) end) end local default_opts = { source = { name = string.format('Keymaps (%s)', scope), preview = preview, choose = choose } } return H.pick_start(items, default_opts, opts) end --- Neovim lists picker --- --- Pick and navigate to elements of the following Neovim lists: --- - |quickfix| list. --- - |location-list| of current window. --- - |jumplist|. --- - |changelist|. --- --- Note: it requires explicit `scope`. --- --- Examples: --- --- - `MiniExtra.pickers.list({ scope = 'quickfix' })` - quickfix list. --- - `:Pick list scope='jump'` - jump list. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- - `(string)` - type of list to show. One of "quickfix", "location", --- "jump", "change". Default: `nil` which means explicit scope is needed. ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.list = function(local_opts, opts) local pick = H.validate_pick('list') local_opts = vim.tbl_deep_extend('force', { scope = nil }, local_opts or {}) if local_opts.scope == nil then H.error('`pickers.list` needs an explicit scope.') end local allowed_scopes = { 'quickfix', 'location', 'jump', 'change' } local scope = H.pick_validate_scope(local_opts, allowed_scopes, 'list') local has_items, items = pcall(H.list_get[scope]) if not has_items then items = {} end items = vim.tbl_filter(function(x) return H.is_valid_buf(x.bufnr) end, items) items = vim.tbl_map(H.list_enhance_item, items) local name = string.format('List (%s)', scope) local show = H.pick_get_config().source.show or H.show_with_icons return H.pick_start(items, { source = { name = name, show = show, choose = H.choose_with_buflisted } }, opts) end --- LSP picker --- --- Pick and navigate with LSP methods. --- Notes: --- - Needs an explicit scope from a list of supported ones: --- - "declaration". --- - "definition". --- - "document_symbol". --- - "implementation". --- - "references". --- - "type_definition". --- - "workspace_symbol". --- - "workspace_symbol_live" - same as "workspace_symbol", but with live --- feedback treating picker's prompt as LSP server query. Similar to --- how |MiniPick.builtin.grep_live()| and |MiniPick.builtin.grep()| are --- related. To use regular matching, activate |MiniPick-actions-refine|. --- - Relies on `vim.lsp.buf` methods supporting |vim.lsp.LocationOpts.OnList|. --- In particular, it means that picker is started only if LSP server returns --- list of locations and not a single location. --- - Doesn't return anything due to async nature of `vim.lsp.buf` methods. --- - Requires set up |mini.icons| to show extra icons and highlighting in --- "document_symbol", "workspace_symbol", "workspace_symbol_live" scopes. --- --- Examples: --- --- - `MiniExtra.pickers.lsp({ scope = 'references' })` - references of the symbol --- under cursor. --- - `:Pick lsp scope='document_symbol'` - symbols in current file. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- - `(string)` - LSP method to use. One of the supported ones (see --- list above). Default: `nil` which means explicit scope is needed. --- - `(string)` - query for `"workspace_symbol"` scope. --- Default: empty string for all symbols (according to LSP specification). ---@param opts __extra_pickers_opts --- ---@return nil Nothing is returned. MiniExtra.pickers.lsp = function(local_opts, opts) local pick = H.validate_pick('lsp') local_opts = vim.tbl_deep_extend('force', { scope = nil, symbol_query = '' }, local_opts or {}) if local_opts.scope == nil then H.error('`pickers.lsp` needs an explicit scope.') end --stylua: ignore local allowed_scopes = { 'declaration', 'definition', 'document_symbol', 'implementation', 'references', 'type_definition', 'workspace_symbol', 'workspace_symbol_live' } local scope = H.pick_validate_scope(local_opts, allowed_scopes, 'lsp') local buf_lsp_opts, picker_opts = H.lsp_make_opts(scope, opts) if scope == 'references' then return vim.lsp.buf[scope](nil, buf_lsp_opts) end if scope == 'workspace_symbol' then local query = tostring(local_opts.symbol_query) return vim.lsp.buf[scope](query, buf_lsp_opts) end if scope == 'workspace_symbol_live' then picker_opts.source.match = function(_, _, query) if #query == 0 then return MiniPick.set_picker_items({}, { do_match = false }) end local win_id = MiniPick.get_picker_state().windows.target local buf_id = vim.api.nvim_win_get_buf(win_id) vim.api.nvim_buf_call(buf_id, function() vim.lsp.buf.workspace_symbol(table.concat(query), buf_lsp_opts) end) end return H.pick_start({}, picker_opts, opts) end vim.lsp.buf[scope](buf_lsp_opts) end --- Manual pages --- --- Pick manual page (like described in |ft-man-plugin|). --- Notes: --- - Depends on |:Man| command to preview and choose items. --- - Shows page in the target window. Use |MiniPick-actions-choose| to split. --- ---@param local_opts __extra_pickers_local_opts --- Not used at the moment. ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.manpages = function(local_opts, opts) local pick = H.validate_pick('manpages') if vim.fn.exists(':Man') ~= 2 then H.error('`manpages` picker needs `:Man` command') end local latest_man_buf_id local show_man = function(win_id, item, is_preview) -- table.insert(_G.log, { item, name, section }) local name, section = (item or ''):match('^(.-)%s-%((.-)%)') if name == nil or section == nil then return end -- - Use first command name = name:gsub(',.*$', '') -- - Extract first valid section. NOTE: using only digits is not enough -- (for example, `man 1 man` and `man 1p man` are different). section = section:match('%w+') or '' -- Show man page directly in the target window vim.api.nvim_win_call(win_id, function() vim.cmd('hide Man ' .. section .. ' ' .. name) end) -- Ensure proper choose. Possibly undo `buflisted=false` from preview. if not is_preview then vim.bo.buflisted = true return end -- Ensure proper preview buffer vim.bo.buflisted, vim.bo.bufhidden, vim.bo.matchpairs = false, 'wipe', '' vim.b.minicursorword_disable = true vim.b.miniindentscope_disable = true -- - Modify after `:Man` to not have "Buffer with this name already exists" vim.api.nvim_buf_set_name(0, 'minipick://' .. vim.api.nvim_get_current_buf() .. '/preview') end local preview = function(buf_id, item) show_man(vim.fn.win_findbuf(buf_id)[1], item, true) end local choose = function(item) show_man(MiniPick.get_picker_state().windows.target, item) end -- Set necessary environment variables for `vim.loop.spawn` (as it doesn't -- inherit environment variables) local env = { 'MANWIDTH=999' } table.insert(env, vim.env.PATH ~= nil and ('PATH=' .. vim.env.PATH) or nil) table.insert(env, vim.env.MANPATH ~= nil and ('MANPATH=' .. vim.env.MANPATH) or nil) local source = { name = 'Manpages', choose = choose, preview = preview } opts = vim.tbl_deep_extend('force', { source = source }, opts or {}) return pick.builtin.cli({ command = { 'man', '-k', '.' }, spawn_opts = { env = env } }, opts) end --- Neovim marks picker --- --- Pick and preview position of Neovim |mark|s. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- - `(string)` - scope to show. One of "all", "global", "buf". --- Default: "all". ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.marks = function(local_opts, opts) local pick = H.validate_pick('marks') local_opts = vim.tbl_deep_extend('force', { scope = 'all' }, local_opts or {}) local scope = H.pick_validate_scope(local_opts, { 'all', 'global', 'buf' }, 'marks') -- Create items local items = {} local populate_items = function(mark_list) for _, info in ipairs(mark_list) do local path if type(info.file) == 'string' then path = vim.fn.fnamemodify(info.file, ':.') end local buf_id if path == nil then buf_id = info.pos[1] end local line, col = info.pos[2], math.abs(info.pos[3]) local text = string.format('%s │ %s%s│%s', info.mark:sub(2), path == nil and '' or (path .. '│'), line, col) table.insert(items, { text = text, bufnr = buf_id, path = path, lnum = line, col = col }) end end if scope == 'all' or scope == 'buf' then populate_items(vim.fn.getmarklist(vim.api.nvim_get_current_buf())) end if scope == 'all' or scope == 'global' then populate_items(vim.fn.getmarklist()) end local default_opts = { source = { name = string.format('Marks (%s)', scope) } } return H.pick_start(items, default_opts, opts) end --- Old files picker --- --- Pick from |v:oldfiles| entries representing readable files. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- - `(boolean)` - whether to return files only from current --- working directory and its subdirectories. Default: `false`. --- __extra_pickers_preserve_order ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.oldfiles = function(local_opts, opts) local pick = H.validate_pick('oldfiles') local_opts = vim.tbl_deep_extend('force', { current_dir = false, preserve_order = false }, local_opts or {}) local oldfiles = vim.v.oldfiles if not H.islist(oldfiles) then H.error('`pickers.oldfiles` picker needs valid `v:oldfiles`.') end local show_all = not local_opts.current_dir local items = vim.schedule_wrap(function() local cwd = H.normalize_path(pick.get_picker_opts().source.cwd) .. '/' local res = {} for _, path in ipairs(oldfiles) do path = H.normalize_path(path) if vim.fn.filereadable(path) == 1 and (show_all or vim.startswith(path, cwd)) then table.insert(res, H.short_path(path, cwd)) end end pick.set_picker_items(res) end) local show = H.pick_get_config().source.show or H.show_with_icons local match_opts = { preserve_order = local_opts.preserve_order } local match = function(stritems, inds, query) pick.default_match(stritems, inds, query, match_opts) end return H.pick_start(items, { source = { name = 'Old files', show = show, match = match } }, opts) end --- Neovim options picker --- --- Pick and preview data about Neovim options. --- Notes: --- - Item line is colored based on whether it was set (dimmed if wasn't). --- - Preview shows option value in target window and its general information. --- - Choosing places option name in Command line to update and apply. --- ---@param local_opts __extra_pickers_local_opts --- Possible fields: --- - `(string)` - options to show. One of "all", "global", "win", "buf". --- Default: "all". ---@param opts __extra_pickers_opts --- ---@return __extra_pickers_return MiniExtra.pickers.options = function(local_opts, opts) local pick = H.validate_pick('options') local_opts = vim.tbl_deep_extend('force', { scope = 'all' }, local_opts or {}) local scope = H.pick_validate_scope(local_opts, { 'all', 'global', 'win', 'buf' }, 'options') local items = {} for name, info in pairs(vim.api.nvim_get_all_options_info()) do if scope == 'all' or scope == info.scope then table.insert(items, { text = name, info = info }) end end table.sort(items, function(a, b) return a.text < b.text 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 if not item.info.was_set then H.pick_highlight_line(buf_id, i, 'Comment', 199) end end end local preview = function(buf_id, item) local pick_windows = pick.get_picker_state().windows local target_win_id = pick_windows.target if not H.is_valid_win(target_win_id) then target_win_id = pick_windows.main end local value_source = ({ global = 'o', win = 'wo', buf = 'bo' })[item.info.scope] local has_value, value = pcall(function() return vim.api.nvim_win_call(target_win_id, function() return vim[value_source][item.info.name] end) end) -- TODO: consider removing after Neovim<=0.10 compatibility is dropped if not has_value then value = '