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 (PrettierCliPath) :PrettierCliPath 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