Class: Solargraph::Pin::Method

Inherits:
Closure show all
Includes:
(rubyvm? ? Rubyvm::NodeMethods : Legacy(rubyvm? ? Rubyvm::NodeMethods : Legacy::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 Closure

#scope

Attributes inherited from Base

#code_object, #location, #name, #source

Attributes included from Common

#closure, #location

Instance Method Summary collapse

Methods inherited from Closure

#binder, #context, #gates

Methods inherited from Base

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

Methods included from Conversions

#completion_item, #link_documentation, #reset_conversions, #resolve_completion_item, #text_documentation

Methods included from Common

#binder, #comments, #context, #name, #namespace

Constructor Details

#initialize(visibility: :public, explicit: true, parameters: [], 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)
  • parameters (Array<Pin::Parameter>) (defaults to: [])
  • node (Parser::AST::Node, RubyVM::AbstractSyntaxTree::Node, nil) (defaults to: nil)
  • attribute (Boolean) (defaults to: false)


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

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

Instance Attribute Details

#nodeParser::AST::Node (readonly)

Returns:

  • (Parser::AST::Node)


17
18
19
# File 'lib/solargraph/pin/method.rb', line 17

def node
  @node
end

#parametersArray<Pin::Parameter> (readonly)

Returns:



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

def parameters
  @parameters
end

#visibility::Symbol (readonly)

Returns :public, :private, or :protected.

Returns:

  • (::Symbol)

    :public, :private, or :protected



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

def visibility
  @visibility
end

Instance Method Details

#anon_splat?Boolean

Returns:

  • (Boolean)


242
243
244
# File 'lib/solargraph/pin/method.rb', line 242

def anon_splat?
  @anon_splat
end

#attribute?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/solargraph/pin/method.rb', line 199

def attribute?
  @attribute
end

#completion_item_kindObject



40
41
42
# File 'lib/solargraph/pin/method.rb', line 40

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

#detailString

Returns:

  • (String)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/solargraph/pin/method.rb', line 96

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

#documentationObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/solargraph/pin/method.rb', line 133

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

#explicit?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/solargraph/pin/method.rb', line 195

def explicit?
  @explicit
end

#generate_signature(parameters, return_type) ⇒ Signature

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/solargraph/pin/method.rb', line 53

def generate_signature(parameters, return_type)
  block = nil
  yieldparam_tags = docstring.tags(:yieldparam)
  yieldreturn_tags = docstring.tags(:yieldreturn)
  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(yield_parameters, yield_return_type)
  end
  Signature.new(parameters, return_type, block)
end

#nearly?(other) ⇒ Boolean

Returns:

  • (Boolean)


203
204
205
206
207
208
# File 'lib/solargraph/pin/method.rb', line 203

def nearly? other
  return false unless super
  parameters == other.parameters and
    scope == other.scope and
    visibility == other.visibility
end

#overloadsArray<Pin::Method>

Returns:



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/solargraph/pin/method.rb', line 221

def overloads
  @overloads ||= docstring.tags(:overload).map do |tag|
    Pin::Signature.new(
      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,
      ComplexType.try_parse(*tag.docstring.tags(:return).flat_map(&:types))
    )
  end
  @overloads
end

#parameter_namesArray<String>

Returns:

  • (Array<String>)


36
37
38
# File 'lib/solargraph/pin/method.rb', line 36

def parameter_names
  @parameter_names ||= parameters.map(&:name)
end

#pathObject



121
122
123
# File 'lib/solargraph/pin/method.rb', line 121

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

#probe(api_map) ⇒ Object



210
211
212
# File 'lib/solargraph/pin/method.rb', line 210

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

#return_typeObject



48
49
50
# File 'lib/solargraph/pin/method.rb', line 48

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

#signature_helpArray<Hash>

Returns:

  • (Array<Hash>)


112
113
114
115
116
117
118
119
# File 'lib/solargraph/pin/method.rb', line 112

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

#signaturesArray<Signature>

Returns:



84
85
86
87
88
89
90
91
92
93
# File 'lib/solargraph/pin/method.rb', line 84

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, top_type) if result.empty?
    result
  end
end

#symbol_kindObject



44
45
46
# File 'lib/solargraph/pin/method.rb', line 44

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

#try_merge!(pin) ⇒ Object



214
215
216
217
218
# File 'lib/solargraph/pin/method.rb', line 214

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

#typify(api_map) ⇒ Object



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

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