Skip to content
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

Closed
tronikelis opened this issue Oct 26, 2024 · 9 comments
Closed

git_status picker, place cursor on first hunk #3341

tronikelis opened this issue Oct 26, 2024 · 9 comments
Labels
enhancement Enhancement to performance, inner workings or existent features

Comments

@tronikelis
Copy link

tronikelis commented Oct 26, 2024

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 hunk

Describe 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 hunk

Describe 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 option

@tronikelis tronikelis added the enhancement Enhancement to performance, inner workings or existent features label Oct 26, 2024
@xzbdmw
Copy link

xzbdmw commented Oct 28, 2024

you can bind cr in status picker to polling gitsigns's hunk information until it return something, then jump to next hunk.

@tronikelis
Copy link
Author

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

@tronikelis tronikelis closed this as not planned Won't fix, can't repro, duplicate, stale Oct 29, 2024
@hoxbro
Copy link

hoxbro commented Nov 16, 2024

Would it be possible to share your solution @tronikelis?

@tronikelis
Copy link
Author

Would it be possible to share your solution @tronikelis?

@hoxbro I tried, but I ran into a blocker with gitsigns plugin, the on_attach gets called before hunks were updated, so you can't do this:

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,
                        },
                    },
                },
} 

@xzbdmw
Copy link

xzbdmw commented Nov 16, 2024

You can use some defer_fn on attach, at some point get_hunks will return information, only then you can jump.

https://github.com/xzbdmw/nvimconfig/blob/c45e82f7fa7965323e73320c3daa1bcb09195bd8/lua/plugins/telescope.lua#L403

Note you need to use existing get_hunks api, the version I use is a modified one to distinguish staged and unstaged hunks.

@tronikelis
Copy link
Author

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

@tronikelis
Copy link
Author

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,
        },
    },
},

@tronikelis
Copy link
Author

as was mentioned here lewis6991/gitsigns.nvim#1147 (comment) GitSignsUpdate works as well

@xzbdmw
Copy link

xzbdmw commented Nov 17, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to performance, inner workings or existent features
Projects
None yet
Development

No branches or pull requests

3 participants