aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormitermayer <mitermayer.reis@gmail.com>2017-06-15 21:34:38 -0700
committermitermayer <mitermayer.reis@gmail.com>2017-06-19 21:02:05 -0700
commite93d69afb780ea2035c7ee3a9297ee37575b0b35 (patch)
treed4ea4562957d74c3db93d3068b306bcad0517090
parent8112c1c949a001957a1c31e3edf6a572cf54b456 (diff)
downloadvim-prettier-e93d69afb780ea2035c7ee3a9297ee37575b0b35.tar.xz
feature: enabling partial buffer conversion
- bumping plugin prettier version - minor fix, avoiding unecessary sync formattings when content has not changed - adding support for partial convertion when on visual selection
-rw-r--r--autoload/prettier.vim47
-rw-r--r--package.json2
-rw-r--r--plugin/prettier.vim4
3 files changed, 35 insertions, 18 deletions
diff --git a/autoload/prettier.vim b/autoload/prettier.vim
index 6c655d2..6a1e23c 100644
--- a/autoload/prettier.vim
+++ b/autoload/prettier.vim
@@ -4,6 +4,8 @@ let s:prettier_job_running = 0
function! prettier#Prettier(...) abort
let l:execCmd = s:Get_Prettier_Exec()
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:config = getbufvar(bufnr('%'), 'prettier_ft_default_args', {})
if l:execCmd != -1
@@ -14,16 +16,16 @@ function! prettier#Prettier(...) abort
cclose
if l:async && v:version >= 800 && exists('*job_start')
- call s:Prettier_Exec_Async(l:cmd)
+ call s:Prettier_Exec_Async(l:cmd, l:startSelection, l:endSelection)
else
- call s:Prettier_Exec_Sync(l:cmd)
+ call s:Prettier_Exec_Sync(l:cmd, l:startSelection, l:endSelection)
endif
else
call s:Suggest_Install_Prettier()
endif
endfunction
-function! prettier#Autoformat() abort
+function! prettier#Autoformat(...) abort
let l:curPos = getpos('.')
let l:maxLineLookup = 50
let l:maxTimeLookupMs = 500
@@ -43,8 +45,8 @@ function! prettier#Autoformat() abort
call cursor(curPos[1], curPos[2])
endfunction
-function! s:Prettier_Exec_Sync(cmd) abort
- let l:out = split(system(a:cmd, getbufline(bufnr('%'), 1, '$')), '\n')
+function! s:Prettier_Exec_Sync(cmd, startSelection, endSelection) abort
+ let l:out = split(system(a:cmd, getbufline(bufnr('%'), a:startSelection, a:endSelection)), '\n')
" check system exit code
if v:shell_error
@@ -52,21 +54,27 @@ function! s:Prettier_Exec_Sync(cmd) abort
return
endif
- call s:Apply_Prettier_Format(l:out)
+ if (s:Has_Content_Changed(a:startSelection, a:endSelection, l:out) == 0)
+ return
+ endif
+
+ call s:Apply_Prettier_Format(l:out, a:startSelection, a:endSelection)
endfunction
-function! s:Prettier_Exec_Async(cmd) abort
+function! s:Prettier_Exec_Async(cmd, startSelection, endSelection) abort
if s:prettier_job_running != 1
let s:prettier_job_running = 1
call job_start(a:cmd, {
\ 'in_io': 'buffer',
+ \ 'in_top': a:startSelection,
+ \ 'in_bot': a:endSelection,
\ 'in_name': bufname('%'),
- \ 'err_cb': 'Prettier_Job_Error',
- \ 'close_cb': 'Prettier_Job_Close' })
+ \ 'err_cb': {channel, msg -> s:Prettier_Job_Error(channel, msg)},
+ \ 'close_cb': {channel -> s:Prettier_Job_Close(channel, a:startSelection, a:endSelection)}})
endif
endfunction
-function! Prettier_Job_Close(channel) abort
+function! s:Prettier_Job_Close(channel, startSelection, endSelection) abort
let l:out = []
while ch_status(a:channel, {'part': 'out'}) == 'buffered'
@@ -74,19 +82,19 @@ function! Prettier_Job_Close(channel) abort
endwhile
" nothing to update
- if (getbufline(bufnr('%'), 1, '$') == l:out)
+ if (s:Has_Content_Changed(a:startSelection, a:endSelection, l:out) == 0)
let s:prettier_job_running = 0
return
endif
if len(l:out)
- call s:Apply_Prettier_Format(l:out)
+ call s:Apply_Prettier_Format(l:out, a:startSelection, a:endSelection)
write
let s:prettier_job_running = 0
endif
endfunction
-function! Prettier_Job_Error(channel, msg) abort
+function! s:Prettier_Job_Error(channel, msg) abort
call s:Prettier_Parse_Error(split(a:msg, '\n'))
let s:prettier_job_running = 0
endfunction
@@ -112,15 +120,24 @@ function! s:Handle_Parsing_Errors(out) abort
endif
endfunction
-function! s:Apply_Prettier_Format(lines) abort
+function! s:Has_Content_Changed(startLine, endLine, content) abort
+ return getbufline(bufnr('%'), 1, line('$')) == s:Get_New_Buffer(a:content, a:startLine, a:endLine) ? 0 : 1
+endfunction
+
+function! s:Get_New_Buffer(lines, start, end) abort
+ return getbufline(bufnr('%'), 1, a:start - 1) + a:lines + getbufline(bufnr('%'), a:end + 1, '$')
+endfunction
+
+function! s:Apply_Prettier_Format(lines, startSelection, endSelection) abort
" store cursor position
let l:curPos = getpos('.')
+ let l:newBuffer = s:Get_New_Buffer(a:lines, a:startSelection, a:endSelection)
" delete all lines on the current buffer
silent! execute 1 . ',' . line('$') . 'delete _'
" replace all lines from the current buffer with output from prettier
- call setline(1, a:lines)
+ call setline(1, l:newBuffer)
" restore cursor position
call cursor(l:curPos[1], l:curPos[2])
diff --git a/package.json b/package.json
index 9f78cf2..9d6eb0a 100644
--- a/package.json
+++ b/package.json
@@ -8,6 +8,6 @@
"url": "git://github.com/mitermayer/vim-prettier.git"
},
"dependencies": {
- "prettier": "^1.4.2"
+ "prettier": "^1.4.4"
}
}
diff --git a/plugin/prettier.vim b/plugin/prettier.vim
index d728353..ca35c79 100644
--- a/plugin/prettier.vim
+++ b/plugin/prettier.vim
@@ -55,10 +55,10 @@ let g:prettier#config#trailing_comma = get(g:,'prettier#config#trailing_comma',
let g:prettier#config#parser = get(g:,'prettier#config#parser', 'flow')
" synchronous by default
-command! Prettier call prettier#Prettier(g:prettier#exec_cmd_async)
+command! -nargs=? -range=% Prettier call prettier#Prettier(g:prettier#exec_cmd_async, <line1>, <line2>)
" prettier async
-command! PrettierAsync call prettier#Prettier(1)
+command! -nargs=? -range=% PrettierAsync call prettier#Prettier(1, <line1>, <line2>)
" map command
if !hasmapto('<Plug>(Prettier)') && maparg('<Leader>p', 'n') ==# ''