solargraph 0.54.1 → 0.56.0

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 (135) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/plugins.yml +2 -0
  3. data/.github/workflows/typecheck.yml +3 -1
  4. data/.gitignore +2 -0
  5. data/CHANGELOG.md +70 -0
  6. data/README.md +13 -3
  7. data/lib/solargraph/api_map/cache.rb +10 -1
  8. data/lib/solargraph/api_map/index.rb +175 -0
  9. data/lib/solargraph/api_map/store.rb +79 -126
  10. data/lib/solargraph/api_map.rb +247 -93
  11. data/lib/solargraph/bench.rb +17 -1
  12. data/lib/solargraph/complex_type/type_methods.rb +13 -1
  13. data/lib/solargraph/complex_type/unique_type.rb +118 -9
  14. data/lib/solargraph/complex_type.rb +48 -18
  15. data/lib/solargraph/convention/base.rb +3 -3
  16. data/lib/solargraph/convention/struct_definition/struct_assignment_node.rb +60 -0
  17. data/lib/solargraph/convention/struct_definition/struct_definition_node.rb +100 -0
  18. data/lib/solargraph/convention/struct_definition.rb +101 -0
  19. data/lib/solargraph/convention.rb +5 -3
  20. data/lib/solargraph/doc_map.rb +274 -56
  21. data/lib/solargraph/equality.rb +33 -0
  22. data/lib/solargraph/gem_pins.rb +53 -37
  23. data/lib/solargraph/language_server/host/message_worker.rb +31 -11
  24. data/lib/solargraph/language_server/host.rb +24 -13
  25. data/lib/solargraph/language_server/message/base.rb +19 -12
  26. data/lib/solargraph/language_server/message/extended/check_gem_version.rb +2 -0
  27. data/lib/solargraph/language_server/message/extended/document.rb +5 -2
  28. data/lib/solargraph/language_server/message/extended/document_gems.rb +3 -3
  29. data/lib/solargraph/language_server/message/initialize.rb +3 -1
  30. data/lib/solargraph/language_server/message/text_document/completion.rb +0 -3
  31. data/lib/solargraph/language_server/message/text_document/formatting.rb +4 -0
  32. data/lib/solargraph/library.rb +13 -11
  33. data/lib/solargraph/location.rb +28 -0
  34. data/lib/solargraph/logging.rb +1 -0
  35. data/lib/solargraph/parser/comment_ripper.rb +12 -6
  36. data/lib/solargraph/parser/flow_sensitive_typing.rb +227 -0
  37. data/lib/solargraph/parser/node_methods.rb +15 -1
  38. data/lib/solargraph/parser/node_processor.rb +3 -1
  39. data/lib/solargraph/parser/parser_gem/class_methods.rb +16 -14
  40. data/lib/solargraph/parser/parser_gem/node_chainer.rb +17 -23
  41. data/lib/solargraph/parser/parser_gem/node_methods.rb +5 -3
  42. data/lib/solargraph/parser/parser_gem/node_processors/alias_node.rb +2 -1
  43. data/lib/solargraph/parser/parser_gem/node_processors/and_node.rb +21 -0
  44. data/lib/solargraph/parser/parser_gem/node_processors/args_node.rb +4 -2
  45. data/lib/solargraph/parser/parser_gem/node_processors/block_node.rb +4 -2
  46. data/lib/solargraph/parser/parser_gem/node_processors/casgn_node.rb +23 -2
  47. data/lib/solargraph/parser/parser_gem/node_processors/cvasgn_node.rb +2 -1
  48. data/lib/solargraph/parser/parser_gem/node_processors/def_node.rb +6 -3
  49. data/lib/solargraph/parser/parser_gem/node_processors/defs_node.rb +2 -1
  50. data/lib/solargraph/parser/parser_gem/node_processors/gvasgn_node.rb +2 -1
  51. data/lib/solargraph/parser/parser_gem/node_processors/if_node.rb +21 -0
  52. data/lib/solargraph/parser/parser_gem/node_processors/ivasgn_node.rb +4 -2
  53. data/lib/solargraph/parser/parser_gem/node_processors/lvasgn_node.rb +4 -3
  54. data/lib/solargraph/parser/parser_gem/node_processors/namespace_node.rb +29 -6
  55. data/lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb +41 -0
  56. data/lib/solargraph/parser/parser_gem/node_processors/resbody_node.rb +2 -1
  57. data/lib/solargraph/parser/parser_gem/node_processors/sclass_node.rb +4 -3
  58. data/lib/solargraph/parser/parser_gem/node_processors/send_node.rb +28 -16
  59. data/lib/solargraph/parser/parser_gem/node_processors/sym_node.rb +2 -1
  60. data/lib/solargraph/parser/parser_gem/node_processors/until_node.rb +29 -0
  61. data/lib/solargraph/parser/parser_gem/node_processors/while_node.rb +29 -0
  62. data/lib/solargraph/parser/parser_gem/node_processors.rb +10 -0
  63. data/lib/solargraph/parser/region.rb +1 -1
  64. data/lib/solargraph/parser.rb +1 -0
  65. data/lib/solargraph/pin/base.rb +326 -43
  66. data/lib/solargraph/pin/base_variable.rb +18 -10
  67. data/lib/solargraph/pin/block.rb +3 -3
  68. data/lib/solargraph/pin/breakable.rb +9 -0
  69. data/lib/solargraph/pin/callable.rb +77 -6
  70. data/lib/solargraph/pin/closure.rb +18 -1
  71. data/lib/solargraph/pin/common.rb +5 -0
  72. data/lib/solargraph/pin/delegated_method.rb +20 -1
  73. data/lib/solargraph/pin/documenting.rb +16 -0
  74. data/lib/solargraph/pin/keyword.rb +7 -2
  75. data/lib/solargraph/pin/local_variable.rb +15 -6
  76. data/lib/solargraph/pin/method.rb +172 -42
  77. data/lib/solargraph/pin/namespace.rb +17 -9
  78. data/lib/solargraph/pin/parameter.rb +61 -11
  79. data/lib/solargraph/pin/proxy_type.rb +12 -6
  80. data/lib/solargraph/pin/reference/override.rb +10 -6
  81. data/lib/solargraph/pin/reference/require.rb +2 -2
  82. data/lib/solargraph/pin/signature.rb +42 -0
  83. data/lib/solargraph/pin/singleton.rb +1 -1
  84. data/lib/solargraph/pin/symbol.rb +3 -2
  85. data/lib/solargraph/pin/until.rb +18 -0
  86. data/lib/solargraph/pin/while.rb +18 -0
  87. data/lib/solargraph/pin.rb +4 -1
  88. data/lib/solargraph/pin_cache.rb +185 -0
  89. data/lib/solargraph/position.rb +16 -0
  90. data/lib/solargraph/range.rb +16 -0
  91. data/lib/solargraph/rbs_map/conversions.rb +199 -66
  92. data/lib/solargraph/rbs_map/core_fills.rb +32 -16
  93. data/lib/solargraph/rbs_map/core_map.rb +34 -11
  94. data/lib/solargraph/rbs_map/stdlib_map.rb +15 -5
  95. data/lib/solargraph/rbs_map.rb +74 -16
  96. data/lib/solargraph/shell.rb +19 -18
  97. data/lib/solargraph/source/chain/array.rb +12 -8
  98. data/lib/solargraph/source/chain/block_symbol.rb +1 -1
  99. data/lib/solargraph/source/chain/block_variable.rb +1 -1
  100. data/lib/solargraph/source/chain/call.rb +62 -25
  101. data/lib/solargraph/source/chain/constant.rb +1 -1
  102. data/lib/solargraph/source/chain/hash.rb +9 -3
  103. data/lib/solargraph/source/chain/head.rb +1 -1
  104. data/lib/solargraph/source/chain/if.rb +6 -1
  105. data/lib/solargraph/source/chain/link.rb +19 -5
  106. data/lib/solargraph/source/chain/literal.rb +27 -2
  107. data/lib/solargraph/source/chain/or.rb +1 -1
  108. data/lib/solargraph/source/chain/z_super.rb +1 -1
  109. data/lib/solargraph/source/chain.rb +106 -62
  110. data/lib/solargraph/source/cursor.rb +1 -11
  111. data/lib/solargraph/source/source_chainer.rb +2 -2
  112. data/lib/solargraph/source.rb +2 -1
  113. data/lib/solargraph/source_map/clip.rb +4 -2
  114. data/lib/solargraph/source_map/mapper.rb +9 -5
  115. data/lib/solargraph/source_map.rb +0 -17
  116. data/lib/solargraph/type_checker/checks.rb +4 -0
  117. data/lib/solargraph/type_checker.rb +41 -14
  118. data/lib/solargraph/version.rb +1 -1
  119. data/lib/solargraph/views/_method.erb +10 -10
  120. data/lib/solargraph/views/_namespace.erb +3 -3
  121. data/lib/solargraph/views/document.erb +10 -10
  122. data/lib/solargraph/workspace/config.rb +7 -3
  123. data/lib/solargraph/workspace.rb +16 -6
  124. data/lib/solargraph/yard_map/mapper/to_constant.rb +5 -2
  125. data/lib/solargraph/yard_map/mapper/to_method.rb +55 -15
  126. data/lib/solargraph/yard_map/mapper/to_namespace.rb +5 -2
  127. data/lib/solargraph/yard_map/mapper.rb +5 -3
  128. data/lib/solargraph/yard_map/to_method.rb +4 -2
  129. data/lib/solargraph/yardoc.rb +6 -9
  130. data/lib/solargraph.rb +20 -1
  131. data/rbs/fills/tuple.rbs +150 -0
  132. data/rbs_collection.yaml +19 -0
  133. data/solargraph.gemspec +6 -5
  134. metadata +56 -29
  135. data/lib/solargraph/cache.rb +0 -77
@@ -6,7 +6,9 @@ module Solargraph
6
6
  class RbsMap
7
7
  # Functions for converting RBS declarations to Solargraph pins
8
8
  #
9
- module Conversions
9
+ class Conversions
10
+ include Logging
11
+
10
12
  # A container for tracking the current context of the RBS conversion
11
13
  # process, e.g., what visibility is declared for methods in the current
12
14
  # scope
@@ -20,11 +22,17 @@ module Solargraph
20
22
  end
21
23
  end
22
24
 
23
- # @return [Array<Pin::Base>]
24
- def pins
25
- @pins ||= []
25
+ def initialize(loader:)
26
+ @loader = loader
27
+ @pins = []
28
+ load_environment_to_pins(loader)
26
29
  end
27
30
 
31
+ attr_reader :loader
32
+
33
+ # @return [Array<Pin::Base>]
34
+ attr_reader :pins
35
+
28
36
  private
29
37
 
30
38
  # @return [Hash{String => RBS::AST::Declarations::TypeAlias}]
@@ -37,9 +45,11 @@ module Solargraph
37
45
  def load_environment_to_pins(loader)
38
46
  environment = RBS::Environment.from_loader(loader).resolve_type_names
39
47
  cursor = pins.length
48
+ if environment.declarations.empty?
49
+ Solargraph.logger.info "No RBS declarations found in environment for core_root #{loader.core_root.inspect}, libraries #{loader.libs} and directories #{loader.dirs}"
50
+ return
51
+ end
40
52
  environment.declarations.each { |decl| convert_decl_to_pin(decl, Solargraph::Pin::ROOT_PIN) }
41
- added_pins = pins[cursor..-1]
42
- added_pins.each { |pin| pin.source = :rbs }
43
53
  end
44
54
 
45
55
  # @param decl [RBS::AST::Declarations::Base]
@@ -80,10 +90,14 @@ module Solargraph
80
90
  # @param closure [Pin::Namespace]
81
91
  # @return [void]
82
92
  def convert_self_type_to_pins decl, closure
93
+ type = build_type(decl.name, decl.args)
94
+ generic_values = type.all_params.map(&:to_s)
83
95
  include_pin = Solargraph::Pin::Reference::Include.new(
84
96
  name: decl.name.relative!.to_s,
85
97
  type_location: location_decl_to_pin_location(decl.location),
86
- closure: closure
98
+ generic_values: generic_values,
99
+ closure: closure,
100
+ source: :rbs
87
101
  )
88
102
  pins.push include_pin
89
103
  end
@@ -103,13 +117,13 @@ module Solargraph
103
117
  def convert_member_to_pin member, closure, context
104
118
  case member
105
119
  when RBS::AST::Members::MethodDefinition
106
- method_def_to_pin(member, closure)
120
+ method_def_to_pin(member, closure, context)
107
121
  when RBS::AST::Members::AttrReader
108
- attr_reader_to_pin(member, closure)
122
+ attr_reader_to_pin(member, closure, context)
109
123
  when RBS::AST::Members::AttrWriter
110
- attr_writer_to_pin(member, closure)
124
+ attr_writer_to_pin(member, closure, context)
111
125
  when RBS::AST::Members::AttrAccessor
112
- attr_accessor_to_pin(member, closure)
126
+ attr_accessor_to_pin(member, closure, context)
113
127
  when RBS::AST::Members::Include
114
128
  include_to_pin(member, closure)
115
129
  when RBS::AST::Members::Prepend
@@ -125,9 +139,9 @@ module Solargraph
125
139
  when RBS::AST::Members::InstanceVariable
126
140
  ivar_to_pin(member, closure)
127
141
  when RBS::AST::Members::Public
128
- return Context.new(visibility: :public)
142
+ return Context.new(:public)
129
143
  when RBS::AST::Members::Private
130
- return Context.new(visibility: :private)
144
+ return Context.new(:private)
131
145
  when RBS::AST::Declarations::Base
132
146
  convert_decl_to_pin(member, closure)
133
147
  else
@@ -139,6 +153,14 @@ module Solargraph
139
153
  # @param decl [RBS::AST::Declarations::Class]
140
154
  # @return [void]
141
155
  def class_decl_to_pin decl
156
+ generics = decl.type_params.map(&:name).map(&:to_s)
157
+ generic_defaults = {}
158
+ decl.type_params.each do |param|
159
+ if param.default_type
160
+ tag = other_type_to_tag param.default_type
161
+ generic_defaults[param.name.to_s] = ComplexType.parse(tag).force_rooted
162
+ end
163
+ end
142
164
  class_pin = Solargraph::Pin::Namespace.new(
143
165
  type: :class,
144
166
  name: decl.name.relative!.to_s,
@@ -148,14 +170,20 @@ module Solargraph
148
170
  # @todo some type parameters in core/stdlib have default
149
171
  # values; Solargraph doesn't support that yet as so these
150
172
  # get treated as undefined if not specified
151
- generics: decl.type_params.map(&:name).map(&:to_s)
173
+ generics: generics,
174
+ generic_defaults: generic_defaults,
175
+ source: :rbs
152
176
  )
153
177
  pins.push class_pin
154
178
  if decl.super_class
179
+ type = build_type(decl.super_class.name, decl.super_class.args)
180
+ generic_values = type.all_params.map(&:to_s)
155
181
  pins.push Solargraph::Pin::Reference::Superclass.new(
156
182
  type_location: location_decl_to_pin_location(decl.super_class.location),
157
183
  closure: class_pin,
158
- name: decl.super_class.name.relative!.to_s
184
+ generic_values: generic_values,
185
+ name: decl.super_class.name.relative!.to_s,
186
+ source: :rbs
159
187
  )
160
188
  end
161
189
  add_mixins decl, class_pin
@@ -175,7 +203,8 @@ module Solargraph
175
203
  generics: decl.type_params.map(&:name).map(&:to_s),
176
204
  # HACK: Using :hidden to keep interfaces from appearing in
177
205
  # autocompletion
178
- visibility: :hidden
206
+ visibility: :hidden,
207
+ source: :rbs
179
208
  )
180
209
  class_pin.docstring.add_tag(YARD::Tags::Tag.new(:abstract, '(RBS interface)'))
181
210
  pins.push class_pin
@@ -192,6 +221,7 @@ module Solargraph
192
221
  closure: Solargraph::Pin::ROOT_PIN,
193
222
  comments: decl.comment&.string,
194
223
  generics: decl.type_params.map(&:name).map(&:to_s),
224
+ source: :rbs
195
225
  )
196
226
  pins.push module_pin
197
227
  convert_self_types_to_pins decl, module_pin
@@ -220,7 +250,8 @@ module Solargraph
220
250
  name: name,
221
251
  closure: closure,
222
252
  type_location: location_decl_to_pin_location(decl.location),
223
- comments: comments
253
+ comments: comments,
254
+ source: :rbs
224
255
  )
225
256
  tag = "#{base}<#{tag}>" if base
226
257
  rooted_tag = ComplexType.parse(tag).force_rooted.rooted_tags
@@ -264,32 +295,92 @@ module Solargraph
264
295
  name: name,
265
296
  closure: closure,
266
297
  comments: decl.comment&.string,
298
+ source: :rbs
267
299
  )
268
300
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
269
301
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
270
302
  pins.push pin
271
303
  end
272
304
 
305
+
306
+ # Visibility overrides that will allow the Solargraph project
307
+ # and plugins to pass typechecking using SOLARGRAPH_ASSERTS=on,
308
+ # so that we can detect any regressions/issues elsewhere in the
309
+ # visibility logic.
310
+ #
311
+ # These should either reflect a bug upstream in the RBS
312
+ # definitions, or include a @todo indicating what needs to be
313
+ # fixed in Solargraph to properly understand it.
314
+ #
315
+ # @todo PR these fixes upstream and list open PRs here above
316
+ # related overrides
317
+ # @todo externalize remaining overrides into yaml file, then
318
+ # allow that to be extended via .solargraph.yml
319
+ VISIBILITY_OVERRIDE = {
320
+ ["Rails::Engine", :instance, "run_tasks_blocks"] => :protected,
321
+ # Should have been marked as both instance and class method in module -e.g., 'module_function'
322
+ ["Kernel", :instance, "pretty_inspect"] => :private,
323
+ # marked incorrectly in RBS
324
+ ["WEBrick::HTTPUtils::FormData", :instance, "next_data"] => :protected,
325
+ ["Rails::Command", :class, "command_type"] => :private,
326
+ ["Rails::Command", :class, "lookup_paths"] => :private,
327
+ ["Rails::Command", :class, "file_lookup_paths"] => :private,
328
+ ["Rails::Railtie", :instance, "run_console_blocks"] => :protected,
329
+ ["Rails::Railtie", :instance, "run_generators_blocks"] => :protected,
330
+ ["Rails::Railtie", :instance, "run_runner_blocks"] => :protected,
331
+ ["Rails::Railtie", :instance, "run_tasks_blocks"] => :protected,
332
+ ["ActionController::Base", :instance, "_protected_ivars"] => :private,
333
+ ["ActionView::Template", :instance, "method_name"] => :public,
334
+ ["Module", :instance, "ruby2_keywords"] => :private,
335
+ ["Nokogiri::XML::Node", :instance, "coerce"] => :protected,
336
+ ["Nokogiri::XML::Document", :class, "empty_doc?"] => :private,
337
+ ["Nokogiri::Decorators::Slop", :instance, "respond_to_missing?"] => :public,
338
+ ["RuboCop::Cop::RangeHelp", :instance, "source_range"] => :private,
339
+ ["AST::Node", :instance, "original_dup"] => :private,
340
+ ["Rainbow::Presenter", :instance, "wrap_with_sgr"] => :private,
341
+ }
342
+
343
+ def calculate_method_visibility(decl, context, closure, scope, name)
344
+ override_key = [closure.path, scope, name]
345
+ visibility = VISIBILITY_OVERRIDE[override_key]
346
+ simple_override_key = [closure.path, scope]
347
+ visibility ||= VISIBILITY_OVERRIDE[simple_override_key]
348
+ visibility ||= :private if closure.path == 'Kernel' && Kernel.private_instance_methods(false).include?(decl.name)
349
+ if decl.kind == :singleton_instance
350
+ # this is a 'module function'
351
+ visibility ||= :private
352
+ end
353
+ visibility ||= decl.visibility
354
+ visibility ||= context.visibility
355
+ visibility ||= :public
356
+ visibility
357
+ end
358
+
273
359
  # @param decl [RBS::AST::Members::MethodDefinition]
274
360
  # @param closure [Pin::Closure]
361
+ # @param context [Context]
275
362
  # @return [void]
276
- def method_def_to_pin decl, closure
363
+ def method_def_to_pin decl, closure, context
277
364
  # there may be edge cases here around different signatures
278
365
  # having different type params / orders - we may need to match
279
366
  # this data model and have generics live in signatures to
280
367
  # handle those correctly
281
368
  generics = decl.overloads.map(&:method_type).flat_map(&:type_params).map(&:name).map(&:to_s).uniq
369
+
282
370
  if decl.instance?
371
+ name = decl.name.to_s
372
+ final_scope = :instance
373
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
283
374
  pin = Solargraph::Pin::Method.new(
284
- name: decl.name.to_s,
375
+ name: name,
285
376
  closure: closure,
286
377
  type_location: location_decl_to_pin_location(decl.location),
287
378
  comments: decl.comment&.string,
288
- scope: :instance,
379
+ scope: final_scope,
289
380
  signatures: [],
290
381
  generics: generics,
291
- # @todo RBS core has unreliable visibility definitions
292
- visibility: closure.path == 'Kernel' && Kernel.private_instance_methods(false).include?(decl.name) ? :private : :public
382
+ visibility: visibility,
383
+ source: :rbs
293
384
  )
294
385
  pin.signatures.concat method_def_to_sigs(decl, pin)
295
386
  pins.push pin
@@ -299,14 +390,19 @@ module Solargraph
299
390
  end
300
391
  end
301
392
  if decl.singleton?
393
+ final_scope = :class
394
+ name = decl.name.to_s
395
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
302
396
  pin = Solargraph::Pin::Method.new(
303
- name: decl.name.to_s,
397
+ name: name,
304
398
  closure: closure,
305
399
  comments: decl.comment&.string,
306
400
  type_location: location_decl_to_pin_location(decl.location),
307
- scope: :class,
401
+ visibility: visibility,
402
+ scope: final_scope,
308
403
  signatures: [],
309
- generics: generics
404
+ generics: generics,
405
+ source: :rbs
310
406
  )
311
407
  pin.signatures.concat method_def_to_sigs(decl, pin)
312
408
  pins.push pin
@@ -318,13 +414,15 @@ module Solargraph
318
414
  # @return [void]
319
415
  def method_def_to_sigs decl, pin
320
416
  decl.overloads.map do |overload|
321
- generics = overload.method_type.type_params.map(&:to_s)
417
+ generics = overload.method_type.type_params.map(&:name).map(&:to_s)
322
418
  signature_parameters, signature_return_type = parts_of_function(overload.method_type, pin)
323
419
  block = if overload.method_type.block
324
420
  block_parameters, block_return_type = parts_of_function(overload.method_type.block, pin)
325
- Pin::Signature.new(generics: generics, parameters: block_parameters, return_type: block_return_type)
421
+ Pin::Signature.new(generics: generics, parameters: block_parameters, return_type: block_return_type,
422
+ closure: pin, source: :rbs)
326
423
  end
327
- Pin::Signature.new(generics: generics, parameters: signature_parameters, return_type: signature_return_type, block: block)
424
+ Pin::Signature.new(generics: generics, parameters: signature_parameters, return_type: signature_return_type, block: block,
425
+ closure: pin, source: :rbs)
328
426
  end
329
427
  end
330
428
 
@@ -343,40 +441,44 @@ module Solargraph
343
441
  # @param pin [Pin::Method]
344
442
  # @return [Array(Array<Pin::Parameter>, ComplexType)]
345
443
  def parts_of_function type, pin
346
- return [[Solargraph::Pin::Parameter.new(decl: :restarg, name: 'arg', closure: pin)], ComplexType.try_parse(method_type_to_tag(type)).force_rooted] if defined?(RBS::Types::UntypedFunction) && type.type.is_a?(RBS::Types::UntypedFunction)
444
+ return [[Solargraph::Pin::Parameter.new(decl: :restarg, name: 'arg', closure: pin, source: :rbs)], ComplexType.try_parse(method_type_to_tag(type)).force_rooted] if defined?(RBS::Types::UntypedFunction) && type.type.is_a?(RBS::Types::UntypedFunction)
347
445
 
348
446
  parameters = []
349
447
  arg_num = -1
350
448
  type.type.required_positionals.each do |param|
351
- name = param.name ? param.name.to_s : "arg#{arg_num += 1}"
352
- parameters.push Solargraph::Pin::Parameter.new(decl: :arg, name: name, closure: pin, return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted)
449
+ name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
450
+ parameters.push Solargraph::Pin::Parameter.new(decl: :arg, name: name, closure: pin, return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted, source: :rbs)
353
451
  end
354
452
  type.type.optional_positionals.each do |param|
355
- name = param.name ? param.name.to_s : "arg#{arg_num += 1}"
453
+ name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
356
454
  parameters.push Solargraph::Pin::Parameter.new(decl: :optarg, name: name, closure: pin,
357
- return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted)
455
+ return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted,
456
+ source: :rbs)
358
457
  end
359
458
  if type.type.rest_positionals
360
- name = type.type.rest_positionals.name ? type.type.rest_positionals.name.to_s : "arg#{arg_num += 1}"
361
- parameters.push Solargraph::Pin::Parameter.new(decl: :restarg, name: name, closure: pin)
459
+ name = type.type.rest_positionals.name ? type.type.rest_positionals.name.to_s : "arg_#{arg_num += 1}"
460
+ parameters.push Solargraph::Pin::Parameter.new(decl: :restarg, name: name, closure: pin, source: :rbs)
362
461
  end
363
462
  type.type.trailing_positionals.each do |param|
364
- name = param.name ? param.name.to_s : "arg#{arg_num += 1}"
365
- parameters.push Solargraph::Pin::Parameter.new(decl: :arg, name: name, closure: pin)
463
+ name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
464
+ parameters.push Solargraph::Pin::Parameter.new(decl: :arg, name: name, closure: pin, source: :rbs)
366
465
  end
367
466
  type.type.required_keywords.each do |orig, param|
368
- name = orig ? orig.to_s : "arg#{arg_num += 1}"
467
+ name = orig ? orig.to_s : "arg_#{arg_num += 1}"
369
468
  parameters.push Solargraph::Pin::Parameter.new(decl: :kwarg, name: name, closure: pin,
370
- return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted)
469
+ return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted,
470
+ source: :rbs)
371
471
  end
372
472
  type.type.optional_keywords.each do |orig, param|
373
- name = orig ? orig.to_s : "arg#{arg_num += 1}"
473
+ name = orig ? orig.to_s : "arg_#{arg_num += 1}"
374
474
  parameters.push Solargraph::Pin::Parameter.new(decl: :kwoptarg, name: name, closure: pin,
375
- return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted)
475
+ return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted,
476
+ source: :rbs)
376
477
  end
377
478
  if type.type.rest_keywords
378
- name = type.type.rest_keywords.name ? type.type.rest_keywords.name.to_s : "arg#{arg_num += 1}"
379
- parameters.push Solargraph::Pin::Parameter.new(decl: :kwrestarg, name: type.type.rest_keywords.name.to_s, closure: pin)
479
+ name = type.type.rest_keywords.name ? type.type.rest_keywords.name.to_s : "arg_#{arg_num += 1}"
480
+ parameters.push Solargraph::Pin::Parameter.new(decl: :kwrestarg, name: type.type.rest_keywords.name.to_s, closure: pin,
481
+ source: :rbs)
380
482
  end
381
483
 
382
484
  rooted_tag = method_type_to_tag(type)
@@ -387,32 +489,51 @@ module Solargraph
387
489
  # @param decl [RBS::AST::Members::AttrReader,RBS::AST::Members::AttrAccessor]
388
490
  # @param closure [Pin::Namespace]
389
491
  # @return [void]
390
- def attr_reader_to_pin(decl, closure)
492
+ def attr_reader_to_pin(decl, closure, context)
493
+ name = decl.name.to_s
494
+ final_scope = decl.kind == :instance ? :instance : :class
495
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
391
496
  pin = Solargraph::Pin::Method.new(
392
- name: decl.name.to_s,
497
+ name: name,
393
498
  type_location: location_decl_to_pin_location(decl.location),
394
499
  closure: closure,
395
500
  comments: decl.comment&.string,
396
- scope: :instance,
397
- attribute: true
501
+ scope: final_scope,
502
+ attribute: true,
503
+ visibility: visibility,
504
+ source: :rbs
398
505
  )
399
506
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
400
507
  pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
508
+ logger.debug { "Conversions#attr_reader_to_pin(name=#{name.inspect}, visibility=#{visibility.inspect}) => #{pin.inspect}" }
401
509
  pins.push pin
402
510
  end
403
511
 
404
512
  # @param decl [RBS::AST::Members::AttrWriter, RBS::AST::Members::AttrAccessor]
405
513
  # @param closure [Pin::Namespace]
406
514
  # @return [void]
407
- def attr_writer_to_pin(decl, closure)
515
+ def attr_writer_to_pin(decl, closure, context)
516
+ final_scope = decl.kind == :instance ? :instance : :class
517
+ name = "#{decl.name.to_s}="
518
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
408
519
  pin = Solargraph::Pin::Method.new(
409
- name: "#{decl.name.to_s}=",
520
+ name: name,
410
521
  type_location: location_decl_to_pin_location(decl.location),
411
522
  closure: closure,
523
+ parameters: [],
412
524
  comments: decl.comment&.string,
413
- scope: :instance,
414
- attribute: true
525
+ scope: final_scope,
526
+ attribute: true,
527
+ visibility: visibility,
528
+ source: :rbs
415
529
  )
530
+ pin.parameters <<
531
+ Solargraph::Pin::Parameter.new(
532
+ name: 'value',
533
+ return_type: ComplexType.try_parse(other_type_to_tag(decl.type)).force_rooted,
534
+ source: :rbs,
535
+ closure: pin
536
+ )
416
537
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
417
538
  pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
418
539
  pins.push pin
@@ -421,9 +542,9 @@ module Solargraph
421
542
  # @param decl [RBS::AST::Members::AttrAccessor]
422
543
  # @param closure [Pin::Namespace]
423
544
  # @return [void]
424
- def attr_accessor_to_pin(decl, closure)
425
- attr_reader_to_pin(decl, closure)
426
- attr_writer_to_pin(decl, closure)
545
+ def attr_accessor_to_pin(decl, closure, context)
546
+ attr_reader_to_pin(decl, closure, context)
547
+ attr_writer_to_pin(decl, closure, context)
427
548
  end
428
549
 
429
550
  # @param decl [RBS::AST::Members::InstanceVariable]
@@ -434,7 +555,8 @@ module Solargraph
434
555
  name: decl.name.to_s,
435
556
  closure: closure,
436
557
  type_location: location_decl_to_pin_location(decl.location),
437
- comments: decl.comment&.string
558
+ comments: decl.comment&.string,
559
+ source: :rbs
438
560
  )
439
561
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
440
562
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
@@ -449,7 +571,8 @@ module Solargraph
449
571
  pin = Solargraph::Pin::ClassVariable.new(
450
572
  name: name,
451
573
  closure: closure,
452
- comments: decl.comment&.string
574
+ comments: decl.comment&.string,
575
+ source: :rbs
453
576
  )
454
577
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
455
578
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
@@ -464,7 +587,8 @@ module Solargraph
464
587
  pin = Solargraph::Pin::InstanceVariable.new(
465
588
  name: name,
466
589
  closure: closure,
467
- comments: decl.comment&.string
590
+ comments: decl.comment&.string,
591
+ source: :rbs
468
592
  )
469
593
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
470
594
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
@@ -481,7 +605,8 @@ module Solargraph
481
605
  name: decl.name.relative!.to_s,
482
606
  type_location: location_decl_to_pin_location(decl.location),
483
607
  generic_values: generic_values,
484
- closure: closure
608
+ closure: closure,
609
+ source: :rbs
485
610
  )
486
611
  end
487
612
 
@@ -492,7 +617,8 @@ module Solargraph
492
617
  pins.push Solargraph::Pin::Reference::Prepend.new(
493
618
  name: decl.name.relative!.to_s,
494
619
  type_location: location_decl_to_pin_location(decl.location),
495
- closure: closure
620
+ closure: closure,
621
+ source: :rbs
496
622
  )
497
623
  end
498
624
 
@@ -503,7 +629,8 @@ module Solargraph
503
629
  pins.push Solargraph::Pin::Reference::Extend.new(
504
630
  name: decl.name.relative!.to_s,
505
631
  type_location: location_decl_to_pin_location(decl.location),
506
- closure: closure
632
+ closure: closure,
633
+ source: :rbs
507
634
  )
508
635
  end
509
636
 
@@ -511,11 +638,14 @@ module Solargraph
511
638
  # @param closure [Pin::Namespace]
512
639
  # @return [void]
513
640
  def alias_to_pin decl, closure
641
+ final_scope = decl.singleton? ? :class : :instance
514
642
  pins.push Solargraph::Pin::MethodAlias.new(
515
643
  name: decl.new_name.to_s,
516
644
  type_location: location_decl_to_pin_location(decl.location),
517
645
  original: decl.old_name.to_s,
518
- closure: closure
646
+ closure: closure,
647
+ scope: final_scope,
648
+ source: :rbs,
519
649
  )
520
650
  end
521
651
 
@@ -565,14 +695,13 @@ module Solargraph
565
695
  if type.is_a?(RBS::Types::Optional)
566
696
  "#{other_type_to_tag(type.type)}, nil"
567
697
  elsif type.is_a?(RBS::Types::Bases::Any)
568
- # @todo Not sure what to do with Any yet
569
- 'BasicObject'
698
+ 'undefined'
570
699
  elsif type.is_a?(RBS::Types::Bases::Bool)
571
700
  'Boolean'
572
701
  elsif type.is_a?(RBS::Types::Tuple)
573
702
  "Array(#{type.types.map { |t| other_type_to_tag(t) }.join(', ')})"
574
703
  elsif type.is_a?(RBS::Types::Literal)
575
- type.literal.to_s
704
+ type.literal.inspect
576
705
  elsif type.is_a?(RBS::Types::Union)
577
706
  type.types.map { |t| other_type_to_tag(t) }.join(', ')
578
707
  elsif type.is_a?(RBS::Types::Record)
@@ -628,10 +757,14 @@ module Solargraph
628
757
  def add_mixins decl, namespace
629
758
  decl.each_mixin do |mixin|
630
759
  klass = mixin.is_a?(RBS::AST::Members::Include) ? Pin::Reference::Include : Pin::Reference::Extend
760
+ type = build_type(mixin.name, mixin.args)
761
+ generic_values = type.all_params.map(&:to_s)
631
762
  pins.push klass.new(
632
763
  name: mixin.name.relative!.to_s,
633
- location: location_decl_to_pin_location(mixin.location),
634
- closure: namespace
764
+ type_location: location_decl_to_pin_location(mixin.location),
765
+ generic_values: generic_values,
766
+ closure: namespace,
767
+ source: :rbs
635
768
  )
636
769
  end
637
770
  end
@@ -13,38 +13,54 @@ module Solargraph
13
13
  'elsif', 'end', 'ensure', 'false', 'for', 'if', 'in', 'module', 'next',
14
14
  'nil', 'not', 'or', 'redo', 'rescue', 'retry', 'return', 'self', 'super',
15
15
  'then', 'true', 'undef', 'unless', 'until', 'when', 'while', 'yield'
16
- ].map { |k| Pin::Keyword.new(k) }
16
+ ].map { |k| Pin::Keyword.new(k, source: :core_fill) }
17
17
 
18
18
  MISSING = [
19
- Solargraph::Pin::Method.new(name: 'tap', scope: :instance,
20
- closure: Solargraph::Pin::Namespace.new(name: 'Object')),
21
19
  Solargraph::Pin::Method.new(name: 'class', scope: :instance,
22
- closure: Solargraph::Pin::Namespace.new(name: 'Object'), comments: '@return [::Class<self>]')
20
+ closure: Solargraph::Pin::Namespace.new(name: 'Object', source: :core_fill), comments: '@return [::Class<self>]',
21
+ source: :core_fill)
23
22
  ]
24
23
 
25
24
  OVERRIDES = [
26
- Override.from_comment('BasicObject#instance_eval', '@yieldreceiver [self]'),
27
- Override.from_comment('BasicObject#instance_exec', '@yieldreceiver [self]'),
28
- Override.from_comment('Module#define_method', '@yieldreceiver [::Object<self>]'),
29
- Override.from_comment('Module#class_eval', '@yieldreceiver [::Class<self>]'),
30
- Override.from_comment('Module#class_exec', '@yieldreceiver [::Class<self>]'),
31
- Override.from_comment('Module#module_eval', '@yieldreceiver [::Module<self>]'),
32
- Override.from_comment('Module#module_exec', '@yieldreceiver [::Module<self>]'),
25
+ Override.from_comment('BasicObject#instance_eval', '@yieldreceiver [self]',
26
+ source: :core_fill),
27
+ Override.from_comment('BasicObject#instance_exec', '@yieldreceiver [self]',
28
+ source: :core_fill),
29
+ Override.from_comment('Module#define_method', '@yieldreceiver [::Object<self>]',
30
+ source: :core_fill),
31
+ Override.from_comment('Module#class_eval', '@yieldreceiver [::Class<self>]',
32
+ source: :core_fill),
33
+ Override.from_comment('Module#class_exec', '@yieldreceiver [::Class<self>]',
34
+ source: :core_fill),
35
+ Override.from_comment('Module#module_eval', '@yieldreceiver [::Module<self>]',
36
+ source: :core_fill),
37
+ Override.from_comment('Module#module_exec', '@yieldreceiver [::Module<self>]',
38
+ source: :core_fill),
33
39
  # RBS does not define Class with a generic, so all calls to
34
40
  # generic() return an 'untyped'. We can do better:
35
- Override.method_return('Class#allocate', 'self')
41
+ Override.method_return('Class#allocate', 'self', source: :core_fill),
42
+ ]
43
+
44
+ # @todo I don't see any direct link in RBS to build this from -
45
+ # presumably RBS is using duck typing to match interfaces
46
+ # against concrete classes
47
+ INCLUDES = [
48
+ Solargraph::Pin::Reference::Include.new(name: '_ToAry',
49
+ closure: Solargraph::Pin::Namespace.new(name: 'Array', source: :core_fill),
50
+ generic_values: ['generic<Elem>'],
51
+ source: :core_fill)
36
52
  ]
37
53
 
38
54
  # HACK: Add Errno exception classes
39
- errno = Solargraph::Pin::Namespace.new(name: 'Errno')
55
+ errno = Solargraph::Pin::Namespace.new(name: 'Errno', source: :core_fill)
40
56
  errnos = []
41
57
  Errno.constants.each do |const|
42
- errnos.push Solargraph::Pin::Namespace.new(type: :class, name: const.to_s, closure: errno)
43
- errnos.push Solargraph::Pin::Reference::Superclass.new(closure: errnos.last, name: 'SystemCallError')
58
+ errnos.push Solargraph::Pin::Namespace.new(type: :class, name: const.to_s, closure: errno, source: :core_fill)
59
+ errnos.push Solargraph::Pin::Reference::Superclass.new(closure: errnos.last, name: 'SystemCallError', source: :core_fill)
44
60
  end
45
61
  ERRNOS = errnos
46
62
 
47
- ALL = KEYWORDS + MISSING + OVERRIDES + ERRNOS
63
+ ALL = KEYWORDS + MISSING + OVERRIDES + ERRNOS + INCLUDES
48
64
  end
49
65
  end
50
66
  end
@@ -5,23 +5,46 @@ module Solargraph
5
5
  # Ruby core pins
6
6
  #
7
7
  class CoreMap
8
- include Conversions
9
8
 
10
- def initialize
11
- cache = Cache.load('core.ser')
9
+ def resolved?
10
+ true
11
+ end
12
+
13
+ FILLS_DIRECTORY = File.join(File.dirname(__FILE__), '..', '..', '..', 'rbs', 'fills')
14
+
15
+ def initialize; end
16
+
17
+ def pins
18
+ return @pins if @pins
19
+
20
+ @pins = []
21
+ cache = PinCache.deserialize_core
12
22
  if cache
13
- pins.replace cache
23
+ @pins.replace cache
14
24
  else
15
- loader = RBS::EnvironmentLoader.new(repository: RBS::Repository.new(no_stdlib: false))
16
- RBS::Environment.from_loader(loader).resolve_type_names
17
- load_environment_to_pins(loader)
18
- pins.concat RbsMap::CoreFills::ALL
25
+ loader.add(path: Pathname(FILLS_DIRECTORY))
26
+ @pins = conversions.pins
27
+ @pins.concat RbsMap::CoreFills::ALL
19
28
  processed = ApiMap::Store.new(pins).pins.reject { |p| p.is_a?(Solargraph::Pin::Reference::Override) }
20
- processed.each { |pin| pin.source = :rbs }
21
- pins.replace processed
29
+ @pins.replace processed
22
30
 
23
- Cache.save('core.ser', pins)
31
+ PinCache.serialize_core @pins
24
32
  end
33
+ @pins
34
+ end
35
+
36
+ def loader
37
+ @loader ||= RBS::EnvironmentLoader.new(repository: RBS::Repository.new(no_stdlib: false))
38
+ end
39
+
40
+ private
41
+
42
+ def loader
43
+ @loader ||= RBS::EnvironmentLoader.new(repository: RBS::Repository.new(no_stdlib: false))
44
+ end
45
+
46
+ def conversions
47
+ @conversions ||= Conversions.new(loader: loader)
25
48
  end
26
49
  end
27
50
  end