From 314e4053ca3676dce8deceaa432667ab2f3a0058 Mon Sep 17 00:00:00 2001 From: mitermayer Date: Fri, 4 May 2018 09:37:26 -0700 Subject: Moving formating ultils to buffer module - moving formating utils to buffer module to make it easier to interact and test --- autoload/prettier.vim | 36 ++++-------------------------------- autoload/prettier/utils/buffer.vim | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 32 deletions(-) create mode 100644 autoload/prettier/utils/buffer.vim (limited to 'autoload') diff --git a/autoload/prettier.vim b/autoload/prettier.vim index c1aa35b..b4cc7d7 100644 --- a/autoload/prettier.vim +++ b/autoload/prettier.vim @@ -151,11 +151,11 @@ function! s:Prettier_Exec_Sync(cmd, startSelection, endSelection) abort return endif - if (s:Has_Content_Changed(l:out, a:startSelection, a:endSelection) == 0) + if (prettier#utils#buffer#willUpdatedLinesChangeBuffer(l:out, a:startSelection, a:endSelection) == 0) return endif - call s:Apply_Prettier_Format(l:out, a:startSelection, a:endSelection) + call prettier#utils#buffer#replace(l:out, a:startSelection, a:endSelection) endfunction function! s:Prettier_Exec_Async(cmd, startSelection, endSelection) abort @@ -189,7 +189,7 @@ function! s:Prettier_Job_Close(channel, startSelection, endSelection, bufferName endwhile " nothing to update - if (s:Has_Content_Changed(l:out, a:startSelection, a:endSelection) == 0) + if (prettier#utils#buffer#willUpdatedLinesChangeBuffer(l:out, a:startSelection, a:endSelection) == 0) let s:prettier_job_running = 0 return endif @@ -222,7 +222,7 @@ function! s:Prettier_Job_Close(channel, startSelection, endSelection, bufferName endfunction function! s:Prettier_Format_And_Save(lines, start, end) abort - call s:Apply_Prettier_Format(a:lines, a:start, a:end) + call prettier#utils#buffer#replace(a:lines, a:start, a:end) write endfunction @@ -231,34 +231,6 @@ function! s:Prettier_Job_Error(msg) abort let s:prettier_job_running = 0 endfunction -function! s:Has_Content_Changed(content, startLine, endLine) abort - return getbufline(bufnr('%'), 1, line('$')) == s:Get_New_Buffer(a:content, a:startLine, a:endLine) ? 0 : 1 -endfunction - -function! s:Get_New_Buffer(lines, start, end) abort - return getbufline(bufnr('%'), 1, a:start - 1) + a:lines + getbufline(bufnr('%'), a:end + 1, '$') -endfunction - -function! s:Apply_Prettier_Format(lines, startSelection, endSelection) abort - " store view - let l:winview = winsaveview() - let l:newBuffer = s:Get_New_Buffer(a:lines, a:startSelection, a:endSelection) - - " we should not replace contents if the newBuffer is empty - if empty(l:newBuffer) - return - endif - - " delete all lines on the current buffer - silent! execute len(l:newBuffer) . ',' . line('$') . 'delete _' - - " replace all lines from the current buffer with output from prettier - call setline(1, l:newBuffer) - - " Restore view - call winrestview(l:winview) -endfunction - function! s:Prettier_Parse_Error(errors) abort call prettier#logging#error#log('PARSING_ERROR') if g:prettier#quickfix_enabled && prettier#bridge#parser#onError(a:errors) diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim new file mode 100644 index 0000000..06fd7ff --- /dev/null +++ b/autoload/prettier/utils/buffer.vim @@ -0,0 +1,29 @@ +function! prettier#utils#buffer#replace(lines, startSelection, endSelection) abort + " store view + let l:winview = winsaveview() + let l:newBuffer = prettier#utils#buffer#createBufferFromUpdatedLines(a:lines, a:startSelection, a:endSelection) + + " we should not replace contents if the newBuffer is empty + if empty(l:newBuffer) + return + endif + + " delete all lines on the current buffer + silent! execute len(l:newBuffer) . ',' . line('$') . 'delete _' + + " replace all lines from the current buffer with output from prettier + call setline(1, l:newBuffer) + + " Restore view + call winrestview(l:winview) +endfunction + +" Returns 1 if content has changed +function! prettier#utils#buffer#willUpdatedLinesChangeBuffer(lines, start, end) abort + return getbufline(bufnr('%'), 1, line('$')) == prettier#utils#buffer#createBufferFromUpdatedLines(a:lines, a:start, a:end) ? 0 : 1 +endfunction + +" Returns a new buffer with lines replacing start and end of the contents of the current buffer +function! prettier#utils#buffer#createBufferFromUpdatedLines(lines, start, end) abort + return getbufline(bufnr('%'), 1, a:start - 1) + a:lines + getbufline(bufnr('%'), a:end + 1, '$') +endfunction -- cgit v1.3