aboutsummaryrefslogtreecommitdiff
path: root/autoload
diff options
context:
space:
mode:
Diffstat (limited to 'autoload')
-rw-r--r--autoload/prettier.vim18
-rw-r--r--autoload/prettier/resolver/config.vim14
-rw-r--r--autoload/prettier/resolver/preset.vim2
-rw-r--r--autoload/prettier/utils/buffer.vim21
4 files changed, 52 insertions, 3 deletions
diff --git a/autoload/prettier.vim b/autoload/prettier.vim
index fae1ef8..0e8ade3 100644
--- a/autoload/prettier.vim
+++ b/autoload/prettier.vim
@@ -69,14 +69,30 @@ function! prettier#Prettier(...) abort
let l:async = a:0 > 0 ? a:1 : 0
let l:startSelection = a:0 > 1 ? a:2 : 1
let l:endSelection = a:0 > 2 ? a:3 : line('$')
+ let l:hasSelection = a:0 > 2 ? 1 : 0
+ let l:partialFormat = a:0 > 3 && a:4 ? a:4 : 0
let l:config = getbufvar(bufnr('%'), 'prettier_ft_default_args', {})
+ let l:partialFormatEnabled = l:hasSelection && l:partialFormat
if l:execCmd != -1
- let l:cmd = l:execCmd . prettier#resolver#config#buildCliArgs(prettier#resolver#preset#build(l:config))
+ " TODO
+ " => we should make sure we can resolve --range-start and --range-end when required
+ " => when the above is required we should also update l:startSelection to '1' and l:endSelection to line('$')
+ let l:cmd = l:execCmd . prettier#resolver#config#resolve(
+ \ prettier#resolver#preset#resolve(l:config),
+ \ l:partialFormatEnabled,
+ \ l:startSelection,
+ \ l:endSelection)
" close quickfix if it is opened
call prettier#utils#quickfix#close()
+ " we will be using portion formatting, so we need to send entire buffer to prettier
+ if l:partialFormatEnabled
+ let l:startSelection = 1
+ let l:endSelection = line('$')
+ endif
+
" format buffer
call prettier#job#runner#run(l:cmd, l:startSelection, l:endSelection, l:async)
else
diff --git a/autoload/prettier/resolver/config.vim b/autoload/prettier/resolver/config.vim
index 91fc247..eae6725 100644
--- a/autoload/prettier/resolver/config.vim
+++ b/autoload/prettier/resolver/config.vim
@@ -1,6 +1,6 @@
" By default we will default to our internal
" configuration settings for prettier
-function! prettier#resolver#config#buildCliArgs(config) abort
+function! prettier#resolver#config#resolve(config, hasSelection, start, end) abort
" Allow params to be passed as json format
" convert bellow usage of globals to a get function o the params defaulting to global
" TODO: Use a list, filter() and join() to get a nicer list of args.
@@ -8,6 +8,7 @@ function! prettier#resolver#config#buildCliArgs(config) abort
\ s:Flag_tab_width(a:config) . ' ' .
\ s:Flag_print_width(a:config) . ' ' .
\ s:Flag_parser(a:config) . ' ' .
+ \ s:Flag_range_delimiter(a:config, a:hasSelection, a:start, a:end) . ' ' .
\ ' --semi=' .
\ get(a:config, 'semi', g:prettier#config#semi) .
\ ' --single-quote=' .
@@ -34,6 +35,17 @@ function! prettier#resolver#config#buildCliArgs(config) abort
return l:cmd
endfunction
+" Returns either '--range-start X --range-end Y' or an empty string.
+function! s:Flag_range_delimiter(config, partialFormatEnabled, start, end) abort
+ if (!a:partialFormatEnabled)
+ return ''
+ endif
+
+ let l:range = prettier#utils#buffer#getCharRange(a:start, a:end)
+
+ return '--range-start=' . l:range[0] . ' --range-end=' . l:range[1]
+endfunction
+
" Returns '--tab-width=NN'
function! s:Flag_tab_width(config) abort
let l:value = get(a:config, 'tabWidth', g:prettier#config#tab_width)
diff --git a/autoload/prettier/resolver/preset.vim b/autoload/prettier/resolver/preset.vim
index a0891d2..03f98ee 100644
--- a/autoload/prettier/resolver/preset.vim
+++ b/autoload/prettier/resolver/preset.vim
@@ -1,5 +1,5 @@
" Build config using predefined preset
-function! prettier#resolver#preset#build(fileTypeConfigOverwrites) abort
+function! prettier#resolver#preset#resolve(fileTypeConfigOverwrites) abort
if ( g:prettier#preset#config ==# 'fb' )
return extend(prettier#presets#fb#config(), a:fileTypeConfigOverwrites)
endif
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