mirror of
https://github.com/preservim/nerdtree.git
synced 2024-11-22 10:06:16 +08:00
fix: session restore for nerdtree buffers. (#1405)
This commit is contained in:
parent
bc606c43e2
commit
60b5e602e9
|
@ -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
|
||||
"============================================================
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
|
|
@ -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'
|
||||
|
|
Loading…
Reference in New Issue
Block a user