aboutsummaryrefslogtreecommitdiff
path: root/autoload/prettier/utils/buffer.vim
diff options
context:
space:
mode:
authormitermayer <mitermayer.reis@gmail.com>2018-05-28 20:53:30 -0700
committermitermayer <mitermayer.reis@gmail.com>2018-06-03 22:58:24 -0700
commitec6ede90f3b9948ed7063402189653f8d6721326 (patch)
tree69d08cc06297b9144fd630032368013a450a8aea /autoload/prettier/utils/buffer.vim
parent98845cdbbe243f4a62adbd73cbe7febec1f41efa (diff)
downloadvim-prettier-ec6ede90f3b9948ed7063402189653f8d6721326.tar.xz
Enabling partial formatting but still maintaining support for fragment
formatting
Diffstat (limited to 'autoload/prettier/utils/buffer.vim')
-rw-r--r--autoload/prettier/utils/buffer.vim21
1 files changed, 21 insertions, 0 deletions
diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim
index 1e11ddb..0b3c619 100644
--- a/autoload/prettier/utils/buffer.vim
+++ b/autoload/prettier/utils/buffer.vim
@@ -33,3 +33,24 @@ endfunction
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)
+ 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