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 --- plugin/prettier.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugin/prettier.vim') 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, , ) " prettier async -command! PrettierAsync call prettier#Prettier(1) +command! -nargs=? -range=% PrettierAsync call prettier#Prettier(1, , ) " map command if !hasmapto('(Prettier)') && maparg('p', 'n') ==# '' -- cgit v1.3 From b55d891d2824f98d9fa6e258f5dba28d6b5ccc3c Mon Sep 17 00:00:00 2001 From: mitermayer Date: Mon, 19 Jun 2017 21:15:00 -0700 Subject: feature: enabling partial buffer conversion (tech review) - removing trailing spaces - removing extra param from error callback - changing function signature with more consistent params --- autoload/prettier.vim | 58 ++++++++++++++++++++++++------------------------- ftplugin/css.vim | 2 +- ftplugin/less.vim | 2 +- ftplugin/scss.vim | 2 +- ftplugin/typescript.vim | 2 +- plugin/prettier.vim | 2 +- 6 files changed, 34 insertions(+), 34 deletions(-) (limited to 'plugin/prettier.vim') diff --git a/autoload/prettier.vim b/autoload/prettier.vim index 6a1e23c..fc0e14c 100644 --- a/autoload/prettier.vim +++ b/autoload/prettier.vim @@ -3,9 +3,9 @@ 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: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 @@ -27,25 +27,25 @@ endfunction function! prettier#Autoformat(...) abort let l:curPos = getpos('.') - let l:maxLineLookup = 50 - let l:maxTimeLookupMs = 500 + let l:maxLineLookup = 50 + let l:maxTimeLookupMs = 500 let l:pattern = '@format' - " we need to move selection to the top before looking up to avoid + " we need to move selection to the top before looking up to avoid " scanning a very long file call cursor(1, 1) " Search starting at the start of the document if search(l:pattern, 'n', l:maxLineLookup, l:maxTimeLookupMs) > 0 " autoformat async - call prettier#Prettier(1) + call prettier#Prettier(1) endif " Restore the selection and if greater then before it defaults to end call cursor(curPos[1], curPos[2]) endfunction -function! s:Prettier_Exec_Sync(cmd, startSelection, endSelection) abort +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 @@ -54,14 +54,14 @@ function! s:Prettier_Exec_Sync(cmd, startSelection, endSelection) abort return endif - if (s:Has_Content_Changed(a:startSelection, a:endSelection, l:out) == 0) + if (s:Has_Content_Changed(l:out, a:startSelection, a:endSelection) == 0) return endif call s:Apply_Prettier_Format(l:out, a:startSelection, a:endSelection) endfunction -function! s:Prettier_Exec_Async(cmd, startSelection, endSelection) 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, { @@ -69,12 +69,12 @@ function! s:Prettier_Exec_Async(cmd, startSelection, endSelection) abort \ 'in_top': a:startSelection, \ 'in_bot': a:endSelection, \ 'in_name': bufname('%'), - \ 'err_cb': {channel, msg -> s:Prettier_Job_Error(channel, msg)}, + \ 'err_cb': {channel, msg -> s:Prettier_Job_Error(msg)}, \ 'close_cb': {channel -> s:Prettier_Job_Close(channel, a:startSelection, a:endSelection)}}) endif endfunction -function! s:Prettier_Job_Close(channel, startSelection, endSelection) abort +function! s:Prettier_Job_Close(channel, startSelection, endSelection) abort let l:out = [] while ch_status(a:channel, {'part': 'out'}) == 'buffered' @@ -82,19 +82,19 @@ function! s:Prettier_Job_Close(channel, startSelection, endSelection) abort endwhile " nothing to update - if (s:Has_Content_Changed(a:startSelection, a:endSelection, l:out) == 0) + if (s:Has_Content_Changed(l:out, a:startSelection, a:endSelection) == 0) let s:prettier_job_running = 0 return endif - - if len(l:out) + + if len(l:out) call s:Apply_Prettier_Format(l:out, a:startSelection, a:endSelection) write let s:prettier_job_running = 0 endif endfunction -function! s:Prettier_Job_Error(channel, msg) abort +function! s:Prettier_Job_Error(msg) abort call s:Prettier_Parse_Error(split(a:msg, '\n')) let s:prettier_job_running = 0 endfunction @@ -114,13 +114,13 @@ function! s:Handle_Parsing_Errors(out) abort endif endfor - if len(l:errors) + if len(l:errors) call setqflist(l:errors) botright copen endif endfunction -function! s:Has_Content_Changed(startLine, endLine, content) abort +function! s:Has_Content_Changed(content, startLine, endLine) abort return getbufline(bufnr('%'), 1, line('$')) == s:Get_New_Buffer(a:content, a:startLine, a:endLine) ? 0 : 1 endfunction @@ -172,24 +172,24 @@ endfunction " By default we will search for the following " => locally installed prettier inside node_modules on any parent folder -" => globally installed prettier +" => globally installed prettier " => vim-prettier prettier installation " => if all fails suggest install function! s:Get_Prettier_Exec() abort let l:local_exec = s:Get_Prettier_Local_Exec() if executable(l:local_exec) return l:local_exec - endif + endif let l:global_exec = s:Get_Prettier_Global_Exec() if executable(l:global_exec) return l:global_exec - endif + endif let l:plugin_exec = s:Get_Prettier_Plugin_Exec() if executable(l:plugin_exec) return l:plugin_exec - endif + endif return -1 endfunction @@ -207,11 +207,11 @@ function! s:Get_Prettier_Plugin_Exec() abort endfunction function! s:Get_Exec(...) abort - let l:rootDir = a:0 > 0 ? a:1 : 0 + let l:rootDir = a:0 > 0 ? a:1 : 0 let l:exec = -1 - if isdirectory(l:rootDir) - let l:dir = s:Tranverse_Dir_Search(l:rootDir) + if isdirectory(l:rootDir) + let l:dir = s:Tranverse_Dir_Search(l:rootDir) if dir != -1 let l:exec = s:Get_Path_To_Exec(l:dir) endif @@ -223,8 +223,8 @@ function! s:Get_Exec(...) abort endfunction function! s:Get_Path_To_Exec(...) abort - let l:rootDir = a:0 > 0 ? a:1 : -1 - let l:dir = l:rootDir != -1 ? l:rootDir . '/.bin/' : '' + let l:rootDir = a:0 > 0 ? a:1 : -1 + let l:dir = l:rootDir != -1 ? l:rootDir . '/.bin/' : '' return dir . 'prettier' endfunction @@ -234,12 +234,12 @@ function! s:Tranverse_Dir_Search(rootDir) abort while 1 let l:search_dir = root . '/' . dir - if isdirectory(l:search_dir) + if isdirectory(l:search_dir) return l:search_dir endif let l:parent = fnamemodify(root, ':h') - if l:parent == l:root + if l:parent == l:root return -1 endif diff --git a/ftplugin/css.vim b/ftplugin/css.vim index 92bc628..f268f42 100644 --- a/ftplugin/css.vim +++ b/ftplugin/css.vim @@ -1,5 +1,5 @@ let b:prettier_ft_default_args = { - \ 'parser': 'postcss' + \ 'parser': 'postcss' \ } augroup Prettier diff --git a/ftplugin/less.vim b/ftplugin/less.vim index 92bc628..f268f42 100644 --- a/ftplugin/less.vim +++ b/ftplugin/less.vim @@ -1,5 +1,5 @@ let b:prettier_ft_default_args = { - \ 'parser': 'postcss' + \ 'parser': 'postcss' \ } augroup Prettier diff --git a/ftplugin/scss.vim b/ftplugin/scss.vim index 92bc628..f268f42 100644 --- a/ftplugin/scss.vim +++ b/ftplugin/scss.vim @@ -1,5 +1,5 @@ let b:prettier_ft_default_args = { - \ 'parser': 'postcss' + \ 'parser': 'postcss' \ } augroup Prettier diff --git a/ftplugin/typescript.vim b/ftplugin/typescript.vim index b654a48..8151c09 100644 --- a/ftplugin/typescript.vim +++ b/ftplugin/typescript.vim @@ -1,5 +1,5 @@ let b:prettier_ft_default_args = { - \ 'parser': 'typescript' + \ 'parser': 'typescript' \ } augroup Prettier diff --git a/plugin/prettier.vim b/plugin/prettier.vim index ca35c79..207c46c 100644 --- a/plugin/prettier.vim +++ b/plugin/prettier.vim @@ -20,7 +20,7 @@ let g:loaded_prettier = 1 " autoformating enabled by default upon saving let g:prettier#autoformat = get(g:, 'prettier#autoformat', 1) -" calling :Prettier by default runs synchronous +" calling :Prettier by default runs synchronous let g:prettier#exec_cmd_async = get(g:, 'prettier#exec_cmd_async', 0) " when having formatting errors will open the quickfix by default -- cgit v1.3