aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitermayer Reis <mitermayer.reis@gmail.com>2019-09-13 22:29:36 -0700
committerGitHub <noreply@github.com>2019-09-13 22:29:36 -0700
commit2bc93788e5a48e25ba6ed8f8a46e05cbd65489f3 (patch)
tree4a694e3f668534c6d0c5f1356c034755b8172fa6
parente98b108ded5c906dde32db2f9856184e00dfc945 (diff)
parent46fd3290d1c69fb394a79e5174e7c1aa69b885c0 (diff)
downloadvim-prettier-2bc93788e5a48e25ba6ed8f8a46e05cbd65489f3.tar.xz
Merge pull request #207 from prettier/issues/184-fixing-undo-step
Issues/184 fixing undo step
-rw-r--r--autoload/prettier/job/async/neovim.vim4
-rw-r--r--autoload/prettier/utils/buffer.vim9
-rw-r--r--plugin/prettier.vim2
3 files changed, 11 insertions, 4 deletions
diff --git a/autoload/prettier/job/async/neovim.vim b/autoload/prettier/job/async/neovim.vim
index 1cffe5c..28ecb8a 100644
--- a/autoload/prettier/job/async/neovim.vim
+++ b/autoload/prettier/job/async/neovim.vim
@@ -73,7 +73,7 @@ function! s:onExit(status, info, out, err) abort
try
silent exec 'sp '. escape(bufname(a:info.buf_nr), ' \')
call nvim_buf_set_lines(a:info.buf_nr, a:info.start, a:info.end, 0, l:out)
- write
+ noautocmd write
catch
call prettier#logging#error#log('PARSING_ERROR')
finally
@@ -91,7 +91,7 @@ function! s:onExit(status, info, out, err) abort
" TODO
" we should be auto saving in order to work similar to vim8
call nvim_buf_set_lines(a:info.buf_nr, a:info.start, a:info.end, 0, l:out)
- write
+ noautocmd write
endif
let s:prettier_job_running = 0
diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim
index c22fec7..7bc7ab1 100644
--- a/autoload/prettier/utils/buffer.vim
+++ b/autoload/prettier/utils/buffer.vim
@@ -8,6 +8,12 @@ function! prettier#utils#buffer#replace(lines, startSelection, endSelection) abo
return
endif
+ " https://vim.fandom.com/wiki/Restore_the_cursor_position_after_undoing_text_change_made_by_a_script
+ " create a fake change entry and merge with undo stack prior to do formating
+ normal! ix
+ normal! x
+ try | silent undojoin | catch | endtry
+
" delete all lines on the current buffer
silent! execute '%delete _'
@@ -23,12 +29,13 @@ function! prettier#utils#buffer#replace(lines, startSelection, endSelection) abo
" Restore view
call winrestview(l:winview)
+
endfunction
" Replace and save the buffer
function! prettier#utils#buffer#replaceAndSave(lines, startSelection, endSelection) abort
call prettier#utils#buffer#replace(a:lines, a:startSelection, a:endSelection)
- write
+ noautocmd write
endfunction
" Returns 1 if content has changed
diff --git a/plugin/prettier.vim b/plugin/prettier.vim
index aafa7a2..928630b 100644
--- a/plugin/prettier.vim
+++ b/plugin/prettier.vim
@@ -140,6 +140,6 @@ nnoremap <silent> <Plug>(PrettierCliPath) :PrettierCliPath<CR>
augroup Prettier
autocmd!
if g:prettier#autoformat
- autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md,*.vue,*.yaml,*.html call prettier#Autoformat()
+ autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md,*.vue,*.yaml,*.html noautocmd | call prettier#Autoformat()
endif
augroup end