Skip to content

Commit 0c95862

Browse files
committed
Big refactor to have only Platform, Server and Client.
1 parent 812bcac commit 0c95862

16 files changed

+269
-412
lines changed

Diff for: bin/vimrunner

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ $: << File.expand_path('../../lib', __FILE__)
55
require 'irb'
66
require 'vimrunner'
77

8-
$vim = Vimrunner.start_gui_vim
8+
$vim = Vimrunner.start_gvim
99

1010
IRB.start

Diff for: lib/vimrunner.rb

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
require 'vimrunner/client'
2-
require 'vimrunner/server'
1+
require "vimrunner/server"
2+
require "vimrunner/platform"
33

44
module Vimrunner
5-
# Starts a new Server with a terminal Vim instance and returns a client,
6-
# connected to it.
7-
def self.start_vim
8-
Client.new(Server.start)
5+
def self.start(vim = Platform.vim, &blk)
6+
Server.new(vim).start(&blk)
97
end
108

11-
# Starts a new Server with a GUI Vim instance and returns a client, connected
12-
# to it.
13-
def self.start_gui_vim
14-
Client.new(Server.start(:gui => true))
9+
def self.start_gvim(&blk)
10+
Server.new(Platform.gvim).start(&blk)
1511
end
1612
end

Diff for: lib/vimrunner/client.rb

+56-52
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,74 @@
11
module Vimrunner
2-
3-
# A Client is simply a proxy to a Vim server. It's initialized with a Server
4-
# instance and sends commands, keys and signals to it.
52
class Client
6-
attr_reader :server, :driver
3+
attr_reader :server
74

85
def initialize(server)
96
@server = server
10-
@driver = server.driver
117
end
128

13-
# Adds a plugin to Vim's runtime. Initially, Vim is started without
14-
# sourcing any plugins to ensure a clean state. This method can be used to
15-
# populate the instance's environment.
9+
# Public: Adds a plugin to Vim's runtime. Initially, Vim is started
10+
# without sourcing any plugins to ensure a clean state. This method can be
11+
# used to populate the instance's environment.
1612
#
1713
# dir - The base directory of the plugin, the one that contains
1814
# its autoload, plugin, ftplugin, etc. directories.
19-
# entry_script - The Vim script that's runtime'd to initialize the plugin.
20-
# Optional.
15+
# entry_script - The Vim script that's runtime'd to initialize the plugin
16+
# (optional).
2117
#
2218
# Example:
2319
#
2420
# vim.add_plugin 'rails', 'plugin/rails.vim'
2521
#
22+
# Returns nothing.
2623
def add_plugin(dir, entry_script = nil)
2724
command("set runtimepath+=#{dir}")
2825
command("runtime #{entry_script}") if entry_script
2926
end
3027

31-
# Invokes one of the basic actions the Vim server supports, sending a key
32-
# sequence. The keys are sent as-is, so it'd probably be better to use the
33-
# wrapper methods, #normal, #insert and so on.
28+
def normal(keys = "")
29+
server.remote_send("<C-\\><C-n>#{keys}")
30+
end
31+
32+
# Public: Invokes one of the basic actions the Vim server supports,
33+
# sending a key sequence. The keys are sent as-is, so it'd probably be
34+
# better to use the wrapper methods, #normal, #insert and so on.
35+
#
36+
# Returns nothing.
3437
def type(keys)
35-
invoke_vim '--remote-send', keys
38+
server.remote_send(keys)
3639
end
3740

38-
# Executes the given command in the Vim instance and returns its output,
39-
# stripping all surrounding whitespace.
40-
def command(vim_command)
41+
# Public: Starts a search in Vim for the given text. The result is that
42+
# the cursor is positioned on its first occurrence.
43+
#
44+
# Returns nothing.
45+
def search(text)
4146
normal
47+
type "/#{text}<CR>"
48+
end
4249

43-
escaped_command = vim_command.to_s.gsub("'", "''")
44-
expression = "VimrunnerEvaluateCommandOutput('#{escaped_command}')"
50+
# Public: Switches Vim to insert mode and types in the given text.
51+
#
52+
# Returns nothing.
53+
def insert(text)
54+
normal "i#{text}"
55+
end
4556

46-
invoke_vim('--remote-expr', expression).tap do |output|
47-
raise InvalidCommandError if output =~ /^Vim:E\d+:/
48-
end
57+
# Public: Writes the file being edited to disk. Note that you probably
58+
# want to set the file's name first by using Runner#edit.
59+
def write
60+
command :write
4961
end
5062

51-
# Starts a search in Vim for the given text. The result is that the cursor
52-
# is positioned on its first occurrence.
53-
def search(text)
54-
normal
55-
type "/#{text}<cr>"
63+
# Public: Echo each expression with a space in between.
64+
#
65+
# Returns the String output.
66+
def echo(*expressions)
67+
command "echo #{expressions.join(' ')}"
5668
end
5769

58-
# Sets a setting in Vim. If +value+ is nil, the setting is considered to be
59-
# a boolean.
70+
# Public: Sets a setting in Vim. If +value+ is nil, the setting is
71+
# considered to be a boolean.
6072
#
6173
# Examples:
6274
#
@@ -71,34 +83,26 @@ def set(setting, value = nil)
7183
end
7284
end
7385

74-
# Edits the file +filename+ with Vim.
86+
# Public: Edits the file +filename+ with Vim.
7587
#
7688
# Note that this doesn't use the '--remote' Vim flag, it simply types in
77-
# the command manually. This is necessary to avoid the Vim instance getting
78-
# focus.
89+
# the command manually. This is necessary to avoid the Vim instance
90+
# getting focus.
7991
def edit(filename)
8092
command "edit #{filename}"
8193
end
8294

83-
# Writes the file being edited to disk. Note that you probably want to set
84-
# the file's name first by using Runner#edit.
85-
def write
86-
command :write
87-
end
88-
89-
# Echo each expression with a space in between.
90-
def echo(*expressions)
91-
command "echo #{expressions.join(' ')}"
92-
end
93-
94-
# Switches Vim to insert mode and types in the given text.
95-
def insert(text = '')
96-
normal "i#{text}"
97-
end
95+
# Public: Executes the given command in the Vim instance and returns its
96+
# output, stripping all surrounding whitespace.
97+
#
98+
# Returns the string output.
99+
# Raises InvalidCommandError if the command is not recognised by vim.
100+
def command(commands)
101+
expression = "VimrunnerEvaluateCommandOutput('#{escape(commands)}')"
98102

99-
# Switches Vim to normal mode and types in the given keys.
100-
def normal(keys = '')
101-
type "<c-\\><c-n>#{keys}"
103+
server.remote_expr(expression).tap do |output|
104+
raise InvalidCommandError if output =~ /^Vim:E\d+:/
105+
end
102106
end
103107

104108
# Kills the server it's connected to.
@@ -108,8 +112,8 @@ def kill
108112

109113
private
110114

111-
def invoke_vim(*args)
112-
driver.run('--servername', server.name, *args)
115+
def escape(string)
116+
string.to_s.gsub("'", "''")
113117
end
114118
end
115119
end

Diff for: lib/vimrunner/driver/abstract.rb

-48
This file was deleted.

Diff for: lib/vimrunner/driver/gui.rb

-12
This file was deleted.

Diff for: lib/vimrunner/driver/headless.rb

-16
This file was deleted.

Diff for: lib/vimrunner/errors.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module Vimrunner
2+
class NoSuitableVimError < RuntimeError; end
23
class InvalidCommandError < RuntimeError; end
34

45
class TimeoutError < RuntimeError

Diff for: lib/vimrunner/platform.rb

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require "vimrunner/errors"
2+
3+
require "rbconfig"
4+
5+
module Vimrunner
6+
module Platform
7+
extend self
8+
9+
def vim
10+
vims.find { |vim| suitable?(vim) } or raise NoSuitableVimError
11+
end
12+
13+
def gvim
14+
gvims.find { |gvim| suitable?(gvim) } or raise NoSuitableVimError
15+
end
16+
17+
private
18+
19+
def gvims
20+
if mac?
21+
%w( mvim gvim )
22+
else
23+
%w( gvim )
24+
end
25+
end
26+
27+
def vims
28+
%w( vim ) + gvims
29+
end
30+
31+
def suitable?(vim)
32+
features = features(vim)
33+
34+
if gui?(vim)
35+
features.include?("+clientserver")
36+
else
37+
features.include?("+clientserver") && features.include?("+xterm_clipboard")
38+
end
39+
end
40+
41+
def gui?(vim)
42+
executable = File.basename(vim)
43+
44+
executable[0, 1] == "g" || executable[0, 1] == "m"
45+
end
46+
47+
def features(vim)
48+
IO.popen([vim, "--version"]) { |io| io.read.strip }
49+
rescue Errno::ENOENT
50+
""
51+
end
52+
53+
def mac?
54+
RbConfig::CONFIG["host_os"] =~ /darwin/
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)