Skip to content

Commit

Permalink
feat(openers)!: multiple files are opened as buffers by default
Browse files Browse the repository at this point in the history
BREAKING CHANGE: when multiple files were selected in yazi, the previous
behaviour was to open them as items in the quickfix list. This has been
changed to open them as buffers instead. The previous behaviour can be
restored by setting `config.hooks.yazi_opened_multiple_files` to
`openers.send_files_to_quickfix_list`.

```lua
-- Example for lazy.nvim:
--
---@type LazySpec
{
  'mikavilpas/yazi.nvim',
  ---@type YaziConfig
  opts = {
      ---@diagnostic disable-next-line: missing-fields
      hooks = {
        yazi_opened_multiple_files = function(chosen_files)
          require("yazi.openers").send_files_to_quickfix_list(chosen_files)
        end,
      },
  },
}
```

✨feat(`openers`): New opener `open_multiple_files()` is now the default
value for `config.hooks.yazi_opened_multiple_files`.

Co-authored-by: Mika Vilpas <[email protected]>
  • Loading branch information
Zeioth and mikavilpas authored Aug 4, 2024
1 parent eb12317 commit 5cd3ad7
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 10 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ jobs:
name: prettier
steps:
- uses: actions/[email protected]
with:
ref: ${{ github.head_ref }}
- uses: actions/[email protected]
with:
node-version-file: .nvmrc
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ You can optionally configure yazi.nvim by setting any of the options below.
replace_in_directory = '<c-g>',
cycle_open_buffers = '<tab>',
copy_relative_path_to_selected_files = '<c-y>',
send_to_quickfix_list = '<c-q>',
},

-- completely override the keymappings for yazi. This function will be
Expand Down Expand Up @@ -278,9 +279,11 @@ You can optionally configure yazi.nvim by setting any of the options below.

These are the default keybindings that are available when yazi is open:

- `<c-v>`: open the selected file in a vertical split
- `<c-x>`: open the selected file in a horizontal split
- `<c-t>`: open the selected file in a new tab
- `<f1>`: show the help menu
- `<c-v>`: open the selected file(s) in vertical splits
- `<c-x>`: open the selected file(s) in horizontal splits
- `<c-t>`: open the selected file(s) in new tabs
- `<c-q>`: send the selected file(s) to the quickfix list
- There are also integrations to other plugins, which you need to install
separately:
- `<c-s>`: search in the current yazi directory using
Expand All @@ -292,6 +295,11 @@ These are the default keybindings that are available when yazi is open:
[grug-far](https://github.com/MagicDuck/grug-far.nvim), if available
- if multiple files/directories are selected in yazi, the operation is
limited to those only
- `<c-y>`: copy the relative path of the selected file(s) to the clipboard.
Requires GNU `realpath` or `grealpath` on OSX
- `<tab>`: cycle through the open buffers in Neovim. See
[#232](https://github.com/mikavilpas/yazi.nvim/pull/232) for more
information

## 🪛 Customizing yazi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe("opening files", () => {
cy.typeIntoTerminal(" ")
// also select the next file because multiple files have to be selected
cy.typeIntoTerminal(" ")
cy.typeIntoTerminal("{enter}")
cy.typeIntoTerminal("{control+q}")

// yazi should now be closed
cy.contains("-- TERMINAL --").should("not.exist")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe("opening files", () => {
cy.typeIntoTerminal(" ")
// also select the next file because multiple files have to be selected
cy.typeIntoTerminal(" ")
cy.typeIntoTerminal("{enter}")
cy.typeIntoTerminal("{control+q}")

// yazi should now be closed
cy.contains("-- TERMINAL --").should("not.exist")
Expand Down
20 changes: 18 additions & 2 deletions lua/yazi/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ function M.default()
replace_in_directory = '<c-g>',
cycle_open_buffers = '<tab>',
copy_relative_path_to_selected_files = '<c-y>',
send_to_quickfix_list = '<c-q>',
},
set_keymappings_function = nil,
hooks = {
yazi_opened = function() end,
yazi_closed_successfully = function() end,
yazi_opened_multiple_files = openers.send_files_to_quickfix_list,
yazi_opened_multiple_files = openers.open_multiple_files,
},

highlight_groups = {
Expand Down Expand Up @@ -167,6 +168,18 @@ function M.set_keymappings(yazi_buffer, config, context)
end, { buffer = yazi_buffer })
end

if config.keymaps.send_to_quickfix_list ~= false then
vim.keymap.set({ 't' }, config.keymaps.send_to_quickfix_list, function()
local openers = require('yazi.openers')
keybinding_helpers.select_current_file_and_close_yazi(config, {
on_multiple_files_opened = openers.send_files_to_quickfix_list,
on_file_opened = function(chosen_file)
openers.send_files_to_quickfix_list({ chosen_file })
end,
})
end, { buffer = yazi_buffer })
end

if config.keymaps.show_help ~= false then
vim.keymap.set({ 't' }, config.keymaps.show_help, function()
local w = vim.api.nvim_win_get_width(0)
Expand All @@ -179,7 +192,7 @@ function M.set_keymappings(yazi_buffer, config, context)
bufpos = { 5, 30 },
noautocmd = true,
width = math.min(46, math.floor(w * 0.5)),
height = math.min(12, math.floor(h * 0.5)),
height = math.min(13, math.floor(h * 0.5)),
border = config.yazi_floating_window_border,
})

Expand All @@ -199,6 +212,9 @@ function M.set_keymappings(yazi_buffer, config, context)
''
.. show(config.keymaps.open_file_in_vertical_split)
.. ' - open file in vertical split',
''
.. show(config.keymaps.send_to_quickfix_list)
.. ' - send to the quickfix list',
''
.. show(config.keymaps.grep_in_directory)
.. ' - search in directory / selected files',
Expand Down
5 changes: 5 additions & 0 deletions lua/yazi/openers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ function M.open_file_in_tab(chosen_file)
vim.cmd(string.format('tabedit %s', vim.fn.fnameescape(chosen_file)))
end

---@param chosen_files string[]
function M.open_multiple_files(chosen_files)
vim.cmd('args' .. table.concat(chosen_files, ' '))
end

---@param chosen_files string[]
function M.send_files_to_quickfix_list(chosen_files)
vim.fn.setqflist({}, 'r', {
Expand Down
1 change: 1 addition & 0 deletions lua/yazi/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
---@field replace_in_directory? YaziKeymap # Close yazi and open a replacer (default: grug-far.nvim) narrowed to the directory yazi is in
---@field cycle_open_buffers? YaziKeymap # When Neovim has multiple splits open and visible, make yazi jump to the directory of the next one
---@field copy_relative_path_to_selected_files? YaziKeymap # Copy the relative paths of the selected files to the clipboard
---@field send_to_quickfix_list? YaziKeymap # Send the selected files to the quickfix list for later processing

---@class (exact) YaziActiveContext # context state for a single yazi session
---@field api YaziProcessApi
Expand Down
29 changes: 28 additions & 1 deletion spec/yazi/open_multiple_files_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,39 @@ describe('the default configuration', function()
end
end)

local plenary_path = require('plenary.path')
local test_file_path = plenary_path:new(vim.fn.getcwd(), 'test-file.txt')

it('opens multiple files in buffers by default', function()
local config = require('yazi.config').default()
local chosen_files = { '/abc/test-file.txt', '/abc/test-file2.txt' }

config.hooks.yazi_opened_multiple_files(
chosen_files,
config,
test_file_path
)

local buffers = vim.api.nvim_list_bufs()

assert.equals(2, #buffers)
assert.equals('/abc/test-file.txt', vim.api.nvim_buf_get_name(buffers[1]))
assert.equals('/abc/test-file2.txt', vim.api.nvim_buf_get_name(buffers[2]))
end)

it('can display multiple files in the quickfix list', function()
local config = require('yazi.config').default()
config.hooks.yazi_opened_multiple_files =
require('yazi.openers').send_files_to_quickfix_list

-- include problematic characters in the file names to preserve their behaviour
local chosen_files = { '/abc/[email protected]', '/abc/test-file2.txt' }

config.hooks.yazi_opened_multiple_files(chosen_files, config)
config.hooks.yazi_opened_multiple_files(
chosen_files,
config,
test_file_path
)

local quickfix_list = vim.fn.getqflist()

Expand Down

3 comments on commit 5cd3ad7

@xfzv
Copy link
Contributor

@xfzv xfzv commented on 5cd3ad7 Aug 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new behavior doesn't seem to play well with bufferline.nvim for me.

When selecting multiple files and pressing Enter, bufferline doesn't show up, as if there were only one buffer. :buffers does show all the files I opened in different buffers.

With this hook, bufferline displays all buffers normally:

yazi_opened_multiple_files = function(chosen_files)
        vim.notify("Opening " .. #chosen_files .. " files")
        for _, file in ipairs(chosen_files) do
          vim.api.nvim_command("e " .. file)
        end
      end,

@mikavilpas
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm can you open an isuse for this? I tried to reproduce it but I don't get the same behaviour - for me, bufferline does show up.

@xfzv
Copy link
Contributor

@xfzv xfzv commented on 5cd3ad7 Aug 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't reproduce with a minimal repro.lua either, only with my Neovim config. Not sure what's going on. Opening files from Neotree creates distinct buffers from bufferline as expected.

Edit: I'll experiment and let you know.

Please sign in to comment.