Class: Aiko::Tools::Search

Inherits:
Base
  • Object
show all
Defined in:
lib/aiko/tools/search.rb

Constant Summary collapse

MAX_MATCHES =
200

Constants inherited from Base

Base::OUTPUT_LIMIT

Instance Method Summary collapse

Methods inherited from Base

#approval_message, #initialize, #requires_approval?

Constructor Details

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

Instance Method Details

#call(arguments) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/aiko/tools/search.rb', line 39

def call(arguments)
  regexp = begin
    Regexp.new(arguments.fetch("pattern"))
  rescue RegexpError => e
    return "Error: invalid regexp: #{e.message}"
  end

  base = resolve_path(arguments["path"] || ".")
  return "Error: no such directory: #{arguments["path"]}" unless File.directory?(base)

  matches = search_files(base, regexp, arguments["glob"])
  return "(no matches)" if matches.empty?

  truncated = matches.size > MAX_MATCHES
  matches = matches.first(MAX_MATCHES)
  matches << "[#{MAX_MATCHES}マッチを超えたため省略しました]" if truncated
  truncate(matches.join("\n"))
end

#descriptionObject



12
13
14
15
16
# File 'lib/aiko/tools/search.rb', line 12

def description
  "Search file contents with a Ruby regular expression (like grep). " \
    "Returns matching lines as 'path:line_number: line'. " \
    "Optionally restrict to a subdirectory and/or a filename glob like '*.rb'."
end

#input_schemaObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/aiko/tools/search.rb', line 18

def input_schema
  {
    "type" => "object",
    "properties" => {
      "pattern" => {
        "type" => "string",
        "description" => "Ruby regular expression to search for"
      },
      "path" => {
        "type" => "string",
        "description" => "Directory to search, relative to the working directory (default: working directory)"
      },
      "glob" => {
        "type" => "string",
        "description" => "Filename glob filter, e.g. '*.rb'"
      }
    },
    "required" => ["pattern"]
  }
end

#nameObject



8
9
10
# File 'lib/aiko/tools/search.rb', line 8

def name
  "search"
end