Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PrettierAsync segmentation fault #138

Merged
merged 2 commits into from
May 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions autoload/prettier.vim
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,13 @@ function! s:Prettier_Exec_Async(cmd, startSelection, endSelection) abort

if s:prettier_job_running != 1
let s:prettier_job_running = 1
call job_start([&shell, &shellcmdflag, l:async_cmd], {
\ 'in_io': 'buffer',
\ 'in_top': a:startSelection,
\ 'in_bot': a:endSelection,
\ 'in_name': l:bufferName,
let l:job = job_start([&shell, &shellcmdflag, l:async_cmd], {
\ 'err_cb': {channel, msg -> s:Prettier_Job_Error(msg)},
\ 'close_cb': {channel -> s:Prettier_Job_Close(channel, a:startSelection, a:endSelection, l:bufferName)}})
endif
let l:stdin = job_getchannel(l:job)
call ch_sendraw(l:stdin, join(getbufline(bufnr(l:bufferName), a:startSelection,a:endSelection), "\n"))
call ch_close_in(l:stdin)
endif
endfunction

function! s:Prettier_Job_Close(channel, startSelection, endSelection, bufferName) abort
Expand Down Expand Up @@ -278,11 +277,18 @@ function! s:Apply_Prettier_Format(lines, startSelection, endSelection) abort
endif

" delete all lines on the current buffer
silent! execute len(l:newBuffer) . ',' . line('$') . 'delete _'
silent! execute '%delete _'

" replace all lines from the current buffer with output from prettier
call setline(1, l:newBuffer)

let l:idx = 0
for l:line in l:newBuffer
silent! call append(l:idx, l:line)
let l:idx += 1
endfor

" delete trailing newline introduced by the above append procedure
silent! execute '$delete _'

" Restore view
call winrestview(l:winview)
endfunction
Expand Down