-
-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconvenient Lua API #1030
Comments
Looks like you are trying to use the functions outside their intended use. Consider using |
Well, yeah, since Lua API is designed for convenient mapping, not programming :( While I can use
I would like to split this into following more concrete issues:
Of course, this whole "feature" (minus the new Vim tab) can be internally implemented inside of plugin and exposed as a function, but I'm not sure how many people would use it, so it's questionable. What do you think, should I proceed and create proposed issues? |
You can create issues, but I'll consider each separately, in conjunction with all other issues and feature requests. There would also be no timeline for these either since I use this plugin mostly for myself and have limited time. |
No problem, I fully understand the situation you're in. I guess I'll just spawn smaller tickets aiming to achieve this mapping as I go, given that behavior is constantly changed by your work. At the very least, your last commit solved this one already:
|
Hey @lewis6991, with latest changes I'm able to get (mostly) desired behavior 🎉 vim.keymap.set(
'n',
'<Enter>',
function()
-- Open popup window with commit SHA of blamed line.
gitsigns.blame_line(
{ full = false },
function()
-- In order to focus opened popup window, blame_line needs to be called again.
gitsigns.blame_line({},
function()
-- Now that popup is focused, extract commit SHA from the start of it.
local blamed_commit = vim.fn.getline(1):match("^(%x+)")
-- Close the focused popup.
vim.cmd(":quit")
-- If commit is unavailable (i.e. local changes), reopen the popup window, but without focus.
if blamed_commit == nil then
gitsigns.blame_line()
return
end
-- Create new tab which is viewing current version of the file.
vim.cmd(":tabnew %")
-- React to opening of the gitsigns.show (below) and diff it.
-- FIXME: Note that diff doesn't work until all 3 updates happened, so ignore first two.
local callback_count = 0
vim.api.nvim_create_autocmd("User", {
pattern = "GitSignsUpdate",
callback = function()
callback_count = callback_count + 1
if (callback_count < 3) then
return false
end
-- Diff blamed version of the file with its parent's version of the same file.
gitsigns.diffthis(blamed_commit .. "^")
return true
end,
})
-- Open and focus blamed version of the file in new tab.
gitsigns.show(blamed_commit)
end)
end)
end,
{ buffer = buffer_number, desc = "[Enter] the diff mode between current's line commit and its parent" }) Further improvements:
Obviously, last two can be handled on the user side (for last one, it may even be the best solution), but I really think first one should be somehow handled in the plugin itself. Therefore, I'll open another issue for that one. (Done) I know there are no timelines and appreciate you working on this at all, but if you have some immediate feedback/suggestion, I'd like to hear them! |
Latest version of Gitsigns has fixed the main issue so it's now possible to actually have the <Enter> mapping work in most cases. Code is even nastier and more error-prone, but it'll likely improve with better support from Gitsigns plugin. Ongoing work is captured here: lewis6991/gitsigns.nvim#1030
Hey @lewis6991, after recent fixes, I think this one is in quite good shape now. For the reference, I ended up with following implementation for now: vim.keymap.set(
'n',
'<Enter>',
function()
-- Open popup window with commit SHA of blamed line.
gitsigns.blame_line(
{ full = false },
function()
-- In order to focus opened popup window, blame_line needs to be called again.
gitsigns.blame_line({},
function()
-- Now that popup is focused, extract commit SHA from the start of it.
local blamed_commit = vim.fn.getline(1):match("^(%x+)")
-- Close the focused popup.
vim.cmd(":quit")
-- If commit is unavailable (i.e. local changes), reopen the popup window, but without focus.
if blamed_commit == nil then
gitsigns.blame_line()
return
end
-- Compute parent commit of the blamed one.
local blamed_commit_parent = blamed_commit .. "^"
local result = vim.system({ "git", "rev-parse", "--short", blamed_commit_parent }):wait()
if result.code ~= 0 then
print("Ignoring the command since commit " .. blamed_commit_parent .. " doesn't exist!")
return
end
-- Use concrete commit SHA rather than SHA and ^ notation.
blamed_commit_parent = result.stdout:match("%S+")
-- Create new tab which is viewing current version of the file.
vim.cmd(":tabnew %")
-- Use new tab to show changes that blamed commit introduced.
gitsigns.show(blamed_commit, function() gitsigns.diffthis(blamed_commit_parent) end)
end)
end)
end,
{ buffer = buffer_number, desc = "[Enter] new tab and show previous changes that modified current line" })
|
Description
Thank you for this plugin and sorry for the long post (I wrote it locally, before seeing the template 😞 )!
I'm well aware that this contains a mix of bugs, feature requests and questions for help, but I honestly believe
that as a whole, it should help you better understand struggles and frustration of everyday users with current API.
Please bear with me on this one, and if you prefer, I'll later split it into smaller issues (whichever you consider "in scope").
Goal
I want to create
<Enter>
mapping which will:Check existing support (technical breakdown)
gitsigns.blame_line
gitsigns.blame_line
?gitsigns.show
gitsigns.diffthis
It seems quite reassuring that this is "in scope" of the plugin and should be easy to implement, right?
Implementation
Blame the line under cursor and get commit SHA
Unfortunately,
gitsigns.blame_line
seems to be the only API available for the job.Issues:
Because of these, the only way to extract blamed commit SHA via Gitsigns is the following:
Preferred solution: Having something like
gitsigns.get_blame_line
which will synchronously return blamed line for opened file snapshot.Open snapshot of a file in blamed commit
Here,
gitsigns.show
does just what we want, right?Issues:
Because of this, inconvenient code is forced on us again:
Turn that snapshot into before/after diff
Again, this also looks like a thing that can't go wrong. We just have to
diffthis
, right?Issues:
diffthis
, "Gitsigns" had to already finish all of its configuration magic, and there's no specific event for that.GitSignsUpdate
event in last code snippet, but that one is fired multiple times (at least twice) for every new opened file, either by a bug or design.Gitsigns
is available in that buffer.gitsigns.diffthis(blamed_commit .. "^")
Only thing that fixed "recursive" behavior was doing
diffthis
synchronously after manually changing the base:Test environment
bash
script to create dummy git repo for testing in/tmp
:minimal.lua
when testing.Outstanding bugs
Wrong caching (I assume)
Steps to reproduce:
Open dummy
file
from testing directory in Neovim.Navigate to last line (
cccc
).Press key that will execute our mapping (
<Enter>
for me).Observe that newly created Vim tab has correct diff and that each buffer has correct "signs".
Close the tab with diff:
:tabclose
Observe that "signs" in the original file are "copied" from our
show
ed snapshot and don't represent the correct state.Execute
:Gitsigns blame_line
on marked lines.Observe that
Gitsigns
shows "Not Commited Yet" message, even though there are no local (nor staged) changes.Initial commits are missing signs and cannot be blamed.
Steps to reproduce:
file
from testing directory in Neovim.aaaa
).<Enter>
for me).:Gitsigns blame_line
on any line.NOTE: After retesting with
minimal.lua
, I see the following error, which doesn't popup otherwise.This makes perfect sense given that
fff6a8ba^
is initial commit andfff6a8ba^^
is therefore invalid.But it again makes me stuck because the only way to avoid this is to manually invoke
git
and check this edge case.Gitsigns doesn't provide me any way to handle it gracefully.
Neovim version
NVIM v0.10.0
Operating system and version
Arch Linux (x86_64) Kernel: 6.9.2-arch1-1
Expected behavior
No response
Actual behavior
Everything is in description.
Minimal config
Steps to reproduce
Everything is in description.
Gitsigns debug messages
No response
The text was updated successfully, but these errors were encountered: