Class: Solargraph::Pin::Base

Inherits:
Object
  • Object
show all
Includes:
Common, Conversions, Documenting
Defined in:
lib/solargraph/pin/base.rb

Overview

The base class for map pins.

Direct Known Subclasses

BaseVariable, Closure, Keyword, ProxyType, Reference, Symbol

Instance Attribute Summary collapse

Attributes included from Common

#closure

Instance Method Summary collapse

Methods included from Documenting

#documentation

Methods included from Conversions

#completion_item, #detail, #link_documentation, #reset_conversions, #resolve_completion_item, #signature_help

Methods included from Common

#binder, #context, #namespace

Constructor Details

#initialize(location: nil, kind: KEYWORD, closure: nil, name: '', comments: '') ⇒ Base


30
31
32
33
34
35
36
# File 'lib/solargraph/pin/base.rb', line 30

def initialize location: nil, kind: KEYWORD, closure: nil, name: '', comments: ''
  @location = location
  @kind = kind
  @closure = closure
  @name = name
  @comments = comments
end

Instance Attribute Details

#code_objectYARD::CodeObjects::Base (readonly)


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

def code_object
  @code_object
end

#kindInteger (readonly)


20
21
22
# File 'lib/solargraph/pin/base.rb', line 20

def kind
  @kind
end

#locationSolargraph::Location (readonly)


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

def location
  @location
end

#nameString (readonly)


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

def name
  @name
end

#pathString (readonly)


23
24
25
# File 'lib/solargraph/pin/base.rb', line 23

def path
  @path
end

#return_typeComplexType

The pin's return type.


95
96
97
# File 'lib/solargraph/pin/base.rb', line 95

def return_type
  @return_type ||= ComplexType::UNDEFINED
end

Instance Method Details

#==(other) ⇒ Object

Pin equality is determined using the #nearly? method and also requiring both pins to have the same location.


71
72
73
74
# File 'lib/solargraph/pin/base.rb', line 71

def == other
  return false unless nearly? other
  comments == other.comments and location == other.location
end

#commentsString


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

def comments
  @comments ||= ''
end

#completion_item_kindInteger


50
51
52
# File 'lib/solargraph/pin/base.rb', line 50

def completion_item_kind
  LanguageServer::CompletionItemKinds::KEYWORD
end

#deprecated?Boolean


130
131
132
# File 'lib/solargraph/pin/base.rb', line 130

def deprecated?
  @deprecated ||= docstring.has_tag?('deprecated')
end

#directivesArray<YARD::Tags::Directive>


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

def directives
  parse_comments unless defined?(@directives)
  @directives
end

#docstringYARD::Docstring


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

def docstring
  parse_comments unless defined?(@docstring)
  @docstring ||= Solargraph::Source.parse_docstring('').to_docstring
end

#filenameString


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

def filename
  return nil if location.nil?
  location.filename
end

#infer(api_map) ⇒ ComplexType

Deprecated.

Use #typify and/or #probe instead


157
158
159
160
161
162
# File 'lib/solargraph/pin/base.rb', line 157

def infer api_map
  Solargraph::Logging.logger.warn "WARNING: Pin #infer methods are deprecated. Use #typify or #probe instead."
  type = typify(api_map)
  return type unless type.undefined?
  probe api_map
end

#inspectObject


218
219
220
# File 'lib/solargraph/pin/base.rb', line 218

def inspect
  "#<#{self.class} at #{self.location.inspect}>"
end

#macrosArray<YARD::Tags::MacroDirective>


112
113
114
# File 'lib/solargraph/pin/base.rb', line 112

def macros
  @macros ||= collect_macros
end

#maybe_directives?Boolean

Perform a quick check to see if this pin possibly includes YARD directives. This method does not require parsing the comments.

After the comments have been parsed, this method will return false if no directives were found, regardless of whether it previously appeared possible.


124
125
126
127
# File 'lib/solargraph/pin/base.rb', line 124

def maybe_directives?
  return !@directives.empty? if defined?(@directives)
  @maybe_directives ||= comments.include?('@!')
end

#nearly?(other) ⇒ Boolean

True if the specified pin is a near match to this one. A near match indicates that the pins contain mostly the same data. Any differences between them should not have an impact on the API surface.


82
83
84
85
86
87
88
89
90
# File 'lib/solargraph/pin/base.rb', line 82

def nearly? other
  self.class == other.class &&
    name == other.name &&
    (closure == other.closure || (closure && closure.nearly?(other.closure))) &&
    (comments == other.comments ||
      (((maybe_directives? == false && other.maybe_directives? == false) || compare_directives(directives, other.directives)) &&
      compare_docstring_tags(docstring, other.docstring))
    )
end

#probe(api_map) ⇒ ComplexType

Infer the pin's return type via static code analysis.


150
151
152
# File 'lib/solargraph/pin/base.rb', line 150

def probe api_map
  typify api_map
end

#probed?Boolean


188
189
190
# File 'lib/solargraph/pin/base.rb', line 188

def probed?
  @probed ||= false
end

#proxied?Boolean


184
185
186
# File 'lib/solargraph/pin/base.rb', line 184

def proxied?
  @proxied ||= false
end

#proxy(return_type) ⇒ self

Return a proxy for this pin with the specified return type. Other than the return type and the #proxied? setting, the proxy should be a clone of the original.


211
212
213
214
215
216
# File 'lib/solargraph/pin/base.rb', line 211

def proxy return_type
  result = dup
  result.return_type = return_type
  result.proxied = true
  result
end

#realize(api_map) ⇒ self


194
195
196
197
198
199
200
201
202
203
# File 'lib/solargraph/pin/base.rb', line 194

def realize api_map
  return self if return_type.defined?
  type = typify(api_map)
  return proxy(type) if type.defined?
  type = probe(api_map)
  return self if type.undefined?
  result = proxy(type)
  result.probed = true
  result
end

#symbol_kindInteger


55
56
57
# File 'lib/solargraph/pin/base.rb', line 55

def symbol_kind
  nil
end

#to_sObject


59
60
61
# File 'lib/solargraph/pin/base.rb', line 59

def to_s
  name.to_s
end

#try_merge!(pin) ⇒ Boolean

Try to merge data from another pin. Merges are only possible if the pins are near matches (see the #nearly? method). The changes should not have any side effects on the API surface.


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

def try_merge! pin
  return false unless nearly?(pin)
  @location = pin.location
  @closure = pin.closure
  return true if comments == pin.comments
  @comments = pin.comments
  @docstring = pin.docstring
  @return_type = pin.return_type
  @documentation = nil
  @deprecated = nil
  reset_conversions
  true
end

#typify(api_map) ⇒ ComplexType

Get a fully qualified type from the pin's return type.

The relative type is determined from YARD documentation (@return, @param, @type, etc.) and its namespaces are fully qualified using the provided ApiMap.


142
143
144
# File 'lib/solargraph/pin/base.rb', line 142

def typify api_map
  return_type.qualify(api_map, namespace)
end

#variable?Boolean


64
65
66
# File 'lib/solargraph/pin/base.rb', line 64

def variable?
  false
end