aboutsummaryrefslogtreecommitdiff
path: root/autoload/prettier.vim
diff options
context:
space:
mode:
Diffstat (limited to 'autoload/prettier.vim')
-rw-r--r--autoload/prettier.vim89
1 files changed, 21 insertions, 68 deletions
diff --git a/autoload/prettier.vim b/autoload/prettier.vim
index 0c52358..9be30f5 100644
--- a/autoload/prettier.vim
+++ b/autoload/prettier.vim
@@ -34,74 +34,6 @@ function! prettier#PrettierCli(user_input) abort
endif
endfunction
-" Main prettier command
-function! prettier#Prettier(...) abort
- let l:execCmd = prettier#resolver#executable#getPath()
- 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
- let l:cmd = l:execCmd . prettier#resolver#config#buildCliArgs(l:config)
-
- " close quickfix if it is opened
- call prettier#utils#quickfix#close()
-
- if l:async && has('nvim') && g:prettier#nvim_unstable_async
- call s:Prettier_Exec_Async_Nvim(l:cmd, l:startSelection, l:endSelection)
- else
- call prettier#job#runner#run(l:cmd, l:startSelection, l:endSelection, l:async)
- endif
- else
- call prettier#logging#error#log('EXECUTABLE_NOT_FOUND_ERROR')
- 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([&shell, &shellcmdflag, 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) abort
- 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
@@ -129,3 +61,24 @@ function! prettier#Autoformat(...) abort
" Restore search
let @/=l:search
endfunction
+
+" Main prettier command
+function! prettier#Prettier(...) abort
+ let l:execCmd = prettier#resolver#executable#getPath()
+ 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
+ let l:cmd = l:execCmd . prettier#resolver#config#buildCliArgs(l:config)
+
+ " close quickfix if it is opened
+ call prettier#utils#quickfix#close()
+
+ " format buffer
+ call prettier#job#runner#run(l:cmd, l:startSelection, l:endSelection, l:async)
+ else
+ call prettier#logging#error#log('EXECUTABLE_NOT_FOUND_ERROR')
+ endif
+endfunction