-
-
Notifications
You must be signed in to change notification settings - Fork 849
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
git_status picker, place cursor on first hunk #3341
Comments
you can bind cr in status picker to polling gitsigns's hunk information until it return something, then jump to next hunk. |
thats a great idea, closing this issue |
Would it be possible to share your solution @tronikelis? |
@hoxbro I tried, but I ran into a blocker with on_attach = function(buf)
if vim.b[buf].jump_to_hunk then require("gitsigns").nav_hunk("first") end
end this just prints "No hunks" to messages, on_attach seems implemented incorrectly relevant telescope config require("telescope").setup({
pickers = {
git_status = {
mappings = {
i = {
["<cr>"] = function(prompt_bufnr)
actions.select_default(prompt_bufnr)
vim.b.jump_to_hunk = true
end,
},
},
},
} |
You can use some defer_fn on attach, at some point get_hunks will return information, only then you can jump. Note you need to use existing get_hunks api, the version I use is a modified one to distinguish staged and unstaged hunks. |
Yeah I tried with defer_fn and it works, but I just really dislike this solution as it is unstable and pc dependent. I have a PR for a new config lewis6991/gitsigns.nvim#1146 hope it gets accepted |
The PR was closed without comment, so I assume the author does not think this should be in the gitsigns plugin, which I understand. So my current solution is this @hoxbro git_status = {
mappings = {
i = {
["<cr>"] = function(prompt_bufnr)
local JUMPED_TO_HUNK = "_jumped_to_hunk"
actions.select_default(prompt_bufnr)
local buf = vim.api.nvim_get_current_buf()
if vim.b[buf][JUMPED_TO_HUNK] then
return
end
vim.system(
{ "git", "diff", "-U0", "HEAD", vim.fn.expand("%:p") },
{},
vim.schedule_wrap(function(out)
if out.code ~= 0 or not out.stdout then
return
end
if vim.api.nvim_get_current_buf() ~= buf then
return
end
local line = out.stdout:match("@@.++(%d+)")
line = tonumber(line)
vim.b[buf][JUMPED_TO_HUNK] = true
vim.api.nvim_win_set_cursor(0, { line, 0 })
end)
)
end,
},
},
}, |
as was mentioned here lewis6991/gitsigns.nvim#1147 (comment) |
I'm updating with these local goto_next_hunk_cr = function(prompt_bufnr)
actions.select_default(prompt_bufnr)
local ok = false
local fn = function()
if ok then
return
end
local hunks = require("gitsigns").get_hunks(api.nvim_get_current_buf())
if hunks ~= nil and #hunks > 0 then
ok = true
require("gitsigns").nav_hunk("first", { navigation_message = false })
end
end
fn()
local id
id = api.nvim_create_autocmd("User", {
pattern = "GitSignsUpdate",
callback = function()
if ok then
api.nvim_del_autocmd(id)
end
fn()
end,
})
end
|
Is your feature request related to a problem? Please describe.
Not a problem, but a tiny annoyance, when I open the
git_status
picker and select an entry, it opens the file with the cursor at the top, and I always need to find the closest hunkDescribe the solution you'd like
A new config option in telescope git_status picker, e.g.
move_cursor
which would move the cursor to the first hunkDescribe alternatives you've considered
I could make this logic myself without telescope by checking if a new opened file has hunks and jump to them, but this is bad because now everytime it would jump, even when I don't want to, when with telescope picker I always want to jump
Additional context
I am willing to open a PR for this
Although a bit of searching around, git does not support a standard way of showing line number in
git status
, I guess this could be an extra plugin and not a native optionThe text was updated successfully, but these errors were encountered: