solargraph 0.60.4 → 0.62.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 (127) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/linting.yml +24 -2
  3. data/.github/workflows/plugins.yml +19 -1
  4. data/.github/workflows/rspec.yml +17 -2
  5. data/.github/workflows/typecheck.yml +6 -0
  6. data/CHANGELOG.md +54 -0
  7. data/Gemfile +0 -3
  8. data/lib/solargraph/api_map/index.rb +28 -4
  9. data/lib/solargraph/api_map/store.rb +19 -0
  10. data/lib/solargraph/api_map.rb +91 -80
  11. data/lib/solargraph/cache_dir.rb +58 -0
  12. data/lib/solargraph/collection/base.rb +50 -0
  13. data/lib/solargraph/collection/core.rb +17 -0
  14. data/lib/solargraph/collection/gem.rb +59 -0
  15. data/lib/solargraph/collection/stdlib.rb +25 -0
  16. data/lib/solargraph/collection.rb +30 -0
  17. data/lib/solargraph/complex_type/unique_type.rb +13 -0
  18. data/lib/solargraph/complex_type.rb +23 -3
  19. data/lib/solargraph/convention.rb +5 -7
  20. data/lib/solargraph/diagnostics/require_not_found.rb +0 -4
  21. data/lib/solargraph/diagnostics/type_check.rb +0 -1
  22. data/lib/solargraph/external.rb +187 -0
  23. data/lib/solargraph/language_server/host/diagnoser.rb +1 -1
  24. data/lib/solargraph/language_server/host/dispatch.rb +4 -2
  25. data/lib/solargraph/language_server/host.rb +10 -2
  26. data/lib/solargraph/language_server/message/completion_item/resolve.rb +10 -3
  27. data/lib/solargraph/language_server/message/text_document/definition.rb +5 -2
  28. data/lib/solargraph/language_server/message/text_document/hover.rb +1 -2
  29. data/lib/solargraph/library.rb +26 -53
  30. data/lib/solargraph/metagem.rb +80 -0
  31. data/lib/solargraph/parser/node_processor/base.rb +6 -0
  32. data/lib/solargraph/parser/parser_gem/node_chainer.rb +22 -3
  33. data/lib/solargraph/parser/parser_gem/node_methods.rb +29 -6
  34. data/lib/solargraph/parser/parser_gem/node_processors/class_definition/class_assignment_node.rb +98 -0
  35. data/lib/solargraph/parser/parser_gem/node_processors/class_definition.rb +54 -0
  36. data/lib/solargraph/parser/parser_gem/node_processors/cvasgn_node.rb +1 -0
  37. data/lib/solargraph/parser/parser_gem/node_processors/data_definition/data_assignment_node.rb +65 -0
  38. data/lib/solargraph/parser/parser_gem/node_processors/data_definition/data_definition_node.rb +97 -0
  39. data/lib/solargraph/parser/parser_gem/node_processors/data_definition.rb +110 -0
  40. data/lib/solargraph/parser/parser_gem/node_processors/gvasgn_node.rb +1 -0
  41. data/lib/solargraph/parser/parser_gem/node_processors/ivasgn_node.rb +2 -0
  42. data/lib/solargraph/parser/parser_gem/node_processors/lvasgn_node.rb +1 -0
  43. data/lib/solargraph/parser/parser_gem/node_processors/send_node.rb +185 -0
  44. data/lib/solargraph/parser/parser_gem/node_processors/struct_definition/struct_assignment_node.rb +66 -0
  45. data/lib/solargraph/parser/parser_gem/node_processors/struct_definition/struct_definition_node.rb +108 -0
  46. data/lib/solargraph/parser/parser_gem/node_processors/struct_definition.rb +170 -0
  47. data/lib/solargraph/parser/parser_gem/node_processors.rb +33 -29
  48. data/lib/solargraph/pin/base.rb +25 -1
  49. data/lib/solargraph/pin/base_variable.rb +31 -1
  50. data/lib/solargraph/pin/callable.rb +11 -2
  51. data/lib/solargraph/pin/closure.rb +17 -0
  52. data/lib/solargraph/pin/delegated_method.rb +5 -0
  53. data/lib/solargraph/pin/duck_method.rb +36 -16
  54. data/lib/solargraph/pin/factory_parameter.rb +84 -0
  55. data/lib/solargraph/pin/instance_variable.rb +45 -0
  56. data/lib/solargraph/pin/method.rb +10 -0
  57. data/lib/solargraph/pin/method_alias.rb +4 -0
  58. data/lib/solargraph/pin/parameter.rb +1 -1
  59. data/lib/solargraph/pin/reference/included_mixin.rb +22 -0
  60. data/lib/solargraph/pin/reference.rb +1 -0
  61. data/lib/solargraph/pin.rb +1 -0
  62. data/lib/solargraph/plugin/active_support.rb +100 -0
  63. data/lib/solargraph/plugin/addon.rb +16 -0
  64. data/lib/solargraph/plugin/base.rb +25 -0
  65. data/lib/solargraph/plugin/panel.rb +37 -0
  66. data/lib/solargraph/plugin.rb +26 -0
  67. data/lib/solargraph/rbs_collection.rb +119 -0
  68. data/lib/solargraph/rbs_map/base.rb +31 -0
  69. data/lib/solargraph/rbs_map/conversions.rb +109 -48
  70. data/lib/solargraph/rbs_map/{core_map.rb → core.rb} +18 -39
  71. data/lib/solargraph/rbs_map/core_fills.rb +1 -1
  72. data/lib/solargraph/rbs_map/gem.rb +16 -0
  73. data/lib/solargraph/rbs_map/helpers.rb +61 -0
  74. data/lib/solargraph/rbs_map/path.rb +19 -0
  75. data/lib/solargraph/rbs_map/stdlib.rb +24 -0
  76. data/lib/solargraph/rbs_map.rb +7 -198
  77. data/lib/solargraph/rbs_translator.rb +105 -70
  78. data/lib/solargraph/repo.rb +177 -0
  79. data/lib/solargraph/shell.rb +49 -99
  80. data/lib/solargraph/source/chain/call.rb +58 -14
  81. data/lib/solargraph/source/chain/literal.rb +17 -17
  82. data/lib/solargraph/source/chain/parameter.rb +79 -0
  83. data/lib/solargraph/source/chain.rb +8 -0
  84. data/lib/solargraph/source/node_map.rb +27 -0
  85. data/lib/solargraph/source/source_chainer.rb +40 -44
  86. data/lib/solargraph/source.rb +17 -0
  87. data/lib/solargraph/source_map/call.rb +18 -0
  88. data/lib/solargraph/source_map/clip.rb +5 -3
  89. data/lib/solargraph/source_map/mapper.rb +6 -2
  90. data/lib/solargraph/source_map.rb +28 -9
  91. data/lib/solargraph/type_checker/rules.rb +3 -2
  92. data/lib/solargraph/type_checker.rb +4 -1
  93. data/lib/solargraph/version.rb +1 -1
  94. data/lib/solargraph/workspace.rb +0 -1
  95. data/lib/solargraph/yard_map/cache.rb +1 -1
  96. data/lib/solargraph/yard_map/directives/attribute_directive.rb +1 -1
  97. data/lib/solargraph/yard_map/directives/domain_directive.rb +1 -1
  98. data/lib/solargraph/yard_map/directives/method_directive.rb +1 -1
  99. data/lib/solargraph/yard_map/directives/override_directive.rb +1 -1
  100. data/lib/solargraph/yard_map/directives/parse_directive.rb +1 -1
  101. data/lib/solargraph/yard_map/directives/scope_directive.rb +178 -0
  102. data/lib/solargraph/yard_map/directives/visibility_directive.rb +1 -1
  103. data/lib/solargraph/yard_map/directives.rb +5 -2
  104. data/lib/solargraph/yard_map/helpers.rb +1 -1
  105. data/lib/solargraph/yard_map/macro.rb +85 -6
  106. data/lib/solargraph/yard_map/mapper/to_constant.rb +1 -1
  107. data/lib/solargraph/yard_map/mapper/to_method.rb +1 -1
  108. data/lib/solargraph/yard_map/mapper/to_namespace.rb +1 -1
  109. data/lib/solargraph/yard_map/mapper.rb +2 -2
  110. data/lib/solargraph/yard_map.rb +1 -1
  111. data/lib/solargraph/yardoc.rb +43 -60
  112. data/lib/solargraph.rb +8 -3
  113. data/rbs/fills/boolean/0/boolean.rbs +12 -0
  114. data/solargraph.gemspec +2 -2
  115. metadata +41 -18
  116. data/lib/solargraph/convention/active_support_concern.rb +0 -111
  117. data/lib/solargraph/convention/data_definition/data_assignment_node.rb +0 -61
  118. data/lib/solargraph/convention/data_definition/data_definition_node.rb +0 -93
  119. data/lib/solargraph/convention/data_definition.rb +0 -108
  120. data/lib/solargraph/convention/struct_definition/struct_assignment_node.rb +0 -62
  121. data/lib/solargraph/convention/struct_definition/struct_definition_node.rb +0 -103
  122. data/lib/solargraph/convention/struct_definition.rb +0 -168
  123. data/lib/solargraph/doc_map.rb +0 -439
  124. data/lib/solargraph/gem_pins.rb +0 -87
  125. data/lib/solargraph/pin_cache.rb +0 -248
  126. data/lib/solargraph/rbs_map/stdlib_map.rb +0 -71
  127. data/lib/solargraph/workspace/gemspecs.rb +0 -373
@@ -3,7 +3,7 @@
3
3
  require 'rbs'
4
4
 
5
5
  module Solargraph
6
- class RbsMap
6
+ module RbsMap
7
7
  # Functions for converting RBS declarations to Solargraph pins
8
8
  #
9
9
  class Conversions
@@ -37,6 +37,11 @@ module Solargraph
37
37
 
38
38
  private
39
39
 
40
+ # @return [Hash{String => RBS::AST::Declarations::TypeAlias}]
41
+ def type_alias_decls
42
+ @type_alias_decls ||= {}
43
+ end
44
+
40
45
  # @param loader [RBS::EnvironmentLoader]
41
46
  #
42
47
  # @return [void]
@@ -48,7 +53,62 @@ module Solargraph
48
53
  "directories #{loader.dirs}"
49
54
  return
50
55
  end
56
+ # Register up front so expansion does not depend on declaration order.
57
+ core_aware_environment(loader, fallback: environment).type_alias_decls.each_value do |entry|
58
+ type_alias_decls[entry.decl.name.to_s] = entry.decl
59
+ end
60
+ # @sg-ignore Need a downcast here
51
61
  environment.declarations.each { |decl| convert_decl_to_pin(decl, Solargraph::Pin::ROOT_PIN) }
62
+ rescue RBS::DuplicatedDeclarationError => e
63
+ Solargraph.logger.warn "RBS conversion failed due to duplicate declarations: [#{e.class}] #{e.message}"
64
+ end
65
+
66
+ # A non-core RbsMap omits core so its pins do not re-declare CoreMap's,
67
+ # which leaves #resolve_type_names unable to bind an alias pointing into
68
+ # core. Resolve alias names against core separately; pins do not.
69
+ #
70
+ # @param loader [RBS::EnvironmentLoader]
71
+ # @param fallback [RBS::Environment] the already-resolved, core-less
72
+ # environment to use if a core-aware one can't be built
73
+ # @return [RBS::Environment]
74
+ def core_aware_environment loader, fallback:
75
+ return fallback unless loader.core_root.nil?
76
+ return fallback unless rebound_alias?(fallback)
77
+
78
+ # Loading core makes RBS add the stringio library, which only the
79
+ # stdlib repository provides.
80
+ core_repository = RBS::Repository.new(no_stdlib: false)
81
+ loader.repository.dirs.each { |dir| core_repository.add(dir) unless core_repository.dirs.include?(dir) }
82
+ core_loader = RBS::EnvironmentLoader.new(repository: core_repository)
83
+ loader.libs.each { |lib| core_loader.add(library: lib.name, version: lib.version) }
84
+ loader.dirs.each { |dir| core_loader.add(path: dir) }
85
+ RBS::Environment.from_loader(core_loader).resolve_type_names
86
+ rescue RBS::DuplicatedDeclarationError => e
87
+ # A local RBS shim may redeclare a core name; nominal tags beat no pins.
88
+ logger.warn do
89
+ "Could not build a core-aware environment for #{loader.libs}: #{e.message}. " \
90
+ 'Type aliases referencing names declared in RBS core will not be expanded ' \
91
+ 'for these libraries, and will be typed by their alias name instead.'
92
+ end
93
+ fallback
94
+ end
95
+
96
+ # A reference into absent core survives #resolve_type_names naming
97
+ # either the alias itself or a name nothing declares.
98
+ #
99
+ # @param environment [RBS::Environment]
100
+ # @return [Boolean]
101
+ def rebound_alias? environment
102
+ decls = environment.type_alias_decls
103
+ decls.any? do |name, entry|
104
+ # @sg-ignore Unresolved call to decl on generic<D>
105
+ type = entry.decl.type
106
+ # @sg-ignore Unresolved call to is_a? on generic<D>
107
+ next false unless type.is_a?(RBS::Types::Alias)
108
+
109
+ # @sg-ignore Unresolved call to to_s on generic<D>
110
+ type.name.to_s == name.to_s || !decls.key?(type.name)
111
+ end
52
112
  end
53
113
 
54
114
  # @param decl [RBS::AST::Declarations::Base]
@@ -75,11 +135,12 @@ module Solargraph
75
135
  # @sg-ignore flow sensitive typing should support case/when
76
136
  "Ignoring closure #{closure.inspect} on alias type name #{decl.name}")
77
137
  end
138
+ # @sg-ignore flow sensitive typing should support case/when
139
+ alias_return_type = RbsTranslator.to_complex_type(decl.type, type_alias_decls: type_alias_decls).force_rooted
78
140
  pins.push(
79
- # @sg-ignore Wrong argument type for Solargraph::Pin::Reference::TypeAlias.new: return_type expected Solargraph::ComplexType, received Solargraph::ComplexType::UniqueType, Solargraph::ComplexType
80
141
  Solargraph::Pin::Reference::TypeAlias.new(
81
- # @sg-ignore Unresolved calls to name, type, type_location; return_type type mismatch
82
- name: ComplexType.try_parse(decl.name.to_s).to_s, return_type: RbsTranslator.to_complex_type(decl.type).force_rooted, closure: closure, source: :rbs, type_location: location_decl_to_pin_location(decl.location)
142
+ # @sg-ignore flow sensitive typing should support case/when
143
+ name: ComplexType.try_parse(decl.name.to_s).to_s, return_type: alias_return_type, closure: closure, source: :rbs, type_location: location_decl_to_pin_location(decl.location)
83
144
  )
84
145
  )
85
146
  when RBS::AST::Declarations::Module
@@ -253,10 +314,10 @@ module Solargraph
253
314
  # @type [Hash{String => ComplexType, ComplexType::UniqueType}]
254
315
  generic_defaults = {}
255
316
  decl.type_params.each do |param|
256
- if param.default_type
257
- complex_type = RbsTranslator.to_complex_type(param.default_type).force_rooted
258
- generic_defaults[param.name.to_s] = complex_type
259
- end
317
+ next unless param.default_type
318
+ # @sg-ignore Wrong argument type for to_complex_type: type expected RBS::Types::Bases::Base, received union of concrete subtypes, nil
319
+ complex_type = RbsTranslator.to_complex_type(param.default_type, type_alias_decls: type_alias_decls).force_rooted
320
+ generic_defaults[param.name.to_s] = complex_type
260
321
  end
261
322
 
262
323
  class_name = fqns(decl.name)
@@ -280,7 +341,6 @@ module Solargraph
280
341
  if decl.super_class
281
342
  type = build_type(decl.super_class.name, decl.super_class.args)
282
343
  generic_values = type.all_params.map(&:to_s)
283
- superclass_name = decl.super_class.name.to_s
284
344
  pins.push Solargraph::Pin::Reference::Superclass.new(
285
345
  type_location: location_decl_to_pin_location(decl.super_class.location),
286
346
  closure: class_pin,
@@ -392,7 +452,7 @@ module Solargraph
392
452
  # @param decl [RBS::AST::Declarations::Constant]
393
453
  # @return [void]
394
454
  def constant_decl_to_pin decl
395
- tag = RbsTranslator.to_complex_type(decl.type)
455
+ tag = RbsTranslator.to_complex_type(decl.type, type_alias_decls: type_alias_decls)
396
456
  pins.push create_constant(decl.name.relative!.to_s, tag, decl.comment&.string, decl)
397
457
  end
398
458
 
@@ -408,7 +468,7 @@ module Solargraph
408
468
  type_location: location_decl_to_pin_location(decl.location),
409
469
  source: :rbs
410
470
  )
411
- rooted_tag = RbsTranslator.to_complex_type(decl.type).force_rooted.rooted_tags
471
+ rooted_tag = RbsTranslator.to_complex_type(decl.type, type_alias_decls: type_alias_decls).force_rooted.rooted_tags
412
472
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
413
473
  pins.push pin
414
474
  end
@@ -514,51 +574,51 @@ module Solargraph
514
574
  pin.instance_variable_set(:@return_type, ComplexType::VOID)
515
575
  end
516
576
  end
517
- if decl.singleton?
518
- final_scope = :class
519
- name = decl.name.to_s
520
- visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
521
- pin = Solargraph::Pin::Method.new(
522
- name: name,
523
- closure: closure,
524
- comments: decl.comment&.string,
525
- type_location: location_decl_to_pin_location(decl.location),
526
- visibility: visibility,
527
- scope: final_scope,
528
- signatures: [],
529
- generics: generics,
530
- source: :rbs
531
- )
532
- pin.signatures.concat method_def_to_sigs(decl, pin)
533
- pins.push pin
534
- end
577
+ return unless decl.singleton?
578
+ final_scope = :class
579
+ name = decl.name.to_s
580
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
581
+ pin = Solargraph::Pin::Method.new(
582
+ name: name,
583
+ closure: closure,
584
+ comments: decl.comment&.string,
585
+ type_location: location_decl_to_pin_location(decl.location),
586
+ visibility: visibility,
587
+ scope: final_scope,
588
+ signatures: [],
589
+ generics: generics,
590
+ source: :rbs
591
+ )
592
+ pin.signatures.concat method_def_to_sigs(decl, pin)
593
+ pins.push pin
535
594
  end
536
595
 
537
596
  # @param decl [RBS::AST::Members::MethodDefinition]
538
597
  # @param pin [Pin::Method]
539
598
  # @return [void]
540
599
  def method_def_to_sigs decl, pin
541
- # rubocop:disable Style/SafeNavigationChainLength
542
- implicit_nil = decl.overloads.first&.annotations&.map(&:string)&.include?('implicitly-returns-nil') || false
543
- # rubocop:enable Style/SafeNavigationChainLength
544
600
  # @param overload [RBS::AST::Members::MethodDefinition::Overload]
545
601
  decl.overloads.map do |overload|
602
+ # RBS attaches this annotation to a single overload, not to the method as a whole
603
+ implicit_nil = overload.annotations.map(&:string).include?('implicitly-returns-nil')
546
604
  type_location = location_decl_to_pin_location(overload.method_type.location)
547
605
  generics = overload.method_type.type_params.map(&:name).map(&:to_s)
548
606
  signature_parameters, signature_return_type = parts_of_function(overload.method_type, pin, implicit_nil)
549
607
  block = if overload.method_type.block
550
- block_parameters, block_return_type = parts_of_function(overload.method_type.block, pin, implicit_nil)
608
+ # the annotation describes the method's return value, not the block's
609
+ block_parameters, block_return_type = parts_of_function(overload.method_type.block, pin, false)
551
610
  Pin::Signature.new(generics: generics, parameters: block_parameters, return_type: block_return_type, source: :rbs,
552
611
  type_location: type_location, closure: pin)
553
612
  end
554
- Pin::Signature.new(generics: generics, parameters: signature_parameters, return_type: signature_return_type, block: block, source: :rbs,
613
+ Pin::Signature.new(generics: generics, parameters: signature_parameters, return_type: signature_return_type, block: block,
614
+ block_required: overload.method_type.block&.required || false, source: :rbs,
555
615
  type_location: type_location, closure: pin)
556
616
  end
557
617
  end
558
618
 
559
619
  # @param location [RBS::Location, nil]
560
620
  # @return [Solargraph::Location, nil]
561
- def location_decl_to_pin_location(location)
621
+ def location_decl_to_pin_location location
562
622
  return nil if location&.name.nil?
563
623
 
564
624
  start_pos = Position.new(location.start_line - 1, location.start_column)
@@ -573,7 +633,7 @@ module Solargraph
573
633
  # @return [Array(Array<Pin::Parameter>, ComplexType)]
574
634
  def parts_of_function type, pin, implicit_nil
575
635
  [
576
- RbsTranslator.to_parameter_pins(type, pin, pin.parameter_names),
636
+ RbsTranslator.to_parameter_pins(type, pin, pin.parameter_names, type_alias_decls: type_alias_decls),
577
637
  extract_method_type_return_type(type, implicit_nil).force_rooted
578
638
  ]
579
639
  end
@@ -596,7 +656,7 @@ module Solargraph
596
656
  visibility: visibility,
597
657
  source: :rbs
598
658
  )
599
- rooted_tag = RbsTranslator.to_complex_type(decl.type).force_rooted.rooted_tags
659
+ rooted_tag = RbsTranslator.to_complex_type(decl.type, type_alias_decls: type_alias_decls).force_rooted.rooted_tags
600
660
  pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
601
661
  logger.debug do
602
662
  "Conversions#attr_reader_to_pin(name=#{name.inspect}, visibility=#{visibility.inspect}) => #{pin.inspect}"
@@ -627,12 +687,12 @@ module Solargraph
627
687
  pin.parameters <<
628
688
  Solargraph::Pin::Parameter.new(
629
689
  name: 'value',
630
- return_type: RbsTranslator.to_complex_type(decl.type).force_rooted,
690
+ return_type: RbsTranslator.to_complex_type(decl.type, type_alias_decls: type_alias_decls).force_rooted,
631
691
  source: :rbs,
632
692
  closure: pin,
633
693
  type_location: type_location
634
694
  )
635
- rooted_tag = RbsTranslator.to_complex_type(decl.type).force_rooted.rooted_tags
695
+ rooted_tag = RbsTranslator.to_complex_type(decl.type, type_alias_decls: type_alias_decls).force_rooted.rooted_tags
636
696
  pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
637
697
  pins.push pin
638
698
  end
@@ -657,7 +717,7 @@ module Solargraph
657
717
  comments: decl.comment&.string,
658
718
  source: :rbs
659
719
  )
660
- rooted_tag = RbsTranslator.to_complex_type(decl.type).force_rooted.rooted_tags
720
+ rooted_tag = RbsTranslator.to_complex_type(decl.type, type_alias_decls: type_alias_decls).force_rooted.rooted_tags
661
721
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
662
722
  pins.push pin
663
723
  end
@@ -674,7 +734,7 @@ module Solargraph
674
734
  type_location: location_decl_to_pin_location(decl.location),
675
735
  source: :rbs
676
736
  )
677
- rooted_tag = RbsTranslator.to_complex_type(decl.type).force_rooted.rooted_tags
737
+ rooted_tag = RbsTranslator.to_complex_type(decl.type, type_alias_decls: type_alias_decls).force_rooted.rooted_tags
678
738
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
679
739
  pins.push pin
680
740
  end
@@ -691,7 +751,7 @@ module Solargraph
691
751
  type_location: location_decl_to_pin_location(decl.location),
692
752
  source: :rbs
693
753
  )
694
- rooted_tag = RbsTranslator.to_complex_type(decl.type).force_rooted.rooted_tags
754
+ rooted_tag = RbsTranslator.to_complex_type(decl.type, type_alias_decls: type_alias_decls).force_rooted.rooted_tags
695
755
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
696
756
  pins.push pin
697
757
  end
@@ -760,7 +820,7 @@ module Solargraph
760
820
  'int' => 'Integer',
761
821
  'untyped' => '',
762
822
  'NilClass' => 'nil'
763
- }
823
+ }.freeze
764
824
  private_constant :RBS_TO_YARD_TYPE
765
825
 
766
826
  # Extract a ComplexType from a MethodType's return type.
@@ -769,18 +829,19 @@ module Solargraph
769
829
  #
770
830
  # @param type [RBS::MethodType]
771
831
  # @return [ComplexType]
832
+ # @param [Object] implicit_nil
772
833
  def extract_method_type_return_type type, implicit_nil
773
- tag = RbsTranslator.to_complex_type(type.type.return_type)
774
- return ComplexType.parse("#{tag}, nil") if tag && implicit_nil
775
- tag
834
+ tag = RbsTranslator.to_complex_type(type.type.return_type, type_alias_decls: type_alias_decls)
835
+ return ComplexType.parse("#{tag}, nil") if tag && implicit_nil
836
+ tag
776
837
  end
777
838
 
778
839
  # @param type_name [RBS::TypeName]
779
840
  # @param type_args [Enumerable<RBS::Types::Bases::Base>]
780
841
  # @return [ComplexType::UniqueType]
781
- def build_type(type_name, type_args = [])
842
+ def build_type type_name, type_args = []
782
843
  base = RBS_TO_YARD_TYPE[type_name.relative!.to_s] || type_name.relative!.to_s
783
- params = type_args.map { |arg| RbsTranslator.to_complex_type(arg).force_rooted }
844
+ params = type_args.map { |arg| RbsTranslator.to_complex_type(arg, type_alias_decls: type_alias_decls).force_rooted }
784
845
  if base == 'Hash' && params.length == 2
785
846
  ComplexType::UniqueType.new(base, [params.first], [params.last], rooted: true, parameters_type: :hash)
786
847
  else
@@ -1,39 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Solargraph
4
- class RbsMap
5
- # Ruby core pins
6
- #
7
- class CoreMap
8
- include Logging
4
+ module RbsMap
5
+ class Core < Base
6
+ FILLS_DIRECTORY = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'rbs', 'fills'))
9
7
 
10
- def resolved?
11
- true
8
+ def pins
9
+ @pins ||= generate_pins
12
10
  end
13
11
 
14
- FILLS_DIRECTORY = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'rbs', 'fills'))
15
-
16
- def initialize; end
12
+ # @return [RBS::EnvironmentLoader]
13
+ def loader
14
+ @loader ||= RBS::EnvironmentLoader.new(repository: repository)
15
+ end
17
16
 
18
- # @param out [IO, nil] output stream for logging
19
- # @return [Enumerable<Pin::Base>]
20
- def pins out: $stderr
21
- return @pins if @pins
22
- @pins = cache_core(out: out)
17
+ def repository
18
+ @repository ||= RBS::Repository.new(no_stdlib: false)
23
19
  end
24
20
 
25
- # @param out [StringIO, IO, nil] output stream for logging
21
+ private
22
+
26
23
  # @return [Array<Pin::Base>]
27
- def cache_core out: $stderr
28
- new_pins = []
29
- cache = PinCache.deserialize_core
30
- return cache if cache
31
- new_pins.concat conversions.pins
24
+ def generate_pins
25
+ new_pins = RbsMap::Conversions.new(loader: loader).pins
32
26
 
33
27
  # Avoid RBS::DuplicatedDeclarationError by loading in a different EnvironmentLoader
34
28
  fill_loader = RBS::EnvironmentLoader.new(core_root: nil, repository: RBS::Repository.new(no_stdlib: false))
35
29
  fill_loader.add(path: Pathname(FILLS_DIRECTORY))
36
- out&.puts 'Caching RBS pins for Ruby core'
37
30
  fill_conversions = Conversions.new(loader: fill_loader)
38
31
  new_pins.concat fill_conversions.pins
39
32
 
@@ -42,23 +35,9 @@ module Solargraph
42
35
 
43
36
  # process overrides, then remove any which couldn't be resolved
44
37
  processed = ApiMap::Store.new(new_pins).pins.reject { |p| p.is_a?(Solargraph::Pin::Reference::Override) }
45
- new_pins.replace processed
46
-
47
- PinCache.serialize_core new_pins
48
-
49
- new_pins
50
- end
51
-
52
- private
53
-
54
- # @return [RBS::EnvironmentLoader]
55
- def loader
56
- @loader ||= RBS::EnvironmentLoader.new(repository: RBS::Repository.new(no_stdlib: false))
57
- end
58
-
59
- # @return [Conversions]
60
- def conversions
61
- @conversions ||= Conversions.new(loader: loader)
38
+ # serial = Marshal.dump(processed)
39
+ # File.write cache_file, serial, mode: 'wb'
40
+ processed
62
41
  end
63
42
  end
64
43
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Solargraph
4
- class RbsMap
4
+ module RbsMap
5
5
  # Override pins to fill gaps in RbsMap::CoreMap coverage
6
6
  #
7
7
  module CoreFills
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Solargraph
4
+ module RbsMap
5
+ class Gem < Base
6
+ # @param metagem [Metagem]
7
+ def initialize metagem
8
+ super()
9
+ # Gems ship their signatures in sig/, the directory RBS itself
10
+ # loads; other .rbs files (e.g., the rbs gem's core/) are not the
11
+ # gem's own API
12
+ loader.add path: Pathname.new(metagem.full_path).join('sig')
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Solargraph
4
+ module RbsMap
5
+ module Helpers
6
+ module_function
7
+
8
+ # @param orig_pins [Array<Pin::Base>]
9
+ # @param rbs_pins [Array<Pin::Base>]
10
+ # @return [Array<Pin::Base>]
11
+ def combine orig_pins, rbs_pins
12
+ in_orig = Set.new
13
+ # @todo There's gotta be a better way!
14
+ rbs_api_map = Solargraph::ApiMap.new(pins: rbs_pins)
15
+ combined = orig_pins.map do |orig_pin|
16
+ in_orig.add orig_pin.path
17
+ rbs_pin = rbs_api_map.get_path_pins(orig_pin.path).filter { |pin| pin.is_a? Pin::Method }.first
18
+ next orig_pin unless rbs_pin && orig_pin.instance_of?(Pin::Method)
19
+
20
+ unless rbs_pin
21
+ # @sg-ignore https://github.com/castwide/solargraph/pull/1114
22
+ Solargraph.logger.debug { "GemPins.combine: No rbs pin for #{orig_pin.path} - using YARD's '#{orig_pin.inspect} (return_type=#{orig_pin.return_type}; signatures=#{orig_pin.signatures})" }
23
+ next orig_pin
24
+ end
25
+
26
+ out = combine_method_pins(rbs_pin, orig_pin)
27
+ Solargraph.logger.debug { "GemPins.combine: Combining yard.path=#{orig_pin.path} - rbs=#{rbs_pin.inspect} with yard=#{orig_pin.inspect} into #{out}" }
28
+ out
29
+ end
30
+ in_rbs_only = rbs_pins.select do |pin|
31
+ pin.path.nil? || !in_orig.include?(pin.path)
32
+ end
33
+ out = combined + in_rbs_only
34
+ Solargraph.logger.debug { "GemPins#combine: Returning #{out.length} combined pins" }
35
+ out
36
+ end
37
+
38
+ # @param pins [Array<Pin::Method>]
39
+ # @return [Pin::Method, nil]
40
+ def combine_method_pins(*pins)
41
+ # @type [Pin::Method, nil]
42
+ combined_pin = nil
43
+ # @param memo [Pin::Method, nil]
44
+ # @param pin [Pin::Method]
45
+ out = pins.reduce(combined_pin) do |memo, pin|
46
+ next pin if memo.nil?
47
+ # @todo Maybe wrong now. External runs multiple rounds of combinations
48
+ # if memo == pin && memo.source != :combined
49
+ # # @todo we should track down situations where we are handled
50
+ # # the same pin from the same source here and eliminate them -
51
+ # # this is an efficiency workaround for now
52
+ # next memo
53
+ # end
54
+ memo.combine_with(pin)
55
+ end
56
+ Solargraph.logger.debug { "GemPins.combine_method_pins(pins.length=#{pins.length}, pins=#{pins}) => #{out.inspect}" }
57
+ out
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Solargraph
4
+ module RbsMap
5
+ class Path < Base
6
+ # @param path [String]
7
+ # @return [Array<Pin::Base>]
8
+ def self.pins path
9
+ new(path).pins
10
+ end
11
+
12
+ # @param path [String]
13
+ def initialize path
14
+ super()
15
+ loader.add path: Pathname.new(path)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rbs'
4
+
5
+ module Solargraph
6
+ module RbsMap
7
+ # Ruby stdlib pins
8
+ #
9
+ class Stdlib < Base
10
+ def initialize library
11
+ super()
12
+ loader.add(library: library.gsub('/', '-')) if self.class.has?(library)
13
+ end
14
+
15
+ def repository
16
+ @repository ||= RBS::Repository.new(no_stdlib: false)
17
+ end
18
+
19
+ def self.has? library
20
+ !!RBS::Collection::Sources::Stdlib.instance.has?(library.gsub('/', '-'), nil)
21
+ end
22
+ end
23
+ end
24
+ end