Add :ruby, :rubyfile, and :rubydo ex commands

This commit is contained in:
Alex Genco
2016-07-02 21:45:48 -07:00
parent f80eb768c7
commit 0a3c0205c5
6 changed files with 170 additions and 14 deletions

View File

@@ -1,12 +1,18 @@
" The Ruby provider helper " The Ruby provider helper
if exists('s:loaded_ruby_provider') if exists('g:loaded_ruby_provider')
finish finish
endif 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 function! provider#ruby#Require(host) abort
" Collect registered Ruby plugins into args
let args = [] let args = []
let ruby_plugins = remote#host#PluginsForHost(a:host.name) let ruby_plugins = remote#host#PluginsForHost(a:host.name)
@@ -16,19 +22,44 @@ function! provider#ruby#Require(host) abort
try try
let channel_id = rpcstart(provider#ruby#Prog(), args) let channel_id = rpcstart(provider#ruby#Prog(), args)
if rpcrequest(channel_id, 'poll') ==# 'ok'
if rpcrequest(channel_id, 'poll') == 'ok'
return channel_id return channel_id
endif endif
catch catch
echomsg v:throwpoint echomsg v:throwpoint
echomsg v:exception echomsg v:exception
endtry 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 endfunction
function! provider#ruby#Prog() abort function! provider#ruby#Call(method, args)
return 'neovim-ruby-host' 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 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, [])

View File

@@ -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

View File

@@ -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")) { if (!strcmp(name, "clipboard")) {
check_provider(clipboard); check_provider(clipboard);
@@ -22349,6 +22352,9 @@ bool eval_has_provider(char *name)
} else if (!strcmp(name, "python")) { } else if (!strcmp(name, "python")) {
check_provider(python); check_provider(python);
return has_python; return has_python;
} else if (!strcmp(name, "ruby")) {
check_provider(ruby);
return has_ruby;
} }
return false; return false;

View File

@@ -2158,19 +2158,19 @@ return {
command='ruby', command='ruby',
flags=bit.bor(RANGE, EXTRA, NEEDARG, CMDWIN), flags=bit.bor(RANGE, EXTRA, NEEDARG, CMDWIN),
addr_type=ADDR_LINES, addr_type=ADDR_LINES,
func='ex_script_ni', func='ex_ruby',
}, },
{ {
command='rubydo', command='rubydo',
flags=bit.bor(RANGE, DFLALL, EXTRA, NEEDARG, CMDWIN), flags=bit.bor(RANGE, DFLALL, EXTRA, NEEDARG, CMDWIN),
addr_type=ADDR_LINES, addr_type=ADDR_LINES,
func='ex_ni', func='ex_rubydo',
}, },
{ {
command='rubyfile', command='rubyfile',
flags=bit.bor(RANGE, FILE1, NEEDARG, CMDWIN), flags=bit.bor(RANGE, FILE1, NEEDARG, CMDWIN),
addr_type=ADDR_LINES, addr_type=ADDR_LINES,
func='ex_ni', func='ex_rubyfile',
}, },
{ {
command='rviminfo', command='rviminfo',

View File

@@ -897,6 +897,21 @@ void ex_pydo(exarg_T *eap)
script_host_do_range("python", 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) void ex_python3(exarg_T *eap)
{ {
script_host_execute("python3", eap); script_host_execute("python3", eap);

View File

@@ -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)