Skip to content

Commit

Permalink
Merge pull request #394 from lambdalisue/fix-session
Browse files Browse the repository at this point in the history
Properly restore fern on a different tab through session
  • Loading branch information
lambdalisue authored Feb 13, 2022
2 parents 6e4b08b + d551ad6 commit d2ceaeb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
24 changes: 22 additions & 2 deletions autoload/fern/helper/sync.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ endfunction

let s:sync = {}

function! s:sync_winid() abort dict
let helper = self.helper
if win_id2tabwin(helper.winid) != [0, 0]
return helper.winid
endif
" Original window has disappeared
let winids = win_findbuf(helper.bufnr)
let helper.winid = len(winids) is# 0 ? -1 : winids[0]
return helper.winid
endfunction
let s:sync.winid = funcref('s:sync_winid')

function! s:sync_echo(message, ...) abort dict
let hl = a:0 ? a:1 : 'None'
try
Expand Down Expand Up @@ -73,14 +85,22 @@ let s:sync.get_selected_nodes = funcref('s:sync_get_selected_nodes')
function! s:sync_get_cursor() abort dict
let helper = self.helper
let fern = helper.fern
return s:WindowCursor.get_cursor(helper.winid)
let winid = self.winid()
if winid is# -1
return [0, 0]
endif
return s:WindowCursor.get_cursor(winid)
endfunction
let s:sync.get_cursor = funcref('s:sync_get_cursor')

function! s:sync_set_cursor(cursor) abort dict
let helper = self.helper
let fern = helper.fern
call s:WindowCursor.set_cursor(helper.winid, a:cursor)
let winid = self.winid()
if winid is# -1
return
endif
call s:WindowCursor.set_cursor(winid, a:cursor)
call setbufvar(helper.bufnr, 'fern_cursor', a:cursor)
endfunction
let s:sync.set_cursor = funcref('s:sync_set_cursor')
Expand Down
14 changes: 8 additions & 6 deletions autoload/fern/internal/viewer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function! s:init() abort
autocmd! * <buffer>
autocmd BufEnter <buffer> setlocal nobuflisted
autocmd BufReadCmd <buffer> nested call s:BufReadCmd()
autocmd Syntax <buffer> call s:Syntax()
autocmd ColorScheme <buffer> call s:ColorScheme()
autocmd CursorMoved,CursorMovedI,BufLeave <buffer> let b:fern_cursor = getcurpos()[1:2]
augroup END
Expand Down Expand Up @@ -99,9 +100,6 @@ function! s:init() abort

" now the buffer is ready so set filetype to emit FileType
setlocal filetype=fern
call helper.fern.renderer.syntax()
call fern#hook#emit('viewer:syntax', helper)
doautocmd <nomodeline> User FernSyntax
call fern#action#_init()

let l:Profile = fern#profile#start('fern#internal#viewer:init')
Expand Down Expand Up @@ -129,9 +127,6 @@ endfunction
function! s:BufReadCmd() abort
let helper = fern#helper#new()
setlocal filetype=fern
call helper.fern.renderer.syntax()
call fern#hook#emit('viewer:syntax', helper)
doautocmd <nomodeline> User FernSyntax
setlocal modifiable
call setline(1, get(b:, 'fern_viewer_cache_content', []))
setlocal nomodifiable
Expand All @@ -144,6 +139,13 @@ function! s:BufReadCmd() abort
\.catch({ e -> fern#logger#error(e) })
endfunction

function! s:Syntax() abort
let helper = fern#helper#new()
call helper.fern.renderer.syntax()
call fern#hook#emit('viewer:syntax', helper)
doautocmd <nomodeline> User FernSyntax
endfunction

function! s:ColorScheme() abort
let helper = fern#helper#new()
call helper.fern.renderer.highlight()
Expand Down
9 changes: 7 additions & 2 deletions doc/fern-develop.txt
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,12 @@ VARIABLE *fern-develop-helper-variable*

*fern-develop-helper.bufnr*
.bufnr
A buffer number where the target fern instance binded.
A buffer number where the target fern instance is associated.

*fern-develop-helper.winid*
.winid
A window number where a target fern instance binded.
A window number where a target fern instance is associated.
Use |fern-develop-helper.sync.winid()| to get proper value.

*fern-develop-helper.STATUS_NONE*
*fern-develop-helper.STATUS_COLLAPSED*
Expand All @@ -335,6 +336,10 @@ SYNC METHODS *fern-develop-helper.sync*

Following methods are executed synchronously.

*fern-develop-helper.sync.winid()*
.sync.winid()
Return |winid| where a target fern instance is associated.

*fern-develop-helper.sync.echo()*
.sync.echo({message})
Display a temporary |String| {message}.
Expand Down
13 changes: 12 additions & 1 deletion plugin/fern.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,19 @@ function! s:BufReadCmd() abort
\.catch({ e -> fern#logger#error(e) })
endfunction

function! s:SessionLoadPost() abort
let bufnr = bufnr()
call s:BufReadCmd()
" Re-apply required window options
for winid in win_findbuf(bufnr)
let [tabnr, winnr] = win_id2tabwin(winid)
call settabwinvar(tabnr, winnr, '&concealcursor', 'nvic')
call settabwinvar(tabnr, winnr, '&conceallevel', 2)
endfor
endfunction

augroup fern_internal
autocmd! *
autocmd BufReadCmd fern://* nested call s:BufReadCmd()
autocmd SessionLoadPost fern://* nested call s:BufReadCmd()
autocmd SessionLoadPost fern://* nested call s:SessionLoadPost()
augroup END

0 comments on commit d2ceaeb

Please sign in to comment.