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/utils/buffer.vim | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 autoload/prettier/utils/buffer.vim (limited to 'autoload/prettier') 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