Class: Solargraph::Pin::Method

Inherits:
Callable show all
Includes:
ParserGem::NodeMethods
Defined in:
lib/solargraph/pin/method.rb

Overview

The base class for method and attribute pins.

Direct Known Subclasses

DelegatedMethod, DuckMethod, MethodAlias

Instance Attribute Summary collapse

Attributes inherited from Callable

#parameters

Attributes inherited from Closure

#scope

Attributes inherited from Base

#code_object, #location, #name, #source, #type_location

Attributes included from Common

#closure, #context, #location

Instance Method Summary collapse

Methods inherited from Callable

#arity_matches?, #mandatory_positional_param_count, #parameter_names, #resolve_generics_from_context, #resolve_generics_from_context_until_complete

Methods inherited from Closure

#binder, #context, #gates, #generics, #rbs_generics

Methods inherited from Base

#==, #best_location, #comments, #deprecated?, #directives, #docstring, #erase_generics, #filename, #identity, #infer, #inspect, #macros, #maybe_directives?, #probed?, #proxied?, #proxy, #realize, #resolve_generics, #resolve_generics_from_context, #to_s, #variable?

Methods included from Documenting

normalize_indentation, strip_html_comments

Methods included from Conversions

#completion_item, #deprecated?, #link_documentation, #probed?, #proxied?, #reset_conversions, #resolve_completion_item, #text_documentation

Methods included from Common

#binder, #comments, #name, #namespace

Constructor Details

#initialize(visibility: :public, explicit: true, block: :undefined, node: nil, attribute: false, signatures: nil, anon_splat: false, **splat) ⇒ Method

Returns a new instance of Method.

Parameters:

  • visibility (::Symbol) (defaults to: :public)

    :public, :protected, or :private

  • explicit (Boolean) (defaults to: true)
  • block (Pin::Signature, nil, ::Symbol) (defaults to: :undefined)
  • node (Parser::AST::Node, nil) (defaults to: nil)
  • attribute (Boolean) (defaults to: false)
  • signatures (::Array<Signature>, nil) (defaults to: nil)
  • anon_splat (Boolean) (defaults to: false)


23
24
25
26
27
28
29
30
31
32
# File 'lib/solargraph/pin/method.rb', line 23

def initialize visibility: :public, explicit: true, block: :undefined, node: nil, attribute: false, signatures: nil, anon_splat: false, **splat
  super(**splat)
  @visibility = visibility
  @explicit = explicit
  @block = block
  @node = node
  @attribute = attribute
  @signatures = signatures
  @anon_splat = anon_splat
end

Instance Attribute Details

#blockPin::Signature?

Returns:



73
74
75
76
# File 'lib/solargraph/pin/method.rb', line 73

def block
  return @block unless @block == :undefined
  @block = signatures.first&.block
end

#documentationObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/solargraph/pin/method.rb', line 195

def documentation
  if @documentation.nil?
    @documentation ||= super || ''
    param_tags = docstring.tags(:param)
    unless param_tags.nil? or param_tags.empty?
      @documentation += "\n\n" unless @documentation.empty?
      @documentation += "Params:\n"
      lines = []
      param_tags.each do |p|
        l = "* #{p.name}"
        l += " [#{escape_brackets(p.types.join(', '))}]" unless p.types.nil? or p.types.empty?
        l += " #{p.text}"
        lines.push l
      end
      @documentation += lines.join("\n")
    end
    yieldparam_tags = docstring.tags(:yieldparam)
    unless yieldparam_tags.nil? or yieldparam_tags.empty?
      @documentation += "\n\n" unless @documentation.empty?
      @documentation += "Block Params:\n"
      lines = []
      yieldparam_tags.each do |p|
        l = "* #{p.name}"
        l += " [#{escape_brackets(p.types.join(', '))}]" unless p.types.nil? or p.types.empty?
        l += " #{p.text}"
        lines.push l
      end
      @documentation += lines.join("\n")
    end
    yieldreturn_tags = docstring.tags(:yieldreturn)
    unless yieldreturn_tags.empty?
      @documentation += "\n\n" unless @documentation.empty?
      @documentation += "Block Returns:\n"
      lines = []
      yieldreturn_tags.each do |r|
        l = "*"
        l += " [#{escape_brackets(r.types.join(', '))}]" unless r.types.nil? or r.types.empty?
        l += " #{r.text}"
        lines.push l
      end
      @documentation += lines.join("\n")
    end
    return_tags = docstring.tags(:return)
    unless return_tags.empty?
      @documentation += "\n\n" unless @documentation.empty?
      @documentation += "Returns:\n"
      lines = []
      return_tags.each do |r|
        l = "*"
        l += " [#{escape_brackets(r.types.join(', '))}]" unless r.types.nil? or r.types.empty?
        l += " #{r.text}"
        lines.push l
      end
      @documentation += lines.join("\n")
    end
    @documentation += "\n\n" unless @documentation.empty?
    @documentation += "Visibility: #{visibility}"
    concat_example_tags
  end
  @documentation.to_s
end

#nodeParser::AST::Node (readonly)

Returns:

  • (Parser::AST::Node)


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

def node
  @node
end

#signature_help::Array<Hash>

Returns:

  • (::Array<Hash>)


153
154
155
156
157
158
159
160
# File 'lib/solargraph/pin/method.rb', line 153

def signature_help
  @signature_help ||= signatures.map do |sig|
    {
      label: name + '(' + sig.parameters.map(&:full).join(', ') + ')',
      documentation: documentation
    }
  end
end

#signatures::Array<Signature>

Returns:



125
126
127
128
129
130
131
132
133
134
# File 'lib/solargraph/pin/method.rb', line 125

def signatures
  @signatures ||= begin
    top_type = generate_complex_type
    result = []
    result.push generate_signature(parameters, top_type) if top_type.defined?
    result.concat(overloads.map { |meth| generate_signature(meth.parameters, meth.return_type) }) unless overloads.empty?
    result.push generate_signature(parameters, @return_type || ComplexType::UNDEFINED) if result.empty?
    result
  end
end

#visibility::Symbol (readonly)

Returns :public, :private, or :protected.

Returns:

  • (::Symbol)

    :public, :private, or :protected



11
12
13
# File 'lib/solargraph/pin/method.rb', line 11

def visibility
  @visibility
end

Instance Method Details

#all_rooted?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/solargraph/pin/method.rb', line 46

def all_rooted?
  super && parameters.all?(&:all_rooted?) && (!block || block&.all_rooted?) && signatures.all?(&:all_rooted?)
end

#anon_splat?Boolean

Returns:

  • (Boolean)


308
309
310
# File 'lib/solargraph/pin/method.rb', line 308

def anon_splat?
  @anon_splat
end

#attribute?Boolean

Returns:

  • (Boolean)


261
262
263
# File 'lib/solargraph/pin/method.rb', line 261

def attribute?
  @attribute
end

#block?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/solargraph/pin/method.rb', line 68

def block?
  !block.nil?
end

#completion_item_kindObject



78
79
80
# File 'lib/solargraph/pin/method.rb', line 78

def completion_item_kind
  attribute? ? Solargraph::LanguageServer::CompletionItemKinds::PROPERTY : Solargraph::LanguageServer::CompletionItemKinds::METHOD
end

#descObject



162
163
164
165
166
167
168
169
# File 'lib/solargraph/pin/method.rb', line 162

def desc
  # ensure the signatures line up when logged
  if signatures.length > 1
    "\n#{to_rbs}\n"
  else
    to_rbs
  end
end

#detailString?

Returns:

  • (String, nil)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/solargraph/pin/method.rb', line 137

def detail
  # This property is not cached in an instance variable because it can
  # change when pins get proxied.
  detail = String.new
  detail += if signatures.length > 1
    "(*) "
  else
    "(#{signatures.first.parameters.map(&:full).join(', ')}) " unless signatures.first.parameters.empty?
  end.to_s
  detail += "=#{probed? ? '~' : (proxied? ? '^' : '>')} #{return_type.to_s}" unless return_type.undefined?
  detail.strip!
  return nil if detail.empty?
  detail
end

#explicit?Boolean

Returns:

  • (Boolean)


257
258
259
# File 'lib/solargraph/pin/method.rb', line 257

def explicit?
  @explicit
end

#generate_signature(parameters, return_type) ⇒ Signature

Parameters:

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/solargraph/pin/method.rb', line 93

def generate_signature(parameters, return_type)
  block = nil
  yieldparam_tags = docstring.tags(:yieldparam)
  yieldreturn_tags = docstring.tags(:yieldreturn)
  generics = docstring.tags(:generic).map(&:name)
  needs_block_param_signature =
    parameters.last&.block? || !yieldreturn_tags.empty? || !yieldparam_tags.empty?
  if needs_block_param_signature
    yield_parameters = yieldparam_tags.map do |p|
      name = p.name
      decl = :arg
      if name
        decl = select_decl(name, false)
        name = clean_param(name)
      end
      Pin::Parameter.new(
        location: location,
        closure: self,
        comments: p.text,
        name: name,
        decl: decl,
        presence: location ? location.range : nil,
        return_type: ComplexType.try_parse(*p.types)
      )
    end
    yield_return_type = ComplexType.try_parse(*yieldreturn_tags.flat_map(&:types))
    block = Signature.new(generics: generics, parameters: yield_parameters, return_type: yield_return_type)
  end
  Signature.new(generics: generics, parameters: parameters, return_type: return_type, block: block)
end

#nearly?(other) ⇒ Boolean

Returns:

  • (Boolean)


265
266
267
268
269
270
# File 'lib/solargraph/pin/method.rb', line 265

def nearly? other
  super &&
    parameters == other.parameters &&
    scope == other.scope &&
    visibility == other.visibility
end

#overloads::Array<Pin::Method>

Returns:



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/solargraph/pin/method.rb', line 284

def overloads
  # Ignore overload tags with nil parameters. If it's not an array, the
  # tag's source is likely malformed.
  @overloads ||= docstring.tags(:overload).select(&:parameters).map do |tag|
    Pin::Signature.new(
      generics: generics,
      parameters: tag.parameters.map do |src|
        name, decl = parse_overload_param(src.first)
        Pin::Parameter.new(
          location: location,
          closure: self,
          comments: tag.docstring.all.to_s,
          name: name,
          decl: decl,
          presence: location ? location.range : nil,
          return_type: param_type_from_name(tag, src.first)
        )
      end,
      return_type: ComplexType.try_parse(*tag.docstring.tags(:return).flat_map(&:types))
    )
  end
  @overloads
end

#pathObject



183
184
185
# File 'lib/solargraph/pin/method.rb', line 183

def path
  @path ||= "#{namespace}#{(scope == :instance ? '#' : '.')}#{name}"
end

#probe(api_map) ⇒ Object



272
273
274
# File 'lib/solargraph/pin/method.rb', line 272

def probe api_map
  attribute? ? infer_from_iv(api_map) : infer_from_return_nodes(api_map)
end

#resolve_ref_tag(api_map) ⇒ self

Parameters:

Returns:

  • (self)


314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/solargraph/pin/method.rb', line 314

def resolve_ref_tag api_map
  return self if @resolved_ref_tag

  @resolved_ref_tag = true
  return self unless docstring.ref_tags.any?
  docstring.ref_tags.each do |tag|
    ref = if tag.owner.to_s.start_with?(/[#\.]/)
      api_map.get_methods(namespace)
             .select { |pin| pin.path.end_with?(tag.owner.to_s) }
             .first
    else
      # @todo Resolve relative namespaces
      api_map.get_path_pins(tag.owner.to_s).first
    end
    next unless ref

    docstring.add_tag(*ref.docstring.tags(:param))
  end
  self
end

#return_typeObject



86
87
88
# File 'lib/solargraph/pin/method.rb', line 86

def return_type
  @return_type ||= ComplexType.new(signatures.map(&:return_type).flat_map(&:items))
end

#symbol_kindObject



82
83
84
# File 'lib/solargraph/pin/method.rb', line 82

def symbol_kind
  attribute? ? Solargraph::LanguageServer::SymbolKinds::PROPERTY : LanguageServer::SymbolKinds::METHOD
end

#to_rbsObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/solargraph/pin/method.rb', line 171

def to_rbs
  return nil if signatures.empty?

  rbs = "def #{name}: #{signatures.first.to_rbs}"
  signatures[1..].each do |sig|
    rbs += "\n"
    rbs += (' ' * (4 + name.length))
    rbs += "| #{name}: #{sig.to_rbs}"
  end
  rbs
end

#transform_types(&transform) ⇒ Object



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

def transform_types(&transform)
  # @todo 'super' alone should work here I think, but doesn't typecheck at level typed
  m = super(&transform)
  m.signatures = m.signatures.map do |sig|
    sig.transform_types(&transform)
  end
  m.block = block&.transform_types(&transform)
  m.signature_help = nil
  m.documentation = nil
  m
end

#try_merge!(pin) ⇒ Object



276
277
278
279
280
281
# File 'lib/solargraph/pin/method.rb', line 276

def try_merge! pin
  return false unless super
  @node = pin.node
  @resolved_ref_tag = false
  true
end

#typify(api_map) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/solargraph/pin/method.rb', line 187

def typify api_map
  decl = super
  return decl unless decl.undefined?
  type = see_reference(api_map) || typify_from_super(api_map)
  return type.qualify(api_map, namespace) unless type.nil?
  name.end_with?('?') ? ComplexType::BOOLEAN : ComplexType::UNDEFINED
end

#with_single_signature(signature) ⇒ Pin::Method

Parameters:

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/solargraph/pin/method.rb', line 52

def with_single_signature(signature)
  m = proxy signature.return_type
  m.signature_help = nil
  m.documentation = nil
  # @todo populating the single parameters/return_type/block
  #   arguments here seems to be needed for some specs to pass,
  #   even though we have a signature with the same information.
  #   Is this a problem for RBS-populated methods, which don't
  #   populate these three?
  m.parameters = signature.parameters
  m.return_type = signature.return_type
  m.block = signature.block
  m.signatures = [signature]
  m
end