Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
SyntaxSuggest::CaptureCodeContext#capture_last_end_same_indent
capture_last_end_same_indent(block)
lib/syntax_suggest/capture_code_context.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.4.1/lib/syntax_suggest/capture_code_context.rb, line 161
def capture_last_end_same_indent(block)
return if block.visible_lines.length != 1
return unless block.visible_lines.first.is_kw?
visible_line = block.visible_lines.first
lines = @code_lines[visible_line.index..block.lines.last.index]
# Find first end with same indent
# (this would return line 4)
#
# end # 4
matching_end = lines.detect { |line| line.indent == block.current_indent && line.is_end? }
return unless matching_end
@lines_to_output << matching_end
# Work backwards from the end to
# see if there are mis-matched
# keyword/end pairs
#
# Return the first mis-matched keyword
# this would find line 2
#
# def bark # 2
# puts "woof" # 3
# end # 4
end_count = 0
kw_count = 0
kw_line = @code_lines[visible_line.index..matching_end.index].reverse.detect do |line|
end_count += 1 if line.is_end?
kw_count += 1 if line.is_kw?
!kw_count.zero? && kw_count >= end_count
end
return unless kw_line
@lines_to_output << kw_line
end
When there is an invalid block with a keyword missing an end right before another end, it is unclear where which keyword is missing the end
Take this example:
class Dog # 1
def bark # 2
puts "woof" # 3
end # 4
However due to github.com/ruby/syntax_suggest/issues/32 the problem line will be identified as:
> class Dog # 1
Because lines 2, 3, and 4 are technically valid code and are expanded first, deemed valid, and hidden. We need to un-hide the matching end line 4. Also work backwards and if there’s a mis-matched keyword, show it too
See also
#capture_first_kw_end_same_indent
#sorted_lines
#capture_falling_indent
also in
4.0 · 3.3 · 3.2