Class: Docscribe::Types::RBS::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/docscribe/types/rbs/provider.rb

Overview

Resolve method signatures from ‘.rbs` files using the official RBS environment and definition builder APIs.

The provider returns Docscribe’s normalized signature model so the rest of the pipeline can stay independent of the underlying signature source.

Instance Method Summary collapse

Constructor Details

#initialize(sig_dirs:, collection_dirs: [], collapse_generics: false) ⇒ Object

Parameters:

  • sig_dirs (Array<String>)

    directories containing ‘.rbs` files

  • collection_dirs (Array<String>) (defaults to: [])

    RBS collection directories (loaded separately; on error they are silently dropped and only user sig_dirs are used)

  • collapse_generics (Boolean) (defaults to: false)

    whether generic container types should be simplified during formatting



24
25
26
27
28
29
30
31
32
33
# File 'lib/docscribe/types/rbs/provider.rb', line 24

def initialize(sig_dirs:, collection_dirs: [], collapse_generics: false)
  require 'rbs'
  @sig_dirs = Array(sig_dirs).map(&:to_s)
  @collection_dirs = Array(collection_dirs).map(&:to_s)
  @collapse_generics = !!collapse_generics
  @env = nil
  @builder = nil
  @warned = false
  @collection_dropped = false
end

Instance Method Details

#signature_for(container:, scope:, name:) ⇒ Docscribe::Types::MethodSignature?

Look up a normalized method signature from loaded RBS definitions.

Returns nil when the method cannot be resolved or when RBS lookup fails.

Parameters:

  • container (String)

    e.g. “MyModule::MyClass”

  • scope (Symbol)

    :instance or :class

  • name (Symbol, String)

    method name

Returns:

Raises:

  • (::RBS::BaseError)
  • (StandardError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/docscribe/types/rbs/provider.rb', line 45

def signature_for(container:, scope:, name:)
  load_env!

  definition = definition_for(container: container, scope: scope)
  method_def = definition.methods[name.to_sym]
  return nil unless method_def

  method_type = method_def.method_types.first
  return nil unless method_type

  func = method_type.type
  build_signature(func)
rescue ::RBS::BaseError => e
  warn_once("Docscribe: RBS error: #{e.class}: #{e.message}")
  nil
rescue StandardError => e
  warn_once(
    'Docscribe: RBS integration failed (falling back to inference): ' \
    "#{e.class}: #{e.message}\nFeel free to open an issue on github."
  )
  nil
end