Skip to content

Commit

Permalink
feat: pressing tab in yazi jumps to dir of next open split (#232)
Browse files Browse the repository at this point in the history
    This feature is only supported for the latest yazi version. For now,
    you have to compile yazi from source to be able to use it. See the
    readme for instructions on how to do this.

If you have files open in split windows in Neovim, and have started
yazi, you can now press the `tab` key to make yazi jump to the directory
of the next open buffer. This is useful when you have multiple files
open in different directories and want to quickly jump between them.

Right now this has the following limitation: it can only jump to the
directory of the next open buffer, but not necessarily focus the buffer
in yazi. This is because yazi doesn't have a built-in way to jump to a
specific file, only a directory. This is something that might be
improved in the future.
  • Loading branch information
mikavilpas authored Jul 21, 2024
1 parent 6f3c5f2 commit 3cbc40c
Show file tree
Hide file tree
Showing 21 changed files with 331 additions and 97 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ open yazi in a floating window in Neovim.
- 🆕 Plugin manager for Yazi plugins and flavors
([documentation](./documentation/plugin-manager.md)). Please provide your
feedback!
- Features available if you are using a development version of yazi (see
[installing-yazi-from-source.md](documentation/installing-yazi-from-source.md)):
- Highlight the currently hovered yazi file in Neovim
- Restart the last yazi session with a keybinding
- `<tab>` makes yazi jump to the directory of the next open split

For previewing images with yazi, see Yazi's documentation related to Neovim
[here](https://yazi-rs.github.io/docs/image-preview/#neovim).
Expand Down
13 changes: 10 additions & 3 deletions integration-tests/client/testEnvironmentTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
export type MultipleFiles = {
openInVerticalSplits: File[]
}

type File = TestDirectoryFile | "."

/** The arguments given from the tests to send to the server */
export type StartNeovimArguments = {
filename?: TestDirectoryFile | "."
filename?: File | MultipleFiles
startupScriptModifications?: StartupScriptModification[]
}

Expand Down Expand Up @@ -57,9 +63,10 @@ export type TestDirectory = {
["test.lua"]: FileEntry
["file.txt"]: FileEntry
["modify_yazi_config_to_use_ya_as_event_reader.lua"]: FileEntry
["subdirectory/sub.txt"]: FileEntry
["subdirectory/subdirectory-file.txt"]: FileEntry
["other-subdirectory/other-sub-file.txt"]: FileEntry
["routes/posts.$postId/route.tsx"]: FileEntry
["routes/posts.$postId/adjacent-file.tsx"]: FileEntry
["routes/posts.$postId/adjacent-file.txt"]: FileEntry
}
}

Expand Down
18 changes: 12 additions & 6 deletions integration-tests/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,25 @@ export default defineConfig({
stem: "file",
extension: ".txt",
},
"subdirectory/sub.txt": {
name: "sub.txt",
stem: "sub",
"subdirectory/subdirectory-file.txt": {
name: "subdirectory-file.txt",
stem: "subdirectory-file",
extension: ".txt",
},
["other-subdirectory/other-sub-file.txt"]: {
name: "other-sub-file.txt",
stem: "other-sub-file",
extension: ".txt",
},
"modify_yazi_config_to_use_ya_as_event_reader.lua": {
name: "modify_yazi_config_to_use_ya_as_event_reader.lua",
stem: "modify_yazi_config_to_use_ya_as_event_reader",
extension: ".lua",
},
"routes/posts.$postId/adjacent-file.tsx": {
name: "adjacent-file.tsx",
"routes/posts.$postId/adjacent-file.txt": {
name: "adjacent-file.txt",
stem: "adjacent-file",
extension: ".tsx",
extension: ".txt",
},
"routes/posts.$postId/route.tsx": {
name: "route.tsx",
Expand All @@ -110,6 +115,7 @@ export default defineConfig({
execSync(`cp ./test-environment/file.txt ${dir}/`)
execSync(`cp ./test-environment/test-setup.lua ${dir}/test.lua`)
execSync(`cp -r ./test-environment/subdirectory ${dir}/`)
execSync(`cp -r ./test-environment/other-subdirectory ${dir}/`)
execSync(`cp -r ./test-environment/config-modifications/ ${dir}/`)
execSync(`cp -r ./test-environment/routes ${dir}/`)
console.log(`Created test directory at ${dir}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,17 @@ describe("opening files", () => {
it("can send file names to the quickfix list", () => {
cy.startNeovim().then((dir) => {
cy.typeIntoTerminal("{upArrow}")
cy.typeIntoTerminal("{control+a}{enter}")

// wait for yazi to open
cy.contains(dir.contents["test.lua"].name)

// select the initial file, the cursor moves one line down to the next file
cy.typeIntoTerminal(" ")
// also select the next file because multiple files have to be selected
cy.typeIntoTerminal(" ")
cy.typeIntoTerminal("{enter}")

// items in the quickfix list should now be visible
cy.contains(`${dir.contents["file.txt"].name}||`)
cy.contains(`${dir.contents["initial-file.txt"].name}||`)
})
})
Expand All @@ -85,7 +92,7 @@ describe("opening files", () => {

// close yazi just to be sure the file preview is not found instead
cy.get(
dir.contents["routes/posts.$postId/adjacent-file.tsx"].name,
dir.contents["routes/posts.$postId/adjacent-file.txt"].name,
).should("not.exist")

// the file contents should now be visible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ describe("reading events", () => {
// telescope should now be visible. Let's search for the contents of the
// file, which we know beforehand
cy.contains("Grep in")
cy.typeIntoTerminal("Hello")
cy.typeIntoTerminal("This")

// we should see text indicating the search is limited to the current
// directory
cy.contains("Hello from the subdirectory! 👋")
cy.contains("This is other-sub-file.txt")
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { isHovered, isNotHovered } from "./hover-utils"
import { startNeovimWithYa } from "./startNeovimWithYa"

// NOTE: cypress doesn't support the tab key, but control+i seems to work fine
// https://docs.cypress.io/api/commands/type#Typing-tab-key-does-not-work

// TODO make this more robust once we can jump to a file, not just a
// directory - could test with directories that have multiple files

describe("'cd' to another buffer's directory", () => {
beforeEach(() => {
cy.visit("http://localhost:5173")
})

it("can highlight the buffer when hovered", () => {
const view = {
leftFile: { text: "This is other-sub-file.txt" },
centerFile: { text: "this file is adjacent-file.txt" },
rightFile: { text: "ello from the subdirectory!" },
} as const

startNeovimWithYa({
filename: {
openInVerticalSplits: [
"subdirectory/subdirectory-file.txt",
"routes/posts.$postId/adjacent-file.txt",
"other-subdirectory/other-sub-file.txt",
],
},
startupScriptModifications: [
"modify_yazi_config_and_add_hovered_buffer_background.lua",
],
}).then(() => {
// sanity check to make sure the files are open
cy.contains(view.leftFile.text)
cy.contains(view.centerFile.text)
cy.contains(view.rightFile.text)

// before doing anything, both files should be unhovered (have the
// default background color)
isNotHovered(view.leftFile.text)
isNotHovered(view.centerFile.text)
isNotHovered(view.rightFile.text)

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

// Switch to the other buffers' directories in yazi. This should make
// yazi send a hover event for the new, highlighted file.
//
// Since each directory only has one file, it should be highlighted :)
cy.typeIntoTerminal("{control+i}")
isNotHovered(view.leftFile.text)
isHovered(view.centerFile.text)
isNotHovered(view.rightFile.text)

cy.typeIntoTerminal("{control+i}")
isHovered(view.leftFile.text)
isNotHovered(view.centerFile.text)
isNotHovered(view.rightFile.text)

cy.typeIntoTerminal("{control+i}")
isNotHovered(view.leftFile.text)
isNotHovered(view.centerFile.text)
isHovered(view.rightFile.text)

// tab once more to make sure it wraps around
cy.typeIntoTerminal("{control+i}")
isNotHovered(view.leftFile.text)
isHovered(view.centerFile.text)
isNotHovered(view.rightFile.text)
})
})

it("skips highlighting splits for the same file", () => {
const view = {
leftAndCenterFile: { text: "ello from the subdirectory!" },
rightFile: { text: "This is other-sub-file.txt" },
} as const

startNeovimWithYa({
filename: {
openInVerticalSplits: [
// open the same file in two splits
"subdirectory/subdirectory-file.txt",
"subdirectory/subdirectory-file.txt",
"other-subdirectory/other-sub-file.txt",
],
},
startupScriptModifications: [
"modify_yazi_config_and_add_hovered_buffer_background.lua",
],
}).then(() => {
isNotHovered(view.leftAndCenterFile.text)
isNotHovered(view.rightFile.text)

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

cy.typeIntoTerminal("{control+i}")

// the right file should be highlighted
isNotHovered(view.leftAndCenterFile.text)
isHovered(view.rightFile.text)

// tab again to make sure it wraps around. It should highlight both splits
cy.typeIntoTerminal("{control+i}")
isHovered(view.leftAndCenterFile.text)
isNotHovered(view.rightFile.text)

// tab again. Since the left and center file are the same, it should
// skip the center file and highlight the right file

cy.typeIntoTerminal("{control+i}")
isNotHovered(view.leftAndCenterFile.text)
isHovered(view.rightFile.text)
})
})
})
Loading

0 comments on commit 3cbc40c

Please sign in to comment.