class SyntaxSuggest::CodeBlock
多行代码构成一个单独的 CodeBlock
源代码由多个 CodeBlock 构成。
示例
code_block.to_s # => # def foo # puts "foo" # end code_block.valid? # => true code_block.in_valid? # => false
常量
- UNSET
属性
公共类方法
源代码
# File 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
公共实例方法
源代码
# 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
这用于边界排序,我们从最大的缩进搜索到最小的缩进。这允许我们使用多个代码块填充数组,然后调用 'sort!' 而无需指定排序条件
源代码
# File lib/syntax_suggest/code_block.rb, line 67 def current_indent @current_indent ||= lines.select(&:not_empty?).map(&:indent).min || 0 end
源代码
# File lib/syntax_suggest/code_block.rb, line 43 def mark_invisible @lines.map(&:mark_invisible) end
源代码
# 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
源代码
# File lib/syntax_suggest/code_block.rb, line 39 def visible_lines @lines.select(&:visible?).select(&:not_empty?) end