Skip to content

'vsplit' action doesn't respect 'set splitright' if assigned an open window picker  #2089

Closed
@cjun714

Description

@cjun714

Description

Hi, thanks a lot for creating this amazing plugin,

I am new for nvim-tree, I used defx before, I try to config this plugin like defx, but there is an issue I can't handle,
please help, thanks!

Here is my requirement:

I often use this layout by using defx:

-----------------------------------------
|  defx-tree |  left window (work area) |
-----------------------------------------

When I use 'open' action on defx-tree, the file will be opened in 'left window', which is my main work area.

I use left window for coding, and use right window for reference doc.

So if I want to refer a doc or code, I use 'vsplit' action on defx-tree to open that doc in a new right split window,
the layout will change into this:

-------------------------------------------------------------------------
|  defx-tree |  left window (work area) | right window (reference area) |
-------------------------------------------------------------------------

If I keep use 'open' action on defx-tree, the file will always be opened in the same 'left window'.
If I use another 'vsplit' action on defx-tree, the file will be opened in a new right split window, like this:

---------------------------------------------------------------------------------------
|  defx-tree |  left window (work area) | right window (doc 2) | right window (doc 1) |
---------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
|  defx-tree |  left window (work area) | right window (doc 3) | right window (doc 2) | right window (doc 1) |
--------------------------------------------------------------------------------------------------------------

So, the key point is 'open' action always reuse left window,
'vsplit' action always create new split window on the right side.

Since defx 'open' action always open file in left window, and below setting will let 'vsplit' to create split window on right side by using drop parameter:

  nnoremap <silent><buffer><expr> vs
  \ defx#is_directory() ?
  \ defx#do_action('open_tree') : defx#do_action('drop', 'vsplit')

I have read nvim-tree's doc, the 'open' strategy is controlled by window picker,
if picker is disabled, plugin will open file in the last window which invoke the tree.

The picker can be a function, so I create a picker

  actions = {
    open_file = {
      window_picker = {
        enable = true,
        picker = function()
        vim.cmd([[execute "normal \<c-w>\<c-w>"]])
        return vim.fn.win_getid()
        end,
      } }
  },

This made 'open' action can always open file in left split window without considering the last window invoked tree,
but the problem is that 'vsplit' action also open file in left split window,
and I 'set splitright' also doesn't work, the 'vsplit' action doesn't respect this option.

I am not sure it's a bug or feature.

My expectation is the open window picker should change the file open strategy, but should not change 'vsplit' strategy,
'vsplit' should respect 'set splitright' option, that can help make separate strategies for 'open' and 'vsplit',
'open' should open file on left and 'vsplit' should on right, at least that's make sense to me

I find 2 issues below that correlated with this issue, but I cant find a solution.
' vsplit always opens to the left, can it be on the right?' #1626 and 'pick window when vsplit open file not work!' #1233

Is it possiable to always 'open'(open.edit) file in left window and vsplit(open.edit) in new right split window?

I don't know how to solve this, could you please help, thanks!

Neovim version

NVIM 0.9dev

Operating system and version

Ubuntu 22.04

nvim-tree version

latest

Minimal config

set splitright

------------------------------------------

require("lazy").setup({
  { "nvim-tree/nvim-tree.lua", cmd="NvimTreeToggle", version = "*", dependencies = {"nvim-tree/nvim-web-devicons"}, config = function() nvimtree_setup() end },
})

function nvimtree_setup()
  local tree_cb = require'nvim-tree.config'.nvim_tree_callback

  require("nvim-tree").setup {
    actions = {
      open_file = {
        window_picker = {
          enable = true,
          picker = function()
            vim.cmd([[execute "normal \<c-w>\<c-w>"]])
            return vim.fn.win_getid()
          end,
        } }
      },
      view = {
        side = "left",
        mappings = {
          custom_only = false,
          list = {
            { key = "l",  cb = tree_cb("edit") },
            { key = "v",  cb = tree_cb("vsplit") },
          },
        },
      },
    }
  end

Steps to reproduce

  1. open nvim-tree
    :NvimTreeToggle

  2. trigger 'edit' action
    press 'l' on tree node

  3. trigger 'vsplit' action
    press 'v' on another tree node

Expected behavior

  1. toggle tree:
---------------
tree | 
---------------
  1. 'edit' action:
---------------
tree | doc1
---------------

3.'vsplit' action :

------------------
tree | doc1 | doc2
------------------

Actual behavior

  1. toggle tree:
---------------
tree | 
---------------
  1. 'edit' action:
---------------
tree | doc1
---------------

3.'vsplit' action :

------------------
tree | doc2 | doc1
------------------

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions