Class: SyntaxErrorSearch::CodeBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_search/code_block.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines: []) ⇒ CodeBlock

Returns a new instance of CodeBlock.



22
23
24
# File 'lib/syntax_search/code_block.rb', line 22

def initialize(lines: [])
  @lines = Array(lines)
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



20
21
22
# File 'lib/syntax_search/code_block.rb', line 20

def lines
  @lines
end

Instance Method Details

#<=>(other) ⇒ Object

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



46
47
48
# File 'lib/syntax_search/code_block.rb', line 46

def <=>(other)
  self.current_indent <=> other.current_indent
end

#current_indentObject



50
51
52
# File 'lib/syntax_search/code_block.rb', line 50

def current_indent
  @current_indent ||= lines.select(&:not_empty?).map(&:indent).min || 0
end

#ends_atObject



38
39
40
# File 'lib/syntax_search/code_block.rb', line 38

def ends_at
  @ends_at ||= @lines.last&.line_number
end

#invalid?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/syntax_search/code_block.rb', line 54

def invalid?
  !valid?
end

#is_end?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/syntax_search/code_block.rb', line 30

def is_end?
  to_s.strip == "end"
end

#mark_invisibleObject



26
27
28
# File 'lib/syntax_search/code_block.rb', line 26

def mark_invisible
  @lines.map(&:mark_invisible)
end

#starts_atObject



34
35
36
# File 'lib/syntax_search/code_block.rb', line 34

def starts_at
  @starts_at ||= @lines.first&.line_number
end

#to_sObject



62
63
64
# File 'lib/syntax_search/code_block.rb', line 62

def to_s
  @lines.join
end

#valid?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/syntax_search/code_block.rb', line 58

def valid?
  SyntaxErrorSearch.valid?(self.to_s)
end