-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prevent conflicts with custom yazi config for
<enter>
(opt-in)
Issue ===== For custom ways to open files, such as "open in new vertical split", "open in new tab", etc., yazi.nvim uses the `<enter>` key to open the file. This is not very robust because the user might have set a custom keybinding for the `<enter>` key in their yazi config. In this case, the custom keybinding would be triggered instead of the file being opened. Solution ======== Instead of relying on the `<enter>` key, yazi.nvim can now use `ya emit open` to make yazi open the file(s) that are currently selected. This completely avoids the issue, but requires a recent version of yazi (0.4.0 or later). To opt into this behaviour, set the following in your config: ```lua { -- example for lazy.nvim "mikavilpas/yazi.nvim", -- ... (other settings here) ... ---@type YaziConfig opts = { future_features = { ya_emit_open = true, -- 👆🏻 this is the new setting }, }, } ``` This issue was found in #611 where smart-enter.yazi was found to have this behaviour by default: https://github.com/yazi-rs/plugins/tree/main/smart-enter.yazi#advanced
- Loading branch information
1 parent
0b1ca50
commit 6059639
Showing
11 changed files
with
347 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
244 changes: 244 additions & 0 deletions
244
integration-tests/cypress/e2e/using-ya-to-read-events/opening-files-with-legacy-open.cy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
import type { MyTestDirectoryFile } from "MyTestDirectory" | ||
import { | ||
isFileNotSelectedInYazi, | ||
isFileSelectedInYazi, | ||
} from "./utils/yazi-utils" | ||
|
||
describe("opening files with yazi < 0.4.0", () => { | ||
// versions before this do not support `ya emit open`. Instead, they send a | ||
// fake <enter> keypress to yazi and react on the file that was opened by | ||
// yazi. This approach is not very robust because the user might have set a | ||
// custom enter key - in which case the whole thing does not work. | ||
// | ||
// This has been fixed for recent yazi versions by using `ya emit open` to | ||
// send the file path to yazi. These tests are here until the legacy approach | ||
// is removed. | ||
beforeEach(() => { | ||
cy.visit("/") | ||
}) | ||
|
||
it("can open a file in a vertical split", () => { | ||
cy.startNeovim({ | ||
startupScriptModifications: [ | ||
"modify_yazi_config_do_not_use_ya_emit_open.lua", | ||
], | ||
}).then((dir) => { | ||
cy.contains("If you see this text, Neovim is ready!") | ||
cy.typeIntoTerminal("{upArrow}") | ||
isFileNotSelectedInYazi("file2.txt" satisfies MyTestDirectoryFile) | ||
cy.typeIntoTerminal( | ||
`/${"file2.txt" satisfies MyTestDirectoryFile}{enter}`, | ||
) | ||
cy.typeIntoTerminal("{esc}") // hide the search highlight | ||
isFileSelectedInYazi("file2.txt" satisfies MyTestDirectoryFile) | ||
cy.typeIntoTerminal("{control+v}") | ||
|
||
// yazi should now be closed | ||
cy.contains("-- TERMINAL --").should("not.exist") | ||
|
||
// the file path must be visible at the bottom | ||
cy.contains(dir.contents["file2.txt"].name) | ||
cy.contains(dir.contents["initial-file.txt"].name) | ||
}) | ||
}) | ||
|
||
it("can open a file in a horizontal split", () => { | ||
cy.startNeovim({ | ||
startupScriptModifications: [ | ||
"modify_yazi_config_do_not_use_ya_emit_open.lua", | ||
], | ||
}).then((dir) => { | ||
cy.contains("If you see this text, Neovim is ready!") | ||
cy.typeIntoTerminal("{upArrow}") | ||
cy.contains(dir.contents["file2.txt"].name) | ||
cy.typeIntoTerminal(`/${dir.contents["file2.txt"].name}{enter}`) | ||
cy.typeIntoTerminal("{esc}") // hide the search highlight | ||
isFileSelectedInYazi(dir.contents["file2.txt"].name) | ||
cy.typeIntoTerminal("{control+x}") | ||
|
||
// yazi should now be closed | ||
cy.contains("-- TERMINAL --").should("not.exist") | ||
|
||
// the file path must be visible at the bottom | ||
cy.contains(dir.contents["file2.txt"].name) | ||
cy.contains(dir.contents["initial-file.txt"].name) | ||
}) | ||
}) | ||
|
||
it("can open a file in a new tab", () => { | ||
cy.startNeovim({ | ||
startupScriptModifications: [ | ||
"modify_yazi_config_do_not_use_ya_emit_open.lua", | ||
], | ||
}).then((dir) => { | ||
cy.contains("If you see this text, Neovim is ready!") | ||
cy.typeIntoTerminal("{upArrow}") | ||
isFileNotSelectedInYazi(dir.contents["file2.txt"].name) | ||
cy.contains(dir.contents["file2.txt"].name) | ||
cy.typeIntoTerminal(`/${dir.contents["file2.txt"].name}{enter}`) | ||
cy.typeIntoTerminal("{esc}") // hide the search highlight | ||
isFileSelectedInYazi(dir.contents["file2.txt"].name) | ||
cy.typeIntoTerminal("{control+t}") | ||
|
||
// yazi should now be closed | ||
cy.contains("-- TERMINAL --").should("not.exist") | ||
|
||
cy.contains( | ||
// match some text from inside the file | ||
"Hello", | ||
) | ||
cy.runExCommand({ command: "tabnext" }) | ||
|
||
cy.contains("If you see this text, Neovim is ready!") | ||
|
||
cy.contains(dir.contents["file2.txt"].name) | ||
cy.contains(dir.contents["initial-file.txt"].name) | ||
}) | ||
}) | ||
|
||
it("can send file names to the quickfix list", () => { | ||
cy.startNeovim({ | ||
filename: "file2.txt", | ||
startupScriptModifications: [ | ||
"modify_yazi_config_do_not_use_ya_emit_open.lua", | ||
], | ||
}).then((dir) => { | ||
cy.contains("Hello") | ||
cy.typeIntoTerminal("{upArrow}") | ||
|
||
// wait for yazi to open | ||
cy.contains(dir.contents["file2.txt"].name) | ||
|
||
// file2.txt should be selected | ||
isFileSelectedInYazi("file2.txt" satisfies MyTestDirectoryFile) | ||
|
||
// select file2, the cursor moves one line down to the next file | ||
cy.typeIntoTerminal(" ") | ||
isFileNotSelectedInYazi("file2.txt" satisfies MyTestDirectoryFile) | ||
|
||
// also select the next file because multiple files have to be selected | ||
isFileSelectedInYazi("file3.txt" satisfies MyTestDirectoryFile) | ||
cy.typeIntoTerminal(" ") | ||
isFileNotSelectedInYazi("file3.txt" satisfies MyTestDirectoryFile) | ||
cy.typeIntoTerminal("{control+q}") | ||
|
||
// yazi should now be closed | ||
cy.contains("-- TERMINAL --").should("not.exist") | ||
|
||
// items in the quickfix list should now be visible | ||
cy.contains(`${dir.contents["file2.txt"].name}||`) | ||
cy.contains(`${dir.contents["file3.txt"].name}||`) | ||
}) | ||
}) | ||
|
||
it("can copy the relative path to the initial file", () => { | ||
// the copied path should be relative to the file/directory yazi was | ||
// started in (the initial file) | ||
|
||
cy.startNeovim({ | ||
startupScriptModifications: [ | ||
"modify_yazi_config_do_not_use_ya_emit_open.lua", | ||
], | ||
}).then((dir) => { | ||
cy.contains("If you see this text, Neovim is ready!") | ||
|
||
cy.typeIntoTerminal("{upArrow}") | ||
isFileNotSelectedInYazi("file2.txt" satisfies MyTestDirectoryFile) | ||
|
||
// enter another directory and select a file | ||
cy.typeIntoTerminal("/routes{enter}") | ||
cy.contains("posts.$postId") | ||
cy.typeIntoTerminal("{rightArrow}") | ||
cy.contains( | ||
dir.contents.routes.contents["posts.$postId"].contents["route.tsx"] | ||
.name, | ||
) // file in the directory | ||
cy.typeIntoTerminal("{rightArrow}") | ||
cy.typeIntoTerminal( | ||
`/${ | ||
dir.contents.routes.contents["posts.$postId"].contents[ | ||
"adjacent-file.txt" | ||
].name | ||
}{enter}{esc}`, | ||
// esc to hide the search highlight | ||
) | ||
isFileSelectedInYazi( | ||
dir.contents.routes.contents["posts.$postId"].contents[ | ||
"adjacent-file.txt" | ||
].name, | ||
) | ||
|
||
// the file contents should now be visible | ||
cy.contains("this file is adjacent-file.txt") | ||
|
||
cy.typeIntoTerminal("{control+y}") | ||
|
||
// yazi should now be closed | ||
cy.contains( | ||
dir.contents.routes.contents["posts.$postId"].contents["route.tsx"] | ||
.name, | ||
).should("not.exist") | ||
|
||
// the relative path should now be in the clipboard. Let's paste it to | ||
// the file to verify this. | ||
// NOTE: the test-setup configures the `"` register to be the clipboard | ||
cy.typeIntoTerminal("o{enter}{esc}") | ||
cy.runLuaCode({ luaCode: `return vim.fn.getreg('"')` }).then((result) => { | ||
expect(result.value).to.contain( | ||
"routes/posts.$postId/adjacent-file.txt" satisfies MyTestDirectoryFile, | ||
) | ||
}) | ||
}) | ||
}) | ||
|
||
it("can copy the relative paths of multiple selected files", () => { | ||
// similarly, the copied path should be relative to the file/directory yazi | ||
// was started in (the initial file) | ||
|
||
cy.startNeovim({ | ||
startupScriptModifications: [ | ||
"modify_yazi_config_do_not_use_ya_emit_open.lua", | ||
], | ||
}).then((dir) => { | ||
cy.contains("If you see this text, Neovim is ready!") | ||
|
||
cy.typeIntoTerminal("{upArrow}") | ||
cy.contains(dir.contents["file2.txt"].name) | ||
|
||
// enter another directory and select a file | ||
cy.typeIntoTerminal("/routes{enter}") | ||
cy.contains("posts.$postId") | ||
cy.typeIntoTerminal("{rightArrow}") | ||
cy.contains( | ||
dir.contents.routes.contents["posts.$postId"].contents["route.tsx"] | ||
.name, | ||
) // file in the directory | ||
cy.typeIntoTerminal("{rightArrow}") | ||
cy.typeIntoTerminal("{control+a}") | ||
|
||
cy.typeIntoTerminal("{control+y}") | ||
|
||
// yazi should now be closed | ||
cy.contains( | ||
dir.contents.routes.contents["posts.$postId"].contents["route.tsx"] | ||
.name, | ||
).should("not.exist") | ||
|
||
// the relative path should now be in the clipboard. Let's paste it to | ||
// the file to verify this. | ||
// NOTE: the test-setup configures the `"` register to be the clipboard | ||
cy.typeIntoTerminal("o{enter}{esc}") | ||
cy.runLuaCode({ luaCode: `return vim.fn.getreg('"')` }).then((result) => { | ||
expect(result.value).to.eql( | ||
( | ||
[ | ||
"routes/posts.$postId/adjacent-file.txt", | ||
"routes/posts.$postId/route.tsx", | ||
"routes/posts.$postId/should-be-excluded-file.txt", | ||
] satisfies MyTestDirectoryFile[] | ||
).join("\n"), | ||
) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...ests/test-environment/config-modifications/modify_yazi_config_do_not_use_ya_emit_open.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
---@module "yazi" | ||
|
||
require("yazi").setup( | ||
---@type YaziConfig | ||
{ | ||
future_features = { | ||
ya_emit_open = false, | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.