kdheepak / lazygit.nvim

Plugin for calling lazygit from within neovim.
MIT License
1.47k stars 53 forks source link

[Feat] Command: LazygitToggle #106

Open Dimfred opened 1 year ago

Dimfred commented 1 year ago

Is your feature request related to a problem? Please describe.

In my lazygit config I am using:

os:
  editPreset: 'nvim-remote'
  edit: "nvr --remote-send '<C-space>:q<CR><Esc>:lua vim.cmd(\"e \" .. {{filename}})<CR>'"
  editAtLine: "nvr --remote-send '<C-space>:q<CR><Esc>:lua vim.cmd(\"e \" .. {{filename}})<CR>{{line}}G'"

This allows me to open the file from lazygit inside my main window and not in lazygit. As you can see for that before opening the file, I am closing lazygit with q, what would be nice if I could just toggle then window away. Then way I could resume lazygit at the same spot I closed it.

Describe the solution you'd like

A LazygitToggle command, which allows to toggle the Lazygit window, so an extension of the currrent command.

That would be awesome :) Thx for the plugin btw, appreciate it.

EDIT:

Meanwhile I found how to better make use of <CMD> so this is the updated version, I'll leave the old one for reference. Then you also won't get annoying cmd popups n stuff.

os:
  editPreset: 'nvim-remote'
  edit: "nvr --remote-send '<CMD>q<CR><CMD>lua vim.cmd(\"e \" .. {{filename}})<CR>'"
  editAtLine: "nvr --remote-send '<CMD>q<CR><CMD>lua vim.cmd(\"e \" .. {{filename}})<CR>{{line}}G'"
Jasha10 commented 1 year ago

Thanks for sharing your config @Dimfred!

teocns commented 9 months ago

That won't resolve path well if you find yourself in a sub-directory, away from the .git root, and try editing a file; how about...

  editPreset: 'nvim-remote'
  edit: "nvr --remote-send '<CMD>q<CR><CMD>lua local root = vim.fn.trim(vim.fn.system(\"git rev-parse --show-toplevel\")); vim.cmd(\"e \" .. root .. \"/\" .. {{filename}})<CR>'"
  editAtLine: "nvr --remote-send '<CMD>q<CR><CMD>lua local root = vim.fn.trim(vim.fn.system(\"git rev-parse --show-toplevel\")); vim.cmd(\"e \" .. root .. \"/\" .. {{filename}})<CR>{{line}}G'"
Dimfred commented 8 months ago

@teocns man this is awesome, very nice idea, didn't now how to do that properly, thanks a lot!

khris190 commented 6 months ago

for some reason i get shown a window with my command and press ENTER to go back to lazygit and adding \ at the start of the command fixes it if anyone has the same problem.

  editPreset: 'nvim-remote'
  edit: "nvr --remote-send '<CR><CMD>q<CR><CMD>lua local root = vim.fn.trim(vim.fn.system(\"git rev-parse --show-toplevel\")); vim.cmd(\"e \" .. root .. \"/\" .. {{filename}})<CR>'"
dudicoco commented 2 months ago

lazygit now has a native integration with nvim remote: https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#configuring-file-editing

In my case I didn't even have to open nvim with the --listen flag, it just worked out of the box.

vRoussel commented 2 months ago

Native integration does indeed work great!

However (correct me if I'm wrong), there's still no way to "resume lazygit at the same spot" as @Dimfred was asking, right ?

Dimfred commented 2 months ago

So I wrote my own "lazygit" function, which allows me to do that.

local term = nil

M.toggle = function()
    local Terminal = require("toggleterm.terminal").Terminal

    local size = 90
    local direction = "float"

    if not term then
        term = Terminal:new({
            cmd = "lazygit",
            hidden = true,
            count = 10,
            on_exit = function()
                term = nil
            end,
        })
        term:toggle(size, direction)

        vim.cmd("set ft=lazygit")
        map("nvit", "<C-w>", function()
            term:toggle(size, direction)
        end, { buffer = true })
    else
        term:toggle(size, direction)
    end
end
vRoussel commented 2 months ago

Nice, thanks ! I'll try that

Edit: It works pretty well :) Here's my adapted version so that when you edit a file with e from lazygit, it doesn't get closed but hidden.

Don't forget to change my keymaps if needed: <leader>gg to open <a-q> to hide

Somewhere in nvim conf:

local term = nil

local function lg_toggle()
    local Terminal = require("toggleterm.terminal").Terminal

    local size = 90
    local direction = "float"

    if not term then
        term = Terminal:new({
            cmd = "lazygit",
            hidden = true,
            on_exit = function()
                term = nil
            end,
        })
        if term then
            term:toggle(size, direction)

            vim.cmd("set ft=lazygit")
            vim.keymap.set("t", "<a-q>", function()
                term:toggle(size, direction)
            end, { buffer = true })
        end
    else
        term:toggle(size, direction)
    end
end

vim.api.nvim_create_user_command("LazyGitToggle", lg_toggle, {})
vim.keymap.set("n", "<leader>gg", "<cmd>LazyGitToggle<cr>", {})

lazygit conf:

os:
  editPreset: 'nvim-remote'
  edit: '[ -z "$NVIM" ] && (nvim -- {{filename}}) || (nvim --server "$NVIM" --remote-send "<CMD>LazyGitToggle<CR>" && nvim --server "$NVIM" --remote-tab {{filename}})'
  editAtLine: '[ -z "$NVIM" ] && (nvim +{{line}} -- {{filename}}) || (nvim --server "$NVIM" --remote-send "<CMD>LazyGitToggle<CR>" &&  nvim --server "$NVIM" --remote-tab {{filename}} && nvim --server "$NVIM" --remote-send ":{{line}}<CR>")'

promptToReturnFromSubprocess: false