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

DuckMethod, MethodAlias

Instance Attribute Summary collapse

Attributes inherited from Closure

#scope

Attributes inherited from Base

#code_object, #location, #name

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, #detail, #link_documentation, #reset_conversions, #resolve_completion_item, #signature_help, #text_documentation

Methods included from Common

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

Constructor Details

#initialize(visibility: :public, explicit: true, parameters: [], node: nil, attribute: false, **splat) ⇒ Method

Returns a new instance of Method.

Parameters:

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

    :public, :protected, or :private

  • explicit (Boolean) (defaults to: true)


21
22
23
24
25
26
27
28
# File 'lib/solargraph/pin/method.rb', line 21

def initialize visibility: :public, explicit: true, parameters: [], node: nil, attribute: false, **splat
  super(**splat)
  @visibility = visibility
  @explicit = explicit
  @parameters = parameters
  @node = node
  @attribute = attribute
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

#attribute?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/solargraph/pin/method.rb', line 98

def attribute?
  @attribute
end

#completion_item_kindObject



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

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

#documentationObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/solargraph/pin/method.rb', line 59

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
    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}"
  end
  @documentation.to_s
end

#explicit?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/solargraph/pin/method.rb', line 94

def explicit?
  @explicit
end

#nearly?(other) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
# File 'lib/solargraph/pin/method.rb', line 102

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

#overloadsArray<Pin::Method>

Returns:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/solargraph/pin/method.rb', line 120

def overloads
  @overloads ||= docstring.tags(:overload).map do |tag|
    Solargraph::Pin::Method.new(
      name: name,
      closure: self,
      # args: tag.parameters.map(&:first),
      parameters: tag.parameters.map do |src|
        Pin::Parameter.new(
          location: location,
          closure: self,
          comments: tag.docstring.all.to_s,
          name: src.first,
          presence: location ? location.range : nil,
          decl: :arg
        )
      end,
      comments: tag.docstring.all.to_s
    )
  end
end

#parameter_namesArray<String>

Returns:



31
32
33
# File 'lib/solargraph/pin/method.rb', line 31

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

#pathObject



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

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

#probe(api_map) ⇒ Object



109
110
111
# File 'lib/solargraph/pin/method.rb', line 109

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

#return_typeObject



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

def return_type
  @return_type ||= generate_complex_type
end

#symbol_kindObject



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

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

#try_merge!(pin) ⇒ Object



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

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

#typify(api_map) ⇒ Object



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

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