Skip to content

Commit 60b5e60

Browse files
authored
fix: session restore for nerdtree buffers. (#1405)
1 parent bc606c4 commit 60b5e60

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

autoload/nerdtree.vim

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,38 @@ function! nerdtree#pathEquals(lhs, rhs) abort
234234
endif
235235
endfunction
236236

237+
"FUNCTION: nerdtree#onBufLeave() {{{2
238+
" used for handling the nerdtree BufLeave/WinLeave events.
239+
function! nerdtree#onBufLeave() abort
240+
" detect whether we are in the middle of sourcing a session.
241+
" if it is a buffer from the sourced session we need to restore it.
242+
if exists('g:SessionLoad') && !exists('b:NERDTree')
243+
let bname = bufname('%')
244+
" is the buffer for a tab tree?
245+
if bname =~# '^' . g:NERDTreeCreator.BufNamePrefix() . 'tab_\d\+$'
246+
" rename loaded buffer and mark it as trash to prevent this event
247+
" getting fired again
248+
exec 'file TRASH_' . bname
249+
" delete the trash buffer
250+
exec 'bwipeout!'
251+
" rescue the tab tree at the current working directory
252+
call g:NERDTreeCreator.CreateTabTree(getcwd())
253+
" is the buffer for a window tree?
254+
elseif bname =~# '^' . g:NERDTreeCreator.BufNamePrefix(). 'win_\d\+$'
255+
" rescue the window tree at the current working directory
256+
call g:NERDTreeCreator.CreateWindowTree(getcwd())
257+
else " unknown buffer type
258+
" rename buffer to mark it as broken.
259+
exec 'file BROKEN_' . bname
260+
call nerdtree#echoError('Failed to restore "' . bname . '" from session. Is this session created with an older version of NERDTree?')
261+
endif
262+
else
263+
if g:NERDTree.IsOpen()
264+
call b:NERDTree.ui.saveScreenState()
265+
endif
266+
endif
267+
endfunction
268+
237269
" SECTION: View Functions {{{1
238270
"============================================================
239271

lib/nerdtree/creator.vim

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function! s:Creator.createWindowTree(dir)
118118

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

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

212212
if !g:NERDTree.ExistsForTab()
213-
let t:NERDTreeBufName = self._nextBufferName()
213+
let t:NERDTreeBufName = self._nextBufferName('tab')
214214
silent! execute l:splitLocation . l:splitDirection . ' ' . l:splitSize . ' new'
215215
silent! execute 'edit ' . t:NERDTreeBufName
216216
silent! execute l:splitDirection . ' resize '. l:splitSize
@@ -244,10 +244,22 @@ function! s:Creator.New()
244244
return newCreator
245245
endfunction
246246

247-
" FUNCTION: s:Creator._nextBufferName() {{{1
248-
" returns the buffer name for the next nerd tree
249-
function! s:Creator._nextBufferName()
250-
let name = s:Creator.BufNamePrefix() . self._nextBufferNumber()
247+
" FUNCTION: s:Creator._nextBufferName(type='') {{{1
248+
" gets an optional buffer type of either 'tab' or 'win'.
249+
" returns the buffer name for the next nerd tree of such type.
250+
function! s:Creator._nextBufferName(...)
251+
if a:0 > 0
252+
let type = a:1
253+
else
254+
let type = ''
255+
end
256+
let name = s:Creator.BufNamePrefix()
257+
if type ==# 'tab'
258+
let name = name . 'tab_'
259+
elseif type ==# 'win'
260+
let name = name . 'win_'
261+
endif
262+
let name = name . self._nextBufferNumber()
251263
return name
252264
endfunction
253265

plugin/NERD_tree.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ call nerdtree#ui_glue#setupCommands()
151151
"============================================================
152152
augroup NERDTree
153153
"Save the cursor position whenever we close the nerd tree
154-
exec 'autocmd BufLeave,WinLeave '. g:NERDTreeCreator.BufNamePrefix() .'* if g:NERDTree.IsOpen() | call b:NERDTree.ui.saveScreenState() | endif'
154+
exec 'autocmd BufLeave,WinLeave '. g:NERDTreeCreator.BufNamePrefix() .'* call nerdtree#onBufLeave()'
155155

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

0 commit comments

Comments
 (0)