類別 SyntaxSuggest::ParseBlocksFromIndentLine

此類別負責產生稍後會擴充的初始程式碼區塊。

猜測程式碼區塊時最令人擔憂的是,意外擷取到只包含「end」的區塊。在此範例中

def dog
  begonn # mispelled `begin`
  puts "bark"
  end
end

下列行會從下至上配對

1) end

2) puts "bark"
   end

3) begonn
   puts "bark"
   end

此時它沒有其他地方可以擴充,它會將此內部程式碼作為區塊產生

屬性

code_lines[R]

公開類別方法

new(code_lines:) 按一下以切換來源
# File lib/syntax_suggest/parse_blocks_from_indent_line.rb, line 32
def initialize(code_lines:)
  @code_lines = code_lines
end

公開實例方法

each_neighbor_block(target_line) { |block| ... } 按一下以切換來源

由下而上建構區塊

# File lib/syntax_suggest/parse_blocks_from_indent_line.rb, line 37
def each_neighbor_block(target_line)
  scan = AroundBlockScan.new(code_lines: code_lines, block: CodeBlock.new(lines: target_line))
    .force_add_empty
    .force_add_hidden
    .scan_while { |line| line.indent >= target_line.indent }

  neighbors = scan.code_block.lines

  block = CodeBlock.new(lines: neighbors)
  if neighbors.length <= 2 || block.valid?
    yield block
  else
    until neighbors.empty?
      lines = [neighbors.pop]
      while (block = CodeBlock.new(lines: lines)) && block.invalid? && neighbors.any?
        lines.prepend neighbors.pop
      end

      yield block if block
    end
  end
end