nvim-tree.lua icon indicating copy to clipboard operation
nvim-tree.lua copied to clipboard

Use Window Picker Even When Buffer Window Is Present

Open darthdeus opened this issue 2 years ago • 9 comments

Can this functionality be implemented utilising API? Looking at the docs I don't think so, seems this needs to hook into how files are opened.

Is your feature request related to a problem? Please describe. Right now if I browse in nvim-tree and hit enter it always seems to open on the right unless I enable the picker. The picker however doesn't seem very useful since it always triggers.

Describe the solution you'd like A configuration option to always open on the left.

darthdeus avatar Aug 26 '23 12:08 darthdeus

I'm not quite sure I'm understanding your use case...

Add option to always open files from the picker in a fixed split

The picker (letters at the bottom of the available windows) indicates which window the buffer will be opened in.

If you'd like to explicitly open in a new window you can use :help nvim-tree-api.node.open.vertical()

See :help nvim-tree-mappings-default for the current <CR> behaviour i.e. open.edit

Right now if I browse in nvim-tree and hit enter it always seems to open on the right unless I enable the picker.

The window will open according to :help nvim-tree.view.side. You can change this to "right" to open in a window on the left.

The picker however doesn't seem very useful since it always triggers.

The picker will trigger when there are multiple windows that are not in :help nvim-tree.actions.open_file.window_picker.exclude

Default:

                {
                  filetype = { "notify", "packer", "qf", "diff", "fugitive", "fugitiveblame", },
                  buftype  = { "nofile", "terminal", "help", }
                }

Are you seeing unwanted picker prompts? If so, please do raise a bug report with reproduction steps.

alex-courtis avatar Aug 27 '23 01:08 alex-courtis

Sorry I mis-explained, but what I meant is that I want to configure the behavior without using the picker. The problem with having the picker enabled for me is that the picker shows up every time a file is opened, which slows down the workflow for me as I don't want to repeatedly decide this. I'd like to be able to configure where files are opened every time without any extra user input.

If you'd like to explicitly open in a new window you can use :help nvim-tree-api.node.open.vertical()

I want the exact opposite :sweat_smile: I want the file to be opened in the left-most split that is not nvim-tree.

The window will open according to :help nvim-tree.view.side. You can change this to "right" to open in a window on the left.

This does seem to achieve what I want, opening windows on the left. But it also moves nvim-tree to the right.


To rephrase myself, what I want is behavior like this:

  • no picker
  • nvim-tree on the left
  • if there are more vertical splits, open in the leftmost window that is not nvim-tree

So for example if the layout looks like this

image

currently nvim-tree will open C in place of B. I want it to open in place of A.

This makes moving between the file tree and the currently open file much quicker when a lot of files are explored. It's also the default behavior of nerdtree, which I've been using for quite a while.

darthdeus avatar Aug 27 '23 11:08 darthdeus

currently nvim-tree will open C in place of B. I want it to open in place of A.

:help nvim-tree.actions.open_file.window_picker.enable should allow most of the behaviour you desire. It will open in the most recently focused window A or B.

If you wish to customise the behaviour further, you can write a :help nvim-tree.actions.open_file.window_picker.picker function to use the window of your choice.

alex-courtis avatar Aug 28 '23 01:08 alex-courtis

It looks like you have all the tools available to achieve your goals.

Closing this one; please comment if you have any further issues.

alex-courtis avatar Sep 02 '23 02:09 alex-courtis

Thanks for the answer, it took me a while to get back to this, but I figured it out, posting config here in case anyone else runs into wanting the same thing

window_picker = {
  enable = true,
  picker = function()
    -- Get the list of all window IDs
    local win_ids = vim.api.nvim_list_wins()

    -- Sort the windows by their column position
    table.sort(win_ids, function(a, b)
      local col_a = vim.api.nvim_win_get_position(a)[2]
      local col_b = vim.api.nvim_win_get_position(b)[2]
      return col_a < col_b
    end)

    -- Return the ID of the second window, if it exists
    if #win_ids >= 2 then
      return win_ids[2]
    else
      return nil
    end
  end,
},

@alex-courtis I do have one more question that's a maybe a bit unrelated, but this doesn't seem to get called for files which are already open in another window, but looking at the logic in https://github.com/nvim-tree/nvim-tree.lua/blob/master/lua/nvim-tree/actions/node/open-file.lua#L311-L357 (which I think is what gets called) it seems like this is not possible?

Basically what I'd want is an option to always open things in according to the picker function, regardless of whether the file is already open or not.

darthdeus avatar Sep 03 '23 11:09 darthdeus

That solution looks great! I'd be most grateful if you could share that as a Recipe for others.

alex-courtis avatar Sep 04 '23 00:09 alex-courtis

Basically what I'd want is an option to always open things in according to the picker function, regardless of whether the file is already open or not.

That is indeed not possible.

I'd be most grateful if you could create a PR; you've clearly found the codepath and know nvim lua. You might need to change open_in_new_window as well.

An option could be actions.open_file.window_picker.always boolean default false.

alex-courtis avatar Sep 04 '23 00:09 alex-courtis

This is for discussion on PR itself, but: maybe it's better to extend nvim-tree.actions.open_file.window_picker.picker's return values?

gegoune avatar Sep 04 '23 07:09 gegoune

This is for discussion on PR itself, but: maybe it's better to extend nvim-tree.actions.open_file.window_picker.picker's return values?

That may be simpler, and it's always preferable to avoid adding new options.

Things should become clear once it's built.

alex-courtis avatar Sep 09 '23 00:09 alex-courtis