aboutsummaryrefslogtreecommitdiff
path: root/autoload/prettier/utils/buffer.vim
diff options
context:
space:
mode:
authorMitermayer Reis <mitermayer.reis@gmail.com>2020-02-05 11:08:03 +1100
committerGitHub <noreply@github.com>2020-02-05 11:08:03 +1100
commit49d91743b2df43f84edd199f877d494b4d8812f4 (patch)
treecf856d77c9960a09eb3156937aa1b896b855bed6 /autoload/prettier/utils/buffer.vim
parent9eb448e45ef88e90681335fda32bcae52a09d6dc (diff)
parentb064c6ab82a3c57ea64360d762d661ad7e8ee54c (diff)
downloadvim-prettier-49d91743b2df43f84edd199f877d494b4d8812f4.tar.xz
Merge pull request #175 from prettier/release/1.x
Release/1.x
Diffstat (limited to 'autoload/prettier/utils/buffer.vim')
-rw-r--r--autoload/prettier/utils/buffer.vim70
1 files changed, 70 insertions, 0 deletions
diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim
new file mode 100644
index 0000000..7bc7ab1
--- /dev/null
+++ b/autoload/prettier/utils/buffer.vim
@@ -0,0 +1,70 @@
+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
+
+ " 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 _'
+
+ " replace all lines from the current buffer with output from prettier
+ let l:idx = 0
+ for l:line in l:newBuffer
+ silent! call append(l:idx, l:line)
+ let l:idx += 1
+ endfor
+
+ " delete trailing newline introduced by the above append procedure
+ silent! execute '$delete _'
+
+ " 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)
+ noautocmd write
+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
+
+" Adapted from https://github.com/farazdagi/vim-go-ide
+function! s:getCharPosition(line, col) abort
+ if &encoding !=# 'utf-8'
+ " On utf-8 enconding we can't just use bytes so we need to make sure we can count the
+ " characters, we do that by adding the text into a temporary buffer and counting the chars
+ let l:buf = a:line == 1 ? '' : (join(getline(1, a:line - 1), "\n") . "\n")
+ let l:buf .= a:col == 1 ? '' : getline('.')[:a:col - 2]
+ return len(iconv(l:buf, &encoding, 'utf-8'))
+ endif
+ " On non utf-8 the line byte should match the character
+ return line2byte(a:line) + (a:col - 2)
+endfun
+
+" Returns [start, end] byte range when on visual mode
+function! prettier#utils#buffer#getCharRange(startSelection, endSelection) abort
+ let l:range = []
+ call add(l:range, s:getCharPosition(a:startSelection, col("'<")))
+ call add(l:range, s:getCharPosition(a:endSelection, col("'>")))
+ return l:range
+endfunction