Class: Tidewave::Tools::GetSourceLocation

Inherits:
Tidewave::Tool show all
Defined in:
lib/tidewave/tools/get_source_location.rb

Constant Summary collapse

DESCRIPTION =
<<~DESCRIPTION.freeze
  Returns the source location for the given reference.

  The reference may be a constant, most commonly classes and modules
  such as `String`, an instance method, such as `String#gsub`, or class
  method, such as `File.executable?`

  This tool only works if you know the specific constant/method being targeted,
  and it works across the current project and all dependencies.
  If that is the case, prefer this tool over grepping the file system.

  You may also get the root location of a gem, you can use "dep:PACKAGE_NAME".
DESCRIPTION

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tidewave::Tool

descendants, inherited, #validate_and_call

Constructor Details

#initialize(options = {}) ⇒ GetSourceLocation

Returns a new instance of GetSourceLocation.



20
21
22
# File 'lib/tidewave/tools/get_source_location.rb', line 20

def initialize(options = {})
  @root = options[:root] ? Pathname.new(options[:root].to_s) : Pathname.pwd
end

Class Method Details

.get_source_location(reference) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tidewave/tools/get_source_location.rb', line 55

def self.get_source_location(reference)
  constant_path, selector, method_name = reference.rpartition(/\.|#/)
  return Object.const_source_location(method_name) if selector.empty?

  begin
    mod = Object.const_get(constant_path)
  rescue NameError => error
    raise error
  rescue StandardError
    raise "wrong or invalid reference #{reference}"
  end

  raise "reference #{constant_path} does not point a class/module" unless mod.is_a?(Module)

  if selector == "#"
    mod.instance_method(method_name).source_location
  else
    mod.method(method_name).source_location
  end
end

Instance Method Details

#call(arguments) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tidewave/tools/get_source_location.rb', line 42

def call(arguments)
  reference = arguments.fetch("reference")

  if reference.start_with?("dep:")
    get_package_location(reference.delete_prefix("dep:"))
  else
    file_path, line_number = self.class.get_source_location(reference)
    raise NameError, "could not find source location for #{reference}" unless file_path

    format_source_location(file_path, line_number)
  end
end

#definitionObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tidewave/tools/get_source_location.rb', line 24

def definition
  {
    "name" => "get_source_location",
    "description" => DESCRIPTION,
    "inputSchema" => {
      "type" => "object",
      "properties" => {
        "reference" => {
          "type" => "string",
          "description" => "The constant/method to lookup, such String, String#gsub or File.executable?",
          "minLength" => 1
        }
      },
      "required" => [ "reference" ]
    }
  }
end