diff options
-rw-r--r-- | runtime/autoload/provider/ruby.vim | 51 | ||||
-rw-r--r-- | runtime/autoload/provider/script_host.rb | 8 | ||||
-rw-r--r-- | src/nvim/eval.c | 8 | ||||
-rw-r--r-- | src/nvim/ex_cmds.lua | 6 | ||||
-rw-r--r-- | src/nvim/ex_cmds2.c | 15 | ||||
-rw-r--r-- | test/functional/provider/ruby_spec.lua | 96 |
6 files changed, 170 insertions, 14 deletions
diff --git a/runtime/autoload/provider/ruby.vim b/runtime/autoload/provider/ruby.vim index aad8c09d28..c57bb94e7a 100644 --- a/runtime/autoload/provider/ruby.vim +++ b/runtime/autoload/provider/ruby.vim @@ -1,12 +1,18 @@ " The Ruby provider helper -if exists('s:loaded_ruby_provider') +if exists('g:loaded_ruby_provider') finish endif +let g:loaded_ruby_provider = 1 -let s:loaded_ruby_provider = 1 +function! provider#ruby#Detect() abort + return exepath('neovim-ruby-host') +endfunction + +function! provider#ruby#Prog() + return s:prog +endfunction function! provider#ruby#Require(host) abort - " Collect registered Ruby plugins into args let args = [] let ruby_plugins = remote#host#PluginsForHost(a:host.name) @@ -16,19 +22,44 @@ function! provider#ruby#Require(host) abort try let channel_id = rpcstart(provider#ruby#Prog(), args) - - if rpcrequest(channel_id, 'poll') == 'ok' + if rpcrequest(channel_id, 'poll') ==# 'ok' return channel_id endif catch echomsg v:throwpoint echomsg v:exception endtry - - throw remote#host#LoadErrorForHost(a:host.orig_name, - \ '$NVIM_RUBY_LOG_FILE') + throw remote#host#LoadErrorForHost(a:host.orig_name, '$NVIM_RUBY_LOG_FILE') endfunction -function! provider#ruby#Prog() abort - return 'neovim-ruby-host' +function! provider#ruby#Call(method, args) + if s:err != '' + echoerr s:err + return + endif + + if !exists('s:host') + try + let s:host = remote#host#Require('legacy-ruby-provider') + catch + let s:err = v:exception + echohl WarningMsg + echomsg v:exception + echohl None + return + endtry + endif + return call('rpcrequest', insert(insert(a:args, 'ruby_'.a:method), s:host)) endfunction + +let s:err = '' +let s:prog = provider#ruby#Detect() +let s:plugin_path = expand('<sfile>:p:h') . '/script_host.rb' + +if empty(s:prog) + let s:err = 'Couldn''t find the neovim RubyGem. ' . + \ 'Install it with `gem install neovim`.' +endif + +call remote#host#RegisterClone('legacy-ruby-provider', 'ruby') +call remote#host#RegisterPlugin('legacy-ruby-provider', s:plugin_path, []) diff --git a/runtime/autoload/provider/script_host.rb b/runtime/autoload/provider/script_host.rb new file mode 100644 index 0000000000..1dade766c7 --- /dev/null +++ b/runtime/autoload/provider/script_host.rb @@ -0,0 +1,8 @@ +begin + require "neovim/ruby_provider" +rescue LoadError + warn( + "Your neovim RubyGem is missing or out of date. " + + "Install the latest version using `gem install neovim`." + ) +end diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 9a86c5765c..e6c4e36e77 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -22338,7 +22338,10 @@ bool eval_has_provider(char *name) } \ } - static int has_clipboard = -1, has_python = -1, has_python3 = -1; + static int has_clipboard = -1; + static int has_python = -1; + static int has_python3 = -1; + static int has_ruby = -1; if (!strcmp(name, "clipboard")) { check_provider(clipboard); @@ -22349,6 +22352,9 @@ bool eval_has_provider(char *name) } else if (!strcmp(name, "python")) { check_provider(python); return has_python; + } else if (!strcmp(name, "ruby")) { + check_provider(ruby); + return has_ruby; } return false; diff --git a/src/nvim/ex_cmds.lua b/src/nvim/ex_cmds.lua index 6c58879d58..986f33f51d 100644 --- a/src/nvim/ex_cmds.lua +++ b/src/nvim/ex_cmds.lua @@ -2158,19 +2158,19 @@ return { command='ruby', flags=bit.bor(RANGE, EXTRA, NEEDARG, CMDWIN), addr_type=ADDR_LINES, - func='ex_script_ni', + func='ex_ruby', }, { command='rubydo', flags=bit.bor(RANGE, DFLALL, EXTRA, NEEDARG, CMDWIN), addr_type=ADDR_LINES, - func='ex_ni', + func='ex_rubydo', }, { command='rubyfile', flags=bit.bor(RANGE, FILE1, NEEDARG, CMDWIN), addr_type=ADDR_LINES, - func='ex_ni', + func='ex_rubyfile', }, { command='rviminfo', diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index 40074f726c..57b3e380e8 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -897,6 +897,21 @@ void ex_pydo(exarg_T *eap) script_host_do_range("python", eap); } +void ex_ruby(exarg_T *eap) +{ + script_host_execute("ruby", eap); +} + +void ex_rubyfile(exarg_T *eap) +{ + script_host_execute_file("ruby", eap); +} + +void ex_rubydo(exarg_T *eap) +{ + script_host_do_range("ruby", eap); +} + void ex_python3(exarg_T *eap) { script_host_execute("python3", eap); diff --git a/test/functional/provider/ruby_spec.lua b/test/functional/provider/ruby_spec.lua new file mode 100644 index 0000000000..758b294d52 --- /dev/null +++ b/test/functional/provider/ruby_spec.lua @@ -0,0 +1,96 @@ +local helpers = require('test.functional.helpers')(after_each) + +local eq = helpers.eq +local feed = helpers.feed +local clear = helpers.clear +local funcs = helpers.funcs +local meths = helpers.meths +local insert = helpers.insert +local expect = helpers.expect +local command = helpers.command +local write_file = helpers.write_file +local curbufmeths = helpers.curbufmeths + +do + clear() + command('let g:prog = provider#ruby#Detect()') + local prog = meths.get_var('prog') + + if prog == '' then + pending( + "Couldn't find the neovim RubyGem. Install it with `gem install neovim`.", + function() end) + return + end +end + +before_each(function() + clear() +end) + +describe('ruby feature test', function() + it('works', function() + eq(1, funcs.has('ruby')) + end) +end) + +describe(':ruby command', function() + it('evaluates ruby', function() + command('ruby VIM.command("let g:set_by_ruby = [100, 0]")') + eq({100, 0}, meths.get_var('set_by_ruby')) + end) + + it('supports nesting', function() + command([[ruby VIM.command('ruby VIM.command("let set_by_nested_ruby = 555")')]]) + eq(555, meths.get_var('set_by_nested_ruby')) + end) +end) + +describe(':rubyfile command', function() + it('evaluates a ruby file', function() + local fname = 'rubyfile.rb' + write_file(fname, 'VIM.command("let set_by_rubyfile = 123")') + command('rubyfile rubyfile.rb') + eq(123, meths.get_var('set_by_rubyfile')) + os.remove(fname) + end) +end) + +describe(':rubydo command', function() + it('exposes the $_ variable for modifying lines', function() + insert('abc\ndef\nghi\njkl') + expect([[ + abc + def + ghi + jkl]]) + + feed('ggjvj:rubydo $_.upcase!<CR>') + expect([[ + abc + DEF + GHI + jkl]]) + end) + + it('operates on all lines when not given a range', function() + insert('abc\ndef\nghi\njkl') + expect([[ + abc + def + ghi + jkl]]) + + feed(':rubydo $_.upcase!<CR>') + expect([[ + ABC + DEF + GHI + JKL]]) + end) + + it('does not modify the buffer if no changes are made', function() + command('normal :rubydo 42') + eq(false, curbufmeths.get_option('modified')) + end) +end) |