class SetBuilderAdapter::Ag

Public Instance Methods

build_working_set(search, options) click to toggle source
# File lib/set_builder_adapter/ag.rb, line 114
def build_working_set(search, options)
  debug_message "search command: #{command_parts(search, options)}"

  stdout, stderr, status = Open3.capture3(*command_parts(search, options))

  # Ag exits 0 when results found
  # Ag exits 1 when zero results found
  # ... It also exits 1 when there's a problem with options.
  if status.exitstatus == 0 || status.exitstatus == 1
    WorkingSet.new search, options, parse_results(stdout)
  else
    raise "ag command failed: #{stdout} #{stderr}"
    # raise "ag command failed with status #{$?.exitstatus.inspect}: #{stdout}"
  end

end
command_bin() click to toggle source
# File lib/set_builder_adapter/ag.rb, line 88
def command_bin
  "ag"
end
command_options(options) click to toggle source
# File lib/set_builder_adapter/ag.rb, line 92
def command_options(options)
  %W(--search-files -C#{$CONTEXT_LINES} --numbers --column --nogroup --literal) + map_external_options(options)
end
command_parts(search_term, options) click to toggle source
# File lib/set_builder_adapter/ag.rb, line 102
def command_parts(search_term, options)
  [ command_bin, *command_options(options), "--", search_term ]
end
map_external_options(options) click to toggle source
# File lib/set_builder_adapter/ag.rb, line 96
def map_external_options(options)
  [].tap do |ary|
    ary << "--word-regexp" if options["whole_word"]
  end
end
parse_results(results) click to toggle source
# File lib/set_builder_adapter/ag.rb, line 106
def parse_results(results)
  debug_message "Ag Results:\n#{results}"
  Parser.new(results).parse
rescue ParserError => e
  STDERR.puts e
  raise e
end