aboutsummaryrefslogtreecommitdiff
path: root/autoload
diff options
context:
space:
mode:
authormitermayer <mitermayer.reis@gmail.com>2017-08-29 23:05:33 -0700
committermitermayer <mitermayer.reis@gmail.com>2017-08-29 23:06:07 -0700
commitb256344f2b8f0237a67b58bbb56f32a8f17d1903 (patch)
treee251d82c1aa213d03d3976c171db663f67e362f0 /autoload
parent9d4b6b0e4a38c4048ceabb5c60cba3ad5c03f94b (diff)
downloadvim-prettier-b256344f2b8f0237a67b58bbb56f32a8f17d1903.tar.xz
Making sure that after formatting we preserve in the same line
Diffstat (limited to 'autoload')
-rw-r--r--autoload/prettier.vim18
1 files changed, 13 insertions, 5 deletions
diff --git a/autoload/prettier.vim b/autoload/prettier.vim
index 543b543..8982911 100644
--- a/autoload/prettier.vim
+++ b/autoload/prettier.vim
@@ -34,6 +34,8 @@ function! prettier#Autoformat(...) abort
let l:maxLineLookup = 50
let l:maxTimeLookupMs = 500
let l:pattern = '@format'
+ let l:search = @/
+ let l:winview = winsaveview()
" we need to move selection to the top before looking up to avoid
" scanning a very long file
@@ -47,6 +49,12 @@ function! prettier#Autoformat(...) abort
" Restore the selection and if greater then before it defaults to end
call cursor(curPos[1], curPos[2])
+
+ " Restore view
+ call winrestview(l:winview)
+
+ " Restore search
+ let @/=l:search
endfunction
function! s:Prettier_Exec_Sync(cmd, startSelection, endSelection) abort
@@ -145,18 +153,18 @@ function! s:Get_New_Buffer(lines, start, end) abort
endfunction
function! s:Apply_Prettier_Format(lines, startSelection, endSelection) abort
- " store cursor position
- let l:curPos = getpos('.')
+ " store view
+ let l:winview = winsaveview()
let l:newBuffer = s:Get_New_Buffer(a:lines, a:startSelection, a:endSelection)
" delete all lines on the current buffer
- silent! execute 1 . ',' . line('$') . 'delete _'
+ silent! execute len(l:newBuffer) . ',' . line('$') . 'delete _'
" replace all lines from the current buffer with output from prettier
call setline(1, l:newBuffer)
- " restore cursor position
- call cursor(l:curPos[1], l:curPos[2])
+ " Restore view
+ call winrestview(l:winview)
endfunction
" By default we will default to our internal