Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cjun714 opened this issue Mar 26, 2023 · 5 comments
Labels
awaiting feedback bug Something isn't working

Comments

@cjun714
Copy link

cjun714 commented Mar 26, 2023

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
------------------
@cjun714 cjun714 added the bug Something isn't working label Mar 26, 2023
@cjun714 cjun714 changed the title 'vsplit' action doesn't respect 'set splitright' if assigned an open file picker 'vsplit' action doesn't respect 'set splitright' if assigned an open window picker Mar 26, 2023
@alex-courtis
Copy link
Member

Unfortunately I cannot replicate this.

nvim / nvim-tree defaults: actual

:set splitright: expected

Please provide a Clean Room Replication as per the bug report instructions. It is likely that there are other plugins and automation interfering.

@cjun714
Copy link
Author

cjun714 commented Mar 27, 2023

Sorry,
It's seemed I have a autocmd caused this issue

autocmd BufEnter NvimTree_* set nosplitright

This setting is used during test and resolve this issue, but I forget to clean it.

After clean this autocmd, my solution(customize open_window_picker) can work as I expected:

  • open always in left window
  • vsplit always in right split window

Thanks a lot for the quick response.
It's can be closed, thank you!

@cjun714 cjun714 closed this as completed Mar 27, 2023
@alex-courtis
Copy link
Member

FYI @cjun714 view.mappings.list and tree_cb will be deprecated soon.

Now might be good to follow the (automated) Migrating To on_attach

@cjun714
Copy link
Author

cjun714 commented Mar 27, 2023

Thanks for remind,
I update my config with on_attach instead of mappings.list, everything works fine.
NvimTreeGenerationOnAttach also covered exporting old mappings, that's very nice.

Thank you!

@alex-courtis
Copy link
Member

Thanks for remind, I update my config with on_attach instead of mappings.list, everything works fine. NvimTreeGenerationOnAttach also covered exporting old mappings, that's very nice.

Thank you!

Thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting feedback bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants