instance method
SyntaxSuggest::CaptureCodeContext#capture_first_kw_end_same_indent
capture_first_kw_end_same_indent(block)
lib/syntax_suggest/capture_code_context.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/syntax_suggest/capture_code_context.rb, line 221
def capture_first_kw_end_same_indent(block)
return if block.visible_lines.length != 1
return unless block.visible_lines.first.is_end?
visible_line = block.visible_lines.first
lines = @code_lines[block.lines.first.index..visible_line.index]
matching_kw = lines.reverse.detect { |line| line.indent == block.current_indent && line.is_kw? }
return unless matching_kw
@lines_to_output << matching_kw
kw_count = 0
end_count = 0
orphan_end = @code_lines[matching_kw.index..visible_line.index].detect do |line|
kw_count += 1 if line.is_kw?
end_count += 1 if line.is_end?
end_count >= kw_count
end
return unless orphan_end
@lines_to_output << orphan_end
end
The logical inverse of capture_last_end_same_indent
When there is an invalid block with an end missing a keyword right after another end, it is unclear where which end is missing the keyword.
Take this example:
class Dog # 1
puts "woof" # 2
end # 3
end # 4
the problem line will be identified as:
> end # 4
This happens because lines 1, 2, and 3 are technically valid code and are expanded first, deemed valid, and hidden. We need to un-hide the matching keyword on line 1. Also work backwards and if there’s a mis-matched end, show it too
See also
#capture_falling_indent
#capture_last_end_same_indent
#capture_before_after_kws
#sorted_lines
also in
3.4 · 3.3 · 3.2