Skip to content

Commit

Permalink
feat: cycle previewers
Browse files Browse the repository at this point in the history
- new git previewers
- jump to line in bcommit previewer
- vimdiff for bcommits
- dynamic preview window titles

Co-authored-by: Thore Strassburg <thore@weilbier.net>
  • Loading branch information
Conni2461 and weilbith committed May 26, 2021
1 parent 69eb5ea commit 535a4d1
Show file tree
Hide file tree
Showing 9 changed files with 403 additions and 73 deletions.
43 changes: 28 additions & 15 deletions lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ actions._close = function(prompt_bufnr, keepinsert)
local original_win_id = picker.original_win_id

if picker.previewer then
picker.previewer:teardown()
for _, v in ipairs(picker.all_previewers) do
v:teardown()
end
end

actions.close_pum(prompt_bufnr)
Expand Down Expand Up @@ -375,11 +377,10 @@ actions.git_checkout = function(prompt_bufnr)
end
end

-- TODO: add this function header back once the treesitter max-query bug is resolved
-- Switch to git branch
-- If the branch already exists in local, switch to that.
-- If the branch is only in remote, create new branch tracking remote and switch to new one.
--@param prompt_bufnr number: The prompt bufnr
--- Switch to git branch
--- If the branch already exists in local, switch to that.
--- If the branch is only in remote, create new branch tracking remote and switch to new one.
---@param prompt_bufnr number: The prompt bufnr
actions.git_switch_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
Expand Down Expand Up @@ -419,9 +420,8 @@ actions.git_track_branch = function(prompt_bufnr)
end
end

-- TODO: add this function header back once the treesitter max-query bug is resolved
-- Delete the currently selected branch
-- @param prompt_bufnr number: The prompt bufnr
--- Delete the currently selected branch
---@param prompt_bufnr number: The prompt bufnr
actions.git_delete_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
Expand All @@ -442,9 +442,8 @@ actions.git_delete_branch = function(prompt_bufnr)
end
end

-- TODO: add this function header back once the treesitter max-query bug is resolved
-- Rebase to selected git branch
-- @param prompt_bufnr number: The prompt bufnr
--- Rebase to selected git branch
---@param prompt_bufnr number: The prompt bufnr
actions.git_rebase_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
Expand All @@ -465,9 +464,15 @@ actions.git_rebase_branch = function(prompt_bufnr)
end
end

-- TODO: add this function header back once the treesitter max-query bug is resolved
-- Stage/unstage selected file
-- @param prompt_bufnr number: The prompt bufnr
--- Stage/unstage selected file
---@param prompt_bufnr number: The prompt bufnr
actions.git_checkout_current_buffer = function(prompt_bufnr)
local cwd = actions.get_current_picker(prompt_bufnr).cwd
local selection = actions.get_selected_entry()
actions.close(prompt_bufnr)
utils.get_os_command_output({ 'git', 'checkout', selection.value, '--', selection.file }, cwd)
end

actions.git_staging_toggle = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
Expand Down Expand Up @@ -597,6 +602,14 @@ actions.open_qflist = function(_)
vim.cmd [[copen]]
end

actions.cycle_previewers_next = function(prompt_bufnr)
actions.get_current_picker(prompt_bufnr):cycle_previewers(1)
end

actions.cycle_previewers_prev = function(prompt_bufnr)
actions.get_current_picker(prompt_bufnr):cycle_previewers(-1)
end

-- ==================================================
-- Transforms modules and sets the corect metatables.
-- ==================================================
Expand Down
74 changes: 70 additions & 4 deletions lua/telescope/builtin/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local pickers = require('telescope.pickers')
local previewers = require('telescope.previewers')
local utils = require('telescope.utils')
local entry_display = require('telescope.pickers.entry_display')
local path = require('telescope.path')

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

Expand Down Expand Up @@ -48,7 +49,12 @@ git.commits = function(opts)
results = results,
entry_maker = opts.entry_maker or make_entry.gen_from_git_commits(opts),
},
previewer = previewers.git_commit_diff.new(opts),
previewer = {
previewers.git_commit_diff_to_parent.new(opts),
previewers.git_commit_diff_to_head.new(opts),
previewers.git_commit_diff_as_was.new(opts),
previewers.git_commit_message.new(opts),
},
sorter = conf.file_sorter(opts),
attach_mappings = function()
actions.select_default:replace(actions.git_checkout)
Expand Down Expand Up @@ -76,9 +82,17 @@ git.stash = function(opts)
end
}):find()
end

local get_current_buf_line = function(winnr)
local lnum = vim.api.nvim_win_get_cursor(winnr)[1]
return vim.trim(vim.api.nvim_buf_get_lines(vim.api.nvim_win_get_buf(winnr), lnum - 1, lnum, false)[1])
end

git.bcommits = function(opts)
opts.current_line = (not opts.current_file) and get_current_buf_line(0) or nil
opts.current_file = opts.current_file or vim.fn.expand('%')
local results = utils.get_os_command_output({
'git', 'log', '--pretty=oneline', '--abbrev-commit', vim.fn.expand('%')
'git', 'log', '--pretty=oneline', '--abbrev-commit', opts.current_file
}, opts.cwd)

pickers.new(opts, {
Expand All @@ -87,10 +101,62 @@ git.bcommits = function(opts)
results = results,
entry_maker = opts.entry_maker or make_entry.gen_from_git_commits(opts),
},
previewer = previewers.git_commit_diff.new(opts),
previewer = {
previewers.git_commit_diff_to_parent.new(opts),
previewers.git_commit_diff_to_head.new(opts),
previewers.git_commit_diff_as_was.new(opts),
previewers.git_commit_message.new(opts),
},
sorter = conf.file_sorter(opts),
attach_mappings = function()
actions.select_default:replace(actions.git_checkout)
actions.select_default:replace(actions.git_checkout_current_buffer)
local transfrom_file = function()
return opts.current_file and path.make_relative(opts.current_file, opts.cwd)
end

local get_buffer_of_orig = function(selection)
local value = selection.value .. ':' .. transfrom_file()
local content = utils.get_os_command_output({ 'git', '--no-pager', 'show', value }, opts.cwd)

local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, content)
vim.api.nvim_buf_set_name(bufnr, 'Original')
return bufnr
end

local vimdiff = function(selection, command)
local ft = vim.bo.filetype
vim.cmd("diffthis")

local bufnr = get_buffer_of_orig(selection)
vim.cmd(string.format("%s %s", command, bufnr))
vim.bo.filetype = ft
vim.cmd("diffthis")

vim.cmd(string.format(
"autocmd WinClosed <buffer=%s> ++nested ++once :lua vim.api.nvim_buf_delete(%s, { force = true })",
bufnr,
bufnr))
end

actions.select_vertical:replace(function(prompt_bufnr)
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
vimdiff(selection, 'leftabove vert sbuffer')
end)

actions.select_horizontal:replace(function(prompt_bufnr)
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
vimdiff(selection, 'belowright sbuffer')
end)

actions.select_tab:replace(function(prompt_bufnr)
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
vim.cmd('tabedit ' .. transfrom_file())
vimdiff(selection, 'leftabove vert sbuffer')
end)
return true
end
}):find()
Expand Down
8 changes: 8 additions & 0 deletions lua/telescope/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ function config.set_defaults(defaults)

set("file_ignore_patterns", nil)

set("dynamic_preview_title", false, [[
Will change the title of the preview window dynamically, where it
is supported. Means the preview window will for example show the
full filename.
Default: false
]])

set("file_previewer", function(...) return require('telescope.previewers').vim_buffer_cat.new(...) end)
set("grep_previewer", function(...) return require('telescope.previewers').vim_buffer_vimgrep.new(...) end)
set("qflist_previewer", function(...) return require('telescope.previewers').vim_buffer_qflist.new(...) end)
Expand Down
5 changes: 3 additions & 2 deletions lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ function make_entry.gen_from_git_stash()
end


function make_entry.gen_from_git_commits()
function make_entry.gen_from_git_commits(opts)
local displayer = entry_display.create {
separator = " ",
items = {
Expand Down Expand Up @@ -287,7 +287,8 @@ function make_entry.gen_from_git_commits()
value = sha,
ordinal = sha .. ' ' .. msg,
msg = msg,
display = make_display
display = make_display,
current_file = opts.current_file
}
end
end
Expand Down
40 changes: 38 additions & 2 deletions lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ function Picker:new(opts)

finder = opts.finder,
sorter = opts.sorter or require('telescope.sorters').empty(),
previewer = opts.previewer,

all_previewers = opts.previewer,
current_previewer_index = 1,

default_selection_index = opts.default_selection_index,

cwd = opts.cwd,
Expand Down Expand Up @@ -124,6 +127,15 @@ function Picker:new(opts)

obj.get_window_options = opts.get_window_options or p_window.get_window_options

if obj.all_previewers ~= nil and obj.all_previewers ~= false then
if obj.all_previewers[1] == nil then
obj.all_previewers = { obj.all_previewers }
end
obj.previewer = obj.all_previewers[1]
else
obj.previewer = false
end

-- TODO: It's annoying that this is create and everything else is "new"
obj.scroller = p_scroller.create(
get_default(opts.scroll_strategy, config.values.scroll_strategy),
Expand Down Expand Up @@ -453,7 +465,9 @@ function Picker:find()

self.prompt_bufnr = prompt_bufnr

local preview_border_win = preview_opts and preview_opts.border and preview_opts.border.win_id
local preview_border = preview_opts and preview_opts.border
self.preview_border = preview_border
local preview_border_win = (preview_border and preview_border.win_id) and preview_border.win_id

state.set_status(prompt_bufnr, setmetatable({
prompt_bufnr = prompt_bufnr,
Expand Down Expand Up @@ -758,9 +772,31 @@ function Picker:refresh_previewer()
self._selection_entry,
status
)
if self.preview_border then
if config.values.dynamic_preview_title == true then
self.preview_border:change_title(self.previewer:dynamic_title(self._selection_entry))
else
self.preview_border:change_title(self.previewer:title())
end
end
end
end

function Picker:cycle_previewers(next)
local size = table.getn(self.all_previewers)
if size == 1 then return end

self.current_previewer_index = self.current_previewer_index + next
if self.current_previewer_index > size then
self.current_previewer_index = 1
elseif self.current_previewer_index < 1 then
self.current_previewer_index = size
end

self.previewer = self.all_previewers[self.current_previewer_index]
self:refresh_previewer()
end

function Picker:entry_adder(index, entry, _, insert)
if not entry then return end

Expand Down

0 comments on commit 535a4d1

Please sign in to comment.