lib/neovim/ruby_provider.rb in neovim-0.8.1 vs lib/neovim/ruby_provider.rb in neovim-0.9.0.pre.1

- old
+ new

@@ -1,6 +1,7 @@ require "neovim/ruby_provider/vim" +require "neovim/ruby_provider/object_ext" require "neovim/ruby_provider/buffer_ext" require "neovim/ruby_provider/window_ext" require "stringio" module Neovim @@ -16,10 +17,11 @@ Neovim.plugin do |plug| plug.__send__(:script_host!) __define_setup(plug) __define_ruby_execute(plug) + __define_ruby_eval(plug) __define_ruby_execute_file(plug) __define_ruby_do_range(plug) __define_ruby_chdir(plug) end end @@ -42,23 +44,38 @@ # # This is used by the +:ruby+ command. def self.__define_ruby_execute(plug) plug.__send__(:rpc, :ruby_execute) do |nvim, ruby| __wrap_client(nvim) do - eval(ruby, TOPLEVEL_BINDING, "eval") + eval(ruby, TOPLEVEL_BINDING, "ruby_execute") end + nil end end private_class_method :__define_ruby_execute + # Evaluate the provided Ruby code, exposing the +Vim+ constant for + # interactions with the editor and returning the value. + # + # This is used by the +:rubyeval+ command. + def self.__define_ruby_eval(plug) + plug.__send__(:rpc, :ruby_eval) do |nvim, ruby| + __wrap_client(nvim) do + eval(ruby, TOPLEVEL_BINDING, "ruby_eval") + end + end + end + private_class_method :__define_ruby_eval + # Evaluate the provided Ruby file, exposing the +Vim+ constant for # interactions with the editor. # # This is used by the +:rubyfile+ command. def self.__define_ruby_execute_file(plug) plug.__send__(:rpc, :ruby_execute_file) do |nvim, path| __wrap_client(nvim) { load(path) } + nil end end private_class_method :__define_ruby_execute_file # Evaluate the provided Ruby code over each line of a range. The contents @@ -73,18 +90,19 @@ __plug.__send__(:rpc, :ruby_do_range) do |__nvim, *__args| __wrap_client(__nvim) do __start, __stop, __ruby = __args __buffer = __nvim.get_current_buf - __update_lines_in_chunks(__buffer, __start, __stop, 5000) do |__lines| + __update_lines_in_chunks(__buffer, __start, __stop, 1_000) do |__lines| __lines.map do |__line| $_ = __line - eval(__ruby, binding, "eval") + eval(__ruby, binding, "ruby_do_range") $_ end end end + nil end end private_class_method :__define_ruby_do_range def self.__define_ruby_chdir(plug) @@ -101,11 +119,10 @@ __with_exception_handling(client) do __with_std_streams(client) do yield end end - nil end private_class_method :__wrap_client def self.__with_exception_handling(client) yield @@ -120,13 +137,13 @@ old_stderr = $stderr.dup $stdout, $stderr = StringIO.new, StringIO.new begin - yield - - client.out_write($stdout.string + $/) if $stdout.size > 0 - client.err_writeln($stderr.string) if $stderr.size > 0 + yield.tap do + client.out_write($stdout.string + $/) if $stdout.size > 0 + client.err_writeln($stderr.string) if $stderr.size > 0 + end ensure $stdout = old_stdout $stderr = old_stderr end end