1 # frozen_string_literal: true
4 # Multiple lines form a singular CodeBlock
6 # Source code is made of multiple CodeBlocks.
10 # code_block.to_s # =>
15 # code_block.valid? # => true
16 # code_block.in_valid? # => false
20 UNSET = Object.new.freeze
21 attr_reader :lines, :starts_at, :ends_at
23 def initialize(lines: [])
27 @starts_at = @lines.first.number
28 @ends_at = @lines.last.number
40 @lines.select(&:visible?).select(&:not_empty?)
44 @lines.map(&:mark_invisible)
52 @lines.all?(&:hidden?)
55 # This is used for frontier ordering, we are searching from
56 # the largest indentation to the smallest. This allows us to
57 # populate an array with multiple code blocks then call `sort!`
58 # on it without having to specify the sorting criteria
60 out = current_indent <=> other.current_indent
61 return out if out != 0
64 starts_at <=> other.starts_at
68 @current_indent ||= lines.select(&:not_empty?).map(&:indent).min || 0
77 # Performance optimization
79 # If all the lines were previously hidden
80 # and we expand to capture additional empty
81 # lines then the result cannot be invalid
83 # That means there's no reason to re-check all
84 # lines with the parser (which is expensive).
85 # Benchmark in commit message
86 @valid = if lines.all? { |l| l.hidden? || l.empty? }
89 SyntaxSuggest.valid?(lines.map(&:original).join)