diff options
| author | mitermayer <mitermayer.reis@gmail.com> | 2018-05-04 13:27:03 -0700 |
|---|---|---|
| committer | mitermayer <mitermayer.reis@gmail.com> | 2018-05-04 13:27:03 -0700 |
| commit | 866a7ac514ff70ce40476a792db2d979f6b20268 (patch) | |
| tree | 76aa4093d388eaab5b34b51c9ffe629f12af0cba /autoload | |
| parent | 4dfc50d5a689cf27d5915cbff8520388e517f0e5 (diff) | |
| download | vim-prettier-866a7ac514ff70ce40476a792db2d979f6b20268.tar.xz | |
Adding neovim runner module
Diffstat (limited to 'autoload')
| -rw-r--r-- | autoload/prettier.vim | 89 | ||||
| -rw-r--r-- | autoload/prettier/job/async/neovim.vim | 43 | ||||
| -rw-r--r-- | autoload/prettier/job/runner.vim | 2 |
3 files changed, 65 insertions, 69 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 diff --git a/autoload/prettier/job/async/neovim.vim b/autoload/prettier/job/async/neovim.vim index e69de29..9844579 100644 --- a/autoload/prettier/job/async/neovim.vim +++ b/autoload/prettier/job/async/neovim.vim @@ -0,0 +1,43 @@ +function! prettier#job#async#neovim#run(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 diff --git a/autoload/prettier/job/runner.vim b/autoload/prettier/job/runner.vim index b4b1f77..67cdb3c 100644 --- a/autoload/prettier/job/runner.vim +++ b/autoload/prettier/job/runner.vim @@ -27,7 +27,7 @@ function! s:asyncFormat(cmd, startSelection, endSelection) abort if s:isAsyncVim call prettier#job#async#vim#run(a:cmd, a:startSelection, a:endSelection) elseif s:isNeoVim - echom 'neovim' + call prettier#job#async#neovim#run(a:cmd, a:startSelection, a:endSelection) else call s:format(a:cmd, a:startSelection, a:endSelection) endif |
