picker(git_branch): show committer, upstream and date (#695)

* wip: rich finder for branches

for: https://github.com/nvim-telescope/telescope.nvim/issues/569

* fix some diagnostics
This commit is contained in:
Kyoichiro Yamada
2021-03-30 03:04:25 +09:00
committed by GitHub
parent a7fa60411e
commit 2e03f67de9
3 changed files with 72 additions and 18 deletions

View File

@@ -401,7 +401,11 @@ files.tags = function(opts)
end end
files.current_buffer_tags = function(opts) files.current_buffer_tags = function(opts)
return files.tags(vim.tbl_extend("force", {prompt_title = 'Current Buffer Tags', only_current_file = true, hide_filename = true}, opts)) return files.tags(vim.tbl_extend("force", {
prompt_title = 'Current Buffer Tags',
only_current_file = true,
hide_filename = true,
}, opts))
end end
local function apply_checks(mod) local function apply_checks(mod)

View File

@@ -5,6 +5,7 @@ local make_entry = require('telescope.make_entry')
local pickers = require('telescope.pickers') local pickers = require('telescope.pickers')
local previewers = require('telescope.previewers') local previewers = require('telescope.previewers')
local utils = require('telescope.utils') local utils = require('telescope.utils')
local entry_display = require('telescope.pickers.entry_display')
local conf = require('telescope.config').values local conf = require('telescope.config').values
@@ -77,17 +78,70 @@ git.bcommits = function(opts)
end end
git.branches = function(opts) git.branches = function(opts)
local output = utils.get_os_command_output({ 'git', 'branch', '--all' }, opts.cwd) local format = '{'
.. '"head":%(if:equals=*)%(HEAD)%(then)true%(else)false%(end)'
.. ',"refname":"%(refname)"'
.. ',"authorname":"%(authorname)"'
.. '%(if)%(upstream)%(then)'
.. ',"upstream":"%(upstream:lstrip=2)"'
.. '%(else)'
.. ',"upstream":""'
.. '%(end)'
.. ',"committerdate":"%(committerdate:format-local:%Y/%m/%d %H:%M:%S)"'
.. '}'
local output = utils.get_os_command_output({ 'git', 'for-each-ref', '--format', format }, opts.cwd)
local results = {} local results = {}
for _, v in ipairs(output) do local widths = {
if not string.match(v, 'HEAD') and v ~= '' then name = 0,
if vim.startswith(v, '*') then authorname = 0,
table.insert(results, 1, v) upstream = 0,
else committerdate = 0,
table.insert(results, v) }
end local register_entry = function(entry, trim_refname_prefix)
entry.name = string.sub(entry.refname, string.len(trim_refname_prefix)+1)
for key, value in pairs(widths) do
widths[key] = math.max(value, vim.fn.strdisplaywidth(entry[key]))
end end
if string.len(entry.upstream) > 0 then
widths.upstream_indicator = 2
end
table.insert(results, entry)
end
for _, v in ipairs(output) do
local entry = vim.fn.json_decode(v)
if entry.head then
goto continue
elseif vim.startswith(entry.refname, 'refs/remotes/') then
register_entry(entry, 'refs/remotes/')
elseif vim.startswith(entry.refname, 'refs/heads/') then
register_entry(entry, 'refs/heads/')
end
::continue::
end
if #results == 0 then
return
end
local displayer = entry_display.create {
separator = " ",
items = {
{ width = widths.name },
{ width = widths.authorname },
{ width = widths.upstream_indicator },
{ width = widths.upstream },
{ width = widths.committerdate },
}
}
local make_display = function(entry)
return displayer {
{entry.name, 'TelescopeResultsIdentifier'},
{entry.authorname},
{string.len(entry.upstream) > 0 and '=>' or ''},
{entry.upstream, 'TelescopeResultsIdentifier'},
{entry.committerdate}
}
end end
pickers.new(opts, { pickers.new(opts, {
@@ -95,14 +149,10 @@ git.branches = function(opts)
finder = finders.new_table { finder = finders.new_table {
results = results, results = results,
entry_maker = function(entry) entry_maker = function(entry)
local addition = vim.startswith(entry, '*') and '* ' or ' ' entry.value = entry.name
entry = entry:gsub('[* ] ', '') entry.ordinal = entry.name
entry = entry:gsub('^remotes/', '') entry.display = make_display
return { return entry
value = entry,
ordinal = addition .. entry,
display = addition .. entry
}
end end
}, },
previewer = previewers.git_branch_log.new(opts), previewer = previewers.git_branch_log.new(opts),

View File

@@ -43,7 +43,7 @@ end
function Highlighter:clear_display() function Highlighter:clear_display()
if not self if not self
or not self.picker or not self.picker
or not self.picker.results_bufnr or not self.picker.results_bufnr
or not vim.api.nvim_buf_is_valid(self.picker.results_bufnr) or not vim.api.nvim_buf_is_valid(self.picker.results_bufnr)
then then
return return