Skip to content

Tentative Vim support for virtualtext #12

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
98 changes: 81 additions & 17 deletions plugin/crates.vim
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,31 @@ function! s:job_callback_nvim_stdout(_job_id, data, _event) dict abort
call extend(self.stdoutbuf, a:data[1:])
endfunction

function! s:job_callback_vim_stdout(channel, message) dict abort
let self.stdoutbuf[0] .= a:message
endfunction

function! s:job_callback_nvim_exit(_job_id, exitval, _event) dict abort
call self.callback(a:exitval)
endfunction

function! s:job_callback_vim_exit(_channel, exitval) dict abort
let self.exitval = a:exitval
endfunction

function! s:job_callback_vim_close(_channel) dict abort
call self.callback(self.exitval)
endfunction

function! s:callback_show_latest_version(exitval) dict abort
if a:exitval
echomsg "D'oh! Got ". a:exitval
return
endif
if self.stdoutbuf[0] == ''
echomsg "D'oh! Got no output for crate " . self.crate
return
endif
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically unnecessary, but I found it useful while debugging some issues -- and it might be a more useful error than "couldn't parse json"

let data = json_decode(self.stdoutbuf[0])
if !has_key(data, 'versions')
if self.verbose
Expand All @@ -65,8 +81,26 @@ endfunction

function! s:virttext_add_version(lnum, vers_current, vers_latest)
if s:show(a:vers_current, a:vers_latest)
call nvim_buf_set_virtual_text(bufnr(''), nvim_create_namespace('crates'),
\ a:lnum, [[' '. a:vers_latest .' ', 'Crates']], {})
let text = ' '. a:vers_latest .' '

if has('nvim')
call nvim_buf_set_virtual_text(bufnr(''), nvim_create_namespace('crates'),
\ a:lnum, [[text, 'Crates']], {})
else
if empty(prop_type_get('crates', {'bufnr': bufnr('%')}))
call prop_type_add('crates', {
\ 'bufnr': bufnr('%'),
\ 'highlight': 'Crates',
\ 'combine': v:true
\ })
endif

call prop_add(a:lnum + 1, 0, {
\ 'type': 'crates',
\ 'text': text,
\ 'text_padding_left': 1,
\ })
endif
endif
endfunction

Expand All @@ -89,16 +123,38 @@ function! s:make_request_sync(crate)
endfunction

function! s:make_request_async(cmd, crate, vers, lnum, callback) abort
call jobstart(a:cmd, {
\ 'crate': a:crate,
\ 'vers': a:vers,
\ 'lnum': a:lnum,
\ 'callback': a:callback,
\ 'verbose': &verbose,
\ 'stdoutbuf': [''],
\ 'on_stdout': function('s:job_callback_nvim_stdout'),
\ 'on_exit': function('s:job_callback_nvim_exit'),
\ })
if has('nvim')
call jobstart(a:cmd, {
\ 'crate': a:crate,
\ 'vers': a:vers,
\ 'lnum': a:lnum,
\ 'callback': a:callback,
\ 'verbose': &verbose,
\ 'stdoutbuf': [''],
\ 'on_stdout': function('s:job_callback_nvim_stdout'),
\ 'on_exit': function('s:job_callback_nvim_exit'),
\ })
else
let runner = {
\ 'crate': a:crate,
\ 'vers': a:vers,
\ 'lnum': a:lnum,
\ 'callback': a:callback,
\ 'verbose': &verbose,
\ 'stdoutbuf': [''],
\ 'exitval': 0,
\ 'out_cb': function('s:job_callback_vim_stdout'),
\ 'exit_cb': function('s:job_callback_vim_exit'),
\ 'close_cb': function('s:job_callback_vim_close'),
\ }

call job_start(a:cmd, {
\ 'out_cb': runner.out_cb,
\ 'exit_cb': runner.exit_cb,
\ 'close_cb': runner.close_cb,
\ 'noblock': 1,
\ })
endif
endfunction

" Show latest version if it's outside of the given requirement.
Expand Down Expand Up @@ -169,8 +225,8 @@ function! g:CratesComplete(findstart, base)
endfunction

function! s:crates() abort
if !has('nvim')
echomsg 'Sorry, this is a Nvim-only feature.'
if !has('nvim') && !has('patch-9.0.0069')
echomsg 'Sorry, you need Nvim or Vim > 9.0069'
return
endif
call s:virttext_clear('crates')
Expand Down Expand Up @@ -211,7 +267,11 @@ function! s:crates() abort
endfunction

function! s:virttext_clear(ns) abort
call nvim_buf_clear_namespace(bufnr(''), nvim_create_namespace(a:ns), 0, -1)
if has('nvim')
call nvim_buf_clear_namespace(bufnr(''), nvim_create_namespace(a:ns), 0, -1)
else
call prop_clear(1, line('$'), { 'type': a:ns })
endif
endfunction

function! crates#toggle() abort
Expand Down Expand Up @@ -254,8 +314,12 @@ function! crates#up() abort
let line = substitute(line, '"\zs[0-9\.\*]\+\ze"', vers_latest, '')
endif
call setline(lnum, line)
call nvim_buf_clear_namespace(bufnr(''), nvim_create_namespace('crates'),
\ line('.')-1, line('.'))
if has('nvim')
call nvim_buf_clear_namespace(bufnr(''), nvim_create_namespace('crates'),
\ line('.')-1, line('.'))
else
call prop_clear(line('.'), line('.'), { 'type': 'crates' })
endif
endfunction

function! s:setup() abort
Expand Down