Skip to content

Commit

Permalink
wip: loclisthistory
Browse files Browse the repository at this point in the history
DON'T EXPECT THIS TO BE UPSTREAMED due to:

* nvim-telescope#1228
* https://github.com/nvim-telescope/telescope.nvim/blob/master/CONTRIBUTING.md

THIS IS MY PERSONAL changes.

port original patch for quickfixhistory picker while considering file name changes etc.

* 8d1841b ("feat: quickfixhistory picker (nvim-telescope#1878)", 2022-05-04)
* 0621c1c ("break: prefix internal files and add deprecation messages (nvim-telescope#2032)", 2022-07-01)

Related:

* nvim-telescope#1739
* nvim-telescope#1742
* https://github.com/cgsheeh/telescope.nvim (forked repo)

TODO: make this external

https://github.com/nvim-telescope/telescope.nvim/blob/master/developers.md

Signed-off-by: Osamu Aoki <osamu@debian.org>
  • Loading branch information
osamuaoki committed Jun 26, 2024
1 parent 61a4a61 commit 17dfd21
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ Built-in functions. Ready to be bound to any key you like.
| `builtin.quickfix` | Lists items in the quickfix list |
| `builtin.quickfixhistory` | Lists all quickfix lists in your history and open them with `builtin.quickfix` or quickfix window |
| `builtin.loclist` | Lists items from the current window's location list |
| `builtin.loclisthistory` | Lists all location lists in your history and open them with `builtin.locklist` or location list window |
| `builtin.jumplist` | Lists Jump List entries |
| `builtin.vim_options` | Lists vim options, allows you to edit the current value on `<cr>` |
| `builtin.registers` | Lists vim registers, pastes the contents of the register on `<cr>` |
Expand Down
12 changes: 12 additions & 0 deletions doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,18 @@ builtin.loclist({opts}) *telescope.builtin.loclist()*
Options: ~
{show_line} (boolean) show results text (default: true)
{trim_text} (boolean) trim results text (default: false)
{nr} (number) specify the location list number


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


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



builtin.oldfiles({opts}) *telescope.builtin.oldfiles()*
Expand Down
2 changes: 2 additions & 0 deletions lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,9 @@ local send_selected_to_qf = function(prompt_bufnr, mode, target)

vim.api.nvim_exec_autocmds("QuickFixCmdPre", {})
if target == "loclist" then
local qf_title = string.format([[%s (%s)]], picker.prompt_title, prompt)
vim.fn.setloclist(picker.original_win_id, qf_entries, mode)
vim.fn.setloclist(picker.original_win_id, {}, "a", { title = qf_title })
else
local qf_title = string.format([[%s (%s)]], picker.prompt_title, prompt)
vim.fn.setqflist(qf_entries, mode)
Expand Down
71 changes: 70 additions & 1 deletion lua/telescope/builtin/__internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ internal.quickfixhistory = function(opts)
end

internal.loclist = function(opts)
local locations = vim.fn.getloclist(0)
local qf_identifier = opts.id or vim.F.if_nil(opts.nr, "$")
local locations = vim.fn.getloclist(0, { [opts.id and "id" or "nr"] = qf_identifier, items = true }).items
local filenames = {}
for _, value in pairs(locations) do
local bufnr = value.bufnr
Expand Down Expand Up @@ -522,6 +523,74 @@ internal.loclist = function(opts)
:find()
end

internal.loclisthistory = function(opts)
local qflists = {}
for i = 1, 10 do -- (n)vim keeps at most 10 location lists in full
-- qf weirdness: id = 0 gets id of location list nr
local qflist = vim.fn.getloclist(0, { 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 make_entry.set_default_entry_mt({
value = entry.title or "Untitled",
ordinal = entry.title or "Untitled",
display = entry.title or "Untitled",
nr = entry.nr,
id = entry.id,
items = entry.items,
}, opts)
end
local qf_entry_maker = make_entry.gen_from_quickfix(opts)
pickers
.new(opts, {
prompt_title = "Location History",
finder = finders.new_table {
results = qflists,
entry_maker = entry_maker,
},
previewer = previewers.new_buffer_previewer {
title = "Location 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(_, map)
action_set.select:replace(function(prompt_bufnr)
local nr = action_state.get_selected_entry().nr
actions.close(prompt_bufnr)
internal.loclist { nr = nr }
end)

map({ "i", "n" }, "<C-q>", function(prompt_bufnr)
local nr = action_state.get_selected_entry().nr
actions.close(prompt_bufnr)
vim.cmd(nr .. "lhistory")
vim.cmd "botright lopen"
end)
return true
end,
})
:find()
end

internal.oldfiles = function(opts)
opts = apply_cwd_only_aliases(opts)
opts.include_current_session = vim.F.if_nil(opts.include_current_session, true)
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 @@ -294,8 +294,14 @@ builtin.quickfixhistory = require_on_exported_call("telescope.builtin.__internal
---@param opts table: options to pass to the picker
---@field show_line boolean: show results text (default: true)
---@field trim_text boolean: trim results text (default: false)
---@field nr number: specify the location list number
builtin.loclist = require_on_exported_call("telescope.builtin.__internal").loclist

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

--- Lists previously open files, opens on `<cr>`
---@param opts table: options to pass to the picker
---@field cwd string: specify a working directory to filter oldfiles by
Expand Down
5 changes: 4 additions & 1 deletion lua/telescope/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ mappings.default_mappings = config.values.default_mappings
["<S-Tab>"] = actions.toggle_selection + actions.move_selection_better,
["<C-q>"] = actions.send_to_qflist + actions.open_qflist,
["<M-q>"] = actions.send_selected_to_qflist + actions.open_qflist,
["<C-l>"] = actions.complete_tag,
["<C-l>"] = actions.send_to_loclist + actions.open_loclist,
["<M-l>"] = actions.send_selected_to_loclist + actions.open_loclist,
--["<C-l>"] = actions.complete_tag, -- duplicate mapping move to "m"
["<C-m>"] = actions.complete_tag,
["<C-/>"] = actions.which_key,
["<C-_>"] = actions.which_key, -- keys from pressing <C-/>
["<C-w>"] = { "<c-s-w>", type = "command" },
Expand Down

0 comments on commit 17dfd21

Please sign in to comment.