nvim-tree / nvim-tree/nvim-tree.lua

feat: default to opening last accessed window when window picker is disabled

Open
#3,321 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

PR please regression
Dominant language
Lua
Stars
8.6k
Forks
639
Avg merge
1d 14h
Merged PRs (30d)
2

Description

Describe the solution you'd like
I prefer to have the window_picker disabled and would like the default behavior of selecting a window, when window_picker is disabled, to attempt to use your last accessed window.

Describe alternatives you've considered
I created my own picker function to implement the desired behavior, it is a copy paste of the current nvim-tree logic with a one line change.

My setup to get the desired behavior:

---Get all windows in the current tabpage that aren't NvimTree.
---@return table with valid win_ids
local function usable_win_ids()
  local full_name = require('nvim-tree.renderer.components.full-name')
  local view = require('nvim-tree.view')
  local config = require('nvim-tree.config')
  local tabpage = vim.api.nvim_get_current_tabpage()
  local win_ids = vim.api.nvim_tabpage_list_wins(tabpage)
  local tree_winid = view.get_winnr(tabpage)

  return vim.tbl_filter(function(id)
    local bufid = vim.api.nvim_win_get_buf(id)
    for option, v in pairs(config.g.actions.open_file.window_picker.exclude) do
      local ok, option_value = pcall(vim.api.nvim_get_option_value, option, { buf = bufid })

      if ok and vim.tbl_contains(v, option_value) then
        return false
      end
    end

    local win_config = vim.api.nvim_win_get_config(id)
    return id ~= tree_winid and id ~= full_name.popup_win and win_config.focusable and not win_config.hide and not win_config.external or false
  end, win_ids)
end

return {
  'nvim-tree/nvim-tree.lua',
  enabled = true,
  lazy = true,
  dependencies = {
    'nvim-tree/nvim-web-devicons', -- optional, for file icons
  },
  event = 'VeryLazy',
  cmd = { 'NvimTreeToggle', 'NvimTreeFindFile', 'NvimTreeFocus' },
  config = function()
    require('nvim-tree').setup({
      hijack_netrw = false,
      renderer = {
        hidden_display = 'all',
      },
      actions = {
        open_file = {
          window_picker = {
            picker = function()
              -- logic copied from NvimTree with slight modification
              -- attempt to use last accessed window, otherwise fallback to NvimTree logic
              -- also respect excluded window types
              local usable_wins = usable_win_ids()
              local target_winid = vim.fn.win_getid(vim.fn.winnr('#'))
              -- first available usable window
              if not vim.tbl_contains(usable_wins, target_winid) then
                if #usable_wins > 0 then
                  target_winid = usable_wins[1]
                else
                  target_winid = -1
                end
              end
              return target_winid
            end,
          },
        },
      },
    })
  end,
}

Additional context
Locally, I changed https://github.com/nvim-tree/nvim-tree.lua/blob/d277467fc0d1d0e2bca88165a1de6b526f9f6fe8/lua/nvim-tree/actions/node/open-file.lua#L229

from

target_winid = lib.target_winid

to

target_winid = vim.fn.win_getid(vim.fn.winnr('#'))

This gave me the desired results. If this is something you are interested in, I could open a PR. Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lua/nvim-tree/actions/node/open-file.lua around line 229 and trace how the target window is selected when the window picker is disabled. Reproduce the setup with window_picker disabled and compare the current fallback with vim.fn.win_getid(vim.fn.winnr('#')). Done means opening a node attempts the last accessed usable window while preserving fallback behavior for excluded or unavailable windows.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.