Skip to content

Commit

Permalink
fix: session restore for nerdtree buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Feb 5, 2024
1 parent bc606c4 commit 0aac752
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
32 changes: 32 additions & 0 deletions autoload/nerdtree.vim
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,38 @@ function! nerdtree#pathEquals(lhs, rhs) abort
endif
endfunction

"FUNCTION: nerdtree#onBufLeave() {{{2
" used for handling the nerdtree BufLeave/WinLeave events.
function! nerdtree#onBufLeave() abort
" detect whether we are in the middle of sourcing a session.
" if it is a buffer from the sourced session we need to restore it.
if exists('g:SessionLoad') && !exists('b:NERDTree')
let bname = bufname('%')
" is the buffer for a tab tree?
if bname =~# '^' . g:NERDTreeCreator.BufNamePrefix() . 'tab_\d\+$'
" rename loaded buffer and mark it as trash to prevent this event
" getting fired again
exec 'file TRASH_' . bname
" delete the trash buffer
exec 'bwipeout!'
" rescue the tab tree at the current working directory
call g:NERDTreeCreator.CreateTabTree(getcwd())
" is the buffer for a window tree?
elseif bname =~# '^' . g:NERDTreeCreator.BufNamePrefix(). 'win_\d\+$'
" rescue the window tree at the current working directory
call g:NERDTreeCreator.CreateWindowTree(getcwd())
else " unknown buffer type
" rename buffer to mark it as broken.
exec 'file BROKEN_' . bname
call nerdtree#echoError('Failed to restore "' . bname . '" from session. Is this session created with an older version of NERDTree?')
endif
else
if g:NERDTree.IsOpen()
call b:NERDTree.ui.saveScreenState()
endif
endif
endfunction

" SECTION: View Functions {{{1
"============================================================

Expand Down
19 changes: 13 additions & 6 deletions lib/nerdtree/creator.vim
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function! s:Creator.createWindowTree(dir)

"we need a unique name for each window tree buffer to ensure they are
"all independent
exec g:NERDTreeCreatePrefix . ' edit ' . self._nextBufferName()
exec g:NERDTreeCreatePrefix . ' edit ' . self._nextBufferName('win')

call self._createNERDTree(path, 'window')
let b:NERDTree._previousBuf = bufnr(previousBuf)
Expand Down Expand Up @@ -210,7 +210,7 @@ function! s:Creator._createTreeWin()
let l:splitSize = g:NERDTreeWinSize

if !g:NERDTree.ExistsForTab()
let t:NERDTreeBufName = self._nextBufferName()
let t:NERDTreeBufName = self._nextBufferName('tab')
silent! execute l:splitLocation . l:splitDirection . ' ' . l:splitSize . ' new'
silent! execute 'edit ' . t:NERDTreeBufName
silent! execute l:splitDirection . ' resize '. l:splitSize
Expand Down Expand Up @@ -244,10 +244,17 @@ function! s:Creator.New()
return newCreator
endfunction

" FUNCTION: s:Creator._nextBufferName() {{{1
" returns the buffer name for the next nerd tree
function! s:Creator._nextBufferName()
let name = s:Creator.BufNamePrefix() . self._nextBufferNumber()
" FUNCTION: s:Creator._nextBufferName(type='') {{{1
" gets a buffer type of either 'tab' or 'win', defaults to unknown
" returns the buffer name for the next nerd tree of such type.
function! s:Creator._nextBufferName(type='')
let name = s:Creator.BufNamePrefix()
if a:type == 'tab'
let name = name . 'tab_'
elseif a:type == 'win'
let name = name . 'win_'
endif
let name = name . self._nextBufferNumber()
return name
endfunction

Expand Down
2 changes: 1 addition & 1 deletion plugin/NERD_tree.vim
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ call nerdtree#ui_glue#setupCommands()
"============================================================
augroup NERDTree
"Save the cursor position whenever we close the nerd tree
exec 'autocmd BufLeave,WinLeave '. g:NERDTreeCreator.BufNamePrefix() .'* if g:NERDTree.IsOpen() | call b:NERDTree.ui.saveScreenState() | endif'
exec 'autocmd BufLeave,WinLeave '. g:NERDTreeCreator.BufNamePrefix() .'* call nerdtree#onBufLeave()'

"disallow insert mode in the NERDTree
exec 'autocmd BufEnter,WinEnter '. g:NERDTreeCreator.BufNamePrefix() .'* stopinsert'
Expand Down

0 comments on commit 0aac752

Please sign in to comment.