solargraph 0.58.2 → 0.59.0.dev.1

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 (154) hide show
  1. checksums.yaml +4 -4
  2. data/.envrc +3 -0
  3. data/.github/workflows/linting.yml +4 -5
  4. data/.github/workflows/plugins.yml +40 -36
  5. data/.github/workflows/rspec.yml +45 -13
  6. data/.github/workflows/typecheck.yml +2 -2
  7. data/.gitignore +0 -1
  8. data/.rubocop_todo.yml +27 -49
  9. data/CHANGELOG.md +1 -7
  10. data/README.md +3 -3
  11. data/Rakefile +1 -0
  12. data/lib/solargraph/api_map/cache.rb +3 -3
  13. data/lib/solargraph/api_map/constants.rb +13 -3
  14. data/lib/solargraph/api_map/index.rb +22 -11
  15. data/lib/solargraph/api_map/source_to_yard.rb +13 -1
  16. data/lib/solargraph/api_map/store.rb +11 -8
  17. data/lib/solargraph/api_map.rb +105 -50
  18. data/lib/solargraph/complex_type/conformance.rb +176 -0
  19. data/lib/solargraph/complex_type/type_methods.rb +16 -2
  20. data/lib/solargraph/complex_type/unique_type.rb +170 -20
  21. data/lib/solargraph/complex_type.rb +119 -14
  22. data/lib/solargraph/convention/data_definition/data_definition_node.rb +3 -1
  23. data/lib/solargraph/convention/data_definition.rb +4 -1
  24. data/lib/solargraph/convention/struct_definition/struct_assignment_node.rb +1 -0
  25. data/lib/solargraph/convention/struct_definition/struct_definition_node.rb +1 -0
  26. data/lib/solargraph/convention/struct_definition.rb +5 -1
  27. data/lib/solargraph/diagnostics/require_not_found.rb +1 -0
  28. data/lib/solargraph/diagnostics/rubocop.rb +1 -0
  29. data/lib/solargraph/diagnostics/rubocop_helpers.rb +2 -0
  30. data/lib/solargraph/diagnostics/type_check.rb +1 -0
  31. data/lib/solargraph/doc_map.rb +134 -373
  32. data/lib/solargraph/equality.rb +1 -1
  33. data/lib/solargraph/gem_pins.rb +14 -15
  34. data/lib/solargraph/language_server/host/diagnoser.rb +89 -89
  35. data/lib/solargraph/language_server/host/dispatch.rb +1 -0
  36. data/lib/solargraph/language_server/host/message_worker.rb +2 -1
  37. data/lib/solargraph/language_server/host/sources.rb +1 -0
  38. data/lib/solargraph/language_server/host.rb +6 -1
  39. data/lib/solargraph/language_server/message/extended/check_gem_version.rb +2 -7
  40. data/lib/solargraph/language_server/message/extended/document.rb +1 -0
  41. data/lib/solargraph/language_server/message/text_document/completion.rb +2 -0
  42. data/lib/solargraph/language_server/message/text_document/definition.rb +2 -0
  43. data/lib/solargraph/language_server/message/text_document/document_symbol.rb +2 -0
  44. data/lib/solargraph/language_server/message/text_document/formatting.rb +2 -0
  45. data/lib/solargraph/language_server/message/text_document/hover.rb +2 -0
  46. data/lib/solargraph/language_server/message/text_document/signature_help.rb +1 -0
  47. data/lib/solargraph/language_server/message/text_document/type_definition.rb +2 -0
  48. data/lib/solargraph/language_server/message/workspace/workspace_symbol.rb +2 -0
  49. data/lib/solargraph/library.rb +59 -13
  50. data/lib/solargraph/location.rb +9 -4
  51. data/lib/solargraph/logging.rb +21 -1
  52. data/lib/solargraph/parser/comment_ripper.rb +7 -0
  53. data/lib/solargraph/parser/flow_sensitive_typing.rb +330 -102
  54. data/lib/solargraph/parser/node_processor/base.rb +32 -2
  55. data/lib/solargraph/parser/node_processor.rb +7 -6
  56. data/lib/solargraph/parser/parser_gem/class_methods.rb +28 -10
  57. data/lib/solargraph/parser/parser_gem/node_chainer.rb +31 -6
  58. data/lib/solargraph/parser/parser_gem/node_methods.rb +27 -7
  59. data/lib/solargraph/parser/parser_gem/node_processors/and_node.rb +4 -4
  60. data/lib/solargraph/parser/parser_gem/node_processors/args_node.rb +2 -0
  61. data/lib/solargraph/parser/parser_gem/node_processors/begin_node.rb +9 -0
  62. data/lib/solargraph/parser/parser_gem/node_processors/block_node.rb +11 -11
  63. data/lib/solargraph/parser/parser_gem/node_processors/def_node.rb +7 -0
  64. data/lib/solargraph/parser/parser_gem/node_processors/if_node.rb +36 -6
  65. data/lib/solargraph/parser/parser_gem/node_processors/ivasgn_node.rb +3 -2
  66. data/lib/solargraph/parser/parser_gem/node_processors/lvasgn_node.rb +1 -0
  67. data/lib/solargraph/parser/parser_gem/node_processors/masgn_node.rb +3 -1
  68. data/lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb +2 -2
  69. data/lib/solargraph/parser/parser_gem/node_processors/or_node.rb +22 -0
  70. data/lib/solargraph/parser/parser_gem/node_processors/orasgn_node.rb +1 -1
  71. data/lib/solargraph/parser/parser_gem/node_processors/resbody_node.rb +2 -1
  72. data/lib/solargraph/parser/parser_gem/node_processors/sclass_node.rb +1 -0
  73. data/lib/solargraph/parser/parser_gem/node_processors/send_node.rb +12 -7
  74. data/lib/solargraph/parser/parser_gem/node_processors/when_node.rb +23 -0
  75. data/lib/solargraph/parser/parser_gem/node_processors/while_node.rb +5 -1
  76. data/lib/solargraph/parser/parser_gem/node_processors.rb +4 -0
  77. data/lib/solargraph/parser/region.rb +9 -3
  78. data/lib/solargraph/parser/snippet.rb +1 -1
  79. data/lib/solargraph/pin/base.rb +53 -21
  80. data/lib/solargraph/pin/base_variable.rb +312 -20
  81. data/lib/solargraph/pin/block.rb +26 -4
  82. data/lib/solargraph/pin/breakable.rb +5 -1
  83. data/lib/solargraph/pin/callable.rb +50 -3
  84. data/lib/solargraph/pin/closure.rb +2 -6
  85. data/lib/solargraph/pin/common.rb +20 -5
  86. data/lib/solargraph/pin/compound_statement.rb +55 -0
  87. data/lib/solargraph/pin/conversions.rb +2 -1
  88. data/lib/solargraph/pin/delegated_method.rb +15 -4
  89. data/lib/solargraph/pin/documenting.rb +1 -0
  90. data/lib/solargraph/pin/instance_variable.rb +5 -1
  91. data/lib/solargraph/pin/keyword.rb +0 -4
  92. data/lib/solargraph/pin/local_variable.rb +13 -57
  93. data/lib/solargraph/pin/method.rb +90 -42
  94. data/lib/solargraph/pin/method_alias.rb +8 -0
  95. data/lib/solargraph/pin/namespace.rb +7 -1
  96. data/lib/solargraph/pin/parameter.rb +76 -13
  97. data/lib/solargraph/pin/proxy_type.rb +2 -1
  98. data/lib/solargraph/pin/reference/override.rb +1 -1
  99. data/lib/solargraph/pin/reference/superclass.rb +2 -0
  100. data/lib/solargraph/pin/reference.rb +2 -0
  101. data/lib/solargraph/pin/search.rb +1 -0
  102. data/lib/solargraph/pin/signature.rb +8 -0
  103. data/lib/solargraph/pin/symbol.rb +1 -1
  104. data/lib/solargraph/pin/until.rb +1 -1
  105. data/lib/solargraph/pin/while.rb +1 -1
  106. data/lib/solargraph/pin.rb +2 -0
  107. data/lib/solargraph/pin_cache.rb +477 -57
  108. data/lib/solargraph/position.rb +12 -26
  109. data/lib/solargraph/range.rb +6 -6
  110. data/lib/solargraph/rbs_map/conversions.rb +33 -10
  111. data/lib/solargraph/rbs_map/core_map.rb +24 -17
  112. data/lib/solargraph/rbs_map/stdlib_map.rb +34 -5
  113. data/lib/solargraph/rbs_map.rb +74 -20
  114. data/lib/solargraph/shell.rb +73 -28
  115. data/lib/solargraph/source/chain/call.rb +52 -17
  116. data/lib/solargraph/source/chain/constant.rb +2 -0
  117. data/lib/solargraph/source/chain/hash.rb +1 -0
  118. data/lib/solargraph/source/chain/if.rb +1 -0
  119. data/lib/solargraph/source/chain/instance_variable.rb +22 -1
  120. data/lib/solargraph/source/chain/literal.rb +5 -0
  121. data/lib/solargraph/source/chain/or.rb +9 -1
  122. data/lib/solargraph/source/chain.rb +25 -22
  123. data/lib/solargraph/source/change.rb +9 -2
  124. data/lib/solargraph/source/cursor.rb +7 -1
  125. data/lib/solargraph/source/source_chainer.rb +13 -3
  126. data/lib/solargraph/source/updater.rb +4 -0
  127. data/lib/solargraph/source.rb +33 -7
  128. data/lib/solargraph/source_map/clip.rb +13 -2
  129. data/lib/solargraph/source_map/data.rb +4 -1
  130. data/lib/solargraph/source_map/mapper.rb +24 -1
  131. data/lib/solargraph/source_map.rb +14 -6
  132. data/lib/solargraph/type_checker/problem.rb +3 -1
  133. data/lib/solargraph/type_checker/rules.rb +75 -2
  134. data/lib/solargraph/type_checker.rb +111 -30
  135. data/lib/solargraph/version.rb +1 -1
  136. data/lib/solargraph/workspace/config.rb +3 -1
  137. data/lib/solargraph/workspace/gemspecs.rb +367 -0
  138. data/lib/solargraph/workspace/require_paths.rb +1 -0
  139. data/lib/solargraph/workspace.rb +158 -16
  140. data/lib/solargraph/yard_map/helpers.rb +2 -1
  141. data/lib/solargraph/yard_map/mapper/to_method.rb +5 -1
  142. data/lib/solargraph/yard_map/mapper/to_namespace.rb +1 -0
  143. data/lib/solargraph/yard_map/mapper.rb +5 -0
  144. data/lib/solargraph/yardoc.rb +33 -23
  145. data/lib/solargraph.rb +24 -3
  146. data/rbs/fills/rubygems/0/dependency.rbs +193 -0
  147. data/rbs/fills/tuple/tuple.rbs +28 -0
  148. data/rbs/shims/ast/0/node.rbs +1 -1
  149. data/rbs/shims/diff-lcs/1.5/diff-lcs.rbs +11 -0
  150. data/solargraph.gemspec +2 -1
  151. metadata +12 -7
  152. data/lib/solargraph/type_checker/checks.rb +0 -124
  153. data/lib/solargraph/type_checker/param_def.rb +0 -37
  154. data/lib/solargraph/yard_map/to_method.rb +0 -89
@@ -18,6 +18,7 @@ module Solargraph
18
18
  # @type [String]
19
19
  gem_path = Gem::Specification.find_by_name('rubocop', version).full_gem_path
20
20
  gem_lib_path = File.join(gem_path, 'lib')
21
+ # @sg-ignore Should better support meaning of '&' in RBS
21
22
  $LOAD_PATH.unshift(gem_lib_path) unless $LOAD_PATH.include?(gem_lib_path)
22
23
  rescue Gem::MissingSpecVersionError => e
23
24
  # @type [Array<Gem::Specification>]
@@ -50,6 +51,7 @@ module Solargraph
50
51
  # @return [String]
51
52
  def fix_drive_letter path
52
53
  return path unless path.match(/^[a-z]:/)
54
+ # @sg-ignore Need to add nil check here
53
55
  path[0].upcase + path[1..-1]
54
56
  end
55
57
 
@@ -11,6 +11,7 @@ module Solargraph
11
11
  # return [] unless args.include?('always') || api_map.workspaced?(source.filename)
12
12
  severity = Diagnostics::Severities::ERROR
13
13
  level = (args.reverse.find { |a| ['normal', 'typed', 'strict', 'strong'].include?(a) }) || :normal
14
+ # @sg-ignore sensitive typing needs to handle || on nil types
14
15
  checker = Solargraph::TypeChecker.new(source.filename, api_map: api_map, level: level.to_sym)
15
16
  checker.problems
16
17
  .sort { |a, b| a.location.range.start.line <=> b.location.range.start.line }
@@ -5,122 +5,86 @@ require 'benchmark'
5
5
  require 'open3'
6
6
 
7
7
  module Solargraph
8
- # A collection of pins generated from required gems.
8
+ # A collection of pins generated from specific 'require' statements
9
+ # in code. Multiple can be created per workspace, to represent the
10
+ # pins available in different files based on their particular
11
+ # 'require' lines.
9
12
  #
10
13
  class DocMap
11
14
  include Logging
12
15
 
13
- # @return [Array<String>]
14
- attr_reader :requires
15
- alias required requires
16
-
17
- # @return [Array<Gem::Specification>]
18
- attr_reader :preferences
19
-
20
- # @return [Array<Pin::Base>]
21
- attr_reader :pins
22
-
23
- # @return [Array<Gem::Specification>]
24
- def uncached_gemspecs
25
- uncached_yard_gemspecs.concat(uncached_rbs_collection_gemspecs)
26
- .sort
27
- .uniq { |gemspec| "#{gemspec.name}:#{gemspec.version}" }
28
- end
29
-
30
- # @return [Array<Gem::Specification>]
31
- attr_reader :uncached_yard_gemspecs
32
-
33
- # @return [Array<Gem::Specification>]
34
- attr_reader :uncached_rbs_collection_gemspecs
35
-
36
- # @return [String, nil]
37
- attr_reader :rbs_collection_path
38
-
39
- # @return [String, nil]
40
- attr_reader :rbs_collection_config_path
41
-
42
- # @return [Workspace, nil]
16
+ # @return [Workspace]
43
17
  attr_reader :workspace
44
18
 
45
19
  # @return [Environ]
46
20
  attr_reader :environ
47
21
 
48
22
  # @param requires [Array<String>]
49
- # @param preferences [Array<Gem::Specification>]
50
23
  # @param workspace [Workspace, nil]
51
- def initialize(requires, preferences, workspace = nil)
52
- @requires = requires.compact
53
- @preferences = preferences.compact
24
+ # @param out [IO, nil] output stream for logging
25
+ def initialize requires, workspace, out: $stderr
26
+ @provided_requires = requires.compact
54
27
  @workspace = workspace
55
- @rbs_collection_path = workspace&.rbs_collection_path
56
- @rbs_collection_config_path = workspace&.rbs_collection_config_path
57
- @environ = Convention.for_global(self)
58
- @requires.concat @environ.requires if @environ
59
- load_serialized_gem_pins
60
- pins.concat @environ.pins
28
+ @out = out
61
29
  end
62
30
 
63
- # @param out [IO]
64
- # @return [void]
65
- def cache_all!(out)
66
- # if we log at debug level:
67
- if logger.info?
68
- gem_desc = uncached_gemspecs.map { |gemspec| "#{gemspec.name}:#{gemspec.version}" }.join(', ')
69
- logger.info "Caching pins for gems: #{gem_desc}" unless uncached_gemspecs.empty?
70
- end
71
- logger.debug { "Caching for YARD: #{uncached_yard_gemspecs.map(&:name)}" }
72
- logger.debug { "Caching for RBS collection: #{uncached_rbs_collection_gemspecs.map(&:name)}" }
73
- load_serialized_gem_pins
74
- uncached_gemspecs.each do |gemspec|
75
- cache(gemspec, out: out)
31
+ # @return [Array<String>]
32
+ def requires
33
+ @requires ||= @provided_requires + (workspace.global_environ&.requires || [])
34
+ end
35
+ alias required requires
36
+
37
+ # @sg-ignore flow sensitive typing needs to understand reassignment
38
+ # @return [Array<Gem::Specification>]
39
+ def uncached_gemspecs
40
+ if @uncached_gemspecs.nil?
41
+ @uncached_gemspecs = []
42
+ pins # force lazy-loaded pin lookup
76
43
  end
77
- load_serialized_gem_pins
78
- @uncached_rbs_collection_gemspecs = []
79
- @uncached_yard_gemspecs = []
44
+ @uncached_gemspecs
80
45
  end
81
46
 
82
- # @param gemspec [Gem::Specification]
83
- # @param out [IO]
84
- # @return [void]
85
- def cache_yard_pins(gemspec, out)
86
- pins = GemPins.build_yard_pins(yard_plugins, gemspec)
87
- PinCache.serialize_yard_gem(gemspec, pins)
88
- logger.info { "Cached #{pins.length} YARD pins for gem #{gemspec.name}:#{gemspec.version}" } unless pins.empty?
47
+ # @return [Array<Pin::Base>]
48
+ def pins
49
+ @pins ||= load_serialized_gem_pins + (workspace.global_environ&.pins || [])
89
50
  end
90
51
 
91
- # @param gemspec [Gem::Specification]
92
- # @param out [IO]
93
52
  # @return [void]
94
- def cache_rbs_collection_pins(gemspec, out)
95
- rbs_map = RbsMap.from_gemspec(gemspec, rbs_collection_path, rbs_collection_config_path)
96
- pins = rbs_map.pins
97
- rbs_version_cache_key = rbs_map.cache_key
98
- # cache pins even if result is zero, so we don't retry building pins
99
- pins ||= []
100
- PinCache.serialize_rbs_collection_gem(gemspec, rbs_version_cache_key, pins)
101
- logger.info { "Cached #{pins.length} RBS collection pins for gem #{gemspec.name} #{gemspec.version} with cache_key #{rbs_version_cache_key.inspect}" unless pins.empty? }
53
+ def reset_pins!
54
+ @uncached_gemspecs = nil
55
+ @pins = nil
102
56
  end
103
57
 
104
- # @param gemspec [Gem::Specification]
58
+ # @return [Solargraph::PinCache]
59
+ def pin_cache
60
+ @pin_cache ||= workspace.fresh_pincache
61
+ end
62
+
63
+ def any_uncached?
64
+ uncached_gemspecs.any?
65
+ end
66
+
67
+ # Cache all pins needed for the sources in this doc_map
68
+ # @param out [StringIO, IO, nil] output stream for logging
105
69
  # @param rebuild [Boolean] whether to rebuild the pins even if they are cached
106
- # @param out [IO, nil] output stream for logging
107
70
  # @return [void]
108
- def cache(gemspec, rebuild: false, out: nil)
109
- build_yard = uncached_yard_gemspecs.include?(gemspec) || rebuild
110
- build_rbs_collection = uncached_rbs_collection_gemspecs.include?(gemspec) || rebuild
111
- if build_yard || build_rbs_collection
112
- type = []
113
- type << 'YARD' if build_yard
114
- type << 'RBS collection' if build_rbs_collection
115
- out.puts("Caching #{type.join(' and ')} pins for gem #{gemspec.name}:#{gemspec.version}") if out
71
+ def cache_doc_map_gems! out, rebuild: false
72
+ unless uncached_gemspecs.empty?
73
+ logger.info do
74
+ gem_desc = uncached_gemspecs.map { |gemspec| "#{gemspec.name}:#{gemspec.version}" }.join(', ')
75
+ "Caching pins for gems: #{gem_desc}"
76
+ end
116
77
  end
117
- cache_yard_pins(gemspec, out) if build_yard
118
- cache_rbs_collection_pins(gemspec, out) if build_rbs_collection
119
- end
120
-
121
- # @return [Array<Gem::Specification>]
122
- def gemspecs
123
- @gemspecs ||= required_gems_map.values.compact.flatten
78
+ time = Benchmark.measure do
79
+ uncached_gemspecs.each do |gemspec|
80
+ cache(gemspec, rebuild: rebuild, out: out)
81
+ end
82
+ end
83
+ milliseconds = (time.real * 1000).round
84
+ if (milliseconds > 500) && uncached_gemspecs.any? && out && uncached_gemspecs.any?
85
+ out.puts "Built #{uncached_gemspecs.length} gems in #{milliseconds} ms"
86
+ end
87
+ reset_pins!
124
88
  end
125
89
 
126
90
  # @return [Array<String>]
@@ -128,312 +92,109 @@ module Solargraph
128
92
  @unresolved_requires ||= required_gems_map.select { |_, gemspecs| gemspecs.nil? }.keys
129
93
  end
130
94
 
131
- # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
132
- def self.all_yard_gems_in_memory
133
- @yard_gems_in_memory ||= {}
134
- end
135
-
136
- # @return [Hash{String => Hash{Array(String, String) => Array<Pin::Base>}}] stored by RBS collection path
137
- def self.all_rbs_collection_gems_in_memory
138
- @rbs_collection_gems_in_memory ||= {}
139
- end
140
-
141
- # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
142
- def yard_pins_in_memory
143
- self.class.all_yard_gems_in_memory
144
- end
145
-
146
- # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
147
- def rbs_collection_pins_in_memory
148
- self.class.all_rbs_collection_gems_in_memory[rbs_collection_path] ||= {}
149
- end
150
-
151
- # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
152
- def self.all_combined_pins_in_memory
153
- @combined_pins_in_memory ||= {}
95
+ # @return [Array<Gem::Specification>]
96
+ # @param out [IO, nil]
97
+ def dependencies out: $stderr
98
+ @dependencies ||=
99
+ begin
100
+ gem_deps = gemspecs
101
+ .flat_map { |spec| workspace.fetch_dependencies(spec, out: out) }
102
+ .uniq(&:name)
103
+ stdlib_deps = gemspecs
104
+ .flat_map { |spec| workspace.stdlib_dependencies(spec.name) }
105
+ .flat_map { |dep_name| workspace.resolve_require(dep_name) }
106
+ .compact
107
+ existing_gems = gemspecs.map(&:name)
108
+ (gem_deps + stdlib_deps).reject { |gemspec| existing_gems.include? gemspec.name }
109
+ end
154
110
  end
155
111
 
156
- # @todo this should also include an index by the hash of the RBS collection
157
- # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
158
- def combined_pins_in_memory
159
- self.class.all_combined_pins_in_memory
112
+ # Cache gem documentation if needed for this doc_map
113
+ #
114
+ # @param gemspec [Gem::Specification]
115
+ # @param rebuild [Boolean] whether to rebuild the pins even if they are cached
116
+ # @param out [StringIO, IO, nil] output stream for logging
117
+ #
118
+ # @return [void]
119
+ def cache gemspec, rebuild: false, out: nil
120
+ pin_cache.cache_gem(gemspec: gemspec,
121
+ rebuild: rebuild,
122
+ out: out)
160
123
  end
161
124
 
162
- # @return [Array<String>]
163
- def yard_plugins
164
- @environ.yard_plugins
165
- end
125
+ private
166
126
 
167
- # @return [Set<Gem::Specification>]
168
- def dependencies
169
- @dependencies ||= (gemspecs.flat_map { |spec| fetch_dependencies(spec) } - gemspecs).to_set
127
+ # @return [Array<Gem::Specification>]
128
+ def gemspecs
129
+ @gemspecs ||= required_gems_map.values.compact.flatten
170
130
  end
171
131
 
172
- private
173
-
174
- # @return [void]
175
- def load_serialized_gem_pins
176
- @pins = []
177
- @uncached_yard_gemspecs = []
178
- @uncached_rbs_collection_gemspecs = []
132
+ # @param out [IO, nil]
133
+ # @return [Array<Pin::Base>]
134
+ def load_serialized_gem_pins out: @out
135
+ serialized_pins = []
179
136
  with_gemspecs, without_gemspecs = required_gems_map.partition { |_, v| v }
180
- # @sg-ignore Need support for RBS duck interfaces like _ToHash
137
+ # @sg-ignore Need better typing for Hash[]
181
138
  # @type [Array<String>]
182
- paths = Hash[without_gemspecs].keys
183
- # @sg-ignore Need support for RBS duck interfaces like _ToHash
139
+ missing_paths = Hash[without_gemspecs].keys
184
140
  # @type [Array<Gem::Specification>]
185
- gemspecs = Hash[with_gemspecs].values.flatten.compact + dependencies.to_a
186
-
187
- paths.each do |path|
188
- rbs_pins = deserialize_stdlib_rbs_map path
141
+ gemspecs = Hash[with_gemspecs].values.flatten.compact + dependencies(out: out).to_a
142
+
143
+ # if we are type checking a gem project, we should not include
144
+ # pins from rbs or yard from that gem here - we use our own
145
+ # parser for those pins
146
+
147
+ # @param gemspec [Gem::Specification, Bundler::LazySpecification, Bundler::StubSpecification]
148
+ gemspecs.reject! do |gemspec|
149
+ gemspec.respond_to?(:source) &&
150
+ gemspec.source.instance_of?(Bundler::Source::Gemspec) &&
151
+ gemspec.source.respond_to?(:path) &&
152
+ gemspec.source.path == Pathname.new('.')
153
+ end
154
+
155
+ missing_paths.each do |path|
156
+ # this will load from disk if needed; no need to manage
157
+ # uncached_gemspecs to trigger that later
158
+ stdlib_name_guess = path.split('/').first
159
+
160
+ # try to resolve the stdlib name
161
+ # @type [Array<String>]
162
+ deps = workspace.stdlib_dependencies(stdlib_name_guess) || []
163
+ [stdlib_name_guess, *deps].compact.each do |potential_stdlib_name|
164
+ # @sg-ignore Need to support splatting in literal array
165
+ rbs_pins = pin_cache.cache_stdlib_rbs_map potential_stdlib_name
166
+ serialized_pins.concat rbs_pins if rbs_pins
167
+ end
189
168
  end
190
169
 
191
- logger.debug { "DocMap#load_serialized_gem_pins: Combining pins..." }
170
+ existing_pin_count = serialized_pins.length
192
171
  time = Benchmark.measure do
193
172
  gemspecs.each do |gemspec|
194
- pins = deserialize_combined_pin_cache gemspec
195
- @pins.concat pins if pins
173
+ # only deserializes already-cached gems
174
+ gemspec_pins = pin_cache.deserialize_combined_pin_cache gemspec
175
+ if gemspec_pins
176
+ serialized_pins.concat gemspec_pins
177
+ else
178
+ uncached_gemspecs << gemspec
179
+ end
196
180
  end
197
181
  end
198
- logger.info { "DocMap#load_serialized_gem_pins: Loaded and processed serialized pins together in #{time.real} seconds" }
199
- @uncached_yard_gemspecs.uniq!
200
- @uncached_rbs_collection_gemspecs.uniq!
201
- nil
182
+ pins_processed = serialized_pins.length - existing_pin_count
183
+ milliseconds = (time.real * 1000).round
184
+ if (milliseconds > 500) && out && gemspecs.any?
185
+ out.puts "Deserialized #{serialized_pins.length} gem pins from #{PinCache.base_dir} in #{milliseconds} ms"
186
+ end
187
+ uncached_gemspecs.uniq! { |gemspec| "#{gemspec.name}:#{gemspec.version}" }
188
+ serialized_pins
202
189
  end
203
190
 
204
191
  # @return [Hash{String => Array<Gem::Specification>}]
205
192
  def required_gems_map
206
- @required_gems_map ||= requires.to_h { |path| [path, resolve_path_to_gemspecs(path)] }
207
- end
208
-
209
- # @return [Hash{String => Gem::Specification}]
210
- def preference_map
211
- @preference_map ||= preferences.to_h { |gemspec| [gemspec.name, gemspec] }
212
- end
213
-
214
- # @param gemspec [Gem::Specification]
215
- # @return [Array<Pin::Base>, nil]
216
- def deserialize_yard_pin_cache gemspec
217
- if yard_pins_in_memory.key?([gemspec.name, gemspec.version])
218
- return yard_pins_in_memory[[gemspec.name, gemspec.version]]
219
- end
220
-
221
- cached = PinCache.deserialize_yard_gem(gemspec)
222
- if cached
223
- logger.info { "Loaded #{cached.length} cached YARD pins from #{gemspec.name}:#{gemspec.version}" }
224
- yard_pins_in_memory[[gemspec.name, gemspec.version]] = cached
225
- cached
226
- else
227
- logger.debug "No YARD pin cache for #{gemspec.name}:#{gemspec.version}"
228
- @uncached_yard_gemspecs.push gemspec
229
- nil
230
- end
231
- end
232
-
233
- # @param gemspec [Gem::Specification]
234
- # @return [void]
235
- def deserialize_combined_pin_cache(gemspec)
236
- unless combined_pins_in_memory[[gemspec.name, gemspec.version]].nil?
237
- return combined_pins_in_memory[[gemspec.name, gemspec.version]]
238
- end
239
-
240
- rbs_map = RbsMap.from_gemspec(gemspec, rbs_collection_path, rbs_collection_config_path)
241
- rbs_version_cache_key = rbs_map.cache_key
242
-
243
- cached = PinCache.deserialize_combined_gem(gemspec, rbs_version_cache_key)
244
- if cached
245
- logger.info { "Loaded #{cached.length} cached YARD pins from #{gemspec.name}:#{gemspec.version}" }
246
- combined_pins_in_memory[[gemspec.name, gemspec.version]] = cached
247
- return combined_pins_in_memory[[gemspec.name, gemspec.version]]
248
- end
249
-
250
- rbs_collection_pins = deserialize_rbs_collection_cache gemspec, rbs_version_cache_key
251
-
252
- yard_pins = deserialize_yard_pin_cache gemspec
253
-
254
- if !rbs_collection_pins.nil? && !yard_pins.nil?
255
- logger.debug { "Combining pins for #{gemspec.name}:#{gemspec.version}" }
256
- combined_pins = GemPins.combine(yard_pins, rbs_collection_pins)
257
- PinCache.serialize_combined_gem(gemspec, rbs_version_cache_key, combined_pins)
258
- combined_pins_in_memory[[gemspec.name, gemspec.version]] = combined_pins
259
- logger.info { "Generated #{combined_pins_in_memory[[gemspec.name, gemspec.version]].length} combined pins for #{gemspec.name} #{gemspec.version}" }
260
- return combined_pins
261
- end
262
-
263
- if !yard_pins.nil?
264
- logger.debug { "Using only YARD pins for #{gemspec.name}:#{gemspec.version}" }
265
- combined_pins_in_memory[[gemspec.name, gemspec.version]] = yard_pins
266
- return combined_pins_in_memory[[gemspec.name, gemspec.version]]
267
- elsif !rbs_collection_pins.nil?
268
- logger.debug { "Using only RBS collection pins for #{gemspec.name}:#{gemspec.version}" }
269
- combined_pins_in_memory[[gemspec.name, gemspec.version]] = rbs_collection_pins
270
- return combined_pins_in_memory[[gemspec.name, gemspec.version]]
271
- else
272
- logger.debug { "Pins not yet cached for #{gemspec.name}:#{gemspec.version}" }
273
- return nil
274
- end
275
- end
276
-
277
- # @param path [String] require path that might be in the RBS stdlib collection
278
- # @return [void]
279
- def deserialize_stdlib_rbs_map path
280
- map = RbsMap::StdlibMap.load(path)
281
- if map.resolved?
282
- logger.debug { "Loading stdlib pins for #{path}" }
283
- @pins.concat map.pins
284
- logger.debug { "Loaded #{map.pins.length} stdlib pins for #{path}" }
285
- map.pins
286
- else
287
- # @todo Temporarily ignoring unresolved `require 'set'`
288
- logger.debug { "Require path #{path} could not be resolved in RBS" } unless path == 'set'
289
- nil
290
- end
291
- end
292
-
293
- # @param gemspec [Gem::Specification]
294
- # @param rbs_version_cache_key [String]
295
- # @return [Array<Pin::Base>, nil]
296
- def deserialize_rbs_collection_cache gemspec, rbs_version_cache_key
297
- return if rbs_collection_pins_in_memory.key?([gemspec, rbs_version_cache_key])
298
- cached = PinCache.deserialize_rbs_collection_gem(gemspec, rbs_version_cache_key)
299
- if cached
300
- logger.info { "Loaded #{cached.length} pins from RBS collection cache for #{gemspec.name}:#{gemspec.version}" } unless cached.empty?
301
- rbs_collection_pins_in_memory[[gemspec, rbs_version_cache_key]] = cached
302
- cached
303
- else
304
- logger.debug "No RBS collection pin cache for #{gemspec.name} #{gemspec.version}"
305
- @uncached_rbs_collection_gemspecs.push gemspec
306
- nil
307
- end
308
- end
309
-
310
- # @param path [String]
311
- # @return [::Array<Gem::Specification>, nil]
312
- def resolve_path_to_gemspecs path
313
- return nil if path.empty?
314
- return gemspecs_required_from_bundler if path == 'bundler/require'
315
-
316
- # @type [Gem::Specification, nil]
317
- gemspec = Gem::Specification.find_by_path(path)
318
- if gemspec.nil?
319
- gem_name_guess = path.split('/').first
320
- begin
321
- # this can happen when the gem is included via a local path in
322
- # a Gemfile; Gem doesn't try to index the paths in that case.
323
- #
324
- # See if we can make a good guess:
325
- potential_gemspec = Gem::Specification.find_by_name(gem_name_guess)
326
- file = "lib/#{path}.rb"
327
- gemspec = potential_gemspec if potential_gemspec.files.any? { |gemspec_file| file == gemspec_file }
328
- rescue Gem::MissingSpecError
329
- logger.debug { "Require path #{path} could not be resolved to a gem via find_by_path or guess of #{gem_name_guess}" }
330
- []
331
- end
332
- end
333
- return nil if gemspec.nil?
334
- [gemspec_or_preference(gemspec)]
193
+ @required_gems_map ||= requires.to_h { |path| [path, workspace.resolve_require(path)] }
335
194
  end
336
195
 
337
- # @param gemspec [Gem::Specification]
338
- # @return [Gem::Specification]
339
- def gemspec_or_preference gemspec
340
- # :nocov: dormant feature
341
- return gemspec unless preference_map.key?(gemspec.name)
342
- return gemspec if gemspec.version == preference_map[gemspec.name].version
343
-
344
- change_gemspec_version gemspec, preference_map[gemspec.name].version
345
- # :nocov:
346
- end
347
-
348
- # @param gemspec [Gem::Specification]
349
- # @param version [Gem::Version]
350
- # @return [Gem::Specification]
351
- def change_gemspec_version gemspec, version
352
- Gem::Specification.find_by_name(gemspec.name, "= #{version}")
353
- rescue Gem::MissingSpecError
354
- Solargraph.logger.info "Gem #{gemspec.name} version #{version} not found. Using #{gemspec.version} instead"
355
- gemspec
356
- end
357
-
358
- # @param gemspec [Gem::Specification]
359
- # @return [Array<Gem::Specification>]
360
- def fetch_dependencies gemspec
361
- # @param spec [Gem::Dependency]
362
- # @param deps [Set<Gem::Specification>]
363
- only_runtime_dependencies(gemspec).each_with_object(Set.new) do |spec, deps|
364
- Solargraph.logger.info "Adding #{spec.name} dependency for #{gemspec.name}"
365
- dep = Gem.loaded_specs[spec.name]
366
- # @todo is next line necessary?
367
- # @sg-ignore Unresolved call to requirement on Gem::Dependency
368
- dep ||= Gem::Specification.find_by_name(spec.name, spec.requirement)
369
- deps.merge fetch_dependencies(dep) if deps.add?(dep)
370
- rescue Gem::MissingSpecError
371
- # @sg-ignore Unresolved call to requirement on Gem::Dependency
372
- Solargraph.logger.warn "Gem dependency #{spec.name} #{spec.requirement} for #{gemspec.name} not found in RubyGems."
373
- end.to_a
374
- end
375
-
376
- # @param gemspec [Gem::Specification]
377
- # @return [Array<Gem::Dependency>]
378
- def only_runtime_dependencies gemspec
379
- gemspec.dependencies - gemspec.development_dependencies
380
- end
381
-
382
-
383
196
  def inspect
384
197
  self.class.inspect
385
198
  end
386
-
387
- # @return [Array<Gem::Specification>, nil]
388
- def gemspecs_required_from_bundler
389
- # @todo Handle projects with custom Bundler/Gemfile setups
390
- return unless workspace.gemfile?
391
-
392
- if workspace.gemfile? && Bundler.definition&.lockfile&.to_s&.start_with?(workspace.directory)
393
- # Find only the gems bundler is now using
394
- Bundler.definition.locked_gems.specs.flat_map do |lazy_spec|
395
- logger.info "Handling #{lazy_spec.name}:#{lazy_spec.version}"
396
- [Gem::Specification.find_by_name(lazy_spec.name, lazy_spec.version)]
397
- rescue Gem::MissingSpecError => e
398
- logger.info("Could not find #{lazy_spec.name}:#{lazy_spec.version} with find_by_name, falling back to guess")
399
- # can happen in local filesystem references
400
- specs = resolve_path_to_gemspecs lazy_spec.name
401
- logger.warn "Gem #{lazy_spec.name} #{lazy_spec.version} from bundle not found: #{e}" if specs.nil?
402
- next specs
403
- end.compact
404
- else
405
- logger.info 'Fetching gemspecs required from Bundler (bundler/require)'
406
- gemspecs_required_from_external_bundle
407
- end
408
- end
409
-
410
- # @return [Array<Gem::Specification>, nil]
411
- def gemspecs_required_from_external_bundle
412
- logger.info 'Fetching gemspecs required from external bundle'
413
- return [] unless workspace&.directory
414
-
415
- Solargraph.with_clean_env do
416
- cmd = [
417
- 'ruby', '-e',
418
- "require 'bundler'; require 'json'; Dir.chdir('#{workspace&.directory}') { puts Bundler.definition.locked_gems.specs.map { |spec| [spec.name, spec.version] }.to_h.to_json }"
419
- ]
420
- o, e, s = Open3.capture3(*cmd)
421
- if s.success?
422
- Solargraph.logger.debug "External bundle: #{o}"
423
- hash = o && !o.empty? ? JSON.parse(o.split("\n").last) : {}
424
- hash.flat_map do |name, version|
425
- Gem::Specification.find_by_name(name, version)
426
- rescue Gem::MissingSpecError => e
427
- logger.info("Could not find #{name}:#{version} with find_by_name, falling back to guess")
428
- # can happen in local filesystem references
429
- specs = resolve_path_to_gemspecs name
430
- logger.warn "Gem #{name} #{version} from bundle not found: #{e}" if specs.nil?
431
- next specs
432
- end.compact
433
- else
434
- Solargraph.logger.warn "Failed to load gems from bundle at #{workspace&.directory}: #{e}"
435
- end
436
- end
437
- end
438
199
  end
439
200
  end
@@ -12,7 +12,7 @@ module Solargraph
12
12
  # @return [Boolean]
13
13
  def eql?(other)
14
14
  self.class.eql?(other.class) &&
15
- # @sg-ignore https://github.com/castwide/solargraph/pull/1114
15
+ # @sg-ignore flow sensitive typing should support .class == .class
16
16
  equality_fields.eql?(other.equality_fields)
17
17
  end
18
18
 
@@ -43,36 +43,35 @@ module Solargraph
43
43
  out
44
44
  end
45
45
 
46
- # @param yard_plugins [Array<String>] The names of YARD plugins to use.
47
- # @param gemspec [Gem::Specification]
48
- # @return [Array<Pin::Base>]
49
- def self.build_yard_pins(yard_plugins, gemspec)
50
- Yardoc.cache(yard_plugins, gemspec) unless Yardoc.cached?(gemspec)
51
- return [] unless Yardoc.cached?(gemspec)
52
- yardoc = Yardoc.load!(gemspec)
53
- YardMap::Mapper.new(yardoc, gemspec).map
54
- end
55
-
56
46
  # @param yard_pins [Array<Pin::Base>]
57
47
  # @param rbs_pins [Array<Pin::Base>]
58
48
  #
59
49
  # @return [Array<Pin::Base>]
60
50
  def self.combine(yard_pins, rbs_pins)
61
51
  in_yard = Set.new
62
- rbs_api_map = Solargraph::ApiMap.new(pins: rbs_pins)
52
+ rbs_store = Solargraph::ApiMap::Store.new(rbs_pins)
63
53
  combined = yard_pins.map do |yard_pin|
64
54
  in_yard.add yard_pin.path
65
- rbs_pin = rbs_api_map.get_path_pins(yard_pin.path).filter { |pin| pin.is_a? Pin::Method }.first
66
- next yard_pin unless rbs_pin && yard_pin.class == Pin::Method
55
+ rbs_pin = rbs_store.get_path_pins(yard_pin.path).filter { |pin| pin.is_a? Pin::Method }.first
56
+
57
+ next yard_pin unless rbs_pin && yard_pin.is_a?(Pin::Method)
67
58
 
68
59
  unless rbs_pin
69
- # @sg-ignore https://github.com/castwide/solargraph/pull/1114
70
60
  logger.debug { "GemPins.combine: No rbs pin for #{yard_pin.path} - using YARD's '#{yard_pin.inspect} (return_type=#{yard_pin.return_type}; signatures=#{yard_pin.signatures})" }
71
61
  next yard_pin
72
62
  end
73
63
 
64
+ # at this point both yard_pins and rbs_pins are methods or
65
+ # method aliases. if not plain methods, prefer the YARD one
66
+ next yard_pin if rbs_pin.class != Pin::Method
67
+
68
+ next rbs_pin if yard_pin.class != Pin::Method
69
+
70
+ # both are method pins
74
71
  out = combine_method_pins(rbs_pin, yard_pin)
75
- logger.debug { "GemPins.combine: Combining yard.path=#{yard_pin.path} - rbs=#{rbs_pin.inspect} with yard=#{yard_pin.inspect} into #{out}" }
72
+ logger.debug do
73
+ "GemPins.combine: Combining yard.path=#{yard_pin.path} - rbs=#{rbs_pin.inspect} with yard=#{yard_pin.inspect} into #{out}"
74
+ end
76
75
  out
77
76
  end
78
77
  in_rbs_only = rbs_pins.select do |pin|