aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitermayer Reis <mitermayer.reis@gmail.com>2017-08-29 23:11:47 -0700
committerGitHub <noreply@github.com>2017-08-29 23:11:47 -0700
commita8bb42d835838f793638cc44ad4fd9fd0fa45a01 (patch)
treef314b80f654d85db95c9abf6184240f8656ff504
parent50267eb34b97677d28c816d8feaaa6f01b3f5ab9 (diff)
parentb256344f2b8f0237a67b58bbb56f32a8f17d1903 (diff)
downloadvim-prettier-a8bb42d835838f793638cc44ad4fd9fd0fa45a01.tar.xz
Merge pull request #50 from prettier/issue/48-fixing-co-jump-to-start-of-file
Making sure that after formatting we preserve in the same line
-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