Class: SyntaxErrorSearch::CodeBlock
- 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
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
This is used for frontier ordering, we are searching from the largest indentation to the smallest.
- #current_indent ⇒ Object
- #ends_at ⇒ Object
-
#initialize(lines: []) ⇒ CodeBlock
constructor
A new instance of CodeBlock.
- #invalid? ⇒ Boolean
- #is_end? ⇒ Boolean
- #mark_invisible ⇒ Object
- #starts_at ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
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
#lines ⇒ Object (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_indent ⇒ Object
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_at ⇒ Object
38 39 40 |
# File 'lib/syntax_search/code_block.rb', line 38 def ends_at @ends_at ||= @lines.last&.line_number end |
#invalid? ⇒ Boolean
54 55 56 |
# File 'lib/syntax_search/code_block.rb', line 54 def invalid? !valid? end |
#is_end? ⇒ Boolean
30 31 32 |
# File 'lib/syntax_search/code_block.rb', line 30 def is_end? to_s.strip == "end" end |
#mark_invisible ⇒ Object
26 27 28 |
# File 'lib/syntax_search/code_block.rb', line 26 def mark_invisible @lines.map(&:mark_invisible) end |
#starts_at ⇒ Object
34 35 36 |
# File 'lib/syntax_search/code_block.rb', line 34 def starts_at @starts_at ||= @lines.first&.line_number end |
#to_s ⇒ Object
62 63 64 |
# File 'lib/syntax_search/code_block.rb', line 62 def to_s @lines.join end |
#valid? ⇒ Boolean
58 59 60 |
# File 'lib/syntax_search/code_block.rb', line 58 def valid? SyntaxErrorSearch.valid?(self.to_s) end |