Skip to content

Commit

Permalink
feat: quickfixhistory picker (nvim-telescope#1878)
Browse files Browse the repository at this point in the history
  • Loading branch information
fdschmidt93 authored May 4, 2022
1 parent c93276a commit 8d1841b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ Built-in functions. Ready to be bound to any key you like.
| `builtin.marks` | Lists vim marks and their value |
| `builtin.colorscheme` | Lists available colorschemes and applies them on `<cr>` |
| `builtin.quickfix` | Lists items in the quickfix list |
| `builtin.quickfixhistory` | Lists all quickfix lists in your history and open them with `builtin.quickfix` |
| `builtin.loclist` | Lists items from the current window's location list |
| `builtin.jumplist` | Lists Jump List entries |
| `builtin.vim_options` | Lists vim options, allows you to edit the current value on `<cr>` |
Expand Down
11 changes: 11 additions & 0 deletions doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,17 @@ builtin.quickfix({opts}) *telescope.builtin.quickfix()*
Options: ~
{ignore_filename} (boolean) dont show filenames (default: true)
{trim_text} (boolean) trim results text (default: false)
{nr} (number) specify the quickfix list number


builtin.quickfixhistory({opts}) *telescope.builtin.quickfixhistory()*
Lists all quickfix lists in your history and open them with
`builtin.quickfix`. It seems that neovim only keeps the full history for 10
lists


Parameters: ~
{opts} (table) options to pass to the picker


builtin.loclist({opts}) *telescope.builtin.loclist()*
Expand Down
6 changes: 6 additions & 0 deletions lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -794,12 +794,15 @@ local send_selected_to_qf = function(prompt_bufnr, mode, target)
table.insert(qf_entries, entry_to_qf(entry))
end

local prompt = picker:_get_prompt()
actions.close(prompt_bufnr)

if target == "loclist" then
vim.fn.setloclist(picker.original_win_id, qf_entries, mode)
else
local qf_title = string.format([[%s (%s)]], picker.prompt_title, prompt)
vim.fn.setqflist(qf_entries, mode)
vim.fn.setqflist({}, "a", { title = qf_title })
end
end

Expand All @@ -812,12 +815,15 @@ local send_all_to_qf = function(prompt_bufnr, mode, target)
table.insert(qf_entries, entry_to_qf(entry))
end

local prompt = picker:_get_prompt()
actions.close(prompt_bufnr)

if target == "loclist" then
vim.fn.setloclist(picker.original_win_id, qf_entries, mode)
else
vim.fn.setqflist(qf_entries, mode)
local qf_title = string.format([[%s (%s)]], picker.prompt_title, prompt)
vim.fn.setqflist({}, "a", { title = qf_title })
end
end

Expand Down
6 changes: 6 additions & 0 deletions lua/telescope/builtin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,14 @@ builtin.commands = require_on_exported_call("telescope.builtin.internal").comman
---@param opts table: options to pass to the picker
---@field ignore_filename boolean: dont show filenames (default: true)
---@field trim_text boolean: trim results text (default: false)
---@field nr number: specify the quickfix list number
builtin.quickfix = require_on_exported_call("telescope.builtin.internal").quickfix

--- Lists all quickfix lists in your history and open them with `builtin.quickfix`. It seems that neovim
--- only keeps the full history for 10 lists
---@param opts table: options to pass to the picker
builtin.quickfixhistory = require_on_exported_call("telescope.builtin.internal").quickfixhistory

--- Lists items from the current window's location list, jumps to location on `<cr>`
---@param opts table: options to pass to the picker
---@field ignore_filename boolean: dont show filenames (default: true)
Expand Down
62 changes: 61 additions & 1 deletion lua/telescope/builtin/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ internal.commands = function(opts)
end

internal.quickfix = function(opts)
local locations = vim.fn.getqflist()
local qf_identifier = opts.id or vim.F.if_nil(opts.nr, "$")
local locations = vim.fn.getqflist({ [opts.id and "id" or "nr"] = qf_identifier, items = true }).items

if vim.tbl_isempty(locations) then
return
Expand All @@ -376,6 +377,65 @@ internal.quickfix = function(opts)
}):find()
end

internal.quickfixhistory = function(opts)
local qflists = {}
for i = 1, 10 do -- (n)vim keeps at most 10 quickfix lists in full
-- qf weirdness: id = 0 gets id of quickfix list nr
local qflist = vim.fn.getqflist { nr = i, id = 0, title = true, items = true }
if not vim.tbl_isempty(qflist.items) then
table.insert(qflists, qflist)
end
end
local entry_maker = opts.make_entry
or function(entry)
return {
value = entry.title or "Untitled",
ordinal = entry.title or "Untitled",
display = entry.title or "Untitled",
nr = entry.nr,
id = entry.id,
items = entry.items,
}
end
local qf_entry_maker = make_entry.gen_from_quickfix(opts)
pickers.new(opts, {
prompt_title = "Quickfix History",
finder = finders.new_table {
results = qflists,
entry_maker = entry_maker,
},
previewer = previewers.new_buffer_previewer {
title = "Quickfix List Preview",
dyn_title = function(_, entry)
return entry.title
end,

get_buffer_by_name = function(_, entry)
return "quickfixlist_" .. tostring(entry.nr)
end,

define_preview = function(self, entry)
if self.state.bufname then
return
end
local entries = vim.tbl_map(function(i)
return qf_entry_maker(i):display()
end, entry.items)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, entries)
end,
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
action_set.select:replace(function(prompt_bufnr)
local nr = action_state.get_selected_entry().nr
actions.close(prompt_bufnr)
internal.quickfix { nr = nr }
end)
return true
end,
}):find()
end

internal.loclist = function(opts)
local locations = vim.fn.getloclist(0)
local filenames = {}
Expand Down

0 comments on commit 8d1841b

Please sign in to comment.