solargraph 0.55.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 (102) 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 +26 -0
  6. data/README.md +13 -3
  7. data/lib/solargraph/api_map/index.rb +23 -15
  8. data/lib/solargraph/api_map/store.rb +8 -4
  9. data/lib/solargraph/api_map.rb +151 -58
  10. data/lib/solargraph/complex_type/type_methods.rb +6 -1
  11. data/lib/solargraph/complex_type/unique_type.rb +10 -2
  12. data/lib/solargraph/convention/base.rb +3 -3
  13. data/lib/solargraph/convention.rb +3 -3
  14. data/lib/solargraph/doc_map.rb +255 -69
  15. data/lib/solargraph/gem_pins.rb +53 -37
  16. data/lib/solargraph/language_server/host.rb +11 -2
  17. data/lib/solargraph/language_server/message/extended/check_gem_version.rb +2 -0
  18. data/lib/solargraph/language_server/message/extended/document.rb +5 -2
  19. data/lib/solargraph/language_server/message/extended/document_gems.rb +3 -3
  20. data/lib/solargraph/library.rb +6 -3
  21. data/lib/solargraph/location.rb +13 -0
  22. data/lib/solargraph/logging.rb +1 -0
  23. data/lib/solargraph/parser/comment_ripper.rb +1 -0
  24. data/lib/solargraph/parser/flow_sensitive_typing.rb +5 -4
  25. data/lib/solargraph/parser/node_processor.rb +3 -1
  26. data/lib/solargraph/parser/parser_gem/class_methods.rb +5 -8
  27. data/lib/solargraph/parser/parser_gem/node_methods.rb +1 -1
  28. data/lib/solargraph/parser/parser_gem/node_processors/alias_node.rb +2 -1
  29. data/lib/solargraph/parser/parser_gem/node_processors/args_node.rb +4 -2
  30. data/lib/solargraph/parser/parser_gem/node_processors/block_node.rb +4 -2
  31. data/lib/solargraph/parser/parser_gem/node_processors/casgn_node.rb +4 -3
  32. data/lib/solargraph/parser/parser_gem/node_processors/cvasgn_node.rb +2 -1
  33. data/lib/solargraph/parser/parser_gem/node_processors/def_node.rb +6 -3
  34. data/lib/solargraph/parser/parser_gem/node_processors/defs_node.rb +2 -1
  35. data/lib/solargraph/parser/parser_gem/node_processors/gvasgn_node.rb +2 -1
  36. data/lib/solargraph/parser/parser_gem/node_processors/ivasgn_node.rb +4 -2
  37. data/lib/solargraph/parser/parser_gem/node_processors/lvasgn_node.rb +2 -1
  38. data/lib/solargraph/parser/parser_gem/node_processors/namespace_node.rb +6 -4
  39. data/lib/solargraph/parser/parser_gem/node_processors/resbody_node.rb +2 -1
  40. data/lib/solargraph/parser/parser_gem/node_processors/sclass_node.rb +4 -3
  41. data/lib/solargraph/parser/parser_gem/node_processors/send_node.rb +28 -16
  42. data/lib/solargraph/parser/parser_gem/node_processors/sym_node.rb +2 -1
  43. data/lib/solargraph/parser/parser_gem/node_processors/until_node.rb +1 -0
  44. data/lib/solargraph/parser/parser_gem/node_processors/while_node.rb +1 -0
  45. data/lib/solargraph/parser/region.rb +1 -1
  46. data/lib/solargraph/pin/base.rb +295 -28
  47. data/lib/solargraph/pin/base_variable.rb +9 -8
  48. data/lib/solargraph/pin/callable.rb +74 -3
  49. data/lib/solargraph/pin/closure.rb +18 -1
  50. data/lib/solargraph/pin/common.rb +5 -0
  51. data/lib/solargraph/pin/delegated_method.rb +2 -0
  52. data/lib/solargraph/pin/documenting.rb +16 -0
  53. data/lib/solargraph/pin/keyword.rb +7 -2
  54. data/lib/solargraph/pin/local_variable.rb +8 -5
  55. data/lib/solargraph/pin/method.rb +147 -25
  56. data/lib/solargraph/pin/namespace.rb +7 -2
  57. data/lib/solargraph/pin/parameter.rb +47 -6
  58. data/lib/solargraph/pin/proxy_type.rb +3 -3
  59. data/lib/solargraph/pin/reference/override.rb +10 -6
  60. data/lib/solargraph/pin/reference/require.rb +2 -2
  61. data/lib/solargraph/pin/signature.rb +42 -0
  62. data/lib/solargraph/pin/singleton.rb +1 -1
  63. data/lib/solargraph/pin/symbol.rb +3 -2
  64. data/lib/solargraph/pin.rb +1 -1
  65. data/lib/solargraph/pin_cache.rb +185 -0
  66. data/lib/solargraph/position.rb +9 -0
  67. data/lib/solargraph/range.rb +9 -0
  68. data/lib/solargraph/rbs_map/conversions.rb +183 -56
  69. data/lib/solargraph/rbs_map/core_fills.rb +24 -15
  70. data/lib/solargraph/rbs_map/core_map.rb +34 -11
  71. data/lib/solargraph/rbs_map/stdlib_map.rb +15 -5
  72. data/lib/solargraph/rbs_map.rb +74 -17
  73. data/lib/solargraph/shell.rb +17 -18
  74. data/lib/solargraph/source/chain/array.rb +7 -4
  75. data/lib/solargraph/source/chain/block_symbol.rb +1 -1
  76. data/lib/solargraph/source/chain/block_variable.rb +1 -1
  77. data/lib/solargraph/source/chain/call.rb +8 -7
  78. data/lib/solargraph/source/chain/hash.rb +1 -1
  79. data/lib/solargraph/source/chain/head.rb +1 -1
  80. data/lib/solargraph/source/chain/if.rb +1 -1
  81. data/lib/solargraph/source/chain/literal.rb +2 -2
  82. data/lib/solargraph/source/chain/or.rb +1 -1
  83. data/lib/solargraph/source/chain.rb +2 -2
  84. data/lib/solargraph/source_map/mapper.rb +9 -5
  85. data/lib/solargraph/source_map.rb +0 -17
  86. data/lib/solargraph/version.rb +1 -1
  87. data/lib/solargraph/views/_method.erb +10 -10
  88. data/lib/solargraph/views/_namespace.erb +3 -3
  89. data/lib/solargraph/views/document.erb +10 -10
  90. data/lib/solargraph/workspace.rb +15 -5
  91. data/lib/solargraph/yard_map/mapper/to_constant.rb +4 -2
  92. data/lib/solargraph/yard_map/mapper/to_method.rb +14 -1
  93. data/lib/solargraph/yard_map/mapper/to_namespace.rb +4 -2
  94. data/lib/solargraph/yard_map/mapper.rb +4 -3
  95. data/lib/solargraph/yard_map/to_method.rb +4 -2
  96. data/lib/solargraph/yardoc.rb +6 -9
  97. data/lib/solargraph.rb +19 -1
  98. data/rbs/fills/tuple.rbs +150 -0
  99. data/rbs_collection.yaml +19 -0
  100. data/solargraph.gemspec +1 -0
  101. metadata +20 -7
  102. 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]
@@ -86,7 +96,8 @@ module Solargraph
86
96
  name: decl.name.relative!.to_s,
87
97
  type_location: location_decl_to_pin_location(decl.location),
88
98
  generic_values: generic_values,
89
- closure: closure
99
+ closure: closure,
100
+ source: :rbs
90
101
  )
91
102
  pins.push include_pin
92
103
  end
@@ -106,13 +117,13 @@ module Solargraph
106
117
  def convert_member_to_pin member, closure, context
107
118
  case member
108
119
  when RBS::AST::Members::MethodDefinition
109
- method_def_to_pin(member, closure)
120
+ method_def_to_pin(member, closure, context)
110
121
  when RBS::AST::Members::AttrReader
111
- attr_reader_to_pin(member, closure)
122
+ attr_reader_to_pin(member, closure, context)
112
123
  when RBS::AST::Members::AttrWriter
113
- attr_writer_to_pin(member, closure)
124
+ attr_writer_to_pin(member, closure, context)
114
125
  when RBS::AST::Members::AttrAccessor
115
- attr_accessor_to_pin(member, closure)
126
+ attr_accessor_to_pin(member, closure, context)
116
127
  when RBS::AST::Members::Include
117
128
  include_to_pin(member, closure)
118
129
  when RBS::AST::Members::Prepend
@@ -128,9 +139,9 @@ module Solargraph
128
139
  when RBS::AST::Members::InstanceVariable
129
140
  ivar_to_pin(member, closure)
130
141
  when RBS::AST::Members::Public
131
- return Context.new(visibility: :public)
142
+ return Context.new(:public)
132
143
  when RBS::AST::Members::Private
133
- return Context.new(visibility: :private)
144
+ return Context.new(:private)
134
145
  when RBS::AST::Declarations::Base
135
146
  convert_decl_to_pin(member, closure)
136
147
  else
@@ -142,6 +153,14 @@ module Solargraph
142
153
  # @param decl [RBS::AST::Declarations::Class]
143
154
  # @return [void]
144
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
145
164
  class_pin = Solargraph::Pin::Namespace.new(
146
165
  type: :class,
147
166
  name: decl.name.relative!.to_s,
@@ -151,14 +170,20 @@ module Solargraph
151
170
  # @todo some type parameters in core/stdlib have default
152
171
  # values; Solargraph doesn't support that yet as so these
153
172
  # get treated as undefined if not specified
154
- generics: decl.type_params.map(&:name).map(&:to_s)
173
+ generics: generics,
174
+ generic_defaults: generic_defaults,
175
+ source: :rbs
155
176
  )
156
177
  pins.push class_pin
157
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)
158
181
  pins.push Solargraph::Pin::Reference::Superclass.new(
159
182
  type_location: location_decl_to_pin_location(decl.super_class.location),
160
183
  closure: class_pin,
161
- 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
162
187
  )
163
188
  end
164
189
  add_mixins decl, class_pin
@@ -178,7 +203,8 @@ module Solargraph
178
203
  generics: decl.type_params.map(&:name).map(&:to_s),
179
204
  # HACK: Using :hidden to keep interfaces from appearing in
180
205
  # autocompletion
181
- visibility: :hidden
206
+ visibility: :hidden,
207
+ source: :rbs
182
208
  )
183
209
  class_pin.docstring.add_tag(YARD::Tags::Tag.new(:abstract, '(RBS interface)'))
184
210
  pins.push class_pin
@@ -195,6 +221,7 @@ module Solargraph
195
221
  closure: Solargraph::Pin::ROOT_PIN,
196
222
  comments: decl.comment&.string,
197
223
  generics: decl.type_params.map(&:name).map(&:to_s),
224
+ source: :rbs
198
225
  )
199
226
  pins.push module_pin
200
227
  convert_self_types_to_pins decl, module_pin
@@ -223,7 +250,8 @@ module Solargraph
223
250
  name: name,
224
251
  closure: closure,
225
252
  type_location: location_decl_to_pin_location(decl.location),
226
- comments: comments
253
+ comments: comments,
254
+ source: :rbs
227
255
  )
228
256
  tag = "#{base}<#{tag}>" if base
229
257
  rooted_tag = ComplexType.parse(tag).force_rooted.rooted_tags
@@ -267,32 +295,92 @@ module Solargraph
267
295
  name: name,
268
296
  closure: closure,
269
297
  comments: decl.comment&.string,
298
+ source: :rbs
270
299
  )
271
300
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
272
301
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
273
302
  pins.push pin
274
303
  end
275
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
+
276
359
  # @param decl [RBS::AST::Members::MethodDefinition]
277
360
  # @param closure [Pin::Closure]
361
+ # @param context [Context]
278
362
  # @return [void]
279
- def method_def_to_pin decl, closure
363
+ def method_def_to_pin decl, closure, context
280
364
  # there may be edge cases here around different signatures
281
365
  # having different type params / orders - we may need to match
282
366
  # this data model and have generics live in signatures to
283
367
  # handle those correctly
284
368
  generics = decl.overloads.map(&:method_type).flat_map(&:type_params).map(&:name).map(&:to_s).uniq
369
+
285
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)
286
374
  pin = Solargraph::Pin::Method.new(
287
- name: decl.name.to_s,
375
+ name: name,
288
376
  closure: closure,
289
377
  type_location: location_decl_to_pin_location(decl.location),
290
378
  comments: decl.comment&.string,
291
- scope: :instance,
379
+ scope: final_scope,
292
380
  signatures: [],
293
381
  generics: generics,
294
- # @todo RBS core has unreliable visibility definitions
295
- visibility: closure.path == 'Kernel' && Kernel.private_instance_methods(false).include?(decl.name) ? :private : :public
382
+ visibility: visibility,
383
+ source: :rbs
296
384
  )
297
385
  pin.signatures.concat method_def_to_sigs(decl, pin)
298
386
  pins.push pin
@@ -302,14 +390,19 @@ module Solargraph
302
390
  end
303
391
  end
304
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)
305
396
  pin = Solargraph::Pin::Method.new(
306
- name: decl.name.to_s,
397
+ name: name,
307
398
  closure: closure,
308
399
  comments: decl.comment&.string,
309
400
  type_location: location_decl_to_pin_location(decl.location),
310
- scope: :class,
401
+ visibility: visibility,
402
+ scope: final_scope,
311
403
  signatures: [],
312
- generics: generics
404
+ generics: generics,
405
+ source: :rbs
313
406
  )
314
407
  pin.signatures.concat method_def_to_sigs(decl, pin)
315
408
  pins.push pin
@@ -325,9 +418,11 @@ module Solargraph
325
418
  signature_parameters, signature_return_type = parts_of_function(overload.method_type, pin)
326
419
  block = if overload.method_type.block
327
420
  block_parameters, block_return_type = parts_of_function(overload.method_type.block, pin)
328
- 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)
329
423
  end
330
- 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)
331
426
  end
332
427
  end
333
428
 
@@ -346,40 +441,44 @@ module Solargraph
346
441
  # @param pin [Pin::Method]
347
442
  # @return [Array(Array<Pin::Parameter>, ComplexType)]
348
443
  def parts_of_function type, pin
349
- 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)
350
445
 
351
446
  parameters = []
352
447
  arg_num = -1
353
448
  type.type.required_positionals.each do |param|
354
449
  name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
355
- 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)
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)
356
451
  end
357
452
  type.type.optional_positionals.each do |param|
358
453
  name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
359
454
  parameters.push Solargraph::Pin::Parameter.new(decl: :optarg, name: name, closure: pin,
360
- 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)
361
457
  end
362
458
  if type.type.rest_positionals
363
459
  name = type.type.rest_positionals.name ? type.type.rest_positionals.name.to_s : "arg_#{arg_num += 1}"
364
- parameters.push Solargraph::Pin::Parameter.new(decl: :restarg, name: name, closure: pin)
460
+ parameters.push Solargraph::Pin::Parameter.new(decl: :restarg, name: name, closure: pin, source: :rbs)
365
461
  end
366
462
  type.type.trailing_positionals.each do |param|
367
463
  name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
368
- parameters.push Solargraph::Pin::Parameter.new(decl: :arg, name: name, closure: pin)
464
+ parameters.push Solargraph::Pin::Parameter.new(decl: :arg, name: name, closure: pin, source: :rbs)
369
465
  end
370
466
  type.type.required_keywords.each do |orig, param|
371
467
  name = orig ? orig.to_s : "arg_#{arg_num += 1}"
372
468
  parameters.push Solargraph::Pin::Parameter.new(decl: :kwarg, name: name, closure: pin,
373
- 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)
374
471
  end
375
472
  type.type.optional_keywords.each do |orig, param|
376
473
  name = orig ? orig.to_s : "arg_#{arg_num += 1}"
377
474
  parameters.push Solargraph::Pin::Parameter.new(decl: :kwoptarg, name: name, closure: pin,
378
- 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)
379
477
  end
380
478
  if type.type.rest_keywords
381
479
  name = type.type.rest_keywords.name ? type.type.rest_keywords.name.to_s : "arg_#{arg_num += 1}"
382
- parameters.push Solargraph::Pin::Parameter.new(decl: :kwrestarg, name: type.type.rest_keywords.name.to_s, closure: pin)
480
+ parameters.push Solargraph::Pin::Parameter.new(decl: :kwrestarg, name: type.type.rest_keywords.name.to_s, closure: pin,
481
+ source: :rbs)
383
482
  end
384
483
 
385
484
  rooted_tag = method_type_to_tag(type)
@@ -390,32 +489,51 @@ module Solargraph
390
489
  # @param decl [RBS::AST::Members::AttrReader,RBS::AST::Members::AttrAccessor]
391
490
  # @param closure [Pin::Namespace]
392
491
  # @return [void]
393
- 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)
394
496
  pin = Solargraph::Pin::Method.new(
395
- name: decl.name.to_s,
497
+ name: name,
396
498
  type_location: location_decl_to_pin_location(decl.location),
397
499
  closure: closure,
398
500
  comments: decl.comment&.string,
399
- scope: :instance,
400
- attribute: true
501
+ scope: final_scope,
502
+ attribute: true,
503
+ visibility: visibility,
504
+ source: :rbs
401
505
  )
402
506
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
403
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}" }
404
509
  pins.push pin
405
510
  end
406
511
 
407
512
  # @param decl [RBS::AST::Members::AttrWriter, RBS::AST::Members::AttrAccessor]
408
513
  # @param closure [Pin::Namespace]
409
514
  # @return [void]
410
- 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)
411
519
  pin = Solargraph::Pin::Method.new(
412
- name: "#{decl.name.to_s}=",
520
+ name: name,
413
521
  type_location: location_decl_to_pin_location(decl.location),
414
522
  closure: closure,
523
+ parameters: [],
415
524
  comments: decl.comment&.string,
416
- scope: :instance,
417
- attribute: true
525
+ scope: final_scope,
526
+ attribute: true,
527
+ visibility: visibility,
528
+ source: :rbs
418
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
+ )
419
537
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
420
538
  pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
421
539
  pins.push pin
@@ -424,9 +542,9 @@ module Solargraph
424
542
  # @param decl [RBS::AST::Members::AttrAccessor]
425
543
  # @param closure [Pin::Namespace]
426
544
  # @return [void]
427
- def attr_accessor_to_pin(decl, closure)
428
- attr_reader_to_pin(decl, closure)
429
- 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)
430
548
  end
431
549
 
432
550
  # @param decl [RBS::AST::Members::InstanceVariable]
@@ -437,7 +555,8 @@ module Solargraph
437
555
  name: decl.name.to_s,
438
556
  closure: closure,
439
557
  type_location: location_decl_to_pin_location(decl.location),
440
- comments: decl.comment&.string
558
+ comments: decl.comment&.string,
559
+ source: :rbs
441
560
  )
442
561
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
443
562
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
@@ -452,7 +571,8 @@ module Solargraph
452
571
  pin = Solargraph::Pin::ClassVariable.new(
453
572
  name: name,
454
573
  closure: closure,
455
- comments: decl.comment&.string
574
+ comments: decl.comment&.string,
575
+ source: :rbs
456
576
  )
457
577
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
458
578
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
@@ -467,7 +587,8 @@ module Solargraph
467
587
  pin = Solargraph::Pin::InstanceVariable.new(
468
588
  name: name,
469
589
  closure: closure,
470
- comments: decl.comment&.string
590
+ comments: decl.comment&.string,
591
+ source: :rbs
471
592
  )
472
593
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
473
594
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
@@ -484,7 +605,8 @@ module Solargraph
484
605
  name: decl.name.relative!.to_s,
485
606
  type_location: location_decl_to_pin_location(decl.location),
486
607
  generic_values: generic_values,
487
- closure: closure
608
+ closure: closure,
609
+ source: :rbs
488
610
  )
489
611
  end
490
612
 
@@ -495,7 +617,8 @@ module Solargraph
495
617
  pins.push Solargraph::Pin::Reference::Prepend.new(
496
618
  name: decl.name.relative!.to_s,
497
619
  type_location: location_decl_to_pin_location(decl.location),
498
- closure: closure
620
+ closure: closure,
621
+ source: :rbs
499
622
  )
500
623
  end
501
624
 
@@ -506,7 +629,8 @@ module Solargraph
506
629
  pins.push Solargraph::Pin::Reference::Extend.new(
507
630
  name: decl.name.relative!.to_s,
508
631
  type_location: location_decl_to_pin_location(decl.location),
509
- closure: closure
632
+ closure: closure,
633
+ source: :rbs
510
634
  )
511
635
  end
512
636
 
@@ -514,11 +638,14 @@ module Solargraph
514
638
  # @param closure [Pin::Namespace]
515
639
  # @return [void]
516
640
  def alias_to_pin decl, closure
641
+ final_scope = decl.singleton? ? :class : :instance
517
642
  pins.push Solargraph::Pin::MethodAlias.new(
518
643
  name: decl.new_name.to_s,
519
644
  type_location: location_decl_to_pin_location(decl.location),
520
645
  original: decl.old_name.to_s,
521
- closure: closure
646
+ closure: closure,
647
+ scope: final_scope,
648
+ source: :rbs,
522
649
  )
523
650
  end
524
651
 
@@ -568,8 +695,7 @@ module Solargraph
568
695
  if type.is_a?(RBS::Types::Optional)
569
696
  "#{other_type_to_tag(type.type)}, nil"
570
697
  elsif type.is_a?(RBS::Types::Bases::Any)
571
- # @todo Not sure what to do with Any yet
572
- 'BasicObject'
698
+ 'undefined'
573
699
  elsif type.is_a?(RBS::Types::Bases::Bool)
574
700
  'Boolean'
575
701
  elsif type.is_a?(RBS::Types::Tuple)
@@ -637,7 +763,8 @@ module Solargraph
637
763
  name: mixin.name.relative!.to_s,
638
764
  type_location: location_decl_to_pin_location(mixin.location),
639
765
  generic_values: generic_values,
640
- closure: namespace
766
+ closure: namespace,
767
+ source: :rbs
641
768
  )
642
769
  end
643
770
  end
@@ -13,24 +13,32 @@ 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
19
  Solargraph::Pin::Method.new(name: 'class', scope: :instance,
20
- 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)
21
22
  ]
22
23
 
23
24
  OVERRIDES = [
24
- Override.from_comment('BasicObject#instance_eval', '@yieldreceiver [self]'),
25
- Override.from_comment('BasicObject#instance_exec', '@yieldreceiver [self]'),
26
- Override.from_comment('Module#define_method', '@yieldreceiver [::Object<self>]'),
27
- Override.from_comment('Module#class_eval', '@yieldreceiver [::Class<self>]'),
28
- Override.from_comment('Module#class_exec', '@yieldreceiver [::Class<self>]'),
29
- Override.from_comment('Module#module_eval', '@yieldreceiver [::Module<self>]'),
30
- 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),
31
39
  # RBS does not define Class with a generic, so all calls to
32
40
  # generic() return an 'untyped'. We can do better:
33
- Override.method_return('Class#allocate', 'self')
41
+ Override.method_return('Class#allocate', 'self', source: :core_fill),
34
42
  ]
35
43
 
36
44
  # @todo I don't see any direct link in RBS to build this from -
@@ -38,16 +46,17 @@ module Solargraph
38
46
  # against concrete classes
39
47
  INCLUDES = [
40
48
  Solargraph::Pin::Reference::Include.new(name: '_ToAry',
41
- closure: Solargraph::Pin::Namespace.new(name: 'Array'),
42
- generic_values: ['generic<Elem>'])
49
+ closure: Solargraph::Pin::Namespace.new(name: 'Array', source: :core_fill),
50
+ generic_values: ['generic<Elem>'],
51
+ source: :core_fill)
43
52
  ]
44
53
 
45
54
  # HACK: Add Errno exception classes
46
- errno = Solargraph::Pin::Namespace.new(name: 'Errno')
55
+ errno = Solargraph::Pin::Namespace.new(name: 'Errno', source: :core_fill)
47
56
  errnos = []
48
57
  Errno.constants.each do |const|
49
- errnos.push Solargraph::Pin::Namespace.new(type: :class, name: const.to_s, closure: errno)
50
- 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)
51
60
  end
52
61
  ERRNOS = errnos
53
62
 
@@ -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
@@ -7,19 +7,29 @@ module Solargraph
7
7
  # Ruby stdlib pins
8
8
  #
9
9
  class StdlibMap < RbsMap
10
+ include Logging
11
+
10
12
  # @type [Hash{String => RbsMap}]
11
13
  @stdlib_maps_hash = {}
12
14
 
13
15
  # @param library [String]
14
16
  def initialize library
15
- cache = Cache.load('stdlib', "#{library}.ser")
16
- if cache
17
- pins.replace cache
17
+ cached_pins = PinCache.deserialize_stdlib_require library
18
+ if cached_pins
19
+ @pins = cached_pins
18
20
  @resolved = true
21
+ @loaded = true
22
+ logger.debug { "Deserialized #{cached_pins.length} cached pins for stdlib require #{library.inspect}" }
19
23
  else
20
24
  super
21
- return unless resolved?
22
- Cache.save('stdlib', "#{library}.ser", pins)
25
+ unless resolved?
26
+ @pins = []
27
+ logger.info { "Could not resolve #{library.inspect}" }
28
+ return
29
+ end
30
+ generated_pins = pins
31
+ logger.debug { "Found #{generated_pins.length} pins for stdlib library #{library}" }
32
+ PinCache.serialize_stdlib_require library, generated_pins
23
33
  end
24
34
  end
25
35