aboutsummaryrefslogtreecommitdiff
path: root/autoload/prettier
diff options
context:
space:
mode:
authormitermayer <mitermayer.reis@gmail.com>2018-05-04 12:09:01 -0700
committermitermayer <mitermayer.reis@gmail.com>2018-05-04 12:09:01 -0700
commite78c17a9fa8aab9067a44f9836d46fa9dc2d5914 (patch)
treef75b59b3f612ec837ffb6eadb5a586891a3681e7 /autoload/prettier
parent04f122063407c829b8762a1425fe55a0bb2f686b (diff)
downloadvim-prettier-e78c17a9fa8aab9067a44f9836d46fa9dc2d5914.tar.xz
Refactoring out sync jobs and quickfix module
Diffstat (limited to 'autoload/prettier')
-rw-r--r--autoload/prettier/bridge/parser.vim14
-rw-r--r--autoload/prettier/job/runner.vim54
-rw-r--r--autoload/prettier/utils/quickfix.vim22
3 files changed, 76 insertions, 14 deletions
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