Class: SyntaxSuggest::CodeBlock
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/syntax_suggest/code_block.rb |
Overview
Constant Summary
-
UNSET =
# File 'lib/syntax_suggest/code_block.rb', line 20Object.new.freeze
Class Method Summary
- .new(lines: []) ⇒ CodeBlock constructor
Instance Attribute Summary
- #deleted? ⇒ Boolean readonly
- #ends_at readonly
- #hidden? ⇒ Boolean readonly
- #invalid? ⇒ Boolean readonly
- #is_end? ⇒ Boolean readonly
- #lines readonly
- #starts_at readonly
- #valid? ⇒ Boolean readonly
Instance Method Summary
-
#<=>(other)
This is used for frontier ordering, we are searching from the largest indentation to the smallest.
- #current_indent
- #delete
- #mark_invisible
- #to_s
- #visible_lines
Constructor Details
.new(lines: []) ⇒ CodeBlock
Instance Attribute Details
#deleted? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/syntax_suggest/code_block.rb', line 35
def deleted? @deleted end
#ends_at (readonly)
[ GitHub ]
#invalid? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/syntax_suggest/code_block.rb', line 71
def invalid? !valid? end
#is_end? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/syntax_suggest/code_block.rb', line 47
def is_end? to_s.strip == "end" end
#lines (readonly)
[ GitHub ]#starts_at (readonly)
[ GitHub ]
#valid? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/syntax_suggest/code_block.rb', line 75
def valid? if @valid == UNSET # Performance optimization # # If all the lines were previously hidden # and we expand to capture additional empty # lines then the result cannot be invalid # # That means there's no reason to re-check all # lines with the parser (which is expensive). # Benchmark in commit message @valid = if lines.all? { |l| l.hidden? || l.empty? } true else SyntaxSuggest.valid?(lines.map(&:original).join) end else @valid end end
Instance Method Details
#<=>(other)
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/syntax_suggest/code_block.rb', line 59
def <=>(other) out = current_indent <=> other.current_indent return out if out != 0 # Stable sort starts_at <=> other.starts_at end
#current_indent
[ GitHub ]# File 'lib/syntax_suggest/code_block.rb', line 67
def current_indent @current_indent ||= lines.select(&:not_empty?).map(&:indent).min || 0 end
#delete
[ GitHub ]# File 'lib/syntax_suggest/code_block.rb', line 31
def delete @deleted = true end
#mark_invisible
[ GitHub ]# File 'lib/syntax_suggest/code_block.rb', line 43
def mark_invisible @lines.map(&:mark_invisible) end
#to_s
[ GitHub ]# File 'lib/syntax_suggest/code_block.rb', line 96
def to_s @lines.join end
#visible_lines
[ GitHub ]# File 'lib/syntax_suggest/code_block.rb', line 39
def visible_lines @lines.select(&:visible?).select(&:not_empty?) end