[mini.extra:git_hunks] Intuitive show for git_hunks
#2190
Closed
drowning-cat
started this conversation in
Ideas
Replies: 2 comments 1 reply
-
This is not the case. Hunks are displayed using hunk header as a text. Git itself is responsible to compute a proper text and it is often not a part of context. It has somewhat smart per-language rules and can also be configured via Git. I think using this directly is the most practical and reasonable choice. |
Beta Was this translation helpful? Give feedback.
1 reply
-
MiniPick.registry.git_hunks = function(local_opts, opts)
local_opts = local_opts or {}
local show = function(buf, data, query)
opts = opts or {}
MiniPick.default_show(buf, data, query, opts)
-- NOTE: Replace `hunk header` with the first changed line [+-]
-- See https://github.com/nvim-mini/mini.nvim/discussions/2190
local lines = vim.api.nvim_buf_get_lines(buf, 0, #data, true)
for i = 1, #data do
local item, line = data[i], lines[i]
local first_changed = vim.iter(item.hunk):find(function(l)
return string.match(l, "^[+-]")
end)
if first_changed then
local row_0 = i - 1
local col_0 = string.match(line, "^.-│.-│()") - 1
line = string.gsub(first_changed, "^([+-])%s*", "%1 ")
vim.api.nvim_buf_set_text(buf, row_0, col_0, row_0, -1, { line })
end
end
end
-- NOTE: See https://github.com/nvim-mini/mini.nvim/discussions/2052#discussioncomment-14625845
local choose_marked = function(items)
if vim.tbl_isempty(items) then
items = { MiniPick.get_picker_matches().current }
end
local patches = {}
for _, item in ipairs(items) do
vim.list_extend(patches, item.header)
vim.list_extend(patches, item.hunk)
end
local cmd = { "git", "apply", "--cached" }
if local_opts.scope == "staged" then
table.insert(cmd, "--reverse")
end
vim.system(cmd, { stdin = patches })
end
return MiniExtra.pickers.git_hunks(
local_opts, --
vim.tbl_deep_extend("keep", opts or {}, { source = { show = show, choose_marked = choose_marked } })
)
end |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Contributing guidelines
Module(s)
mini.extra
Additional info
Description
Currently, hunks are displayed using the first context line as the item text, even when that line is empty. This can be visually confusing:
In my opinion, it would be better to display the first changed line (that starts with
+or-) instead.Beta Was this translation helpful? Give feedback.
All reactions