Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indent multiline percent literals #643

Merged
merged 1 commit into from
Jul 15, 2023

Conversation

tompng
Copy link
Member

@tompng tompng commented Jul 14, 2023

This pull request implements percent literal indenting, which I pend implementing in #500 to not change the behavior at that time.
percent_literal_indent

Indented percent literal is used in IRB and also in ruby/ruby.
You can search with this regexp: %[a-z]?[({\[]\n

# Words. Indent is fixed to `prev_indent + 1`.
ReservedWords = %w[
  __ENCODING__ __LINE__ __FILE__
  BEGIN END
  ...
]
# String. Indent is initially `prev_indent + 1` but can add or delete space because space is part of the content.
line = __LINE__; Context.module_eval %[
  def foo(*opts, &b)
    bar
  end
], __FILE__, line

@st0012
Copy link
Member

st0012 commented Jul 14, 2023

While testing this in IRB, I found a weird behaviour and a crash, which are both recorded in this video:

Screen.Recording.2023-07-14.at.18.41.49.mov

I tried to type the input listed in test_heredoc_with_embexpr. And when I typed C after a, for some reason an empty line was added. I did experience this with master too though so it's likely not caused by this change.

But after typing C, if I type a again, it'd crash with:

/Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline/line_editor.rb:1131:in `+': no implicit conversion of nil into String (TypeError)

    if Reline::Unicode::CSI_REGEXP.match?(prompt + line_to_render)
                                                   ^^^^^^^^^^^^^^
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline/line_editor.rb:1131:in `render_partial'
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline/line_editor.rb:498:in `rerender'
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline.rb:356:in `block (3 levels) in inner_readline'
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline.rb:354:in `each'
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline.rb:354:in `block (2 levels) in inner_readline'
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline.rb:429:in `block in read_io'
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline.rb:399:in `loop'
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline.rb:399:in `read_io'
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline.rb:352:in `block in inner_readline'
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline.rb:350:in `loop'
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline.rb:350:in `inner_readline'
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline.rb:278:in `block in readmultiline'
        from /Users/hung-wulo/.gem/ruby/3.2.2/gems/reline-0.3.6/lib/reline/ansi.rb:149:in `block in with_raw_input'

On master it doesn't crash, just adds another blank line:

Screenshot 2023-07-14 at 18 46 39

@tompng
Copy link
Member Author

tompng commented Jul 14, 2023

The weird behavior is mostly https://bugs.ruby-lang.org/issues/19563 and maybe partially https://bugs.ruby-lang.org/issues/19736

code = ["<<A+%W[\#{<<B", "\#{<<C+%W[", "a", "C", "", ""].join("\n")
puts '===original==='
puts code
puts '===ripper==='
puts Ripper.tokenize(code).join
puts '===RubyLex===' # interpolates DATA part and missing token ignored by ripper
puts RubyLex.ripper_lex_without_warning(code).map(&:tok).join
===original===
<<A+%W[#{<<B
#{<<C+%W[
a
C

===ripper===
<<A#{<<C+%W[

a
C
===RubyLex===
<<A+%W[#{<<B
#{<<C+%W[

a
C

This is causing line number mismatch of input text, colored text and prompt list.
On master, instead of typing a in the last step, typing three spaces will make IRB crash.

@st0012
Copy link
Member

st0012 commented Jul 15, 2023

Thanks for the explanation. I can confirm that crash and the weird issue don't happen with Ruby head.

@st0012 st0012 added enhancement New feature or request bug Something isn't working and removed enhancement New feature or request labels Jul 15, 2023
Copy link
Member

@st0012 st0012 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@tompng tompng merged commit 18bb402 into ruby:master Jul 15, 2023
24 checks passed
@tompng tompng deleted the indent_percent_words_literal branch July 15, 2023 18:12
matzbot pushed a commit to ruby/ruby that referenced this pull request Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging this pull request may close these issues.

None yet

2 participants