Class: SetBuilderAdapter::Ag

Inherits:
Object
  • Object
show all
Defined in:
lib/set_builder_adapter/ag.rb

Defined Under Namespace

Classes: Parser, ParserError

Instance Method Summary collapse

Instance Method Details

#build_working_set(search, options) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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_binObject



88
89
90
# File 'lib/set_builder_adapter/ag.rb', line 88

def command_bin
  "ag"
end

#command_options(options) ⇒ Object



92
93
94
# 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) ⇒ Object



102
103
104
# 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) ⇒ Object



96
97
98
99
100
# 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) ⇒ Object



106
107
108
109
110
111
112
# 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