lib/textbringer/modes/ruby_mode.rb in textbringer-0.1.4 vs lib/textbringer/modes/ruby_mode.rb in textbringer-0.1.5

- old
+ new

@@ -18,10 +18,11 @@ # Return true if modified. def indent_line result = false level = calculate_indentation + return result if level.nil? @buffer.save_excursion do @buffer.beginning_of_line has_space = @buffer.looking_at?(/[ \t]+/) if has_space s = @buffer.match_string(0) @@ -86,20 +87,73 @@ while /\s/ =~ @buffer.char_after @buffer.forward_char end end - def compile - cmd = read_from_minibuffer("Compile: ", default: default_compile_command) + def compile(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 + def default_compile_command + @buffer[:ruby_compile_command] || + if File.exist?("Rakefile") + prefix = File.exist?("Gemfile") ? "bundle exec " : "" + prefix + "rake" + elsif @buffer.file_name + "ruby " + @buffer.file_name + else + nil + 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/**/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 + private def calculate_indentation if @buffer.current_line == 1 return 0 @@ -107,16 +161,33 @@ @buffer.save_excursion do @buffer.beginning_of_line bol_pos = @buffer.point tokens = Ripper.lex(@buffer.substring(@buffer.point_min, @buffer.point)) + _, event, = tokens.last + if event == :on_tstring_beg || + event == :on_heredoc_beg || + event == :on_tstring_content + return nil + end line, column, event, = find_nearest_beginning_token(tokens) if event == :on_lparen return column + 1 end if line @buffer.goto_line(line) + while !@buffer.beginning_of_buffer? + if @buffer.save_excursion { + @buffer.backward_char + @buffer.skip_re_backward(/\s/) + @buffer.char_before == ?, + } + @buffer.backward_line + else + break + end + end else @buffer.backward_line end @buffer.looking_at?(/[ \t]*/) base_indentation = @buffer.match_string(0). @@ -180,59 +251,8 @@ end stack.pop end end return nil - end - - def default_compile_command - @buffer[:ruby_compile_command] || - if File.exist?("Rakefile") - 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