Class: Solargraph::Pin::Parameter

Inherits:
LocalVariable show all
Defined in:
lib/solargraph/pin/parameter.rb

Constant Summary

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary collapse

Attributes inherited from LocalVariable

#presence

Attributes inherited from BaseVariable

#assignment, #mass_assignment

Attributes inherited from Base

#code_object, #directives, #docstring, #name, #path, #source

Attributes included from Common

#closure, #context

Instance Method Summary collapse

Methods inherited from LocalVariable

#presence_certain?, #visible_at?

Methods inherited from BaseVariable

#==, #completion_item_kind, #nil_assignment?, #probe, #return_types_from_node, #symbol_kind, #type_desc, #variable?

Methods inherited from Base

#==, #all_location_text, #all_rooted?, #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, #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?, #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, #variable?

Methods included from Logging

logger

Methods included from Documenting

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

#binder, #comments, #name, #namespace, #path

Constructor Details

#initialize(decl: :arg, asgn_code: nil, **splat) ⇒ Parameter

Returns a new instance of Parameter.

Parameters:

  • decl (::Symbol) (defaults to: :arg)

    :arg, :optarg, :kwarg, :kwoptarg, :restarg, :kwrestarg, :block, :blockarg

  • asgn_code (String, nil) (defaults to: nil)


18
19
20
21
22
# File 'lib/solargraph/pin/parameter.rb', line 18

def initialize decl: :arg, asgn_code: nil, **splat
  super(**splat)
  @asgn_code = asgn_code
  @decl = decl
end

Instance Attribute Details

#asgn_codeString (readonly)

Returns:

  • (String)


10
11
12
# File 'lib/solargraph/pin/parameter.rb', line 10

def asgn_code
  @asgn_code
end

#closure=(value) ⇒ Object (writeonly)

allow this to be set to the method after the method itself has been created



14
15
16
# File 'lib/solargraph/pin/parameter.rb', line 14

def closure=(value)
  @closure = value
end

#decl::Symbol (readonly)

Returns:

  • (::Symbol)


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

def decl
  @decl
end

Instance Method Details

#arg?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/solargraph/pin/parameter.rb', line 75

def arg?
  decl == :arg
end

#arity_declString

Returns:

  • (String)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/solargraph/pin/parameter.rb', line 54

def arity_decl
  name = (self.name || '(anon)')
  type = (return_type&.to_rbs || 'untyped')
  case decl
  when :arg
    ""
  when :optarg
    "?"
  when :kwarg
    "#{name}:"
  when :kwoptarg
    "?#{name}:"
  when :restarg
    "*"
  when :kwrestarg
    "**"
  else
    "(unknown decl: #{decl})"
  end
end

#block?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/solargraph/pin/parameter.rb', line 87

def block?
  [:block, :blockarg].include?(decl)
end

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



32
33
34
35
36
37
38
39
# File 'lib/solargraph/pin/parameter.rb', line 32

def combine_with(other, attrs={})
  new_attrs = {
    decl: assert_same(other, :decl),
    presence: choose(other, :presence),
    asgn_code: choose(other, :asgn_code),
  }.merge(attrs)
  super(other, new_attrs)
end

#compatible_arg?(atype, api_map) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


165
166
167
168
169
170
# File 'lib/solargraph/pin/parameter.rb', line 165

def compatible_arg?(atype, api_map)
  # make sure we get types from up the method

  # inheritance chain if we don't have them on this pin

  ptype = typify api_map
  ptype.undefined? || ptype.can_assign?(api_map, atype) || ptype.generic?
end

#documentationObject



172
173
174
175
176
# File 'lib/solargraph/pin/parameter.rb', line 172

def documentation
  tag = param_tag
  return '' if tag.nil? || tag.text.nil?
  tag.text
end

#fullString

Returns:

  • (String)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/solargraph/pin/parameter.rb', line 109

def full
  case decl
  when :optarg
    "#{name} = #{asgn_code || '?'}"
  when :kwarg
    "#{name}:"
  when :kwoptarg
    "#{name}: #{asgn_code || '?'}"
  when :restarg
    "*#{name}"
  when :kwrestarg
    "**#{name}"
  when :block, :blockarg
    "&#{name}"
  else
    name
  end
end

#indexInteger

The parameter’s zero-based location in the block’s signature.

Returns:

  • (Integer)


151
152
153
154
155
# File 'lib/solargraph/pin/parameter.rb', line 151

def index
  # @type [Method, Block]

  method_pin = closure
  method_pin.parameter_names.index(name)
end

#keyword?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/solargraph/pin/parameter.rb', line 41

def keyword?
  [:kwarg, :kwoptarg].include?(decl)
end

#kwrestarg?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/solargraph/pin/parameter.rb', line 45

def kwrestarg?
  decl == :kwrestarg || (assignment && [:HASH, :hash].include?(assignment.type))
end

#locationObject



28
29
30
# File 'lib/solargraph/pin/parameter.rb', line 28

def location
  super || closure&.type_location
end

#needs_consistent_name?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/solargraph/pin/parameter.rb', line 49

def needs_consistent_name?
  keyword?
end

#rest?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/solargraph/pin/parameter.rb', line 83

def rest?
  decl == :restarg || decl == :kwrestarg
end

#restarg?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/solargraph/pin/parameter.rb', line 79

def restarg?
  decl == :restarg
end

#return_typeComplexType

Returns:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/solargraph/pin/parameter.rb', line 129

def return_type
  if @return_type.nil?
    @return_type = ComplexType::UNDEFINED
    found = param_tag
    @return_type = ComplexType.try_parse(*found.types) unless found.nil? or found.types.nil?
    if @return_type.undefined?
      if decl == :restarg
        @return_type = ComplexType.try_parse('::Array')
      elsif decl == :kwrestarg
        @return_type = ComplexType.try_parse('::Hash')
      elsif decl == :blockarg
        @return_type = ComplexType.try_parse('::Proc')
      end
    end
  end
  super
  @return_type
end

#to_rbsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/solargraph/pin/parameter.rb', line 91

def to_rbs
  case decl
  when :optarg
    "?#{super}"
  when :kwarg
    "#{name}: #{return_type.to_rbs}"
  when :kwoptarg
    "?#{name}: #{return_type.to_rbs}"
  when :restarg
    "*#{super}"
  when :kwrestarg
    "**#{super}"
  else
    super
  end
end

#type_locationObject



24
25
26
# File 'lib/solargraph/pin/parameter.rb', line 24

def type_location
  super || closure&.type_location
end

#typify(api_map) ⇒ Object

Parameters:



158
159
160
161
# File 'lib/solargraph/pin/parameter.rb', line 158

def typify api_map
  return return_type.qualify(api_map, closure.context.namespace) unless return_type.undefined?
  closure.is_a?(Pin::Block) ? typify_block_param(api_map) : typify_method_param(api_map)
end