-
Notifications
You must be signed in to change notification settings - Fork 29
/
ruby.vim
176 lines (149 loc) · 4.6 KB
/
ruby.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
if exists("g:loaded_vimux_ruby_test") || &cp
finish
endif
let g:loaded_vimux_ruby_test = 1
if !has("ruby")
finish
end
if !exists("g:vimux_ruby_cmd_unit_test")
let g:vimux_ruby_cmd_unit_test = "ruby"
endif
if !exists("g:vimux_ruby_cmd_all_tests")
let g:vimux_ruby_cmd_all_tests = "ruby"
endif
if !exists("g:vimux_ruby_cmd_context")
let g:vimux_ruby_cmd_context = "ruby"
endif
if !exists("g:vimux_ruby_clear_console_on_run")
let g:vimux_ruby_clear_console_on_run = 1
endif
if !exists("g:vimux_ruby_file_relative_paths")
let g:vimux_ruby_file_relative_paths = 0
endif
command RunAllRubyTests :call s:RunAllRubyTests()
command RunAllRailsTests :call s:RunAllRailsTests()
command RunRubyFocusedTest :call s:RunRubyFocusedTest()
command RunRailsFocusedTest :call s:RunRailsFocusedTest()
command RunRubyFocusedContext :call s:RunRubyFocusedContext()
function s:RunAllRubyTests()
ruby RubyTest.create_from_settings.run_all
endfunction
function s:RunAllRailsTests()
ruby RubyTest.create_from_settings.run_all(true)
endfunction
function s:RunRubyFocusedTest()
ruby RubyTest.create_from_settings.run_test
endfunction
function s:RunRailsFocusedTest()
ruby RubyTest.create_from_settings.run_test(true)
endfunction
function s:RunRubyFocusedContext()
ruby RubyTest.create_from_settings.run_context
endfunction
ruby << EOF
module VIM
class Buffer
def method_missing(method, *args, &block)
VIM.command "#{method} #{self.name}"
end
end
end
class RubyTest
attr_reader :ruby_command
attr_reader :use_relative_path
def self.create_from_settings
self.new(Vim.evaluate('g:vimux_ruby_cmd_all_tests'), Vim.evaluate('g:vimux_ruby_file_relative_paths'))
end
def initialize(ruby_command, use_relative_path)
@ruby_command = ruby_command
@use_relative_path = use_relative_path
end
def current_file
use_relative_path == 0 ? VIM::Buffer.current.name : Vim.evaluate('expand("%")')
end
def rails_test_dir
current_file.split('/')[0..-current_file.split('/').reverse.index('test')-1].join('/')
end
def spec_file?
current_file =~ /spec_|_spec/
end
def line_number
VIM::Buffer.current.line_number
end
def run_spec
send_to_vimux("#{spec_command} #{current_file}:#{line_number}")
end
def run_unit_test(rails=false)
method_name = nil
(line_number + 1).downto(1) do |line_number|
if VIM::Buffer.current[line_number] =~ /def (test_\w+)/
method_name = $1
break
elsif VIM::Buffer.current[line_number] =~ /test "([^"]+)"/ ||
VIM::Buffer.current[line_number] =~ /test '([^']+)'/
method_name = "test_" + $1.split(" ").join("_")
break
elsif VIM::Buffer.current[line_number] =~ /should "([^"]+)"/ ||
VIM::Buffer.current[line_number] =~ /should '([^']+)'/
method_name = "\"/#{Regexp.escape($1)}/\""
break
end
end
send_to_vimux("#{ruby_command} #{"-I #{rails_test_dir} " if rails}#{current_file} -n #{method_name}") if method_name
end
def run_test(rails=false)
if spec_file?
run_spec
else
run_unit_test(rails)
end
end
def run_context
method_name = nil
context_line_number = nil
(line_number + 1).downto(1) do |line_number|
if VIM::Buffer.current[line_number] =~ /(context|describe) "([^"]+)"/ ||
VIM::Buffer.current[line_number] =~ /(context|describe) '([^']+)'/
method_name = $2
context_line_number = line_number
break
end
end
if method_name
if spec_file?
send_to_vimux("#{spec_command} #{current_file}:#{context_line_number}")
else
method_name = Regexp.escape(method_name)
send_to_vimux("#{ruby_command} #{current_file} -n /'#{method_name}'/")
end
end
end
def run_all(rails=false)
if spec_file?
send_to_vimux("#{spec_command} '#{current_file}'")
else
send_to_vimux("#{ruby_command} #{"-I #{rails_test_dir} " if rails}#{current_file}")
end
end
def spec_command
if File.exists?('./.zeus.sock')
'zeus rspec'
elsif File.exists?('./bin/rspec')
'./bin/rspec'
elsif File.exists?("Gemfile") && (match = `bundle show rspec-core`.scan(/(\d+\.\d+\.\d+)/) || match = `bundle show rspec`.scan(/(\d+\.\d+\.\d+)/i))
match.flatten.last.to_f < 2 ? "bundle exec spec" : "bundle exec rspec"
else
system("rspec -v > /dev/null 2>&1") ? "rspec --no-color" : "spec"
end
end
def send_to_vimux(test_command)
cmd = if VIM::evaluate("g:vimux_ruby_clear_console_on_run") != 0
"clear && "
else
''
end
cmd += test_command
Vim.command("call VimuxRunCommand(\"#{cmd}\")")
end
end
EOF