lib/textbringer/modes/ruby_mode.rb in textbringer-1.0.1 vs lib/textbringer/modes/ruby_mode.rb in textbringer-1.0.2

- old
+ new

@@ -83,10 +83,14 @@ [_a-zA-Z\u{0100}-\u{10ffff}] [_a-zA-Z0-9\u{0100}-\u{10ffff}]* ) /x + def comment_start + "#" + end + def initialize(buffer) super(buffer) @buffer[:indent_level] = CONFIG[:ruby_indent_level] @buffer[:indent_tabs_mode] = CONFIG[:ruby_indent_tabs_mode] end @@ -171,11 +175,11 @@ end end private - INDENT_BEG_RE = /^([ \t]*)(class|module|def|if|unless|case|while|until|for|begin|end)\b/ + INDENT_BEG_RE = /^([ \t]*)(class|module|def|if|unless|case|while|until|for|begin)\b/ def space_width(s) s.gsub(/\t/, " " * @buffer[:tab_width]).size end @@ -192,10 +196,26 @@ rescue SearchError @buffer.beginning_of_buffer 0 end + def lex(source) + line_count = source.count("\n") + s = source + lineno = 1 + tokens = [] + loop do + lexer = Ripper::Lexer.new(s, "-", lineno) + tokens.concat(lexer.lex) + last_line = tokens.dig(-1, 0, 0) + return tokens if last_line.nil? || last_line >= line_count + s = source.sub(/(.*\n?){#{last_line}}/, "") + return tokens if last_line + 1 <= lineno + lineno = last_line + 1 + end + end + def calculate_indentation if @buffer.current_line == 1 return 0 end @buffer.save_excursion do @@ -203,11 +223,11 @@ 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)) + tokens = lex(@buffer.substring(start_pos, bol_pos)) _, event, text = tokens.last if event == :on_nl _, event, text = tokens[-2] end if event == :on_tstring_beg || @@ -215,11 +235,11 @@ event == :on_regexp_beg || (event == :on_regexp_end && text.size > 1) || event == :on_tstring_content return nil end - i = find_nearest_beginning_token(tokens) + i, extra_end_count = 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 @@ -238,11 +258,12 @@ @buffer.looking_at?(/[ \t]*/) base_indentation = space_width(@buffer.match_string(0)) end @buffer.goto_char(bol_pos) if line.nil? - indentation = base_indentation + indentation = + base_indentation - extra_end_count * @buffer[:indent_level] else indentation = base_indentation + @buffer[:indent_level] end if @buffer.looking_at?(/[ \t]*([}\])]|(end|else|elsif|when|in|rescue|ensure)\b)/) indentation -= @buffer[:indent_level] @@ -291,22 +312,22 @@ end stack.pop when "end" stack.push(text) end - when :on_rbrace, :on_rparen, :on_rbracket + when :on_rbrace, :on_rparen, :on_rbracket, :on_embexpr_end stack.push(text) - when :on_lbrace, :on_lparen, :on_lbracket, :on_tlambeg + when :on_lbrace, :on_lparen, :on_lbracket, :on_tlambeg, :on_embexpr_beg if stack.empty? return i end if stack.last != BLOCK_END[text] raise EditorError, "#{@buffer.name}:#{line}: Unmatched #{text}" end stack.pop end end - return nil + return nil, stack.size end def find_test_target_path(base, namespace, name) patterns = [] if namespace