solargraph 0.47.2 → 0.53.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.github/workflows/plugins.yml +40 -0
  4. data/.github/workflows/rspec.yml +4 -8
  5. data/.github/workflows/typecheck.yml +34 -0
  6. data/.yardopts +2 -2
  7. data/CHANGELOG.md +137 -3
  8. data/LICENSE +1 -1
  9. data/README.md +19 -16
  10. data/SPONSORS.md +2 -9
  11. data/lib/solargraph/api_map/cache.rb +60 -20
  12. data/lib/solargraph/api_map/source_to_yard.rb +17 -10
  13. data/lib/solargraph/api_map/store.rb +60 -12
  14. data/lib/solargraph/api_map.rb +171 -99
  15. data/lib/solargraph/bench.rb +3 -2
  16. data/lib/solargraph/cache.rb +77 -0
  17. data/lib/solargraph/complex_type/type_methods.rb +61 -12
  18. data/lib/solargraph/complex_type/unique_type.rb +193 -16
  19. data/lib/solargraph/complex_type.rb +113 -10
  20. data/lib/solargraph/convention/rakefile.rb +17 -0
  21. data/lib/solargraph/convention.rb +2 -3
  22. data/lib/solargraph/converters/dd.rb +5 -0
  23. data/lib/solargraph/converters/dl.rb +3 -0
  24. data/lib/solargraph/converters/dt.rb +3 -0
  25. data/lib/solargraph/diagnostics/rubocop.rb +23 -8
  26. data/lib/solargraph/diagnostics/rubocop_helpers.rb +4 -1
  27. data/lib/solargraph/diagnostics/type_check.rb +1 -0
  28. data/lib/solargraph/diagnostics.rb +2 -2
  29. data/lib/solargraph/doc_map.rb +171 -0
  30. data/lib/solargraph/gem_pins.rb +64 -0
  31. data/lib/solargraph/language_server/host/cataloger.rb +2 -1
  32. data/lib/solargraph/language_server/host/diagnoser.rb +2 -2
  33. data/lib/solargraph/language_server/host/dispatch.rb +15 -5
  34. data/lib/solargraph/language_server/host/message_worker.rb +4 -0
  35. data/lib/solargraph/language_server/host/sources.rb +7 -4
  36. data/lib/solargraph/language_server/host.rb +50 -26
  37. data/lib/solargraph/language_server/message/completion_item/resolve.rb +3 -1
  38. data/lib/solargraph/language_server/message/extended/check_gem_version.rb +13 -1
  39. data/lib/solargraph/language_server/message/extended/download_core.rb +1 -5
  40. data/lib/solargraph/language_server/message/initialize.rb +13 -0
  41. data/lib/solargraph/language_server/message/initialized.rb +1 -0
  42. data/lib/solargraph/language_server/message/text_document/document_symbol.rb +4 -1
  43. data/lib/solargraph/language_server/message/text_document/formatting.rb +4 -4
  44. data/lib/solargraph/language_server/message/text_document/hover.rb +2 -0
  45. data/lib/solargraph/language_server/message/text_document/signature_help.rb +1 -6
  46. data/lib/solargraph/language_server/message/text_document/type_definition.rb +24 -0
  47. data/lib/solargraph/language_server/message/text_document.rb +1 -1
  48. data/lib/solargraph/language_server/message/workspace/did_change_configuration.rb +5 -0
  49. data/lib/solargraph/language_server/message/workspace/did_change_watched_files.rb +10 -3
  50. data/lib/solargraph/language_server/message.rb +1 -0
  51. data/lib/solargraph/language_server/transport/adapter.rb +16 -1
  52. data/lib/solargraph/language_server/transport/data_reader.rb +2 -0
  53. data/lib/solargraph/library.rb +124 -37
  54. data/lib/solargraph/location.rb +1 -0
  55. data/lib/solargraph/page.rb +6 -0
  56. data/lib/solargraph/parser/comment_ripper.rb +4 -0
  57. data/lib/solargraph/parser/node_methods.rb +47 -7
  58. data/lib/solargraph/parser/node_processor/base.rb +9 -0
  59. data/lib/solargraph/parser/{legacy → parser_gem}/class_methods.rb +31 -5
  60. data/lib/solargraph/parser/{legacy → parser_gem}/flawed_builder.rb +3 -1
  61. data/lib/solargraph/parser/{legacy → parser_gem}/node_chainer.rb +57 -41
  62. data/lib/solargraph/parser/parser_gem/node_methods.rb +499 -0
  63. data/lib/solargraph/parser/{rubyvm → parser_gem}/node_processors/alias_node.rb +1 -1
  64. data/lib/solargraph/parser/parser_gem/node_processors/args_node.rb +53 -0
  65. data/lib/solargraph/parser/{rubyvm → parser_gem}/node_processors/begin_node.rb +1 -1
  66. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/block_node.rb +3 -2
  67. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/casgn_node.rb +14 -4
  68. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/cvasgn_node.rb +1 -1
  69. data/lib/solargraph/parser/{rubyvm → parser_gem}/node_processors/def_node.rb +7 -20
  70. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/defs_node.rb +2 -2
  71. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/gvasgn_node.rb +1 -1
  72. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/ivasgn_node.rb +2 -2
  73. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/lvasgn_node.rb +2 -2
  74. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/namespace_node.rb +2 -2
  75. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/orasgn_node.rb +1 -1
  76. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/resbody_node.rb +3 -3
  77. data/lib/solargraph/parser/parser_gem/node_processors/sclass_node.rb +42 -0
  78. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/send_node.rb +2 -2
  79. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/sym_node.rb +1 -1
  80. data/lib/solargraph/parser/parser_gem/node_processors.rb +54 -0
  81. data/lib/solargraph/parser/parser_gem.rb +12 -0
  82. data/lib/solargraph/parser/region.rb +1 -1
  83. data/lib/solargraph/parser/snippet.rb +2 -0
  84. data/lib/solargraph/parser.rb +9 -10
  85. data/lib/solargraph/pin/base.rb +69 -11
  86. data/lib/solargraph/pin/base_variable.rb +8 -4
  87. data/lib/solargraph/pin/block.rb +21 -28
  88. data/lib/solargraph/pin/closure.rb +17 -2
  89. data/lib/solargraph/pin/common.rb +7 -3
  90. data/lib/solargraph/pin/conversions.rb +34 -8
  91. data/lib/solargraph/pin/delegated_method.rb +97 -0
  92. data/lib/solargraph/pin/documenting.rb +25 -34
  93. data/lib/solargraph/pin/instance_variable.rb +4 -0
  94. data/lib/solargraph/pin/local_variable.rb +13 -1
  95. data/lib/solargraph/pin/method.rb +270 -16
  96. data/lib/solargraph/pin/namespace.rb +17 -1
  97. data/lib/solargraph/pin/parameter.rb +52 -17
  98. data/lib/solargraph/pin/reference/override.rb +2 -2
  99. data/lib/solargraph/pin/reference.rb +8 -0
  100. data/lib/solargraph/pin/search.rb +4 -4
  101. data/lib/solargraph/pin/signature.rb +143 -0
  102. data/lib/solargraph/pin.rb +2 -1
  103. data/lib/solargraph/range.rb +4 -6
  104. data/lib/solargraph/rbs_map/conversions.rb +601 -0
  105. data/lib/solargraph/rbs_map/core_fills.rb +47 -0
  106. data/lib/solargraph/rbs_map/core_map.rb +28 -0
  107. data/lib/solargraph/rbs_map/stdlib_map.rb +33 -0
  108. data/lib/solargraph/rbs_map.rb +84 -0
  109. data/lib/solargraph/shell.rb +69 -48
  110. data/lib/solargraph/source/chain/array.rb +32 -0
  111. data/lib/solargraph/source/chain/block_symbol.rb +13 -0
  112. data/lib/solargraph/source/chain/call.rb +125 -61
  113. data/lib/solargraph/source/chain/constant.rb +15 -1
  114. data/lib/solargraph/source/chain/if.rb +23 -0
  115. data/lib/solargraph/source/chain/link.rb +8 -2
  116. data/lib/solargraph/source/chain/or.rb +1 -1
  117. data/lib/solargraph/source/chain/z_super.rb +3 -3
  118. data/lib/solargraph/source/chain.rb +44 -14
  119. data/lib/solargraph/source/change.rb +3 -0
  120. data/lib/solargraph/source/cursor.rb +2 -0
  121. data/lib/solargraph/source/source_chainer.rb +8 -5
  122. data/lib/solargraph/source.rb +18 -19
  123. data/lib/solargraph/source_map/clip.rb +30 -23
  124. data/lib/solargraph/source_map/mapper.rb +20 -5
  125. data/lib/solargraph/source_map.rb +28 -13
  126. data/lib/solargraph/type_checker/checks.rb +10 -2
  127. data/lib/solargraph/type_checker.rb +201 -98
  128. data/lib/solargraph/version.rb +1 -1
  129. data/lib/solargraph/views/environment.erb +2 -2
  130. data/lib/solargraph/workspace/config.rb +14 -11
  131. data/lib/solargraph/workspace.rb +28 -17
  132. data/lib/solargraph/yard_map/cache.rb +6 -0
  133. data/lib/solargraph/yard_map/helpers.rb +1 -1
  134. data/lib/solargraph/yard_map/mapper/to_method.rb +18 -5
  135. data/lib/solargraph/yard_map/mapper.rb +1 -1
  136. data/lib/solargraph/yard_map/to_method.rb +11 -4
  137. data/lib/solargraph/yard_map.rb +1 -443
  138. data/lib/solargraph/yard_tags.rb +20 -0
  139. data/lib/solargraph/yardoc.rb +52 -0
  140. data/lib/solargraph.rb +8 -6
  141. data/solargraph.gemspec +19 -8
  142. metadata +162 -98
  143. data/.travis.yml +0 -19
  144. data/lib/solargraph/api_map/bundler_methods.rb +0 -22
  145. data/lib/solargraph/compat.rb +0 -37
  146. data/lib/solargraph/convention/rspec.rb +0 -30
  147. data/lib/solargraph/documentor.rb +0 -76
  148. data/lib/solargraph/parser/legacy/node_methods.rb +0 -325
  149. data/lib/solargraph/parser/legacy/node_processors/alias_node.rb +0 -23
  150. data/lib/solargraph/parser/legacy/node_processors/args_node.rb +0 -35
  151. data/lib/solargraph/parser/legacy/node_processors/begin_node.rb +0 -15
  152. data/lib/solargraph/parser/legacy/node_processors/def_node.rb +0 -63
  153. data/lib/solargraph/parser/legacy/node_processors/sclass_node.rb +0 -21
  154. data/lib/solargraph/parser/legacy/node_processors.rb +0 -54
  155. data/lib/solargraph/parser/legacy.rb +0 -12
  156. data/lib/solargraph/parser/rubyvm/class_methods.rb +0 -144
  157. data/lib/solargraph/parser/rubyvm/node_chainer.rb +0 -160
  158. data/lib/solargraph/parser/rubyvm/node_methods.rb +0 -315
  159. data/lib/solargraph/parser/rubyvm/node_processors/args_node.rb +0 -85
  160. data/lib/solargraph/parser/rubyvm/node_processors/block_node.rb +0 -42
  161. data/lib/solargraph/parser/rubyvm/node_processors/casgn_node.rb +0 -22
  162. data/lib/solargraph/parser/rubyvm/node_processors/cvasgn_node.rb +0 -23
  163. data/lib/solargraph/parser/rubyvm/node_processors/defs_node.rb +0 -57
  164. data/lib/solargraph/parser/rubyvm/node_processors/gvasgn_node.rb +0 -23
  165. data/lib/solargraph/parser/rubyvm/node_processors/ivasgn_node.rb +0 -38
  166. data/lib/solargraph/parser/rubyvm/node_processors/kw_arg_node.rb +0 -39
  167. data/lib/solargraph/parser/rubyvm/node_processors/lit_node.rb +0 -20
  168. data/lib/solargraph/parser/rubyvm/node_processors/lvasgn_node.rb +0 -27
  169. data/lib/solargraph/parser/rubyvm/node_processors/namespace_node.rb +0 -39
  170. data/lib/solargraph/parser/rubyvm/node_processors/opt_arg_node.rb +0 -26
  171. data/lib/solargraph/parser/rubyvm/node_processors/orasgn_node.rb +0 -15
  172. data/lib/solargraph/parser/rubyvm/node_processors/resbody_node.rb +0 -45
  173. data/lib/solargraph/parser/rubyvm/node_processors/sclass_node.rb +0 -21
  174. data/lib/solargraph/parser/rubyvm/node_processors/scope_node.rb +0 -15
  175. data/lib/solargraph/parser/rubyvm/node_processors/send_node.rb +0 -277
  176. data/lib/solargraph/parser/rubyvm/node_processors/sym_node.rb +0 -18
  177. data/lib/solargraph/parser/rubyvm/node_processors.rb +0 -63
  178. data/lib/solargraph/parser/rubyvm.rb +0 -40
  179. data/lib/solargraph/yard_map/core_docs.rb +0 -170
  180. data/lib/solargraph/yard_map/core_fills.rb +0 -208
  181. data/lib/solargraph/yard_map/core_gen.rb +0 -76
  182. data/lib/solargraph/yard_map/rdoc_to_yard.rb +0 -140
  183. data/lib/solargraph/yard_map/stdlib_fills.rb +0 -43
  184. data/lib/yard-solargraph.rb +0 -33
  185. data/yardoc/2.2.2.tar.gz +0 -0
@@ -7,7 +7,7 @@ module Solargraph
7
7
  class Method < Closure
8
8
  include Solargraph::Parser::NodeMethods
9
9
 
10
- # @return [Array<Pin::Parameter>]
10
+ # @return [::Array<Pin::Parameter>]
11
11
  attr_reader :parameters
12
12
 
13
13
  # @return [::Symbol] :public, :private, or :protected
@@ -18,16 +18,66 @@ module Solargraph
18
18
 
19
19
  # @param visibility [::Symbol] :public, :protected, or :private
20
20
  # @param explicit [Boolean]
21
- def initialize visibility: :public, explicit: true, parameters: [], node: nil, attribute: false, **splat
21
+ # @param parameters [::Array<Pin::Parameter>]
22
+ # @param block [Pin::Signature, nil, ::Symbol]
23
+ # @param node [Parser::AST::Node, RubyVM::AbstractSyntaxTree::Node, nil]
24
+ # @param attribute [Boolean]
25
+ # @param signatures [::Array<Signature>, nil]
26
+ # @param anon_splat [Boolean]
27
+ # @param return_type [ComplexType, nil]
28
+ def initialize visibility: :public, explicit: true, parameters: [], block: :undefined, node: nil, attribute: false, signatures: nil, anon_splat: false, return_type: nil, **splat
22
29
  super(**splat)
23
30
  @visibility = visibility
24
31
  @explicit = explicit
25
32
  @parameters = parameters
33
+ @block = block
26
34
  @node = node
27
35
  @attribute = attribute
36
+ @signatures = signatures
37
+ @anon_splat = anon_splat
38
+ @return_type = return_type
28
39
  end
29
40
 
30
- # @return [Array<String>]
41
+ def transform_types(&transform)
42
+ # @todo 'super' alone should work here I think, but doesn't typecheck at level typed
43
+ m = super(&transform)
44
+ m.signatures = m.signatures.map do |sig|
45
+ sig.transform_types(&transform)
46
+ end
47
+ m.parameters = m.parameters.map do |param|
48
+ param.transform_types(&transform)
49
+ end
50
+ m.block = block&.transform_types(&transform)
51
+ m.signature_help = nil
52
+ m.documentation = nil
53
+ m
54
+ end
55
+
56
+ # @param signature [Pin::Signature]
57
+ # @return [Pin::Method]
58
+ def with_single_signature(signature)
59
+ m = proxy signature.return_type
60
+ m.signature_help = nil
61
+ m.documentation = nil
62
+ # @todo populating the single parameters/return_type/block
63
+ # arguments here seems to be needed for some specs to pass,
64
+ # even though we have a signature with the same information.
65
+ # Is this a problem for RBS-populated methods, which don't
66
+ # populate these three?
67
+ m.parameters = signature.parameters
68
+ m.return_type = signature.return_type
69
+ m.block = signature.block
70
+ m.signatures = [signature]
71
+ m
72
+ end
73
+
74
+ # @return [Pin::Signature, nil]
75
+ def block
76
+ return @block unless @block == :undefined
77
+ @block = signatures.first.block
78
+ end
79
+
80
+ # @return [::Array<String>]
31
81
  def parameter_names
32
82
  @parameter_names ||= parameters.map(&:name)
33
83
  end
@@ -41,7 +91,100 @@ module Solargraph
41
91
  end
42
92
 
43
93
  def return_type
44
- @return_type ||= generate_complex_type
94
+ @return_type ||= ComplexType.new(signatures.map(&:return_type).flat_map(&:items))
95
+ end
96
+
97
+ # @param parameters [::Array<Parameter>]
98
+ # @param return_type [ComplexType]
99
+ # @return [Signature]
100
+ def generate_signature(parameters, return_type)
101
+ block = nil
102
+ yieldparam_tags = docstring.tags(:yieldparam)
103
+ yieldreturn_tags = docstring.tags(:yieldreturn)
104
+ generics = docstring.tags(:generic).map(&:name)
105
+ needs_block_param_signature =
106
+ parameters.last&.block? || !yieldreturn_tags.empty? || !yieldparam_tags.empty?
107
+ if needs_block_param_signature
108
+ yield_parameters = yieldparam_tags.map do |p|
109
+ name = p.name
110
+ decl = :arg
111
+ if name
112
+ decl = select_decl(name, false)
113
+ name = clean_param(name)
114
+ end
115
+ Pin::Parameter.new(
116
+ location: location,
117
+ closure: self,
118
+ comments: p.text,
119
+ name: name,
120
+ decl: decl,
121
+ presence: location ? location.range : nil,
122
+ return_type: ComplexType.try_parse(*p.types)
123
+ )
124
+ end
125
+ yield_return_type = ComplexType.try_parse(*yieldreturn_tags.flat_map(&:types))
126
+ block = Signature.new(generics, yield_parameters, yield_return_type)
127
+ end
128
+ Signature.new(generics, parameters, return_type, block)
129
+ end
130
+
131
+ # @return [::Array<Signature>]
132
+ def signatures
133
+ @signatures ||= begin
134
+ top_type = generate_complex_type
135
+ result = []
136
+ result.push generate_signature(parameters, top_type) if top_type.defined?
137
+ result.concat(overloads.map { |meth| generate_signature(meth.parameters, meth.return_type) }) unless overloads.empty?
138
+ result.push generate_signature(parameters, top_type) if result.empty?
139
+ result
140
+ end
141
+ end
142
+
143
+ # @return [String, nil]
144
+ def detail
145
+ # This property is not cached in an instance variable because it can
146
+ # change when pins get proxied.
147
+ detail = String.new
148
+ detail += if signatures.length > 1
149
+ "(*) "
150
+ else
151
+ "(#{signatures.first.parameters.map(&:full).join(', ')}) " unless signatures.first.parameters.empty?
152
+ end.to_s
153
+ detail += "=#{probed? ? '~' : (proxied? ? '^' : '>')} #{return_type.to_s}" unless return_type.undefined?
154
+ detail.strip!
155
+ return nil if detail.empty?
156
+ detail
157
+ end
158
+
159
+ # @return [::Array<Hash>]
160
+ def signature_help
161
+ @signature_help ||= signatures.map do |sig|
162
+ {
163
+ label: name + '(' + sig.parameters.map(&:full).join(', ') + ')',
164
+ documentation: documentation
165
+ }
166
+ end
167
+ end
168
+
169
+ def desc
170
+ # ensure the signatures line up when logged
171
+ if signatures.length > 1
172
+ "\n#{to_rbs}\n"
173
+ else
174
+ to_rbs
175
+ end
176
+ end
177
+
178
+ def to_rbs
179
+ return nil if signatures.empty?
180
+
181
+ rbs = "def #{name}: #{signatures.first.to_rbs}"
182
+ signatures[1..].each do |sig|
183
+ rbs += "\n"
184
+ rbs += (' ' * (4 + name.length))
185
+ rbs += "| #{name}: #{sig.to_rbs}"
186
+ end
187
+ rbs
45
188
  end
46
189
 
47
190
  def path
@@ -72,6 +215,32 @@ module Solargraph
72
215
  end
73
216
  @documentation += lines.join("\n")
74
217
  end
218
+ yieldparam_tags = docstring.tags(:yieldparam)
219
+ unless yieldparam_tags.nil? or yieldparam_tags.empty?
220
+ @documentation += "\n\n" unless @documentation.empty?
221
+ @documentation += "Block Params:\n"
222
+ lines = []
223
+ yieldparam_tags.each do |p|
224
+ l = "* #{p.name}"
225
+ l += " [#{escape_brackets(p.types.join(', '))}]" unless p.types.nil? or p.types.empty?
226
+ l += " #{p.text}"
227
+ lines.push l
228
+ end
229
+ @documentation += lines.join("\n")
230
+ end
231
+ yieldreturn_tags = docstring.tags(:yieldreturn)
232
+ unless yieldreturn_tags.empty?
233
+ @documentation += "\n\n" unless @documentation.empty?
234
+ @documentation += "Block Returns:\n"
235
+ lines = []
236
+ yieldreturn_tags.each do |r|
237
+ l = "*"
238
+ l += " [#{escape_brackets(r.types.join(', '))}]" unless r.types.nil? or r.types.empty?
239
+ l += " #{r.text}"
240
+ lines.push l
241
+ end
242
+ @documentation += lines.join("\n")
243
+ end
75
244
  return_tags = docstring.tags(:return)
76
245
  unless return_tags.empty?
77
246
  @documentation += "\n\n" unless @documentation.empty?
@@ -87,6 +256,7 @@ module Solargraph
87
256
  end
88
257
  @documentation += "\n\n" unless @documentation.empty?
89
258
  @documentation += "Visibility: #{visibility}"
259
+ concat_example_tags
90
260
  end
91
261
  @documentation.to_s
92
262
  end
@@ -116,33 +286,88 @@ module Solargraph
116
286
  true
117
287
  end
118
288
 
119
- # @return [Array<Pin::Method>]
289
+ # @return [::Array<Pin::Method>]
120
290
  def overloads
121
291
  @overloads ||= docstring.tags(:overload).map do |tag|
122
- Solargraph::Pin::Method.new(
123
- name: name,
124
- closure: self,
125
- # args: tag.parameters.map(&:first),
126
- parameters: tag.parameters.map do |src|
292
+ Pin::Signature.new(
293
+ generics,
294
+ tag.parameters.map do |src|
295
+ name, decl = parse_overload_param(src.first)
127
296
  Pin::Parameter.new(
128
297
  location: location,
129
298
  closure: self,
130
299
  comments: tag.docstring.all.to_s,
131
- name: src.first,
300
+ name: name,
301
+ decl: decl,
132
302
  presence: location ? location.range : nil,
133
- decl: :arg
303
+ return_type: param_type_from_name(tag, src.first)
134
304
  )
135
305
  end,
136
- comments: tag.docstring.all.to_s
306
+ ComplexType.try_parse(*tag.docstring.tags(:return).flat_map(&:types))
137
307
  )
138
308
  end
309
+ @overloads
139
310
  end
140
311
 
312
+ def anon_splat?
313
+ @anon_splat
314
+ end
315
+
316
+ protected
317
+
318
+ attr_writer :block
319
+
320
+ attr_writer :parameters
321
+
322
+ attr_writer :signatures
323
+
324
+ attr_writer :signature_help
325
+
326
+ attr_writer :documentation
327
+
141
328
  private
142
329
 
330
+ # @param name [String]
331
+ # @param asgn [Boolean]
332
+ #
333
+ # @return [::Symbol]
334
+ def select_decl name, asgn
335
+ if name.start_with?('**')
336
+ :kwrestarg
337
+ elsif name.start_with?('*')
338
+ :restarg
339
+ elsif name.start_with?('&')
340
+ :blockarg
341
+ elsif name.end_with?(':') && asgn
342
+ :kwoptarg
343
+ elsif name.end_with?(':')
344
+ :kwarg
345
+ elsif asgn
346
+ :optarg
347
+ else
348
+ :arg
349
+ end
350
+ end
351
+
352
+ # @param name [String]
353
+ # @return [String]
354
+ def clean_param name
355
+ name.gsub(/[*&:]/, '')
356
+ end
357
+
358
+ # @param tag [YARD::Tags::OverloadTag]
359
+ # @param name [String]
360
+ #
361
+ # @return [ComplexType]
362
+ def param_type_from_name(tag, name)
363
+ param = tag.tags(:param).select { |t| t.name == name }.first
364
+ return ComplexType::UNDEFINED unless param
365
+ ComplexType.try_parse(*param.types)
366
+ end
367
+
143
368
  # @return [ComplexType]
144
369
  def generate_complex_type
145
- tags = docstring.tags(:return).map(&:types).flatten.reject(&:nil?)
370
+ tags = docstring.tags(:return).map(&:types).flatten.compact
146
371
  return ComplexType::UNDEFINED if tags.empty?
147
372
  ComplexType.try_parse *tags
148
373
  end
@@ -173,7 +398,7 @@ module Solargraph
173
398
 
174
399
  # @param ref [String]
175
400
  # @param api_map [ApiMap]
176
- # @return [ComplexType]
401
+ # @return [ComplexType, nil]
177
402
  def resolve_reference ref, api_map
178
403
  parts = ref.split(/[\.#]/)
179
404
  if parts.first.empty? || parts.one?
@@ -208,7 +433,7 @@ module Solargraph
208
433
  result = []
209
434
  has_nil = false
210
435
  return ComplexType::NIL if method_body_node.nil?
211
- returns_from(method_body_node).each do |n|
436
+ returns_from_method_body(method_body_node).each do |n|
212
437
  if n.nil? || [:NIL, :nil].include?(n.type)
213
438
  has_nil = true
214
439
  next
@@ -228,6 +453,8 @@ module Solargraph
228
453
  ComplexType.try_parse(*result.map(&:tag).uniq)
229
454
  end
230
455
 
456
+ # @param [ApiMap] api_map
457
+ # @return [ComplexType]
231
458
  def infer_from_iv api_map
232
459
  types = []
233
460
  varname = "@#{name.gsub(/=$/, '')}"
@@ -240,6 +467,33 @@ module Solargraph
240
467
  return ComplexType::UNDEFINED if types.empty?
241
468
  ComplexType.try_parse(*types.map(&:tag).uniq)
242
469
  end
470
+
471
+ # When YARD parses an overload tag, it includes rest modifiers in the parameters names.
472
+ #
473
+ # @param name [String]
474
+ # @return [::Array(String, ::Symbol)]
475
+ def parse_overload_param(name)
476
+ if name.start_with?('**')
477
+ [name[2..-1], :kwrestarg]
478
+ elsif name.start_with?('*')
479
+ [name[1..-1], :restarg]
480
+ else
481
+ [name, :arg]
482
+ end
483
+ end
484
+
485
+ # @return [void]
486
+ def concat_example_tags
487
+ example_tags = docstring.tags(:example)
488
+ return if example_tags.empty?
489
+ @documentation += "\n\nExamples:\n\n```ruby\n"
490
+ @documentation += example_tags.map do |tag|
491
+ (tag.name && !tag.name.empty? ? "# #{tag.name}\n" : '') +
492
+ "#{tag.text}\n"
493
+ end
494
+ .join("\n")
495
+ .concat("```\n")
496
+ end
243
497
  end
244
498
  end
245
499
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'solargraph/yard_tags'
4
+
3
5
  module Solargraph
4
6
  module Pin
5
7
  class Namespace < Closure
@@ -11,7 +13,7 @@ module Solargraph
11
13
 
12
14
  # @param type [::Symbol] :class or :module
13
15
  # @param visibility [::Symbol] :public or :private
14
- # @param gates [Array<String>]
16
+ # @param gates [::Array<String>]
15
17
  def initialize type: :class, visibility: :public, gates: [''], **splat
16
18
  # super(location, namespace, name, comments)
17
19
  super(**splat)
@@ -38,6 +40,18 @@ module Solargraph
38
40
  end
39
41
  end
40
42
 
43
+ def to_rbs
44
+ "#{@type.to_s} #{generics_as_rbs}#{return_type.to_rbs}"
45
+ end
46
+
47
+ def desc
48
+ if name.nil?
49
+ '(top-level)'
50
+ else
51
+ to_rbs
52
+ end
53
+ end
54
+
41
55
  def namespace
42
56
  context.namespace
43
57
  end
@@ -63,6 +77,7 @@ module Solargraph
63
77
  (type == :class ? LanguageServer::SymbolKinds::CLASS : LanguageServer::SymbolKinds::MODULE)
64
78
  end
65
79
 
80
+ # @return [String]
66
81
  def path
67
82
  @path ||= (namespace.empty? ? '' : "#{namespace}::") + name
68
83
  end
@@ -71,6 +86,7 @@ module Solargraph
71
86
  @return_type ||= ComplexType.try_parse( (type == :class ? 'Class' : 'Module') + "<#{path}>" )
72
87
  end
73
88
 
89
+ # @return [Array<String>]
74
90
  def domains
75
91
  @domains ||= []
76
92
  end
@@ -9,10 +9,14 @@ module Solargraph
9
9
  # @return [String]
10
10
  attr_reader :asgn_code
11
11
 
12
- def initialize decl: :arg, asgn_code: nil, **splat
12
+ # @param decl [::Symbol] :arg, :optarg, :kwarg, :kwoptarg, :restarg, :kwrestarg, :block, :blockarg
13
+ # @param asgn_code [String, nil]
14
+ # @param return_type [ComplexType, nil]
15
+ def initialize decl: :arg, asgn_code: nil, return_type: nil, **splat
13
16
  super(**splat)
14
17
  @asgn_code = asgn_code
15
18
  @decl = decl
19
+ @return_type = return_type
16
20
  end
17
21
 
18
22
  def keyword?
@@ -31,14 +35,36 @@ module Solargraph
31
35
  decl == :restarg || decl == :kwrestarg
32
36
  end
33
37
 
38
+ def block?
39
+ [:block, :blockarg].include?(decl)
40
+ end
41
+
42
+ def to_rbs
43
+ case decl
44
+ when :optarg
45
+ "?#{super}"
46
+ when :kwarg
47
+ "#{name}: #{return_type.to_rbs}"
48
+ when :kwoptarg
49
+ "?#{name}: #{return_type.to_rbs}"
50
+ when :restarg
51
+ "*#{super}"
52
+ when :kwrestarg
53
+ "**#{super}"
54
+ else
55
+ super
56
+ end
57
+ end
58
+
59
+ # @return [String]
34
60
  def full
35
61
  case decl
36
62
  when :optarg
37
- "#{name} = #{asgn_code}"
63
+ "#{name} = #{asgn_code || '?'}"
38
64
  when :kwarg
39
65
  "#{name}:"
40
66
  when :kwoptarg
41
- "#{name}: #{asgn_code}"
67
+ "#{name}: #{asgn_code || '?'}"
42
68
  when :restarg
43
69
  "*#{name}"
44
70
  when :kwrestarg
@@ -95,7 +121,7 @@ module Solargraph
95
121
 
96
122
  private
97
123
 
98
- # @return [YARD::Tags::Tag]
124
+ # @return [YARD::Tags::Tag, nil]
99
125
  def param_tag
100
126
  found = nil
101
127
  params = closure.docstring.tags(:param)
@@ -114,21 +140,30 @@ module Solargraph
114
140
  # @return [ComplexType]
115
141
  def typify_block_param api_map
116
142
  if closure.is_a?(Pin::Block) && closure.receiver
117
- chain = Parser.chain(closure.receiver, filename)
143
+ chain = Parser.chain(closure.receiver, filename, closure.node)
118
144
  clip = api_map.clip_at(location.filename, location.range.start)
119
145
  locals = clip.locals - [self]
120
146
  meths = chain.define(api_map, closure, locals)
147
+ receiver_type = chain.base.infer(api_map, closure, locals)
121
148
  meths.each do |meth|
122
- if meth.docstring.has_tag?(:yieldparam_single_parameter)
123
- type = chain.base.infer(api_map, closure, locals)
124
- if type.defined? && !type.subtypes.empty?
125
- bmeth = chain.base.define(api_map, closure, locals).first
126
- return type.subtypes.first.qualify(api_map, bmeth.context.namespace)
127
- end
149
+ block_signature = meth.block
150
+ if block_signature
151
+ yield_type = block_signature.parameters[index]&.return_type
128
152
  else
153
+ # @todo move the yieldparam tag parsing logic into the
154
+ # creation of Method pins so we don't need this else
155
+ # statement
129
156
  yps = meth.docstring.tags(:yieldparam)
130
157
  unless yps[index].nil? or yps[index].types.nil? or yps[index].types.empty?
131
- return ComplexType.try_parse(yps[index].types.first).self_to(chain.base.infer(api_map, closure, locals).namespace).qualify(api_map, meth.context.namespace)
158
+ yield_type = ComplexType.try_parse(yps[index].types.first)
159
+ end
160
+ end
161
+ unless yield_type.nil?
162
+ if yield_type.generic? && receiver_type.defined?
163
+ namespace_pin = api_map.get_namespace_pins(meth.namespace, closure.namespace).first
164
+ return yield_type.resolve_generics(namespace_pin, receiver_type)
165
+ else
166
+ return yield_type.self_to(chain.base.infer(api_map, closure, locals).namespace).qualify(api_map, meth.context.namespace)
132
167
  end
133
168
  end
134
169
  end
@@ -139,7 +174,7 @@ module Solargraph
139
174
  # @param api_map [ApiMap]
140
175
  # @return [ComplexType]
141
176
  def typify_method_param api_map
142
- meths = api_map.get_method_stack(closure.full_context.namespace, closure.name, scope: closure.scope)
177
+ meths = api_map.get_method_stack(closure.full_context.tag, closure.name, scope: closure.scope)
143
178
  # meths.shift # Ignore the first one
144
179
  meths.each do |meth|
145
180
  found = nil
@@ -159,8 +194,8 @@ module Solargraph
159
194
 
160
195
  # @param heredoc [YARD::Docstring]
161
196
  # @param api_map [ApiMap]
162
- # @param skip [Array]
163
- # @return [Array<YARD::Tags::Tag>]
197
+ # @param skip [::Array]
198
+ # @return [::Array<YARD::Tags::Tag>]
164
199
  def see_reference heredoc, api_map, skip = []
165
200
  heredoc.ref_tags.each do |ref|
166
201
  next unless ref.tag_name == 'param' && ref.owner
@@ -172,8 +207,8 @@ module Solargraph
172
207
 
173
208
  # @param ref [String]
174
209
  # @param api_map [ApiMap]
175
- # @param skip [Array]
176
- # @return [Array<YARD::Tags::Tag>, nil]
210
+ # @param skip [::Array]
211
+ # @return [::Array<YARD::Tags::Tag>, nil]
177
212
  def resolve_reference ref, api_map, skip
178
213
  return nil if skip.include?(ref)
179
214
  skip.push ref
@@ -4,10 +4,10 @@ module Solargraph
4
4
  module Pin
5
5
  class Reference
6
6
  class Override < Reference
7
- # @return [Array<YARD::Tags::Tag>]
7
+ # @return [::Array<YARD::Tags::Tag>]
8
8
  attr_reader :tags
9
9
 
10
- # @return [Array<Symbol>]
10
+ # @return [::Array<Symbol>]
11
11
  attr_reader :delete
12
12
 
13
13
  def initialize location, name, tags, delete = []
@@ -9,6 +9,14 @@ module Solargraph
9
9
  autoload :Prepend, 'solargraph/pin/reference/prepend'
10
10
  autoload :Extend, 'solargraph/pin/reference/extend'
11
11
  autoload :Override, 'solargraph/pin/reference/override'
12
+
13
+ attr_reader :generic_values
14
+
15
+ # @param generic_values [Array<String>]
16
+ def initialize generic_values: [], **splat
17
+ super(**splat)
18
+ @generic_values = generic_values
19
+ end
12
20
  end
13
21
  end
14
22
  end
@@ -18,21 +18,21 @@ module Solargraph
18
18
  end
19
19
  end
20
20
 
21
- # @param pins [Array<Pin::Base>]
21
+ # @param pins [::Array<Pin::Base>]
22
22
  # @param query [String]
23
23
  def initialize pins, query
24
24
  @pins = pins
25
25
  @query = query
26
26
  end
27
27
 
28
- # @return [Array<Pin::Base>]
28
+ # @return [::Array<Pin::Base>]
29
29
  def results
30
30
  @results ||= do_query
31
31
  end
32
32
 
33
33
  private
34
34
 
35
- # @return [Array<Pin::Base>]
35
+ # @return [::Array<Pin::Base>]
36
36
  def do_query
37
37
  return @pins if @query.nil? || @query.empty?
38
38
  @pins.map do |pin|
@@ -49,7 +49,7 @@ module Solargraph
49
49
  # @return [Float]
50
50
  def fuzzy_string_match str1, str2
51
51
  return (1.0 + (str2.length.to_f / str1.length.to_f)) if str1.downcase.include?(str2.downcase)
52
- JaroWinkler.distance(str1, str2, ignore_case: true)
52
+ JaroWinkler.similarity(str1, str2, ignore_case: true)
53
53
  end
54
54
  end
55
55
  end