nvim-tree / nvim-tree.lua

A file explorer tree for neovim written in lua
Other
7.3k stars 609 forks source link

NvimTreeToggle always puts you back in leftmost split #782

Closed mikatpt closed 2 years ago

mikatpt commented 3 years ago

Steps to reproduce:

  1. nvim -u min.lua (minimal config below)
  2. Open a vertical split and place cursor in rightmost split.
  3. Run NvimTreeToggle twice

Previously, using NvimTreeToggle had memory of which split I was currently in - please let me know if there's a way to restore the previous behavior? Thanks so much!

-- nvim -u min.lua

vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.cmd([[set packpath=/tmp/nvim/site]])

local package_root = '/tmp/nvim/site/pack'
local install_path = package_root .. '/packer/start/packer.nvim'

local function install_packer()
    if vim.fn.isdirectory(install_path) == 0 then
        print('Installing dependencies...')
        vim.fn.system({ 'git', 'clone', '--depth=1', 'https://github.com/wbthomason/packer.nvim', install_path })
    end
end

local function load_plugins()
    require('packer').startup({
        {
            'wbthomason/packer.nvim',
            'kyazdani42/nvim-web-devicons',
            'kyazdani42/nvim-tree.lua',
        },
        config = {
            package_root = package_root,
            compile_path = install_path .. '/plugin/packer_compiled.lua',
            display = { non_interactive = true },
        },
    })
end

_G.load_config = function()
    local g = vim.g

    g.nvim_tree_git_hl = 1
    g.nvim_tree_special_files = { ['README.md'] = 1, Makefile = 1, MAKEFILE = 1 } -- List of filenames that gets highlighted with NvimTreeSpecialFile
    g.nvim_tree_show_icons = {
        git = 1,
        folders = 1,
        files = 1,
        folder_arrows = 1,
    }
    g.nvim_tree_icons = {
        default = '',
        symlink = '',
        git = {
            unstaged = '',
            staged = '✓',
            unmerged = '',
            renamed = '➜',
            untracked = '★',
            deleted = '',
            ignored = '◌',
        },
        folder = {
            arrow_open = '',
            arrow_closed = '',
            default = '',
            open = '',
            empty = '',
            empty_open = '',
            symlink = '',
            symlink_open = '',
        },
    }

    require('nvim-web-devicons').setup()
    require('nvim-tree').setup({
        disable_netrw = false,
        hijack_netrw = true,
        open_on_setup = true,
        ignore_ft_on_setup = { '.git', 'node_modules', '.cache' },
        auto_close = true,
        -- opens the tree when changing/opening a new tab if the tree wasn't previously opened
        open_on_tab = false,
        -- hijack the cursor in the tree to put it at the start of the filename
        hijack_cursor = true,
        -- updates the root directory of the tree on `DirChanged` (when your run `:cd` usually)
        update_cwd = true,
        diagnostics = {
            enable = true,
        },
        update_focused_file = {
            enable = true,
            update_cwd = true,
            ignore_list = {},
        },
        -- configuration options for the system open command (`s` in the tree by default)
        system_open = {
            -- the command to run this, leaving nil should work in most cases
            cmd = nil,
            -- the command arguments as a list
            args = {},
        },

        view = {
            -- width of the window, can be either a number (columns) or a string in `%`
            width = 40,
            side = 'left',
            auto_resize = true,
        },
    })
end

install_packer()
load_plugins()
require('packer').sync()

vim.cmd([[autocmd User PackerComplete ++once echo "Ready!" | lua load_config()]])
punk-dev-robot commented 2 years ago

I have the same issue

nyngwang commented 2 years ago

I have the same issue. I put the drawer on the right by default and the cursor is put back on the rightmost split.

Remich commented 2 years ago

I have the same issue. Here is a dirty workaround, until the issue is resolved:

" START: Workaround for buggy NvimTreeToggle

let g:nt_workaround_stack = []
function! NvimTreeToggleWorkaround()
    let l:ft   = &filetype
    let l:last = g:nt_workaround_stack[len(g:nt_workaround_stack)-2]
    if l:ft ==# "NvimTree"
        NvimTreeClose
        let g:nt_workaround_stack = []
        call win_gotoid(l:last)
    else
        NvimTreeToggle
    endif
endfunction

function! NvimTreeToggleAdd()
    if len(g:nt_workaround_stack) > 10
        call remove(g:nt_workaround_stack, 0, -2)
    endif
    call add(g:nt_workaround_stack, win_getid())
endfunction

augroup my_nvimtreegroup
    autocmd!
    au WinEnter *   call NvimTreeToggleAdd()
augroup END

nnoremap <C-n>  :<c-u>call NvimTreeToggleWorkaround()<cr>

" END: Workaround for buggy NvimTreeToogle
nyngwang commented 2 years ago

@Remich While you have implemented one, I have a shorter version without au:

local NOREF_NOERR_TRUNC = { noremap = true, silent = true, nowait = true }
vim.api.nvim_set_keymap('n', ';', '<cmd>lua toggle_nvim_tree_open()<CR>', NOREF_NOERR_TRUNC)
function _G.toggle_nvim_tree_open()
  -- Check nvim-tree is open: https://github.com/kyazdani42/nvim-tree.lua/blob/65b8b19c8bcea36e37474338c7e2d2fea95553d3/lua/nvim-tree.lua#L22
  if (not require'nvim-tree.view'.win_open() or vim.bo.filetype ~= 'NvimTree') then
    vim.cmd('NvimTreeFindFileToggle')
  else
    vim.cmd('NvimTreeFindFileToggle')
    vim.cmd('wincmd p')
  end
end

Explanation:

  1. Change ; to your favorite shortcut.
  2. On line 5, if ...:
    • We don't have to fix the two cases: (1) the nvimtree is not open, and we want to open it. (2) the nvim tree is open but the cursor is not on it. In both cases we just leave the cursor where it will be after the call of NvimTreeFindFileToggle.
  3. On line 7, else:
    • When the nvimtree is open and the cursor is on it, we need to call wincmd p, which moves the cursor to the previous buffer, to fix this issue after we call NvimTreeFindFileToggle.