-
Notifications
You must be signed in to change notification settings - Fork 136
Improve indentation: bugfix, heredoc, embdoc, strings #515
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
Conversation
8012d9a to
32a68b4
Compare
c6ca53b to
177395f
Compare
177395f to
7c4719b
Compare
| irb(main):008:0> | ||
| irb(main):009:0> a | ||
| irb(main):010:0> .a | ||
| irb(main):011:0> .b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This indent retain feature dropped in this pull request had a bug.
Paste this code and press return key
a
.b
.c
if dthen you will get wrong indent
irb(main):001:0> a
irb(main):002:0> .b
irb(main):003:0> .c
irb(main):004:1* if d
irb(main):005:1*
733070d to
540b64c
Compare
540b64c to
c382a02
Compare
lib/irb/ruby-lex.rb
Outdated
| next nil if !is_newline && lines[line_index]&.byteslice(0, byte_pointer)&.match?(/\A\s*\z/) | ||
|
|
||
| code = lines[0..line_index].map { |l| "#{l}\n" }.join | ||
| next nil if code == "\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean lines is either [nil] or [""] when the condition is true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, it looks like almost-unreachable code.
I think I added this to returns nil if lines are empty, but empty lines are not passed to this proc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I type CTRL+d, [nil] is passed. It might be a bug of reline.
I added again with more simple condition and comment.
next nil if lines == [nil] # Workaround for exit IRB with CTRL+d2a4519a to
1e493c9
Compare
lib/irb/ruby-lex.rb
Outdated
| end | ||
| elsif prev_opens.last&.event == :on_heredoc_beg | ||
| tok = prev_opens.last.tok | ||
| if prev_opens.size < next_opens.size || prev_opens.last == next_opens.last |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this condition simpler to if prev_opens.size <= next_opens.size
When heredoc is closed, next_opens.size is always smaller than prev_opens.size because only heredoc_end can be on the line.
<<A # heredoc_beg
A + ( # heredoc_end + lparen ← this is not possible
A
↑ heredoc ends here1e493c9 to
fd8feba
Compare
(ruby/irb#515) * Implement heredoc embdoc and string indentation with bugfix * Fix test_ruby_lex's indentation value * Add embdoc indent test * Add workaround for lines==[nil] passed to auto_indent when exit IRB with CTRL+d
Description
left: irb 1.6.2 right: this branch
perfect_indent.mp4
Heredoc and embdoc indent
I implemented these heredoc and embdoc indent
String indent
string, backtick, regexp and symbol
Indentation bugfix
These bugs will be fixed
Internal
auto_indent_proc
->(lines, line_index, byte_pointer, is_newline){}was implemented as:But it is wrong. In most of the cases, it should return a number to avoid bug when inserting newline.
Also, special treatment is done for heredoc, embdoc and free-indent-acceptable literal like string.