aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--autoload/prettier.vim45
1 files changed, 45 insertions, 0 deletions
diff --git a/autoload/prettier.vim b/autoload/prettier.vim
index 7325dfa..ac183b7 100644
--- a/autoload/prettier.vim
+++ b/autoload/prettier.vim
@@ -55,6 +55,8 @@ function! prettier#Prettier(...) abort
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')
+ call s:Prettier_Exec_Async_Nvim(l:cmd, l:startSelection, l:endSelection)
else
call s:Prettier_Exec_Sync(l:cmd, l:startSelection, l:endSelection)
endif
@@ -63,6 +65,49 @@ function! prettier#Prettier(...) abort
endif
endfunction
+function! s:Prettier_Exec_Async_Nvim(cmd, startSelection, endSelection) abort
+ let l:async_cmd = a:cmd
+
+ if has('win32') || has('win64')
+ let l:async_cmd = 'cmd.exe /c ' . a:cmd
+ endif
+
+ let l:lines = getline(a:startSelection, a:endSelection)
+ let l:dict = {
+ \ 'start': a:startSelection - 1,
+ \ 'end': a:endSelection,
+ \ 'buf_nr': bufnr('%'),
+ \ 'content': join(l:lines, "\n"),
+ \}
+ let l:out = []
+ let l:err = []
+
+ let l:job = jobstart(l:async_cmd, {
+ \ 'on_stdout': {job_id, data, event -> extend(l:out, data)},
+ \ 'on_stderr': {job_id, data, event -> extend(l:err, data)},
+ \ 'on_exit': {job_id, status, event -> s:Prettier_Job_Nvim_Exit(status, l:dict, l:out, l:err)},
+ \ })
+ call jobsend(l:job, l:lines)
+ call jobclose(l:job, 'stdin')
+endfunction
+
+function! s:Prettier_Job_Nvim_Exit(status, info, out, err)
+ if a:status != 0
+ echoerr join(a:err, "\n")
+ return
+ endif
+ if len(a:out) == 0 | return | endif
+
+ let l:last = a:out[len(a:out) - 1]
+ let l:out = l:last == '' ? a:out[0:len(a:out) - 2] : a:out
+ if a:info.content == join(l:out, "\n")
+ " no change
+ return
+ endif
+
+ call nvim_buf_set_lines(a:info.buf_nr, a:info.start, a:info.end, 0, l:out)
+endfunction
+
function! prettier#Autoformat(...) abort
let l:curPos = getpos('.')
let l:maxLineLookup = 50