Class
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
No documentation available
Attributes
Read
No documentation available
Read
No documentation available
Read
No documentation available
Class Methods
lib/syntax_suggest/code_block.rb
View on GitHub
# File tmp/rubies/ruby-3.2.0/lib/syntax_suggest/code_block.rb, line 23
def initialize(lines: [])
@lines = Array(lines)
@valid = UNSET
@deleted = false
@starts_at = @lines.first.number
@ends_at = @lines.last.number
end
No documentation available
Instance Methods
lib/syntax_suggest/code_block.rb
View on GitHub
# File tmp/rubies/ruby-3.2.0/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
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
lib/syntax_suggest/code_block.rb
View on GitHub
# File tmp/rubies/ruby-3.2.0/lib/syntax_suggest/code_block.rb, line 67
def current_indent
@current_indent ||= lines.select(&:not_empty?).map(&:indent).min || 0
end
No documentation available
#
lib/syntax_suggest/code_block.rb
View on GitHub
# File tmp/rubies/ruby-3.2.0/lib/syntax_suggest/code_block.rb, line 31
def delete
@deleted = true
end
No documentation available
#
lib/syntax_suggest/code_block.rb
View on GitHub
# File tmp/rubies/ruby-3.2.0/lib/syntax_suggest/code_block.rb, line 35
def deleted?
@deleted
end
No documentation available
#
lib/syntax_suggest/code_block.rb
View on GitHub
# File tmp/rubies/ruby-3.2.0/lib/syntax_suggest/code_block.rb, line 51
def hidden?
@lines.all?(&:hidden?)
end
No documentation available
#
lib/syntax_suggest/code_block.rb
View on GitHub
# File tmp/rubies/ruby-3.2.0/lib/syntax_suggest/code_block.rb, line 71
def invalid?
!valid?
end
No documentation available
#
lib/syntax_suggest/code_block.rb
View on GitHub
# File tmp/rubies/ruby-3.2.0/lib/syntax_suggest/code_block.rb, line 47
def is_end?
to_s.strip == "end"
end
No documentation available
lib/syntax_suggest/code_block.rb
View on GitHub
# File tmp/rubies/ruby-3.2.0/lib/syntax_suggest/code_block.rb, line 43
def mark_invisible
@lines.map(&:mark_invisible)
end
No documentation available
#
lib/syntax_suggest/code_block.rb
View on GitHub
# File tmp/rubies/ruby-3.2.0/lib/syntax_suggest/code_block.rb, line 96
def to_s
@lines.join
end
No documentation available
#
lib/syntax_suggest/code_block.rb
View on GitHub
# File tmp/rubies/ruby-3.2.0/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 ripper (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
No documentation available
lib/syntax_suggest/code_block.rb
View on GitHub
# File tmp/rubies/ruby-3.2.0/lib/syntax_suggest/code_block.rb, line 39
def visible_lines
@lines.select(&:visible?).select(&:not_empty?)
end
No documentation available