diff options
| author | mitermayer <mitermayer.reis@gmail.com> | 2018-05-04 12:09:01 -0700 |
|---|---|---|
| committer | mitermayer <mitermayer.reis@gmail.com> | 2019-08-25 21:11:49 -0700 |
| commit | ca9f2d3e391e7fd70b2948a22e9edc391b89cb0e (patch) | |
| tree | e8c71d2033a84b2d276a5ff80a25b01c39c5c4c7 | |
| parent | dc3572ac2b10f091607bf8d1101604ed9b18b996 (diff) | |
| download | vim-prettier-ca9f2d3e391e7fd70b2948a22e9edc391b89cb0e.tar.xz | |
Refactoring out sync jobs and quickfix module
| -rw-r--r-- | autoload/prettier.vim | 35 | ||||
| -rw-r--r-- | autoload/prettier/bridge/parser.vim | 14 | ||||
| -rw-r--r-- | autoload/prettier/job/runner.vim | 54 | ||||
| -rw-r--r-- | autoload/prettier/utils/quickfix.vim | 22 |
4 files changed, 80 insertions, 45 deletions
diff --git a/autoload/prettier.vim b/autoload/prettier.vim index b4cc7d7..331d0f1 100644 --- a/autoload/prettier.vim +++ b/autoload/prettier.vim @@ -11,9 +11,7 @@ "========================================================================================================== " }}} -let s:root_dir = fnamemodify(resolve(expand('<sfile>:p')), ':h') let s:prettier_job_running = 0 -let s:prettier_quickfix_open = 0 function! prettier#PrettierCliPath() abort let l:execCmd = prettier#resolver#executable#getPath() @@ -47,18 +45,14 @@ function! prettier#Prettier(...) abort let l:cmd = l:execCmd . prettier#resolver#config#buildCliArgs(l:config) " close quickfix if it is opened - if s:prettier_quickfix_open - call setqflist([], 'r') - cclose - let s:prettier_quickfix_open = 0 - endif + call prettier#utils#quickfix#close() if l:async && v:version >= 800 && exists('*job_start') call s:Prettier_Exec_Async(l:cmd, l:startSelection, l:endSelection) elseif l:async && has('nvim') && g:prettier#nvim_unstable_async call s:Prettier_Exec_Async_Nvim(l:cmd, l:startSelection, l:endSelection) else - call s:Prettier_Exec_Sync(l:cmd, l:startSelection, l:endSelection) + call prettier#job#runner#run(l:cmd, l:startSelection, l:endSelection, l:async) endif else call prettier#logging#error#log('EXECUTABLE_NOT_FOUND_ERROR') @@ -137,27 +131,6 @@ function! prettier#Autoformat(...) abort let @/=l:search endfunction -function! s:Prettier_Exec_Sync(cmd, startSelection, endSelection) abort - let l:bufferLinesList = getbufline(bufnr('%'), a:startSelection, a:endSelection) - - " vim 7 does not have support for passing a list to system() - let l:bufferLines = v:version <= 800 ? join(l:bufferLinesList, "\n") : l:bufferLinesList - - let l:out = split(system(a:cmd, l:bufferLines), '\n') - - " check system exit code - if v:shell_error - call s:Prettier_Parse_Error(l:out) - return - endif - - if (prettier#utils#buffer#willUpdatedLinesChangeBuffer(l:out, a:startSelection, a:endSelection) == 0) - return - endif - - call prettier#utils#buffer#replace(l:out, a:startSelection, a:endSelection) -endfunction - function! s:Prettier_Exec_Async(cmd, startSelection, endSelection) abort let l:async_cmd = a:cmd @@ -233,7 +206,7 @@ endfunction function! s:Prettier_Parse_Error(errors) abort call prettier#logging#error#log('PARSING_ERROR') - if g:prettier#quickfix_enabled && prettier#bridge#parser#onError(a:errors) - let s:prettier_quickfix_open = 1 + if g:prettier#quickfix_enabled + call prettier#bridge#parser#onError(a:errors, g:prettier#quickfix_auto_focus) endif endfunction diff --git a/autoload/prettier/bridge/parser.vim b/autoload/prettier/bridge/parser.vim index eb23210..51c4069 100644 --- a/autoload/prettier/bridge/parser.vim +++ b/autoload/prettier/bridge/parser.vim @@ -1,4 +1,7 @@ -function! prettier#bridge#parser#onError(out) abort +" TODO +" this function should just returns the parsed errors list instead +" of opening the quickfix +function! prettier#bridge#parser#onError(out, autoFocus) abort let l:errors = [] for l:line in a:out @@ -16,13 +19,6 @@ function! prettier#bridge#parser#onError(out) abort endfor if len(l:errors) - let l:winnr = winnr() - call setqflist(l:errors, 'r') - botright copen - if !g:prettier#quickfix_auto_focus - " Return the cursor back to the main buffer. - exe l:winnr . 'wincmd w' - endif - return 1 + call prettier#utils#quickfix#open(l:errors, a:autoFocus) endif endfunction diff --git a/autoload/prettier/job/runner.vim b/autoload/prettier/job/runner.vim index 3c6b3d9..f5291dd 100644 --- a/autoload/prettier/job/runner.vim +++ b/autoload/prettier/job/runner.vim @@ -1,9 +1,53 @@ +" TODO +" move the bellow vim checks to UTILS +" +" TODO +" we are currently feature protecting async on NVIM with g:prettier#nvim_unstable_async +" we should remove this once its fully supported +let s:isNeoVim = has('nvim') && g:prettier#nvim_unstable_async +let s:isAsyncVim = v:version >= 800 && exists('*job_start') +let s:isLegacyVim = v:version <= 800 + function! prettier#job#runner#run(cmd, startSelection, endSelection, async) abort - if a:async && v:version >= 800 && exists('*job_start') - "call s:Prettier_Exec_Async(l:cmd, l:startSelection, l:endSelection) - elseif a:async && has('nvim') && g:prettier#nvim_unstable_async - "call s:Prettier_Exec_Async_Nvim(l:cmd, l:startSelection, l:endSelection) + if a:async && (s:isAsyncVim || s:isNeoVim) + call s:asyncFormat(a:cmd, a:startSelection, a:endSelection) else - "call s:Prettier_Exec_Sync(l:cmd, l:startSelection, l:endSelection) + call s:format(a:cmd, a:startSelection, a:endSelection) endif endfunction + +function! prettier#job#runner#onError(errors) abort + call prettier#logging#error#log('PARSING_ERROR') + if g:prettier#quickfix_enabled + call prettier#bridge#parser#onError(a:errors, g:prettier#quickfix_auto_focus) + endif +endfunction + +function! s:asyncFormat(cmd, startSelection, endSelection, autoFocus) abort + if s:isAsyncVim + echom 'async vim' + else + echom 'neovim' + endif +endfunction + +function! s:format(cmd, startSelection, endSelection) abort + let l:bufferLinesList = getbufline(bufnr('%'), a:startSelection, a:endSelection) + + " vim 7 does not have support for passing a list to system() + let l:bufferLines = s:isLegacyVim ? join(l:bufferLinesList, "\n") : l:bufferLinesList + + let l:out = split(system(a:cmd, l:bufferLines), '\n') + + " check system exit code + if v:shell_error + call prettier#job#runner#onError(l:out) + return + endif + + if (prettier#utils#buffer#willUpdatedLinesChangeBuffer(l:out, a:startSelection, a:endSelection) == 0) + return + endif + + call prettier#utils#buffer#replace(l:out, a:startSelection, a:endSelection) +endfunction diff --git a/autoload/prettier/utils/quickfix.vim b/autoload/prettier/utils/quickfix.vim new file mode 100644 index 0000000..b4766a7 --- /dev/null +++ b/autoload/prettier/utils/quickfix.vim @@ -0,0 +1,22 @@ +" We use this flag so that we ensure only clearing quickfix if it was created by prettier itself +let s:prettier_quickfix_open = 0 + +function! prettier#utils#quickfix#close() abort + " close quickfix if it is opened + if s:prettier_quickfix_open + call setqflist([], 'r') + cclose + let s:prettier_quickfix_open = 0 + endif +endfunction + +function! prettier#utils#quickfix#open(errors, focus) abort + let s:prettier_quickfix_open = 1 + let l:winnr = winnr() + call setqflist(a:errors, 'r') + botright copen + if !a:focus + " Return the cursor back to the main buffer. + exe l:winnr . 'wincmd w' + endif +endfunction |
