aboutsummaryrefslogtreecommitdiff
path: root/autoload/prettier/utils
diff options
context:
space:
mode:
authormitermayer <mitermayer.reis@gmail.com>2018-05-04 09:37:26 -0700
committermitermayer <mitermayer.reis@gmail.com>2018-05-04 09:37:26 -0700
commit314e4053ca3676dce8deceaa432667ab2f3a0058 (patch)
tree04d1aa258485b836eea02bd1fe885462e62f7820 /autoload/prettier/utils
parent84b1da26c6204961d735562a1a080e4140de5072 (diff)
downloadvim-prettier-314e4053ca3676dce8deceaa432667ab2f3a0058.tar.xz
Moving formating ultils to buffer module
- moving formating utils to buffer module to make it easier to interact and test
Diffstat (limited to 'autoload/prettier/utils')
-rw-r--r--autoload/prettier/utils/buffer.vim29
1 files changed, 29 insertions, 0 deletions
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