From e93d69afb780ea2035c7ee3a9297ee37575b0b35 Mon Sep 17 00:00:00 2001 From: mitermayer Date: Thu, 15 Jun 2017 21:34:38 -0700 Subject: 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 --- autoload/prettier.vim | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'autoload/prettier.vim') 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]) -- cgit v1.3