Skip to content

Commit

Permalink
fix: renaming a file twice not updating the buffer name (#259)
Browse files Browse the repository at this point in the history
When a file was renamed once, the buffer name was updated correctly. But
when the file was renamed a second time, the buffer name was not
updated.

I no longer remember why I had the `rename_or_close_buffer` function in
the first place, but this simpler implementation seems to work.

Anyway, this is quite a rare bug, so I don't expect a huge impact one
way or the other.
  • Loading branch information
mikavilpas authored Jul 25, 2024
1 parent 172ed53 commit 98caf39
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ describe("reading events", () => {
cy.contains("If you see this text, Neovim is ready").should("not.exist")
})
})
})

describe("'rename' events", () => {
beforeEach(() => {
cy.visit("http://localhost:5173")
})

it("can read 'rename' events and update the buffer name when the file was renamed", () => {
startNeovimWithYa().then((dir) => {
Expand Down Expand Up @@ -112,4 +118,44 @@ describe("reading events", () => {
cy.contains(`${file.stem}2${file.extension}`)
})
})

it("can rename twice and keep track of the correct file name", () => {
startNeovimWithYa().then((dir) => {
// the default file should already be open
cy.contains(dir.contents["initial-file.txt"].name)
cy.contains("If you see this text, Neovim is ready!")

// start yazi
cy.typeIntoTerminal("{upArrow}")

// start file renaming
cy.typeIntoTerminal("r")
cy.contains("Rename:")
cy.typeIntoTerminal("2{enter}")

cy.get("Rename").should("not.exist")

// yazi should be showing the new file name
const file = dir.contents["initial-file.txt"]
cy.contains(`${file.stem}2${file.extension}`)

// close yazi
cy.typeIntoTerminal("q")

const newName = `${file.stem}2${file.extension}`
// the buffer name should now be updated
cy.contains(newName)

// rename a second time, returning to the original name
cy.typeIntoTerminal("{upArrow}")
cy.typeIntoTerminal("r")
cy.contains("Rename:")
cy.typeIntoTerminal("{backspace}")
cy.contains(`${file.stem}${file.extension}`)
cy.typeIntoTerminal("{enter}")

cy.typeIntoTerminal("q")
cy.contains(newName).should("not.exist")
})
})
})
10 changes: 7 additions & 3 deletions lua/yazi/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,14 @@ function M.rename_or_close_buffer(instruction)
-- If the target buffer is already open in neovim, just close the old buffer.
-- It causes an error to try to rename to a buffer that's already open.
if M.is_buffer_open(instruction.path.filename) then
vim.api.nvim_buf_delete(instruction.bufnr, {})
else
vim.api.nvim_buf_set_name(instruction.bufnr, instruction.path.filename)
pcall(function()
vim.api.nvim_buf_delete(instruction.bufnr, { force = true })
end)
end

pcall(function()
vim.api.nvim_buf_set_name(instruction.bufnr, instruction.path.filename)
end)
end

---@param prev_win integer
Expand Down

0 comments on commit 98caf39

Please sign in to comment.