Skip to content

Commit

Permalink
feat: make line_blame() preview be configurable with formatting string
Browse files Browse the repository at this point in the history
  • Loading branch information
danielleontiev committed Jun 16, 2023
1 parent 256569c commit bbf027e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 45 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ require('gitsigns').setup {
update_debounce = 100,
status_formatter = nil, -- Use default
max_file_length = 40000, -- Disable if file is longer than this (in lines)
line_blame_formatter = {
{{'<abbrev_sha> ', 'Directory'}, {'<author> ', 'MoreMsg'}, {'(<author_time:%Y-%m-%d %H:%M>)', 'Label'}, {':', 'NormalFloat'}},
{{'<summary>', 'NormalFloat'}},

-- example of the blame format that previously was achieved by
-- running blame_line { full = true }
-- {{'<abbrev_sha> ', 'Directory'}, {'<author> ', 'MoreMsg'}, {'(<author_time:%Y-%m-%d %H:%M>)', 'Label'}, {':', 'NormalFloat'}},
-- {{'<body>', 'NormalFloat'}},
-- {{'', 'Label'}},
-- {{'Hunk <hunk_no> of <num_hunks>', 'Title'}, {' <hunk_head>', 'LineNr'}},
-- {{'<hunk>', 'NormalFloat'}}
},
line_blame_ignore_whitespace = false,
preview_config = {
-- Options passed to nvim_open_win
border = 'single',
Expand Down
38 changes: 29 additions & 9 deletions doc/gitsigns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ of the default settings:
update_debounce = 100,
status_formatter = nil, -- Use default
max_file_length = 40000, -- Disable if file is longer than this (in lines)
line_blame_formatter = {
{{'<abbrev_sha> ', 'Directory'}, {'<author> ', 'MoreMsg'}, {'(<author_time:%Y-%m-%d %H:%M>)', 'Label'}, {':', 'NormalFloat'}},
{{'<summary>', 'NormalFloat'}},
-- example of the blame format that previously was achieved by
-- running blame_line { full = true }
-- {{'<abbrev_sha> ', 'Directory'}, {'<author> ', 'MoreMsg'}, {'(<author_time:%Y-%m-%d %H:%M>)', 'Label'}, {':', 'NormalFloat'}},
-- {{'<body>', 'NormalFloat'}},
-- {{'', 'Label'}},
-- {{'Hunk <hunk_no> of <num_hunks>', 'Title'}, {' <hunk_head>', 'LineNr'}},
-- {{'<hunk>', 'NormalFloat'}}
},
line_blame_ignore_whitespace = false,
preview_config = {
-- Options passed to nvim_open_win
border = 'single',
Expand Down Expand Up @@ -290,19 +303,11 @@ change_base({base}, {global}) *gitsigns.change_base()*
For a more complete list of ways to specify bases, see
|gitsigns-revision|.

blame_line({opts}) *gitsigns.blame_line()*
blame_line() *gitsigns.blame_line()*
Run git blame on the current line and show the results in a
floating window. If already open, calling this will cause the
window to get focus.

Parameters: ~
{opts} (table|nil):
Additional options:
{full}: (boolean)
Display full commit message with hunk.
• {ignore_whitespace}: (boolean)
Ignore whitespace when running blame.

Attributes: ~
{async}

Expand Down Expand Up @@ -695,6 +700,21 @@ max_file_length *gitsigns-config-max_file_length*

Max file length (in lines) to attach to.

line_blame_formatter *gitsigns-config-line_blame_formatter*
Type: `{{{string}}}`
Default: >
{
{{'<abbrev_sha> ', 'Directory'}, {'<author> ', 'MoreMsg'}, {'(<author_time:%Y-%m-%d %H:%M>)', 'Label'}, {':', 'NormalFloat'}},
{{'<summary>', 'NormalFloat'}},
}
<
Line blame formatter string.

line_blame_ignore_whitespace *gitsigns-config-line_blame_ignore_whitespace*
Type: `boolean`, Default: `false`

Ignore whitespace when running blame.

preview_config *gitsigns-config-preview_config*
Type: `table[extended]`
Default: >
Expand Down
68 changes: 32 additions & 36 deletions lua/gitsigns/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,30 @@ M.get_hunks = function(bufnr)
return ret
end

-- drops empty lines from the beginning and the end
local function trim(lines)
local start_index = 1
local end_index = #lines

-- Find the first non-empty line from the beginning
while start_index <= #lines and lines[start_index] == "" do
start_index = start_index + 1
end

-- Find the last non-empty line from the end
while end_index > start_index and lines[end_index] == "" do
end_index = end_index - 1
end

-- Return the trimmed lines
local trimmed_lines = {}
for i = start_index, end_index do
table.insert(trimmed_lines, lines[i])
end

return trimmed_lines
end

--- @param repo Gitsigns.Repo
--- @param info Gitsigns.BlameInfo
--- @return Gitsigns.Hunk.Hunk?, integer?, integer
Expand All @@ -811,56 +835,27 @@ local function get_blame_hunk(repo, info)
return hunk, i, #hunks
end

local function create_blame_fmt(is_committed, full)
local function create_blame_fmt(is_committed)
if not is_committed then
return {
{ { '<author>', 'Label' } },
}
end

local header = {
{ '<abbrev_sha> ', 'Directory' },
{ '<author> ', 'MoreMsg' },
{ '(<author_time:%Y-%m-%d %H:%M>)', 'Label' },
{ ':', 'NormalFloat' },
}

if full then
return {
header,
{ { '<body>', 'NormalFloat' } },
{ { 'Hunk <hunk_no> of <num_hunks>', 'Title' }, { ' <hunk_head>', 'LineNr' } },
{ { '<hunk>', 'NormalFloat' } },
}
end

return {
header,
{ { '<summary>', 'NormalFloat' } },
}
return vim.deepcopy(config.line_blame_formatter)
end

--- Run git blame on the current line and show the results in a
--- floating window. If already open, calling this will cause the
--- window to get focus.
---
--- Parameters: ~
--- {opts} (table|nil):
--- Additional options:
--- • {full}: (boolean)
--- Display full commit message with hunk.
--- • {ignore_whitespace}: (boolean)
--- Ignore whitespace when running blame.
---
--- Attributes: ~
--- {async}
M.blame_line = void(function(opts)
M.blame_line = void(function()
if popup.focus_open('blame') then
return
end

opts = opts or {}

local bufnr = current_buf()
local bcache = cache[bufnr]
if not bcache then
Expand All @@ -875,7 +870,7 @@ M.blame_line = void(function(opts)
local buftext = util.buf_lines(bufnr)
local fileformat = vim.bo[bufnr].fileformat
local lnum = api.nvim_win_get_cursor(0)[1]
local result = bcache.git_obj:run_blame(buftext, lnum, opts.ignore_whitespace)
local result = bcache.git_obj:run_blame(buftext, lnum, config.line_blame_ignore_whitespace)
pcall(function()
loading:close()
end)
Expand All @@ -884,10 +879,11 @@ M.blame_line = void(function(opts)

local is_committed = result.sha and tonumber('0x' .. result.sha) ~= 0

local blame_fmt = create_blame_fmt(is_committed, opts.full)
local blame_fmt = create_blame_fmt(is_committed)

if is_committed and opts.full then
result.body = bcache.git_obj:command({ 'show', '-s', '--format=%B', result.sha })
if is_committed then
local body = bcache.git_obj:command({ 'show', '-s', '--format=%B', result.sha })
result.body = trim(body)

local hunk

Expand Down
21 changes: 21 additions & 0 deletions lua/gitsigns/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ end
--- @field current_line_blame_formatter string|Gitsigns.CurrentLineBlameFmtFun
--- @field current_line_blame_formatter_nc string|Gitsigns.CurrentLineBlameFmtFun
--- @field current_line_blame_opts Gitsigns.CurrentLineBlameOpts
--- @field line_blame_formatter table<table<table<string>>>
--- @field line_blame_ignore_whitespace boolean
--- @field preview_config table<string,any>
--- @field attach_to_untracked boolean
--- @field yadm { enable: boolean }
Expand Down Expand Up @@ -493,6 +495,25 @@ M.schema = {
]],
},

line_blame_formatter = {
type = 'table',
default = {
{{'<abbrev_sha> ', 'Directory'}, {'<author> ', 'MoreMsg'}, {'(<author_time:%Y-%m-%d %H:%M>)', 'Label'}, {':', 'NormalFloat'}},
{{'<summary>', 'NormalFloat'}},
},
description = [[
Line blame formatter string.
]],
},

line_blame_ignore_whitespace = {
type = 'boolean',
default = false,
description = [[
Ignore whitespace when running blame.
]],
},

preview_config = {
type = 'table',
deep_extend = true,
Expand Down

0 comments on commit bbf027e

Please sign in to comment.