Skip to content

Commit

Permalink
Merge pull request #125 from aycabta/support-shortening-lambda-notation
Browse files Browse the repository at this point in the history
Support shortening lambda notation
  • Loading branch information
aycabta authored Aug 17, 2020
2 parents d6f3448 + f1a775a commit 5a6f0e8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def process_nesting_level
end

case t[1]
when :on_lbracket, :on_lbrace, :on_lparen
when :on_lbracket, :on_lbrace, :on_lparen, :on_tlambeg
indent += 1
when :on_rbracket, :on_rbrace, :on_rparen
indent -= 1
Expand Down Expand Up @@ -398,7 +398,7 @@ def check_newline_depth_difference
next
end
case t[1]
when :on_lbracket, :on_lbrace, :on_lparen
when :on_lbracket, :on_lbrace, :on_lparen, :on_tlambeg
depth_difference += 1
open_brace_on_line += 1
when :on_rbracket, :on_rbrace, :on_rparen
Expand Down Expand Up @@ -481,7 +481,7 @@ def check_corresponding_token_depth
next
end
case t[1]
when :on_lbracket, :on_lbrace, :on_lparen
when :on_lbracket, :on_lbrace, :on_lparen, :on_tlambeg
spaces_of_nest.push(spaces_at_line_head + open_brace_on_line * 2)
open_brace_on_line += 1
when :on_rbracket, :on_rbrace, :on_rparen
Expand Down
28 changes: 27 additions & 1 deletion test/irb/test_ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module TestIRB
class TestRubyLex < Test::Unit::TestCase
Row = Struct.new(:content, :current_line_spaces, :new_line_spaces)
Row = Struct.new(:content, :current_line_spaces, :new_line_spaces, :nesting_level)

class MockIO
def initialize(params, &assertion)
Expand Down Expand Up @@ -34,6 +34,15 @@ def assert_indenting(lines, correct_space_count, add_new_line)
ruby_lex.set_auto_indent(context)
end

def assert_nesting_level(lines, expected)
ruby_lex = RubyLex.new()
io = proc{ lines.join("\n") }
ruby_lex.set_input(io, io)
ruby_lex.lex
error_message = "Calculated the wrong number of nesting level for:\n #{lines.join("\n")}"
assert_equal(expected, ruby_lex.instance_variable_get(:@indent), error_message)
end

def test_auto_indent
input_with_correct_indents = [
Row.new(%q(def each_top_level_statement), nil, 2),
Expand Down Expand Up @@ -234,5 +243,22 @@ def test_oneliner_method_definition
assert_indenting(lines, row.new_line_spaces, true)
end
end

def test_tlambda
input_with_correct_indents = [
Row.new(%q(if true), nil, 2, 1),
Row.new(%q( -> {), nil, 4, 2),
Row.new(%q( }), 2, 2, 1),
Row.new(%q(end), 0, 0, 0),
]

lines = []
input_with_correct_indents.each do |row|
lines << row.content
assert_indenting(lines, row.current_line_spaces, false)
assert_indenting(lines, row.new_line_spaces, true)
assert_nesting_level(lines, row.nesting_level)
end
end
end
end

0 comments on commit 5a6f0e8

Please sign in to comment.