class SyntaxTree::CLI::Search

An action of the CLI that searches for the given pattern matching pattern in the given files.

Attributes

Public Class Methods

new(query) click to toggle source
# File lib/syntax_tree/cli.rb, line 346
def initialize(query)
  query = File.read(query) if File.readable?(query)
  pattern =
    begin
      Pattern.new(query).compile
    rescue Pattern::CompilationError => error
      warn(error.message)
      exit(1)
    end

  @search = SyntaxTree::Search.new(pattern)
end

Public Instance Methods

run(item) click to toggle source
# File lib/syntax_tree/cli.rb, line 359
def run(item)
  search.scan(item.handler.parse(item.source)) do |node|
    location = node.location
    line = location.start_line

    bold_range =
      if line == location.end_line
        location.start_column...location.end_column
      else
        location.start_column..
      end

    source = item.source.lines[line - 1].chomp
    source[bold_range] = Color.bold(source[bold_range]).to_s

    puts("#{item.filepath}:#{line}:#{location.start_column}: #{source}")
  end
end