Sha256: 8f389a0bba66411d71d398c715edb09c4990b5f839a4e39debd5a45e0a7fdc11

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module SyntaxErrorSearch
  # This class is responsible for generating initial code blocks
  # that will then later be expanded.
  #
  # The biggest concern when guessing about code blocks, is accidentally
  # grabbing one that contains only an "end". In this example:
  #
  #   def dog
  #     begonn # mispelled `begin`
  #     puts "bark"
  #     end
  #   end
  #
  # The following lines would be matched (from bottom to top):
  #
  #   1) end
  #
  #   2) puts "bark"
  #      end
  #
  #   3) begonn
  #      puts "bark"
  #      end
  #
  # At this point it has no where else to expand, and it will yield this inner
  # code as a block
  class ParseBlocksFromIndentLine
    attr_reader :code_lines

    def initialize(code_lines: )
      @code_lines = code_lines
    end

    # Builds blocks from bottom up
    def each_neighbor_block(target_line)
      scan = AroundBlockScan.new(code_lines: code_lines, block: CodeBlock.new(lines: target_line))
        .skip(:empty?)
        .skip(:hidden?)
        .scan_while {|line| line.indent >= target_line.indent }

      neighbors = @code_lines[scan.before_index..scan.after_index]

      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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
syntax_search-0.2.0 lib/syntax_search/parse_blocks_from_indent_line.rb
syntax_search-0.1.5 lib/syntax_search/parse_blocks_from_indent_line.rb
syntax_search-0.1.4 lib/syntax_search/parse_blocks_from_indent_line.rb
syntax_search-0.1.3 lib/syntax_search/parse_blocks_from_indent_line.rb