Skip to content
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

fix: session restore for nerdtree buffers. #1405

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 18 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,22 @@ 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 an optional buffer type of either 'tab' or 'win'.
" returns the buffer name for the next nerd tree of such type.
function! s:Creator._nextBufferName(...)
if a:0 > 0
let type = a:1
else
let type = ''
end
let name = s:Creator.BufNamePrefix()
if type ==# 'tab'
let name = name . 'tab_'
elseif 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
Loading