Class: Solargraph::Pin::Closure

Inherits:
Base
  • Object
show all
Defined in:
lib/solargraph/pin/closure.rb

Direct Known Subclasses

Callable, Namespace, Singleton

Constant Summary

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary collapse

Attributes inherited from Base

#code_object, #combine_priority, #directives, #docstring, #location, #name, #path, #return_type, #source, #type_location

Attributes included from Common

#location

Instance Method Summary collapse

Methods inherited from Base

#==, #all_location_text, #all_rooted?, #assert_location_provided, #assert_same, #assert_same_array_content, #assert_same_count, #assert_same_macros, #assert_source_provided, #best_location, #choose, #choose_longer, #choose_node, #choose_pin_attr, #choose_pin_attr_with_same_name, #choose_priority, #closure, #combine_directives, #combine_name, #combine_return_type, #comments, #completion_item_kind, #deprecated?, #desc, #dodgy_return_type_source?, #erase_generics, #filename, #identity, #infer, #inner_desc, #inspect, #macros, #maybe_directives?, #nearly?, #needs_consistent_name?, #prefer_rbs_location, #presence_certain?, #probe, #probed?, #proxied?, #proxy, #rbs_location?, #realize, #reset_generated!, #resolve_generics, #resolve_generics_from_context, #symbol_kind, #to_s, #transform_types, #type_desc, #typify, #variable?

Methods included from Logging

logger

Methods included from Documenting

#documentation, normalize_indentation, strip_html_comments

Methods included from Conversions

#completion_item, #completion_item_kind, #deprecated?, #detail, #link_documentation, #probed?, #proxied?, #reset_conversions, #resolve_completion_item, #signature_help, #text_documentation

Methods included from Common

#closure, #comments, #name, #namespace, #path, #return_type, #source

Constructor Details

#initialize(scope: :class, generics: nil, generic_defaults: {}, **splat) ⇒ Closure

Returns a new instance of Closure.

Parameters:

  • scope (::Symbol) (defaults to: :class)

    :class or :instance

  • generics (::Array<Pin::Parameter>, nil) (defaults to: nil)
  • generic_defaults (Hash{String => ComplexType}) (defaults to: {})


12
13
14
15
16
17
# File 'lib/solargraph/pin/closure.rb', line 12

def initialize scope: :class, generics: nil, generic_defaults: {},  **splat
  super(**splat)
  @scope = scope
  @generics = generics
  @generic_defaults = generic_defaults
end

Instance Attribute Details

#scope::Symbol (readonly)

Returns :class or :instance.

Returns:

  • (::Symbol)

    :class or :instance



7
8
9
# File 'lib/solargraph/pin/closure.rb', line 7

def scope
  @scope
end

Instance Method Details

#binderObject



47
48
49
# File 'lib/solargraph/pin/closure.rb', line 47

def binder
  @binder || context
end

#combine_with(other, attrs = {}) ⇒ self

Parameters:

  • other (self)
  • attrs (Hash{Symbol => Object}) (defaults to: {})

Returns:

  • (self)


28
29
30
31
32
33
34
# File 'lib/solargraph/pin/closure.rb', line 28

def combine_with(other, attrs={})
  new_attrs = {
    scope: assert_same(other, :scope),
    generics: generics.empty? ? other.generics : generics,
  }.merge(attrs)
  super(other, new_attrs)
end

#contextObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/solargraph/pin/closure.rb', line 36

def context
  @context ||= begin
    result = super
    if scope == :instance
      result.reduce_class_type
    else
      result
    end
  end
end

#gates::Array<String>

Returns:

  • (::Array<String>)


52
53
54
55
56
# File 'lib/solargraph/pin/closure.rb', line 52

def gates
  # @todo This check might not be necessary. There should always be a
  #   root pin
  closure ? closure.gates : ['']
end

#generic_defaultsHash{String => ComplexType}

Returns:



20
21
22
# File 'lib/solargraph/pin/closure.rb', line 20

def generic_defaults
  @generic_defaults ||= {}
end

#generics::Array<String>

Returns:

  • (::Array<String>)


59
60
61
# File 'lib/solargraph/pin/closure.rb', line 59

def generics
  @generics ||= docstring.tags(:generic).map(&:name)
end

#rbs_genericsString

Returns:

  • (String)


69
70
71
72
73
# File 'lib/solargraph/pin/closure.rb', line 69

def rbs_generics
  return '' if generics.empty?

  '[' + generics.map { |gen| gen.to_s }.join(', ') + '] '
end

#to_rbsString

Returns:

  • (String)


64
65
66
# File 'lib/solargraph/pin/closure.rb', line 64

def to_rbs
  rbs_generics + return_type.to_rbs
end