diff options
author | tomoya ishida <tomoyapenguin@gmail.com> | 2023-01-18 14:28:13 +0900 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-01-18 05:28:18 +0000 |
commit | 2d7e63954956645a4a050c78df71c8f4a56851d8 (patch) | |
tree | f508ee98419b452c9d20de38b4cc60dbf5f8b0f7 | |
parent | 46066d0b96473589038810ac3aa19a1e26fdb837 (diff) |
[ruby/reline] multiline_repl do not need to depend on RubyLex
(https://github.com/ruby/reline/pull/502)
* multiline_repl do not need to depend on RubyLex
* Add auto indent test
-rwxr-xr-x | test/reline/yamatanooroti/multiline_repl | 7 | ||||
-rw-r--r-- | test/reline/yamatanooroti/termination_checker.rb | 36 | ||||
-rw-r--r-- | test/reline/yamatanooroti/test_rendering.rb | 29 |
3 files changed, 50 insertions, 22 deletions
diff --git a/test/reline/yamatanooroti/multiline_repl b/test/reline/yamatanooroti/multiline_repl index c429494442..7e58b602eb 100755 --- a/test/reline/yamatanooroti/multiline_repl +++ b/test/reline/yamatanooroti/multiline_repl @@ -53,7 +53,9 @@ opt.on('--color-bold') { } } opt.on('--auto-indent') { - AutoIndent.new + Reline.auto_indent_proc = lambda do |lines, line_index, byte_pointer, is_newline| + AutoIndent.calculate_indent(lines, line_index, byte_pointer, is_newline) + end } opt.on('--dialog VAL') { |v| Reline.add_dialog_proc(:simple_dialog, lambda { @@ -194,8 +196,7 @@ end begin prompt = ENV['RELINE_TEST_PROMPT'] || 'prompt> ' puts 'Multiline REPL.' - checker = TerminationChecker.new - while code = Reline.readmultiline(prompt, true) { |code| checker.terminated?(code) } + while code = Reline.readmultiline(prompt, true) { |code| TerminationChecker.terminated?(code) } case code.chomp when 'exit', 'quit', 'q' exit 0 diff --git a/test/reline/yamatanooroti/termination_checker.rb b/test/reline/yamatanooroti/termination_checker.rb index 15ec7d5913..b97c798c59 100644 --- a/test/reline/yamatanooroti/termination_checker.rb +++ b/test/reline/yamatanooroti/termination_checker.rb @@ -1,28 +1,26 @@ require 'ripper' -require 'irb/ruby-lex' -class TerminationChecker < RubyLex - def terminated?(code) - code.gsub!(/\n*$/, '').concat("\n") - tokens = self.class.ripper_lex_without_warning(code) - continue = process_continue(tokens) - code_block_open = check_code_block(code, tokens) - indent = process_nesting_level(tokens) - ltype = process_literal_type(tokens) - if code_block_open or ltype or continue or indent > 0 - false - else - true - end +module TerminationChecker + def self.terminated?(code) + Ripper.sexp(code) ? true : false end end -class AutoIndent < RubyLex - def initialize - @context = Struct.new("MockIRBContext", :auto_indent_mode, :workspace, :local_variables).new(true, nil, []) +module AutoIndent + def self.calculate_indent(lines, line_index, byte_pointer, is_newline) + if is_newline + 2 * nesting_level(lines[0..line_index - 1]) + else + lines = lines.dup + lines[line_index] = lines[line_index]&.byteslice(0, byte_pointer) + prev_level = nesting_level(lines[0..line_index - 1]) + level = nesting_level(lines[0..line_index]) + 2 * level if level < prev_level + end end - def auto_indent(&block) - Reline.auto_indent_proc = block + def self.nesting_level(lines) + code = lines.join("\n") + code.scan(/if|def|\(|\[|\{/).size - code.scan(/end|\)|\]|\}/).size end end diff --git a/test/reline/yamatanooroti/test_rendering.rb b/test/reline/yamatanooroti/test_rendering.rb index 42d2b173d5..560216d93c 100644 --- a/test/reline/yamatanooroti/test_rendering.rb +++ b/test/reline/yamatanooroti/test_rendering.rb @@ -661,6 +661,35 @@ begin EOC end + def test_auto_indent + start_terminal(10, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --auto-indent}, startup_message: 'Multiline REPL.') + "def hoge\nputs(\n1,\n2\n)\nend".lines do |line| + write line + end + close + assert_screen(<<~EOC) + Multiline REPL. + prompt> def hoge + prompt> puts( + prompt> 1, + prompt> 2 + prompt> ) + prompt> end + EOC + end + + def test_auto_indent_when_inserting_line + start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --auto-indent}, startup_message: 'Multiline REPL.') + write 'aa(bb(cc(dd(ee(' + write "\C-b" * 5 + "\n" + close + assert_screen(<<~EOC) + Multiline REPL. + prompt> aa(bb(cc(d + prompt> d(ee( + EOC + end + def test_suppress_auto_indent_just_after_pasted start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --auto-indent}, startup_message: 'Multiline REPL.') write("def hoge\n [[\n 3]]\ned") |