Skip to content

Commit

Permalink
WIP: Cycle prompt history
Browse files Browse the repository at this point in the history
  • Loading branch information
Conni2461 committed Feb 9, 2021
1 parent 3a7fa41 commit 31a467e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
64 changes: 63 additions & 1 deletion lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,50 @@ local a = vim.api

local log = require('telescope.log')
local path = require('telescope.path')
local ppath = require('plenary.path')
local state = require('telescope.state')
local utils = require('telescope.utils')

local transform_mod = require('telescope.actions.mt').transform_mod

local conf = require('telescope.config').values

local history_cache
local gen_history_cache = function()
if not history_cache then
history_cache = {}
local p = ppath:new(vim.fn.expand(conf.telescope_history))
if not p:exists() then p:touch({ parents = true }) end
history_cache.path = p
end
end

local ensure_history = function()
if not conf.telescope_history then return end
gen_history_cache()
if not history_cache.content then
history_cache.content = history_cache.path:readlines()
table.remove(history_cache.content, table.getn(history_cache.content))
history_cache.index = table.getn(history_cache.content) + 1
end
return true
end

local append_to_history = function(line)
if not conf.telescope_history then return end
gen_history_cache()
ensure_history()
-- TODO(conni2461): WINDOWS? :sob:
if line ~= '' then
line = line .. '\n'
if history_cache.content[table.getn(history_cache.content)] ~= line then
history_cache.path:write_text(line, 'a')
end
end
history_cache.content = nil
history_cache.index = nil
end

local actions = setmetatable({}, {
__index = function(_, k)
error("Actions does not have a value: " .. tostring(k))
Expand Down Expand Up @@ -68,7 +107,8 @@ end

-- TODO: It seems sometimes we get bad styling.
function actions._goto_file_selection(prompt_bufnr, command)
local entry = actions.get_selected_entry(prompt_bufnr)
local entry = actions.get_selected_entry()
append_to_history(actions.get_current_line())

if not entry then
print("[telescope] Nothing currently selected")
Expand Down Expand Up @@ -322,6 +362,28 @@ actions.send_to_qflist = function(prompt_bufnr)
vim.fn.setqflist(qf_entries, 'r')
end

actions.cycle_history_next = function(prompt_bufnr)
if not ensure_history() then return end
local current_picker = actions.get_current_picker(prompt_bufnr)
local next_idx = history_cache.index + 1
if next_idx <= table.getn(history_cache.content) then
history_cache.index = next_idx
current_picker:reset_prompt()
current_picker:set_prompt(history_cache.content[next_idx]:sub(1, -2))
end
end

actions.cycle_history_prev = function(prompt_bufnr)
if not ensure_history() then return end
local current_picker = actions.get_current_picker(prompt_bufnr)
local next_idx = history_cache.index - 1
if next_idx >= 1 then
history_cache.index = next_idx
current_picker:reset_prompt()
current_picker:set_prompt(history_cache.content[next_idx])
end
end

actions.open_qflist = function(_)
vim.cmd [[copen]]
end
Expand Down
1 change: 1 addition & 0 deletions lua/telescope/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function config.set_defaults(defaults)
set("file_sorter", sorters.get_fuzzy_file)

set("file_ignore_patterns", nil)
set("telescope_history", nil)

set("file_previewer", function(...) return require('telescope.previewers').cat.new(...) end)
set("grep_previewer", function(...) return require('telescope.previewers').vimgrep.new(...) end)
Expand Down
21 changes: 20 additions & 1 deletion lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ function Picker:find()
pcall(a.nvim_buf_set_option, prompt_bufnr, 'filetype', 'TelescopePrompt')

if self.default_text then
vim.api.nvim_buf_set_lines(prompt_bufnr, 0, 1, false, {self.default_text})
self:set_prompt(self.default_text)
end

if self.initial_mode == "insert" then
Expand All @@ -531,6 +531,25 @@ function Picker:hide_preview()
-- 2. Resize prompt & results windows accordingly
end

function Picker:reset_prompt()
vim.api.nvim_buf_set_lines(self.prompt_bufnr, 0, -1, false, {})
if self.prompt_prefix ~= '' then
vim.api.nvim_buf_add_highlight(self.prompt_bufnr,
ns_telescope_prompt_prefix,
'TelescopePromptPrefix',
0,
0,
#self.prompt_prefix
)
end
end

function Picker:set_prompt(str)
-- TODO(conni2461): As soon as prompt_buffers are fix use this:
-- vim.api.nvim_buf_set_lines(self.prompt_bufnr, 0, 1, false, { str })
-- vim.api.nvim_buf_set_text(self.prompt_bufnr, 0, 0, 0, #str, { str })
vim.api.nvim_feedkeys(str, 'n', false)
end

function Picker.close_windows(status)
local prompt_win = status.prompt_win
Expand Down

0 comments on commit 31a467e

Please sign in to comment.