Skip to content

Commit

Permalink
preserve display information while formatting (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Nov 11, 2015
1 parent 90f412a commit aebaadb
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions autoload/crystal_lang.vim
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function! crystal_lang#format_string(code, ...) abort
return output
endfunction

function! s:get_positions() abort
function! s:get_saved_states() abort
let result = {}
let fname = bufname('%')
let current_winnr = winnr()
Expand All @@ -280,7 +280,10 @@ function! s:get_positions() abort
endif
if bufname(bufnr) ==# fname
execute i 'wincmd w'
let result[i] = getpos('.')
let result[i] = {
\ 'pos': getpos('.'),
\ 'screen': winsaveview()
\ }
endif
endfor
execute current_winnr 'wincmd w'
Expand All @@ -301,7 +304,7 @@ function! crystal_lang#format(option_str) abort
let &l:selection = 'inclusive'
let &virtualedit = ''
let [save_g_reg, save_g_regtype] = [getreg('g'), getregtype('g')]
let positions_save = s:get_positions()
let windows_save = s:get_saved_states()

try
call setreg('g', formatted, 'v')
Expand All @@ -311,9 +314,11 @@ function! crystal_lang#format(option_str) abort
let &l:selection = sel_save
let &virtualedit = ve_save
let winnr = winnr()
for w in keys(positions_save)
execute w 'wincmd w'
call setpos('.', positions_save[w])
for winnr in keys(windows_save)
let w = windows_save[winnr]
execute winnr 'wincmd w'
call setpos('.', w.pos)
call winrestview(w.screen)
endfor
execute winnr 'wincmd w'
endtry
Expand Down

0 comments on commit aebaadb

Please sign in to comment.