Class: Solargraph::Pin::Parameter

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

Instance Attribute Summary collapse

Attributes inherited from LocalVariable

#presence

Attributes inherited from BaseVariable

#assignment

Attributes inherited from Base

#code_object, #location, #name, #path, #source

Attributes included from Common

#closure, #context, #location

Instance Method Summary collapse

Methods inherited from LocalVariable

#visible_at?

Methods inherited from BaseVariable

#==, #completion_item_kind, #desc, #nil_assignment?, #probe, #symbol_kind, #variable?

Methods inherited from Base

#==, #comments, #completion_item_kind, #deprecated?, #desc, #directives, #docstring, #erase_generics, #filename, #identity, #infer, #inspect, #macros, #maybe_directives?, #nearly?, #probe, #probed?, #proxied?, #proxy, #realize, #resolve_generics, #resolve_generics_from_context, #symbol_kind, #to_s, #transform_types, #variable?

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, return_type: 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)
  • return_type (ComplexType, nil) (defaults to: nil)


15
16
17
18
19
20
# File 'lib/solargraph/pin/parameter.rb', line 15

def initialize decl: :arg, asgn_code: nil, return_type: nil, **splat
  super(**splat)
  @asgn_code = asgn_code
  @decl = decl
  @return_type = return_type
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

#declSymbol (readonly)

Returns:



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

def decl
  @decl
end

Instance Method Details

#block?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/solargraph/pin/parameter.rb', line 38

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

#documentationObject



111
112
113
114
115
# File 'lib/solargraph/pin/parameter.rb', line 111

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

#fullString

Returns:

  • (String)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/solargraph/pin/parameter.rb', line 60

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)


101
102
103
# File 'lib/solargraph/pin/parameter.rb', line 101

def index
  closure.parameter_names.index(name)
end

#keyword?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/solargraph/pin/parameter.rb', line 22

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

#kwrestarg?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/solargraph/pin/parameter.rb', line 26

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

#rest?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/solargraph/pin/parameter.rb', line 34

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

#restarg?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/solargraph/pin/parameter.rb', line 30

def restarg?
  decl == :restarg
end

#return_typeObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/solargraph/pin/parameter.rb', line 79

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/solargraph/pin/parameter.rb', line 42

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

#try_merge!(pin) ⇒ Object



117
118
119
120
# File 'lib/solargraph/pin/parameter.rb', line 117

def try_merge! pin
  return false unless super && closure == pin.closure
  true
end

#typify(api_map) ⇒ Object

Parameters:



106
107
108
109
# File 'lib/solargraph/pin/parameter.rb', line 106

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