Class: RubynCode::Tools::Grep

Inherits:
Base
  • Object
show all
Defined in:
lib/rubyn_code/tools/grep.rb

Constant Summary collapse

TOOL_NAME =
'grep'
DESCRIPTION =
'Searches file contents using regular expressions. ' \
'Returns matching lines with file paths and line numbers.'
PARAMETERS =
{
  pattern: { type: :string, required: true, description: 'Regular expression pattern to search for' },
  path: { type: :string, required: false,
          description: 'File or directory to search in (defaults to project root)' },
  glob_filter: { type: :string, required: false, description: "Glob pattern to filter files (e.g. '*.rb')" },
  max_results: { type: :integer, required: false, default: 50,
                 description: 'Maximum number of matching lines to return' }
}.freeze
RISK_LEVEL =
:read
REQUIRES_CONFIRMATION =
false

Instance Attribute Summary

Attributes inherited from Base

#project_root

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

description, #initialize, parameters, requires_confirmation?, risk_level, #safe_path, to_schema, tool_name, #truncate

Constructor Details

This class inherits a constructor from RubynCode::Tools::Base

Class Method Details

.summarize(output, args) ⇒ Object



23
24
25
26
27
# File 'lib/rubyn_code/tools/grep.rb', line 23

def self.summarize(output, args)
  pattern = args['pattern'] || args[:pattern] || ''
  count = output.to_s.lines.count
  count.zero? || output.to_s.start_with?('No matches') ? "grep #{pattern} (0 matches)" : "grep #{pattern} (#{count} lines)"
end

Instance Method Details

#execute(pattern:, path: nil, glob_filter: nil, max_results: 50) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubyn_code/tools/grep.rb', line 29

def execute(pattern:, path: nil, glob_filter: nil, max_results: 50)
  search_path = path ? safe_path(path) : project_root
  regex = Regexp.new(pattern)

  files = collect_files(search_path, glob_filter)
  results = []

  files.each do |file|
    break if results.length >= max_results

    search_file(file, regex, results, max_results)
  end

  return "No matches found for pattern: #{pattern}" if results.empty?

  results.join("\n")
end