diff options
| author | mitermayer <mitermayer.reis@gmail.com> | 2018-05-04 22:11:07 -0700 |
|---|---|---|
| committer | mitermayer <mitermayer.reis@gmail.com> | 2018-05-04 22:11:07 -0700 |
| commit | 2c16dd83051380a9d651621062b7775be4ad0e1e (patch) | |
| tree | ae60a1f1b7ef6a0a1047bafb8c736bf9fd6a03f9 | |
| parent | 26c9cb2d676c2bdc1f9c9e073fb9f15bb9e69cd4 (diff) | |
| download | vim-prettier-2c16dd83051380a9d651621062b7775be4ad0e1e.tar.xz | |
Adding support for neovim finalized
| -rw-r--r-- | autoload/prettier/job/async/neovim.vim | 79 | ||||
| -rw-r--r-- | autoload/prettier/job/async/vim.vim | 58 | ||||
| -rw-r--r-- | autoload/prettier/job/runner.vim | 18 |
3 files changed, 98 insertions, 57 deletions
diff --git a/autoload/prettier/job/async/neovim.vim b/autoload/prettier/job/async/neovim.vim index 8bef25f..c324f17 100644 --- a/autoload/prettier/job/async/neovim.vim +++ b/autoload/prettier/job/async/neovim.vim @@ -4,24 +4,19 @@ function! prettier#job#async#neovim#run(cmd, startSelection, endSelection) abort if s:prettier_job_running == 1 return endif - - let l:cmd = a:cmd - - if has('win32') || has('win64') - let l:cmd = 'cmd.exe /c ' . a:cmd - endif + let s:prettier_job_running = 1 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"), + \ 'content': l:lines, \} let l:out = [] let l:err = [] - let l:job = jobstart([&shell, &shellcmdflag, l:cmd], { + let l:job = jobstart([&shell, &shellcmdflag, a: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:onExit(status, l:dict, l:out, l:err)}, @@ -30,28 +25,72 @@ function! prettier#job#async#neovim#run(cmd, startSelection, endSelection) abort call jobclose(l:job, 'stdin') endfunction +" todo +" Lets refactor this onExit to work similar to our solution for vim8 +" at the moment an info json object is been passed with some cached data +" that is than used to do the formatting, its not following the same convetion +" as the vim8 one and is error prone +" +" we should: +" +" 1. make it similar to the vim8 approach +" 2. extract common functionality either above to the runner or to some other module +" +" to test this it rellies on using nvim and having the flag +" g:prettier#nvim_unstable_async=1 - enabled +" +" +" note that somehow we exectuing both async and sync on nvim when using the autoformat function! s:onExit(status, info, out, err) abort let s:prettier_job_running = 0 + let l:currentBufferNumber = bufnr('%') + let l:isInsideAnotherBuffer = a:info.buf_nr != l:currentBufferNumber ? 1 : 0 + let l:last = a:out[len(a:out) - 1] + let l:out = l:last ==? '' ? a:out[0:len(a:out) - 3] : a:out + " parsing errors if a:status != 0 - echoerr join(a:err, "\n") + call prettier#job#runner#onError(a:err) return endif + " we have no prettier output so lets exit 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 + " nothing to update + if (prettier#utils#buffer#willUpdatedLinesChangeBuffer(l:out, a:info.start, a:info.end) == 0) + let s:prettier_job_running = 0 return endif - " TODO - " move this to the buffer util and let it abstract away the saving buffer - " from here - " - " TODO - " we should be auto saving in order to work similar to vim8 - call nvim_buf_set_lines(a:info.buf_nr, a:info.start, a:info.end, 0, l:out) + " This is required due to race condition when user quickly switch buffers while the async + " cli has not finished running, vim 8.0.1039 has introduced setbufline() which can be used + " to fix this issue in a cleaner way, however since we still need to support older vim versions + " we will apply a more generic solution + if (l:isInsideAnotherBuffer) + " Do no try to format buffers that have been closed + if (bufloaded(a:info.buf_nr)) + try + silent exec 'sp '. escape(bufname(a:info.buf_nr), ' \') + call nvim_buf_set_lines(a:info.buf_nr, a:info.start, a:info.end, 0, l:out) + write + catch + call prettier#logging#error#log('PARSING_ERROR') + finally + " we should then hide this buffer again + if a:info.buf_nr == bufnr('%') + silent hide + endif + endtry + endif + else + " TODO + " move this to the buffer util and let it abstract away the saving buffer + " from here + " + " TODO + " we should be auto saving in order to work similar to vim8 + call nvim_buf_set_lines(a:info.buf_nr, a:info.start, a:info.end, 0, l:out) + write + endif endfunction diff --git a/autoload/prettier/job/async/vim.vim b/autoload/prettier/job/async/vim.vim index ef9e37d..d84d406 100644 --- a/autoload/prettier/job/async/vim.vim +++ b/autoload/prettier/job/async/vim.vim @@ -4,18 +4,11 @@ function! prettier#job#async#vim#run(cmd, startSelection, endSelection) abort if s:prettier_job_running == 1 return endif - let s:prettier_job_running = 1 - let l:cmd = a:cmd - - if has('win32') || has('win64') - let l:cmd = 'cmd.exe /c ' . a:cmd - endif - let l:bufferName = bufname('%') - call job_start([&shell, &shellcmdflag, l:cmd], { + call job_start([&shell, &shellcmdflag, a:cmd], { \ 'in_io': 'buffer', \ 'in_top': a:startSelection, \ 'in_bot': a:endSelection, @@ -30,6 +23,8 @@ function! s:onError(msg) abort endfunction function! s:onClose(channel, startSelection, endSelection, bufferName) abort + let s:prettier_job_running = 0 + let l:out = [] let l:currentBufferName = bufname('%') let l:isInsideAnotherBuffer = a:bufferName != l:currentBufferName ? 1 : 0 @@ -38,35 +33,34 @@ function! s:onClose(channel, startSelection, endSelection, bufferName) abort call add(l:out, ch_read(a:channel)) endwhile + " we have no prettier output so lets exit + if len(l:out) == 0 | return | endif + " nothing to update if (prettier#utils#buffer#willUpdatedLinesChangeBuffer(l:out, a:startSelection, a:endSelection) == 0) - let s:prettier_job_running = 0 return endif - if len(l:out) - " This is required due to race condition when user quickly switch buffers while the async - " cli has not finished running, vim 8.0.1039 has introduced setbufline() which can be used - " to fix this issue in a cleaner way, however since we still need to support older vim versions - " we will apply a more generic solution - if (l:isInsideAnotherBuffer) - if (bufloaded(str2nr(a:bufferName))) - try - silent exec 'sp '. escape(bufname(bufnr(a:bufferName)), ' \') - call prettier#utils#buffer#replaceAndSave(l:out, a:startSelection, a:endSelection) - catch - call prettier#logging#error#log('PARSING_ERROR', a:bufferName) - finally - " we should then hide this buffer again - if a:bufferName == bufname('%') - silent hide - endif - endtry - endif - else + " This is required due to race condition when user quickly switch buffers while the async + " cli has not finished running, vim 8.0.1039 has introduced setbufline() which can be used + " to fix this issue in a cleaner way, however since we still need to support older vim versions + " we will apply a more generic solution + if (l:isInsideAnotherBuffer) + " Do no try to format buffers that have been closed + if (bufloaded(str2nr(a:bufferName))) + try + silent exec 'sp '. escape(bufname(bufnr(a:bufferName)), ' \') call prettier#utils#buffer#replaceAndSave(l:out, a:startSelection, a:endSelection) - endif - - let s:prettier_job_running = 0 + catch + call prettier#logging#error#log('PARSING_ERROR', a:bufferName) + finally + " we should then hide this buffer again + if a:bufferName == bufname('%') + silent hide + endif + endtry + endif + else + call prettier#utils#buffer#replaceAndSave(l:out, a:startSelection, a:endSelection) endif endfunction diff --git a/autoload/prettier/job/runner.vim b/autoload/prettier/job/runner.vim index 67cdb3c..92a27b9 100644 --- a/autoload/prettier/job/runner.vim +++ b/autoload/prettier/job/runner.vim @@ -24,12 +24,20 @@ function! prettier#job#runner#onError(errors) abort endfunction 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 - call prettier#job#async#neovim#run(a:cmd, a:startSelection, a:endSelection) - else + if !s:isAsyncVim && !s:isNeoVim call s:format(a:cmd, a:startSelection, a:endSelection) + endif + + " required for Windows support on async operations + let l:cmd = a:cmd + if has('win32') || has('win64') + let l:cmd = 'cmd.exe /c ' . a:cmd + endif + + if s:isAsyncVim + call prettier#job#async#vim#run(l:cmd, a:startSelection, a:endSelection) + else + call prettier#job#async#neovim#run(l:cmd, a:startSelection, a:endSelection) endif endfunction |
