feat: cycle prompt history (#521)

history is enabled on default but cycle_history_next and cycle_history_prev is not mapped yet

Example:
require('telescope').setup {
  defaults = {
    mappings = {
      i = {
        ["<C-Down>"] = require('telescope.actions').cycle_history_next,
        ["<C-Up>"] = require('telescope.actions').cycle_history_prev,
      }
    }
  }
}

For more information :help telescope.defaults.history

big thanks to clason and all other testers :)
This commit is contained in:
Simon Hauser
2021-07-09 20:45:29 +02:00
committed by GitHub
parent 385020eb23
commit 3699605627
9 changed files with 1098 additions and 625 deletions

View File

@@ -196,21 +196,53 @@ function actions.center(_)
vim.cmd(':normal! zz')
end
function actions.select_default(prompt_bufnr)
return action_set.select(prompt_bufnr, "default")
end
actions.select_default = {
pre = function(prompt_bufnr)
action_state.get_current_history():append(
action_state.get_current_line(),
action_state.get_current_picker(prompt_bufnr)
)
end,
action = function(prompt_bufnr)
return action_set.select(prompt_bufnr, "default")
end
}
function actions.select_horizontal(prompt_bufnr)
return action_set.select(prompt_bufnr, "horizontal")
end
actions.select_horizontal = {
pre = function(prompt_bufnr)
action_state.get_current_history():append(
action_state.get_current_line(),
action_state.get_current_picker(prompt_bufnr)
)
end,
action = function(prompt_bufnr)
return action_set.select(prompt_bufnr, "horizontal")
end
}
function actions.select_vertical(prompt_bufnr)
return action_set.select(prompt_bufnr, "vertical")
end
actions.select_vertical = {
pre = function(prompt_bufnr)
action_state.get_current_history():append(
action_state.get_current_line(),
action_state.get_current_picker(prompt_bufnr)
)
end,
action = function(prompt_bufnr)
return action_set.select(prompt_bufnr, "vertical")
end
}
function actions.select_tab(prompt_bufnr)
return action_set.select(prompt_bufnr, "tab")
end
actions.select_tab = {
pre = function(prompt_bufnr)
action_state.get_current_history():append(
action_state.get_current_line(),
action_state.get_current_picker(prompt_bufnr)
)
end,
action = function(prompt_bufnr)
return action_set.select(prompt_bufnr, "tab")
end
}
-- TODO: consider adding float!
-- https://github.com/nvim-telescope/telescope.nvim/issues/365
@@ -238,6 +270,7 @@ function actions.close_pum(_)
end
actions._close = function(prompt_bufnr, keepinsert)
action_state.get_current_history():reset()
local picker = action_state.get_current_picker(prompt_bufnr)
local prompt_win = state.get_status(prompt_bufnr).prompt_win
local original_win_id = picker.original_win_id
@@ -695,6 +728,33 @@ actions.complete_tag = function(prompt_bufnr)
end
actions.cycle_history_next = function(prompt_bufnr)
local history = action_state.get_current_history()
local current_picker = actions.get_current_picker(prompt_bufnr)
local line = action_state.get_current_line()
local entry = history:get_next(line, current_picker)
if entry == false then return end
current_picker:reset_prompt()
if entry ~= nil then
current_picker:set_prompt(entry)
end
end
actions.cycle_history_prev = function(prompt_bufnr)
local history = action_state.get_current_history()
local current_picker = actions.get_current_picker(prompt_bufnr)
local line = action_state.get_current_line()
local entry = history:get_prev(line, current_picker)
if entry == false then return end
if entry ~= nil then
current_picker:reset_prompt()
current_picker:set_prompt(entry)
end
end
--- Open the quickfix list
actions.open_qflist = function(_)
vim.cmd [[copen]]