lib/textbringer/modes/ruby_mode.rb in textbringer-0.1.3 vs lib/textbringer/modes/ruby_mode.rb in textbringer-0.1.4
- old
+ new
@@ -6,11 +6,12 @@
CONFIG[:ruby_indent_level] = 2
CONFIG[:ruby_indent_tabs_mode] = false
class RubyMode < ProgrammingMode
self.file_name_pattern = /\A(?:.*\.(?:rb|ru|rake|thor)|
- (?:Gem|Rake|Cap|Thor|Vagrant|Guard|Pod)file)\z/x
+ (?:Gem|Rake|Cap|Thor|Vagrant|Guard|Pod)file)\z/ix
+ self.interpreter_name_pattern = /ruby/i
def initialize(buffer)
super(buffer)
@buffer[:indent_tabs_mode] = CONFIG[:ruby_indent_tabs_mode]
end
@@ -47,15 +48,15 @@
def forward_definition(n = number_prefix_arg || 1)
tokens = Ripper.lex(@buffer.to_s)
@buffer.forward_line
n.times do |i|
- tokens = tokens.drop_while { |(l, c), e, t|
+ tokens = tokens.drop_while { |(l, _), e, t|
l < @buffer.current_line ||
e != :on_kw || /\A(?:class|module|def)\z/ !~ t
}
- (line, column), event, text = tokens.first
+ (line,), = tokens.first
if line.nil?
@buffer.end_of_buffer
break
end
@buffer.goto_line(line)
@@ -68,15 +69,15 @@
def backward_definition(n = number_prefix_arg || 1)
tokens = Ripper.lex(@buffer.to_s).reverse
@buffer.beginning_of_line
n.times do |i|
- tokens = tokens.drop_while { |(l, c), e, t|
+ tokens = tokens.drop_while { |(l, _), e, t|
l >= @buffer.current_line ||
e != :on_kw || /\A(?:class|module|def)\z/ !~ t
}
- (line, column), event, text = tokens.first
+ (line,), = tokens.first
if line.nil?
@buffer.beginning_of_buffer
break
end
@buffer.goto_line(line)
@@ -91,10 +92,14 @@
cmd = read_from_minibuffer("Compile: ", default: default_compile_command)
shell_execute(cmd, "*Ruby compile result*")
backtrace_mode
end
+ def symbol_pattern
+ /[\p{Letter}\p{Number}_$@!?]/
+ end
+
private
def calculate_indentation
if @buffer.current_line == 1
return 0
@@ -102,11 +107,11 @@
@buffer.save_excursion do
@buffer.beginning_of_line
bol_pos = @buffer.point
tokens = Ripper.lex(@buffer.substring(@buffer.point_min,
@buffer.point))
- line, column, event, text = find_nearest_beginning_token(tokens)
+ line, column, event, = find_nearest_beginning_token(tokens)
if event == :on_lparen
return column + 1
end
if line
@buffer.goto_line(line)
@@ -185,8 +190,49 @@
prefix = File.exist?("Gemfile") ? "bundle exec " : ""
prefix + "rake"
else
"ruby " + @buffer.file_name
end
+ end
+
+ def toggle_test
+ case @buffer.file_name
+ when %r'(.*)/test/(.*/)?test_(.*?)\.rb\z'
+ base = $1
+ namespace = $2
+ name = $3
+ if namespace
+ paths = Dir.glob("#{base}/{lib,app}/**/#{namespace}#{name}.rb")
+ if !paths.empty?
+ find_file(paths.first)
+ return
+ end
+ end
+ paths = Dir.glob("#{base}/{lib,app}/**/#{name}.rb")
+ if !paths.empty?
+ find_file(paths.first)
+ return
+ end
+ raise EditorError, "Test subject not found"
+ when %r'(.*)/(?:lib|app)/(.*/)?(.*?)\.rb\z'
+ base = $1
+ namespace = $2
+ name = $3
+ if namespace
+ paths = Dir.glob("#{base}/test/**/#{namespace}test_#{name}.rb")
+ if !paths.empty?
+ find_file(paths.first)
+ return
+ end
+ end
+ paths = Dir.glob("#{base}/test/**/#{name}.rb")
+ if !paths.empty?
+ find_file(paths.first)
+ return
+ end
+ raise EditorError, "Test not found"
+ else
+ raise EditorError, "Unknown file type"
+ end
end
end
end