Skip to content

fix: sometimes focusing on the wrong split after closing lazygit #198

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

Merged
merged 2 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 47 additions & 26 deletions integration-tests/cypress/e2e/lazygit.cy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { flavors } from "@catppuccin/palette"
import { rgbify } from "@tui-sandbox/library/dist/src/client/color-utilities"
import assert from "assert"
import type { MyTestDirectoryFile } from "MyTestDirectory"
import {
assertCurrentBufferName,
initializeGitRepositoryInDirectory,
} from "./test-utils"

const colors = {
selectedItem: rgbify(flavors.macchiato.colors.blue.rgb),
Expand All @@ -13,12 +15,14 @@ const lazygit = {
donateMessage: "Donate",
} as const

const fakeGitRepoFileText = "fake-git-repository-file-contents-71f64aabd056"

describe("testing", () => {
it("can toggle lazygit on/off", () => {
cy.visit("/")
cy.startNeovim({ filename: "fakegitrepo/file.txt" }).then(() => {
// wait until text on the start screen is visible
cy.contains("fake-git-repository-file-contents-71f64aabd056")
cy.contains(fakeGitRepoFileText)
initializeGitRepositoryInDirectory()

cy.typeIntoTerminal("{rightarrow}")
Expand Down Expand Up @@ -61,22 +65,52 @@ describe("testing", () => {
cy.visit("/")

cy.startNeovim({ filename: "fakegitrepo/file.txt" }).then(() => {
cy.contains("fake-git-repository-file-contents-71f64aabd056")
cy.contains(fakeGitRepoFileText)
initializeGitRepositoryInDirectory()

cy.typeIntoTerminal("{rightarrow}")
cy.contains(lazygit.donateMessage)

cy.contains(fakeGitRepoFileText).click()
cy.contains(lazygit.donateMessage).should("not.exist")
})
})

it("keeps focus on the previous buffer after closing", () => {
cy.visit("/")

cy.startNeovim({
filename: {
openInVerticalSplits: [
"fakegitrepo/file.txt",
"fakegitrepo/other-file.txt",
],
},
}).then((nvim) => {
cy.contains(fakeGitRepoFileText)
initializeGitRepositoryInDirectory()

// make sure the first file is focused
assertCurrentBufferName("fakegitrepo/file.txt")

// focus on the second file
nvim.runExCommand({ command: "wincmd l" })
assertCurrentBufferName("fakegitrepo/other-file.txt")

cy.typeIntoTerminal("{rightarrow}")
cy.contains(lazygit.donateMessage)

cy.contains("fake-git-repository-file-contents-71f64aabd056").click()
cy.typeIntoTerminal("q")
cy.contains(lazygit.donateMessage).should("not.exist")
assertCurrentBufferName("fakegitrepo/other-file.txt")
})
})

it("can open lazygit after COMMIT_EDITMSG is closed", () => {
cy.visit("/")

cy.startNeovim({ filename: "fakegitrepo/file.txt" }).then((nvim) => {
cy.contains("fake-git-repository-file-contents-71f64aabd056")
cy.contains(fakeGitRepoFileText)
initializeGitRepositoryInDirectory()
nvim.runExCommand({ command: "e %:h/.git/COMMIT_EDITMSG" })
cy.typeIntoTerminal("itest commit message{esc}", { delay: 0 })
Expand All @@ -96,7 +130,7 @@ describe("testing", () => {
VISUAL: "nvim",
},
}).then((nvim) => {
cy.contains("fake-git-repository-file-contents-71f64aabd056")
cy.contains(fakeGitRepoFileText)
initializeGitRepositoryInDirectory()

cy.typeIntoTerminal("{rightarrow}")
Expand Down Expand Up @@ -146,7 +180,7 @@ describe("testing", () => {
},
}).then((nvim) => {
initializeGitRepositoryInDirectory()
cy.contains("fake-git-repository-file-contents-71f64aabd056")
cy.contains(fakeGitRepoFileText)
nvim.runBlockingShellCommand({
command:
"cd fakegitrepo && git add file.txt && git commit -a -m 'initial commit'",
Expand Down Expand Up @@ -182,9 +216,7 @@ describe("testing", () => {
cy.contains("Donate").should("not.exist")

// the file should have been opened in neovim
cy.contains("fake-git-repository-file-contents-71f64aabd056").should(
"not.exist",
)
cy.contains(fakeGitRepoFileText).should("not.exist")
cy.contains("file2-contents")
})
})
Expand All @@ -200,7 +232,7 @@ describe("testing", () => {
},
}).then((nvim) => {
initializeGitRepositoryInDirectory()
cy.contains("fake-git-repository-file-contents-71f64aabd056")
cy.contains(fakeGitRepoFileText)
nvim.runBlockingShellCommand({
command:
"cd fakegitrepo && git add file.txt && git commit -a -m 'initial commit'",
Expand Down Expand Up @@ -236,7 +268,7 @@ describe("testing", () => {
cy.visit("/")
cy.startNeovim({ filename: "fakegitrepo/file.txt" }).then((_nvim) => {
// wait until text on the start screen is visible
cy.contains("fake-git-repository-file-contents-71f64aabd056")
cy.contains(fakeGitRepoFileText)
initializeGitRepositoryInDirectory()

cy.typeIntoTerminal("{rightarrow}")
Expand Down Expand Up @@ -276,7 +308,7 @@ describe("toggle_for_file", () => {
cy.visit("/")
cy.startNeovim({ filename: "fakegitrepo/file.txt" }).then((nvim) => {
// wait until text on the start screen is visible
cy.contains("fake-git-repository-file-contents-71f64aabd056")
cy.contains(fakeGitRepoFileText)
initializeGitRepositoryInDirectory()

// create a commit for the root repo that includes all files in this mini
Expand Down Expand Up @@ -313,7 +345,7 @@ describe("toggle_for_file", () => {
// verify that the commit message for that file only is visible when
// toggle_for_file is used
nvim.runExCommand({ command: "edit %:h/file.txt" })
cy.contains("fake-git-repository-file-contents-71f64aabd056")
cy.contains(fakeGitRepoFileText)

nvim.runLuaCode({
luaCode: `require('tsugit').toggle_for_file()`,
Expand Down Expand Up @@ -362,14 +394,3 @@ describe("in a git workspace", () => {
})
})
})

function initializeGitRepositoryInDirectory(
relativePath: MyTestDirectoryFile = "fakegitrepo",
) {
cy.nvim_runBlockingShellCommand({
command: `git init`,
cwdRelative: relativePath,
}).and((result) => {
assert(result.type === "success", "Failed to initialize git repository")
})
}
23 changes: 23 additions & 0 deletions integration-tests/cypress/e2e/test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { RunLuaCodeOutput } from "@tui-sandbox/library/src/server/types"
import type { MyTestDirectoryFile } from "MyTestDirectory"

export function initializeGitRepositoryInDirectory(
relativePath: MyTestDirectoryFile = "fakegitrepo",
): void {
cy.nvim_runBlockingShellCommand({
command: `git init`,
cwdRelative: relativePath,
}).and((result) => {
assert(result.type === "success", "Failed to initialize git repository")
})
}

export function assertCurrentBufferName(
name: MyTestDirectoryFile,
): Cypress.Chainable<RunLuaCodeOutput> {
return cy
.nvim_runLuaCode({ luaCode: `return vim.api.nvim_buf_get_name(0)` })
.then((result) => {
expect(result.value).to.match(new RegExp(name))
})
}
6 changes: 6 additions & 0 deletions lua/tsugit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ function M.toggle(args, options)

assert(lazygit, "tsugit.nvim: failed to create lazygit terminal")
vim.api.nvim_buf_set_var(lazygit.buf, "minicursorword_disable", true)
local previous_buffer = vim.api.nvim_get_current_buf()
lazygit:show()

if created then
Expand All @@ -158,6 +159,11 @@ function M.toggle(args, options)
-- the terminal application has exited.
if vim.api.nvim_buf_is_valid(lazygit.buf) then
vim.api.nvim_buf_delete(lazygit.buf, { force = true })

-- focus the previous_buffer after closing lazygit
if vim.api.nvim_buf_is_valid(previous_buffer) then
vim.api.set_current_buf(previous_buffer)
end
end

-- warm up the next instance
Expand Down
Loading