lib/textbringer/modes/ruby_mode.rb in textbringer-0.2.9 vs lib/textbringer/modes/ruby_mode.rb in textbringer-0.3.0

- old
+ new

@@ -158,43 +158,18 @@ 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" + path = find_test_target_path($1, $2, $3) + find_file(path) + when %r'(.*)/spec/(.*/)?(.*?)_spec\.rb\z' + path = find_test_target_path($1, $2, $3) + find_file(path) 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/**/test_#{name}.rb") - if !paths.empty? - find_file(paths.first) - return - end - raise EditorError, "Test not found" + path = find_test_path($1, $2, $3) + find_file(path) else raise EditorError, "Unknown file type" end end @@ -225,10 +200,11 @@ if @buffer.current_line == 1 return 0 end @buffer.save_excursion do @buffer.beginning_of_line + start_with_period = @buffer.looking_at?(/[ \t]*\./) bol_pos = @buffer.point base_indentation = beginning_of_indentation start_pos = @buffer.point start_line = @buffer.current_line tokens = Ripper.lex(@buffer.substring(start_pos, bol_pos)) @@ -238,12 +214,13 @@ event == :on_regexp_beg || (event == :on_regexp_end && text.size > 1) || event == :on_tstring_content return nil end - line, column, event, = find_nearest_beginning_token(tokens) - if event == :on_lparen + i = find_nearest_beginning_token(tokens) + (line, column), event, = i ? tokens[i] : nil + if event == :on_lparen && tokens.dig(i + 1, 1) != :on_ignored_nl return column + 1 end if line @buffer.goto_line(start_line - 1 + line) while !@buffer.beginning_of_buffer? @@ -270,14 +247,15 @@ indentation -= @buffer[:indent_level] end _, last_event, last_text = tokens.reverse_each.find { |_, e, _| e != :on_sp && e != :on_nl && e != :on_ignored_nl } - if (last_event == :on_op && last_text != "|") || + if start_with_period || + (last_event == :on_op && last_text != "|") || last_event == :on_period || - (last_event == :on_comma && - event != :on_lbrace && event != :on_lbracket) + (last_event == :on_comma && event != :on_lbrace && + event != :on_lparen && event != :on_lbracket) indentation += @buffer[:indent_level] end indentation end end @@ -289,11 +267,11 @@ } def find_nearest_beginning_token(tokens) stack = [] (tokens.size - 1).downto(0) do |i| - (line, column), event, text = tokens[i] + (line, ), event, text = tokens[i] case event when :on_kw _, prev_event, _ = tokens[i - 1] next if prev_event == :on_symbeg case text @@ -303,11 +281,11 @@ ts = tokens[0...i].reverse_each.take_while { |(l,_),| l == line } t = ts.find { |_, e| e != :on_sp } next if t && !(t[1] == :on_op && t[2] == "=") end if stack.empty? - return line, column, event, text + return i end if stack.last != "end" raise EditorError, "#{@buffer.name}:#{line}: Unmatched #{text}" end stack.pop @@ -316,20 +294,48 @@ end when :on_rbrace, :on_rparen, :on_rbracket stack.push(text) when :on_lbrace, :on_lparen, :on_lbracket, :on_tlambeg if stack.empty? - return line, column, event, text + return i end if stack.last != BLOCK_END[text] raise EditorError, "#{@buffer.name}:#{line}: Unmatched #{text}" end stack.pop end end return nil end + def find_test_target_path(base, namespace, name) + patterns = [] + if namespace + patterns.push("#{base}/{lib,app}/**/#{namespace}#{name}.rb") + end + patterns.push("#{base}/{lib,app}/**/#{name}.rb") + find_first_path(patterns) or raise EditorError, "Test target not found" + end + + def find_test_path(base, namespace, name) + patterns = [] + if namespace + patterns.push("#{base}/test/**/#{namespace}test_#{name}.rb") + patterns.push("#{base}/spec/**/#{namespace}#{name}_spec.rb") + end + patterns.push("#{base}/test/**/test_#{name}.rb") + patterns.push("#{base}/spec/**/#{name}_spec.rb") + find_first_path(patterns) or raise EditorError, "Test not found" + end + + def find_first_path(patterns) + patterns.each do |pattern| + paths = Dir.glob(pattern) + return paths.first if !paths.empty? + end + nil + end + class PartialLiteralAnalyzer < Ripper def self.in_literal?(src) new(src).in_literal? end