Skip to content

Commit a0950b8

Browse files
authored
Merge pull request #69 from neovim/ruby_eval
Add `rubyeval` support
2 parents 85b1597 + 1ed10f5 commit a0950b8

File tree

6 files changed

+74
-10
lines changed

6 files changed

+74
-10
lines changed

.rubocop.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ Metrics/MethodLength:
6565
Max: 50
6666

6767
Metrics/ModuleLength:
68-
Exclude:
69-
- spec/**/*
68+
Enabled: false
7069

7170
Metrics/ParameterLists:
7271
Max: 6
@@ -93,6 +92,11 @@ Style/ConditionalAssignment:
9392
EnforcedStyle: assign_inside_condition
9493
IncludeTernaryExpressions: false
9594

95+
Style/Documentation:
96+
Exclude:
97+
- spec/**/*
98+
- lib/neovim/ruby_provider/object_ext.rb
99+
96100
Style/DoubleNegation:
97101
Enabled: false
98102

Rakefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ namespace :spec do
1212

1313
desc "Run acceptance specs"
1414
task acceptance: "acceptance:deps" do
15-
run_script("run_acceptance.rb", "--reporter", "dot", "spec/acceptance")
15+
run_script(
16+
"run_acceptance.rb",
17+
"--reporter", "dot",
18+
# TODO: remove once rubyeval is merged in nvim
19+
"--exclude", "rubyeval",
20+
"spec/acceptance"
21+
)
1622
end
1723

1824
namespace :acceptance do

lib/neovim/ruby_provider.rb

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "neovim/ruby_provider/vim"
2+
require "neovim/ruby_provider/object_ext"
23
require "neovim/ruby_provider/buffer_ext"
34
require "neovim/ruby_provider/window_ext"
45
require "stringio"
@@ -18,6 +19,7 @@ def self.__define_plugin!
1819

1920
__define_setup(plug)
2021
__define_ruby_execute(plug)
22+
__define_ruby_eval(plug)
2123
__define_ruby_execute_file(plug)
2224
__define_ruby_do_range(plug)
2325
__define_ruby_chdir(plug)
@@ -44,19 +46,34 @@ def self.__define_setup(plug)
4446
def self.__define_ruby_execute(plug)
4547
plug.__send__(:rpc, :ruby_execute) do |nvim, ruby|
4648
__wrap_client(nvim) do
47-
eval(ruby, TOPLEVEL_BINDING, "eval")
49+
eval(ruby, TOPLEVEL_BINDING, "ruby_execute")
4850
end
51+
nil
4952
end
5053
end
5154
private_class_method :__define_ruby_execute
5255

56+
# Evaluate the provided Ruby code, exposing the +Vim+ constant for
57+
# interactions with the editor and returning the value.
58+
#
59+
# This is used by the +:rubyeval+ command.
60+
def self.__define_ruby_eval(plug)
61+
plug.__send__(:rpc, :ruby_eval) do |nvim, ruby|
62+
__wrap_client(nvim) do
63+
eval(ruby, TOPLEVEL_BINDING, "ruby_eval")
64+
end
65+
end
66+
end
67+
private_class_method :__define_ruby_eval
68+
5369
# Evaluate the provided Ruby file, exposing the +Vim+ constant for
5470
# interactions with the editor.
5571
#
5672
# This is used by the +:rubyfile+ command.
5773
def self.__define_ruby_execute_file(plug)
5874
plug.__send__(:rpc, :ruby_execute_file) do |nvim, path|
5975
__wrap_client(nvim) { load(path) }
76+
nil
6077
end
6178
end
6279
private_class_method :__define_ruby_execute_file
@@ -78,11 +95,12 @@ def self.__define_ruby_do_range(__plug)
7895
__update_lines_in_chunks(__buffer, __start, __stop, 1_000) do |__lines|
7996
__lines.map do |__line|
8097
$_ = __line
81-
eval(__ruby, binding, "eval")
98+
eval(__ruby, binding, "ruby_do_range")
8299
$_
83100
end
84101
end
85102
end
103+
nil
86104
end
87105
end
88106
private_class_method :__define_ruby_do_range
@@ -103,7 +121,6 @@ def self.__wrap_client(client)
103121
yield
104122
end
105123
end
106-
nil
107124
end
108125
private_class_method :__wrap_client
109126

@@ -122,10 +139,10 @@ def self.__with_std_streams(client)
122139
$stdout, $stderr = StringIO.new, StringIO.new
123140

124141
begin
125-
yield
126-
127-
client.out_write($stdout.string + $/) if $stdout.size > 0
128-
client.err_writeln($stderr.string) if $stderr.size > 0
142+
yield.tap do
143+
client.out_write($stdout.string + $/) if $stdout.size > 0
144+
client.err_writeln($stderr.string) if $stderr.size > 0
145+
end
129146
ensure
130147
$stdout = old_stdout
131148
$stderr = old_stderr
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Object
2+
def to_msgpack(packer)
3+
packer.pack(to_s)
4+
end
5+
end

spec/acceptance/rubyeval_spec.vim

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
let s:suite = themis#suite(":rubyeval")
2+
let s:expect = themis#helper("expect")
3+
4+
function! s:suite.evaluates_ruby_objects() abort
5+
call s:expect(rubyeval("123")).to_equal(123)
6+
call s:expect(rubyeval("1.2")).to_equal(1.2)
7+
call s:expect(rubyeval("'test'")).to_equal("test")
8+
call s:expect(rubyeval("nil")).to_equal(v:null)
9+
call s:expect(rubyeval("true")).to_equal(v:true)
10+
call s:expect(rubyeval("false")).to_equal(v:false)
11+
call s:expect(rubyeval("{x: 1}")).to_equal({"x": 1})
12+
call s:expect(rubyeval(":test")).to_equal("test")
13+
call s:expect(rubyeval(":test.class")).to_equal("Symbol")
14+
endfunction
15+
16+
function! s:suite.propagates_exceptions() abort
17+
try
18+
rubyeval("raise 'BOOM'")
19+
throw "Nothing raised"
20+
catch /BOOM/
21+
endtry
22+
endfunction
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require "helper"
2+
require "neovim/ruby_provider/object_ext"
3+
4+
RSpec.describe Object do
5+
describe "#to_msgpack" do
6+
it "converts classes to strings" do
7+
expect(MessagePack.pack(String)).to eq(MessagePack.pack("String"))
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)