class DeadEnd::CodeBlock

Multiple lines form a singular CodeBlock

Source code is made of multiple CodeBlocks.

Example:

code_block.to_s # =>
  #   def foo
  #     puts "foo"
  #   end

code_block.valid? # => true
code_block.in_valid? # => false

Constants

UNSET

Attributes

lines[R]

Public Class Methods

new(lines: []) click to toggle source
# File lib/dead_end/code_block.rb, line 23
def initialize(lines: [])
  @lines = Array(lines)
  @valid = UNSET
end

Public Instance Methods

<=>(other) click to toggle source

This is used for frontier ordering, we are searching from the largest indentation to the smallest. This allows us to populate an array with multiple code blocks then call `sort!` on it without having to specify the sorting criteria

# File lib/dead_end/code_block.rb, line 56
def <=>(other)
  out = self.current_indent <=> other.current_indent
  return out if out != 0

  # Stable sort
  self.starts_at <=> other.starts_at
end
current_indent() click to toggle source
# File lib/dead_end/code_block.rb, line 64
def current_indent
  @current_indent ||= lines.select(&:not_empty?).map(&:indent).min || 0
end
ends_at() click to toggle source
# File lib/dead_end/code_block.rb, line 48
def ends_at
  @ends_at ||= @lines.last&.line_number
end
hidden?() click to toggle source
# File lib/dead_end/code_block.rb, line 40
def hidden?
  @lines.all?(&:hidden?)
end
invalid?() click to toggle source
# File lib/dead_end/code_block.rb, line 68
def invalid?
  !valid?
end
is_end?() click to toggle source
# File lib/dead_end/code_block.rb, line 36
def is_end?
  to_s.strip == "end"
end
mark_invisible() click to toggle source
# File lib/dead_end/code_block.rb, line 32
def mark_invisible
  @lines.map(&:mark_invisible)
end
starts_at() click to toggle source
# File lib/dead_end/code_block.rb, line 44
def starts_at
  @starts_at ||= @lines.first&.line_number
end
to_s() click to toggle source
# File lib/dead_end/code_block.rb, line 77
def to_s
  @lines.join
end
valid?() click to toggle source
# File lib/dead_end/code_block.rb, line 72
def valid?
  return @valid if @valid != UNSET
  @valid = DeadEnd.valid?(self.to_s)
end
visible_lines() click to toggle source
# File lib/dead_end/code_block.rb, line 28
def visible_lines
  @lines.select(&:visible?).select(&:not_empty?)
end