solargraph 0.59.0.dev.1 → 0.59.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 (175) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/linting.yml +3 -1
  3. data/.github/workflows/plugins.yml +12 -3
  4. data/.github/workflows/rspec.yml +9 -54
  5. data/.github/workflows/typecheck.yml +2 -1
  6. data/.gitignore +1 -0
  7. data/.rubocop.yml +38 -6
  8. data/.rubocop_todo.yml +40 -931
  9. data/CHANGELOG.md +22 -1
  10. data/Gemfile +3 -1
  11. data/Rakefile +25 -23
  12. data/bin/solargraph +2 -1
  13. data/lib/solargraph/api_map/constants.rb +0 -1
  14. data/lib/solargraph/api_map/index.rb +11 -11
  15. data/lib/solargraph/api_map/source_to_yard.rb +9 -8
  16. data/lib/solargraph/api_map/store.rb +28 -20
  17. data/lib/solargraph/api_map.rb +70 -41
  18. data/lib/solargraph/bench.rb +44 -45
  19. data/lib/solargraph/complex_type/type_methods.rb +14 -16
  20. data/lib/solargraph/complex_type/unique_type.rb +56 -47
  21. data/lib/solargraph/complex_type.rb +70 -62
  22. data/lib/solargraph/convention/data_definition/data_assignment_node.rb +61 -61
  23. data/lib/solargraph/convention/data_definition/data_definition_node.rb +4 -4
  24. data/lib/solargraph/convention/data_definition.rb +1 -1
  25. data/lib/solargraph/convention/gemfile.rb +15 -15
  26. data/lib/solargraph/convention/gemspec.rb +23 -23
  27. data/lib/solargraph/convention/rakefile.rb +17 -17
  28. data/lib/solargraph/convention/struct_definition/struct_assignment_node.rb +1 -1
  29. data/lib/solargraph/convention/struct_definition/struct_definition_node.rb +3 -3
  30. data/lib/solargraph/convention/struct_definition.rb +3 -3
  31. data/lib/solargraph/convention.rb +78 -78
  32. data/lib/solargraph/converters/dd.rb +19 -17
  33. data/lib/solargraph/converters/dl.rb +17 -15
  34. data/lib/solargraph/converters/dt.rb +17 -15
  35. data/lib/solargraph/converters/misc.rb +3 -1
  36. data/lib/solargraph/diagnostics/rubocop.rb +10 -10
  37. data/lib/solargraph/diagnostics/rubocop_helpers.rb +3 -3
  38. data/lib/solargraph/diagnostics/type_check.rb +10 -10
  39. data/lib/solargraph/diagnostics/update_errors.rb +37 -41
  40. data/lib/solargraph/doc_map.rb +370 -132
  41. data/lib/solargraph/equality.rb +3 -3
  42. data/lib/solargraph/gem_pins.rb +19 -18
  43. data/lib/solargraph/language_server/error_codes.rb +20 -20
  44. data/lib/solargraph/language_server/host/diagnoser.rb +89 -89
  45. data/lib/solargraph/language_server/host/dispatch.rb +2 -3
  46. data/lib/solargraph/language_server/host/message_worker.rb +2 -2
  47. data/lib/solargraph/language_server/host/sources.rb +1 -1
  48. data/lib/solargraph/language_server/host.rb +24 -21
  49. data/lib/solargraph/language_server/message/base.rb +97 -97
  50. data/lib/solargraph/language_server/message/client/register_capability.rb +13 -15
  51. data/lib/solargraph/language_server/message/completion_item/resolve.rb +58 -60
  52. data/lib/solargraph/language_server/message/extended/check_gem_version.rb +10 -11
  53. data/lib/solargraph/language_server/message/extended/document_gems.rb +32 -32
  54. data/lib/solargraph/language_server/message/extended/download_core.rb +20 -19
  55. data/lib/solargraph/language_server/message/extended/search.rb +20 -20
  56. data/lib/solargraph/language_server/message/initialize.rb +197 -191
  57. data/lib/solargraph/language_server/message/text_document/completion.rb +8 -8
  58. data/lib/solargraph/language_server/message/text_document/definition.rb +41 -34
  59. data/lib/solargraph/language_server/message/text_document/document_highlight.rb +23 -16
  60. data/lib/solargraph/language_server/message/text_document/document_symbol.rb +29 -21
  61. data/lib/solargraph/language_server/message/text_document/formatting.rb +6 -6
  62. data/lib/solargraph/language_server/message/text_document/hover.rb +3 -5
  63. data/lib/solargraph/language_server/message/text_document/prepare_rename.rb +18 -11
  64. data/lib/solargraph/language_server/message/text_document/references.rb +23 -16
  65. data/lib/solargraph/language_server/message/text_document/rename.rb +26 -19
  66. data/lib/solargraph/language_server/message/text_document/signature_help.rb +2 -2
  67. data/lib/solargraph/language_server/message/text_document/type_definition.rb +25 -19
  68. data/lib/solargraph/language_server/message/workspace/did_change_configuration.rb +41 -35
  69. data/lib/solargraph/language_server/message/workspace/did_change_watched_files.rb +48 -40
  70. data/lib/solargraph/language_server/message/workspace/did_change_workspace_folders.rb +32 -26
  71. data/lib/solargraph/language_server/message/workspace/workspace_symbol.rb +27 -19
  72. data/lib/solargraph/language_server/message.rb +94 -94
  73. data/lib/solargraph/language_server/request.rb +29 -27
  74. data/lib/solargraph/language_server/transport/data_reader.rb +72 -74
  75. data/lib/solargraph/language_server/uri_helpers.rb +49 -49
  76. data/lib/solargraph/library.rb +68 -95
  77. data/lib/solargraph/location.rb +10 -12
  78. data/lib/solargraph/logging.rb +4 -6
  79. data/lib/solargraph/page.rb +92 -92
  80. data/lib/solargraph/parser/comment_ripper.rb +12 -4
  81. data/lib/solargraph/parser/flow_sensitive_typing.rb +32 -44
  82. data/lib/solargraph/parser/node_processor/base.rb +4 -4
  83. data/lib/solargraph/parser/node_processor.rb +1 -1
  84. data/lib/solargraph/parser/parser_gem/class_methods.rb +4 -6
  85. data/lib/solargraph/parser/parser_gem/flawed_builder.rb +19 -19
  86. data/lib/solargraph/parser/parser_gem/node_chainer.rb +20 -20
  87. data/lib/solargraph/parser/parser_gem/node_methods.rb +66 -65
  88. data/lib/solargraph/parser/parser_gem/node_processors/args_node.rb +12 -12
  89. data/lib/solargraph/parser/parser_gem/node_processors/block_node.rb +1 -2
  90. data/lib/solargraph/parser/parser_gem/node_processors/def_node.rb +3 -3
  91. data/lib/solargraph/parser/parser_gem/node_processors/defs_node.rb +38 -37
  92. data/lib/solargraph/parser/parser_gem/node_processors/if_node.rb +3 -3
  93. data/lib/solargraph/parser/parser_gem/node_processors/ivasgn_node.rb +2 -1
  94. data/lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb +1 -1
  95. data/lib/solargraph/parser/parser_gem/node_processors/sclass_node.rb +3 -5
  96. data/lib/solargraph/parser/parser_gem/node_processors/send_node.rb +118 -112
  97. data/lib/solargraph/parser/parser_gem/node_processors/until_node.rb +29 -29
  98. data/lib/solargraph/parser/parser_gem/node_processors/when_node.rb +1 -1
  99. data/lib/solargraph/parser/parser_gem/node_processors/while_node.rb +1 -1
  100. data/lib/solargraph/parser/parser_gem.rb +14 -12
  101. data/lib/solargraph/parser/snippet.rb +2 -0
  102. data/lib/solargraph/parser.rb +25 -23
  103. data/lib/solargraph/pin/base.rb +76 -64
  104. data/lib/solargraph/pin/base_variable.rb +28 -71
  105. data/lib/solargraph/pin/block.rb +3 -2
  106. data/lib/solargraph/pin/breakable.rb +2 -0
  107. data/lib/solargraph/pin/callable.rb +23 -26
  108. data/lib/solargraph/pin/closure.rb +5 -4
  109. data/lib/solargraph/pin/common.rb +5 -2
  110. data/lib/solargraph/pin/compound_statement.rb +3 -3
  111. data/lib/solargraph/pin/constant.rb +43 -45
  112. data/lib/solargraph/pin/conversions.rb +9 -4
  113. data/lib/solargraph/pin/delegated_method.rb +4 -4
  114. data/lib/solargraph/pin/documenting.rb +3 -2
  115. data/lib/solargraph/pin/local_variable.rb +4 -4
  116. data/lib/solargraph/pin/method.rb +74 -70
  117. data/lib/solargraph/pin/namespace.rb +13 -12
  118. data/lib/solargraph/pin/parameter.rb +28 -27
  119. data/lib/solargraph/pin/proxy_type.rb +2 -0
  120. data/lib/solargraph/pin/reference/type_alias.rb +16 -0
  121. data/lib/solargraph/pin/reference.rb +18 -0
  122. data/lib/solargraph/pin/search.rb +2 -2
  123. data/lib/solargraph/pin/signature.rb +9 -14
  124. data/lib/solargraph/pin/symbol.rb +1 -0
  125. data/lib/solargraph/pin/until.rb +1 -3
  126. data/lib/solargraph/pin/while.rb +1 -3
  127. data/lib/solargraph/pin_cache.rb +71 -488
  128. data/lib/solargraph/position.rb +38 -17
  129. data/lib/solargraph/range.rb +10 -9
  130. data/lib/solargraph/rbs_map/conversions.rb +327 -221
  131. data/lib/solargraph/rbs_map/core_fills.rb +91 -84
  132. data/lib/solargraph/rbs_map/stdlib_map.rb +0 -1
  133. data/lib/solargraph/rbs_map.rb +5 -15
  134. data/lib/solargraph/server_methods.rb +16 -16
  135. data/lib/solargraph/shell.rb +224 -66
  136. data/lib/solargraph/source/chain/array.rb +39 -37
  137. data/lib/solargraph/source/chain/call.rb +49 -44
  138. data/lib/solargraph/source/chain/class_variable.rb +13 -13
  139. data/lib/solargraph/source/chain/constant.rb +3 -1
  140. data/lib/solargraph/source/chain/global_variable.rb +13 -13
  141. data/lib/solargraph/source/chain/hash.rb +8 -6
  142. data/lib/solargraph/source/chain/if.rb +11 -10
  143. data/lib/solargraph/source/chain/instance_variable.rb +3 -1
  144. data/lib/solargraph/source/chain/link.rb +99 -109
  145. data/lib/solargraph/source/chain/literal.rb +4 -6
  146. data/lib/solargraph/source/chain/or.rb +2 -4
  147. data/lib/solargraph/source/chain/q_call.rb +13 -11
  148. data/lib/solargraph/source/chain/variable.rb +15 -13
  149. data/lib/solargraph/source/chain/z_super.rb +28 -30
  150. data/lib/solargraph/source/chain.rb +26 -16
  151. data/lib/solargraph/source/change.rb +3 -3
  152. data/lib/solargraph/source/cursor.rb +18 -18
  153. data/lib/solargraph/source/encoding_fixes.rb +6 -7
  154. data/lib/solargraph/source/source_chainer.rb +46 -32
  155. data/lib/solargraph/source/updater.rb +1 -1
  156. data/lib/solargraph/source.rb +27 -29
  157. data/lib/solargraph/source_map/clip.rb +38 -30
  158. data/lib/solargraph/source_map/mapper.rb +51 -47
  159. data/lib/solargraph/source_map.rb +8 -4
  160. data/lib/solargraph/type_checker/rules.rb +8 -8
  161. data/lib/solargraph/type_checker.rb +95 -102
  162. data/lib/solargraph/version.rb +1 -1
  163. data/lib/solargraph/workspace/config.rb +11 -10
  164. data/lib/solargraph/workspace/gemspecs.rb +3 -3
  165. data/lib/solargraph/workspace.rb +45 -165
  166. data/lib/solargraph/yard_map/helpers.rb +6 -2
  167. data/lib/solargraph/yard_map/mapper/to_method.rb +8 -6
  168. data/lib/solargraph/yard_map/mapper/to_namespace.rb +1 -1
  169. data/lib/solargraph/yard_map/mapper.rb +12 -12
  170. data/lib/solargraph/yard_map.rb +17 -18
  171. data/lib/solargraph/yard_tags.rb +20 -20
  172. data/lib/solargraph/yardoc.rb +26 -33
  173. data/lib/solargraph.rb +7 -5
  174. data/solargraph.gemspec +36 -35
  175. metadata +33 -38
@@ -1,434 +1,13 @@
1
+ require 'yard-activesupport-concern'
1
2
  require 'fileutils'
3
+ require 'pathname' # @todo Required by RBS but not loaded in some use cases
2
4
  require 'rbs'
3
- require 'rubygems'
4
5
 
5
6
  module Solargraph
6
- class PinCache
7
- include Logging
8
-
9
- attr_reader :directory, :rbs_collection_path, :rbs_collection_config_path, :yard_plugins
10
-
11
- # @param rbs_collection_path [String, nil]
12
- # @param rbs_collection_config_path [String, nil]
13
- # @param directory [String, nil]
14
- # @param yard_plugins [Array<String>]
15
- def initialize rbs_collection_path:, rbs_collection_config_path:,
16
- directory:,
17
- yard_plugins:
18
- @rbs_collection_path = rbs_collection_path
19
- @rbs_collection_config_path = rbs_collection_config_path
20
- @directory = directory
21
- @yard_plugins = yard_plugins
22
- end
23
-
24
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
25
- def cached? gemspec
26
- rbs_version_cache_key = lookup_rbs_version_cache_key(gemspec)
27
- combined_gem?(gemspec, rbs_version_cache_key)
28
- end
29
-
30
- # @param gemspec [Gem::Specification]
31
- # @param rebuild [Boolean] whether to rebuild the cache regardless of whether it already exists
32
- # @param out [StringIO, IO, nil] output stream for logging
33
- # @return [void]
34
- def cache_gem gemspec:, rebuild: false, out: nil
35
- rbs_version_cache_key = lookup_rbs_version_cache_key(gemspec)
36
-
37
- build_yard, build_rbs_collection, build_combined =
38
- calculate_build_needs(gemspec,
39
- rebuild: rebuild,
40
- rbs_version_cache_key: rbs_version_cache_key)
41
-
42
- return unless build_yard || build_rbs_collection || build_combined
43
-
44
- build_combine_and_cache(gemspec,
45
- rbs_version_cache_key,
46
- build_yard: build_yard,
47
- build_rbs_collection: build_rbs_collection,
48
- build_combined: build_combined,
49
- out: out)
50
- end
51
-
52
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
53
- # @param rbs_version_cache_key [String, nil]
54
- def suppress_yard_cache? gemspec, rbs_version_cache_key
55
- if gemspec.name == 'parser' && rbs_version_cache_key != RbsMap::CACHE_KEY_UNRESOLVED
56
- # parser takes forever to build YARD pins, but has excellent RBS collection pins
57
- return true
58
- end
59
- false
60
- end
61
-
62
- # @param out [StringIO, IO, nil] output stream for logging
63
- # @param rebuild [Boolean] build pins regardless of whether we
64
- # have cached them already
65
- #
66
- # @return [void]
67
- def cache_all_stdlibs rebuild: false, out: $stderr
68
- possible_stdlibs.each do |stdlib|
69
- RbsMap::StdlibMap.new(stdlib, rebuild: rebuild, out: out)
70
- end
71
- end
72
-
73
- # @param path [String] require path that might be in the RBS stdlib collection
74
- # @return [void]
75
- def cache_stdlib_rbs_map path
76
- # these are held in memory in RbsMap::StdlibMap
77
- map = RbsMap::StdlibMap.load(path)
78
- if map.resolved?
79
- logger.debug { "Loading stdlib pins for #{path}" }
80
- pins = map.pins
81
- logger.debug { "Loaded #{pins.length} stdlib pins for #{path}" }
82
- pins
83
- else
84
- # @todo Temporarily ignoring unresolved `require 'set'`
85
- logger.debug { "Require path #{path} could not be resolved in RBS" } unless path == 'set'
86
- nil
87
- end
88
- end
89
-
90
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
91
- #
92
- # @return [String]
93
- def lookup_rbs_version_cache_key gemspec
94
- rbs_map = RbsMap.from_gemspec(gemspec, rbs_collection_path, rbs_collection_config_path)
95
- rbs_map.cache_key
96
- end
97
-
98
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
99
- # @param rbs_version_cache_key [String, nil]
100
- # @param yard_pins [Array<Pin::Base>]
101
- # @param rbs_collection_pins [Array<Pin::Base>]
102
- # @return [void]
103
- def cache_combined_pins gemspec, rbs_version_cache_key, yard_pins, rbs_collection_pins
104
- combined_pins = GemPins.combine(yard_pins, rbs_collection_pins)
105
- serialize_combined_gem(gemspec, rbs_version_cache_key, combined_pins)
106
- end
107
-
108
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
109
- # @return [Array<Pin::Base>, nil]
110
- def deserialize_combined_pin_cache gemspec
111
- rbs_version_cache_key = lookup_rbs_version_cache_key(gemspec)
112
-
113
- load_combined_gem(gemspec, rbs_version_cache_key)
114
- end
115
-
116
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
117
- # @param out [StringIO, IO, nil]
118
- # @return [void]
119
- def uncache_gem gemspec, out: nil
120
- PinCache.uncache(yardoc_path(gemspec), out: out)
121
- PinCache.uncache(yard_gem_path(gemspec), out: out)
122
- uncache_by_prefix(rbs_collection_pins_path_prefix(gemspec), out: out)
123
- uncache_by_prefix(combined_path_prefix(gemspec), out: out)
124
- rbs_version_cache_key = lookup_rbs_version_cache_key(gemspec)
125
- combined_pins_in_memory.delete([gemspec.name, gemspec.version, rbs_version_cache_key])
126
- end
127
-
128
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
129
- def yardoc_processing? gemspec
130
- Yardoc.processing?(yardoc_path(gemspec))
131
- end
132
-
133
- # @return [Array<String>] a list of possible standard library names
134
- def possible_stdlibs
135
- # all dirs and .rb files in Gem::RUBYGEMS_DIR
136
- Dir.glob(File.join(Gem::RUBYGEMS_DIR, '*')).map do |file_or_dir|
137
- basename = File.basename(file_or_dir)
138
- # remove .rb
139
- # @sg-ignore flow sensitive typing should be able to handle redefinition
140
- basename = basename[0..-4] if basename.end_with?('.rb')
141
- basename
142
- end.sort.uniq
143
- rescue StandardError => e
144
- logger.info { "Failed to get possible stdlibs: #{e.message}" }
145
- # @sg-ignore Need to add nil check here
146
- logger.debug { e.backtrace.join("\n") }
147
- []
148
- end
149
-
150
- private
151
-
152
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
153
- # @param rebuild [Boolean] whether to rebuild the cache regardless of whether it already exists
154
- # @param rbs_version_cache_key [String, nil] the cache key for the gem in the RBS collection
155
- #
156
- # @return [Array(Boolean, Boolean, Boolean)] whether to build YARD
157
- # pins, RBS collection pins, and combined pins
158
- def calculate_build_needs gemspec, rebuild:, rbs_version_cache_key:
159
- if rebuild
160
- build_yard = true
161
- build_rbs_collection = true
162
- build_combined = true
163
- else
164
- build_yard = !yard_gem?(gemspec)
165
- build_rbs_collection = !rbs_collection_pins?(gemspec, rbs_version_cache_key)
166
- # @sg-ignore Need to add nil check here
167
- build_combined = !combined_gem?(gemspec, rbs_version_cache_key) || build_yard || build_rbs_collection
168
- end
169
-
170
- build_yard = false if suppress_yard_cache?(gemspec, rbs_version_cache_key)
171
-
172
- [build_yard, build_rbs_collection, build_combined]
173
- end
174
-
175
- # @param gemspec [Gem::Specification]
176
- # @param rbs_version_cache_key [String, nil]
177
- # @param build_yard [Boolean]
178
- # @param build_rbs_collection [Boolean]
179
- # @param build_combined [Boolean]
180
- # @param out [StringIO, IO, nil]
181
- #
182
- # @return [void]
183
- def build_combine_and_cache gemspec,
184
- rbs_version_cache_key,
185
- build_yard:,
186
- build_rbs_collection:,
187
- build_combined:,
188
- out:
189
- log_cache_info(gemspec, rbs_version_cache_key,
190
- build_yard: build_yard,
191
- build_rbs_collection: build_rbs_collection,
192
- build_combined: build_combined,
193
- out: out)
194
- cache_yard_pins(gemspec, out) if build_yard
195
- # this can be nil even if we aren't told to build it - see suppress_yard_cache?
196
- yard_pins = deserialize_yard_pin_cache(gemspec) || []
197
- cache_rbs_collection_pins(gemspec, out) if build_rbs_collection
198
- rbs_collection_pins = deserialize_rbs_collection_cache(gemspec, rbs_version_cache_key) || []
199
- cache_combined_pins(gemspec, rbs_version_cache_key, yard_pins, rbs_collection_pins) if build_combined
200
- end
201
-
202
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
203
- # @param rbs_version_cache_key [String, nil]
204
- # @param build_yard [Boolean]
205
- # @param build_rbs_collection [Boolean]
206
- # @param build_combined [Boolean]
207
- # @param out [StringIO, IO, nil]
208
- #
209
- # @return [void]
210
- def log_cache_info gemspec,
211
- rbs_version_cache_key,
212
- build_yard:,
213
- build_rbs_collection:,
214
- build_combined:,
215
- out:
216
- type = []
217
- type << 'YARD' if build_yard
218
- rbs_source_desc = RbsMap.rbs_source_desc(rbs_version_cache_key)
219
- type << rbs_source_desc if build_rbs_collection && !rbs_source_desc.nil?
220
- # we'll build it anyway, but it won't take long to build with
221
- # only a single source
222
-
223
- # 'combining' is awkward terminology in this case
224
- just_yard = build_yard && rbs_source_desc.nil?
225
-
226
- type << 'combined' if build_combined && !just_yard
227
- out&.puts("Caching #{type.join(' and ')} pins for gem #{gemspec.name}:#{gemspec.version}")
228
- end
229
-
230
- # @param gemspec [Gem::Specification]
231
- # @param out [StringIO, IO, nil]
232
- # @return [Array<Pin::Base>]
233
- def cache_yard_pins gemspec, out
234
- gem_yardoc_path = yardoc_path(gemspec)
235
- Yardoc.build_docs(gem_yardoc_path, yard_plugins, gemspec) unless Yardoc.docs_built?(gem_yardoc_path)
236
- pins = Yardoc.build_pins(gem_yardoc_path, gemspec, out: out)
237
- serialize_yard_gem(gemspec, pins)
238
- logger.info { "Cached #{pins.length} YARD pins for gem #{gemspec.name}:#{gemspec.version}" } unless pins.empty?
239
- pins
240
- end
241
-
242
- # @return [Hash{Array(String, String, String) => Array<Pin::Base>}]
243
- def combined_pins_in_memory
244
- PinCache.all_combined_pins_in_memory[yard_plugins] ||= {}
245
- end
246
-
247
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
248
- # @param _out [StringIO, IO, nil]
249
- # @return [Array<Pin::Base>]
250
- def cache_rbs_collection_pins gemspec, _out
251
- rbs_map = RbsMap.from_gemspec(gemspec, rbs_collection_path, rbs_collection_config_path)
252
- pins = rbs_map.pins
253
- rbs_version_cache_key = rbs_map.cache_key
254
- # cache pins even if result is zero, so we don't retry building pins
255
- pins ||= []
256
- serialize_rbs_collection_pins(gemspec, rbs_version_cache_key, pins)
257
- logger.info do
258
- unless pins.empty?
259
- "Cached #{pins.length} RBS collection pins for gem #{gemspec.name} #{gemspec.version} with " \
260
- "cache_key #{rbs_version_cache_key.inspect}"
261
- end
262
- end
263
- pins
264
- end
265
-
266
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
267
- # @return [Array<Pin::Base>, nil]
268
- def deserialize_yard_pin_cache gemspec
269
- cached = load_yard_gem(gemspec)
270
- if cached
271
- cached
272
- else
273
- logger.debug "No YARD pin cache for #{gemspec.name}:#{gemspec.version}"
274
- nil
275
- end
276
- end
277
-
278
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
279
- # @param rbs_version_cache_key [String, nil]
280
- # @return [Array<Pin::Base>, nil]
281
- def deserialize_rbs_collection_cache gemspec, rbs_version_cache_key
282
- cached = load_rbs_collection_pins(gemspec, rbs_version_cache_key)
283
- Solargraph.assert_or_log(:pin_cache_rbs_collection, 'Asked for non-existent rbs collection') if cached.nil?
284
- logger.info do
285
- "Loaded #{cached&.length} pins from RBS collection cache for #{gemspec.name}:#{gemspec.version}"
286
- end
287
- cached
288
- end
289
-
290
- # @return [Array<String>]
291
- def yard_path_components
292
- ["yard-#{YARD::VERSION}",
293
- yard_plugins.sort.uniq.join('-')]
294
- end
295
-
296
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
297
- # @return [String]
298
- def yardoc_path gemspec
299
- File.join(PinCache.base_dir,
300
- *yard_path_components,
301
- "#{gemspec.name}-#{gemspec.version}.yardoc")
302
- end
303
-
304
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
305
- # @return [String]
306
- def yard_gem_path gemspec
307
- File.join(PinCache.work_dir, *yard_path_components, "#{gemspec.name}-#{gemspec.version}.ser")
308
- end
309
-
310
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
311
- # @return [Array<Pin::Base>, nil]
312
- def load_yard_gem gemspec
313
- PinCache.load(yard_gem_path(gemspec))
314
- end
315
-
316
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
317
- # @param pins [Array<Pin::Base>]
318
- # @return [void]
319
- def serialize_yard_gem gemspec, pins
320
- PinCache.save(yard_gem_path(gemspec), pins)
321
- end
322
-
323
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
324
- # @return [Boolean]
325
- def yard_gem? gemspec
326
- exist?(yard_gem_path(gemspec))
327
- end
328
-
329
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
330
- # @param hash [String, nil]
331
- # @return [String]
332
- def rbs_collection_pins_path gemspec, hash
333
- rbs_collection_pins_path_prefix(gemspec) + "#{hash || 0}.ser"
334
- end
335
-
336
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
337
- # @return [String]
338
- def rbs_collection_pins_path_prefix gemspec
339
- File.join(PinCache.work_dir, 'rbs', "#{gemspec.name}-#{gemspec.version}-")
340
- end
341
-
342
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
343
- # @param hash [String, nil]
344
- #
345
- # @return [Array<Pin::Base>, nil]
346
- def load_rbs_collection_pins gemspec, hash
347
- PinCache.load(rbs_collection_pins_path(gemspec, hash))
348
- end
349
-
350
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
351
- # @param hash [String, nil]
352
- # @param pins [Array<Pin::Base>]
353
- # @return [void]
354
- def serialize_rbs_collection_pins gemspec, hash, pins
355
- PinCache.save(rbs_collection_pins_path(gemspec, hash), pins)
356
- end
357
-
358
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
359
- # @param hash [String, nil]
360
- # @return [String]
361
- def combined_path gemspec, hash
362
- File.join(combined_path_prefix(gemspec) + "-#{hash || 0}.ser")
363
- end
364
-
365
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
366
- # @return [String]
367
- def combined_path_prefix gemspec
368
- File.join(PinCache.work_dir, 'combined', yard_plugins.sort.join('-'), "#{gemspec.name}-#{gemspec.version}")
369
- end
370
-
371
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
372
- # @param hash [String, nil]
373
- # @param pins [Array<Pin::Base>]
374
- # @return [void]
375
- def serialize_combined_gem gemspec, hash, pins
376
- PinCache.save(combined_path(gemspec, hash), pins)
377
- end
378
-
379
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
380
- # @param hash [String]
381
- def combined_gem? gemspec, hash
382
- exist?(combined_path(gemspec, hash))
383
- end
384
-
385
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
386
- # @param hash [String, nil]
387
- # @return [Array<Pin::Base>, nil]
388
- def load_combined_gem gemspec, hash
389
- cached = combined_pins_in_memory[[gemspec.name, gemspec.version, hash]]
390
- return cached if cached
391
- loaded = PinCache.load(combined_path(gemspec, hash))
392
- combined_pins_in_memory[[gemspec.name, gemspec.version, hash]] = loaded if loaded
393
- loaded
394
- end
395
-
396
- # @param gemspec [Gem::Specification, Bundler::LazySpecification]
397
- # @param hash [String, nil]
398
- def rbs_collection_pins? gemspec, hash
399
- exist?(rbs_collection_pins_path(gemspec, hash))
400
- end
401
-
402
- include Logging
403
-
404
- # @param path [String]
405
- def exist? *path
406
- File.file? File.join(*path)
407
- end
408
-
409
- # @return [void]
410
- # @param path_segments [Array<String>]
411
- def uncache_by_prefix *path_segments, out: nil
412
- path = File.join(*path_segments)
413
- glob = "#{path}*"
414
- out&.puts "Clearing pin cache in #{glob}"
415
- Dir.glob(glob).each do |file|
416
- next unless File.file?(file)
417
- FileUtils.rm_rf file, secure: true
418
- out&.puts "Clearing pin cache in #{file}"
419
- end
420
- end
421
-
7
+ module PinCache
422
8
  class << self
423
9
  include Logging
424
10
 
425
- # @return [Hash{Array<String> => Hash{Array(String, String) =>
426
- # Array<Pin::Base>}}] yard plugins, then gemspec name and
427
- # version
428
- def all_combined_pins_in_memory
429
- @all_combined_pins_in_memory ||= {}
430
- end
431
-
432
11
  # The base directory where cached YARD documentation and serialized pins are serialized
433
12
  #
434
13
  # @return [String]
@@ -440,47 +19,6 @@ module Solargraph
440
19
  File.join(Dir.home, '.cache', 'solargraph')
441
20
  end
442
21
 
443
- # @param path_segments [Array<String>]
444
- # @param out [IO, nil]
445
- # @return [void]
446
- def uncache *path_segments, out: nil
447
- path = File.join(*path_segments)
448
- if File.exist?(path)
449
- FileUtils.rm_rf path, secure: true
450
- out.puts "Clearing pin cache in #{path}" unless out.nil?
451
- else
452
- out&.puts "Pin cache file #{path} does not exist"
453
- end
454
- end
455
-
456
- # @return [void]
457
- # @param out [IO, nil]
458
- # @param path_segments [Array<String>]
459
- def uncache_by_prefix *path_segments, out: nil
460
- path = File.join(*path_segments)
461
- glob = "#{path}*"
462
- out.puts "Clearing pin cache in #{glob}" unless out.nil?
463
- Dir.glob(glob).each do |file|
464
- next unless File.file?(file)
465
- FileUtils.rm_rf file, secure: true
466
- out.puts "Clearing pin cache in #{file}" unless out.nil?
467
- end
468
- end
469
-
470
- # @param out [StringIO, IO, nil]
471
- # @return [void]
472
- def uncache_core out: nil
473
- uncache(core_path, out: out)
474
- # ApiMap keep this in memory
475
- ApiMap.reset_core(out: out)
476
- end
477
-
478
- # @param out [StringIO, IO, nil]
479
- # @return [void]
480
- def uncache_stdlib out: nil
481
- uncache(stdlib_path, out: out)
482
- end
483
-
484
22
  # The working directory for the current Ruby, RBS, and Solargraph versions.
485
23
  #
486
24
  # @return [String]
@@ -490,6 +28,15 @@ module Solargraph
490
28
  File.join(base_dir, "ruby-#{RUBY_VERSION}", "rbs-#{RBS::VERSION}", "solargraph-#{Solargraph::VERSION}")
491
29
  end
492
30
 
31
+ # @param gemspec [Gem::Specification]
32
+ # @return [String]
33
+ def yardoc_path gemspec
34
+ File.join(base_dir,
35
+ "yard-#{YARD::VERSION}",
36
+ "yard-activesupport-concern-#{YARD::ActiveSupport::Concern::VERSION}",
37
+ "#{gemspec.name}-#{gemspec.version}.yardoc")
38
+ end
39
+
493
40
  # @return [String]
494
41
  def stdlib_path
495
42
  File.join(work_dir, 'stdlib')
@@ -538,40 +85,40 @@ module Solargraph
538
85
 
539
86
  # @param gemspec [Gem::Specification]
540
87
  # @return [Array<Pin::Base>, nil]
541
- def deserialize_yard_gem(gemspec)
88
+ def deserialize_yard_gem gemspec
542
89
  load(yard_gem_path(gemspec))
543
90
  end
544
91
 
545
92
  # @param gemspec [Gem::Specification]
546
93
  # @param pins [Array<Pin::Base>]
547
94
  # @return [void]
548
- def serialize_yard_gem(gemspec, pins)
95
+ def serialize_yard_gem gemspec, pins
549
96
  save(yard_gem_path(gemspec), pins)
550
97
  end
551
98
 
552
99
  # @param gemspec [Gem::Specification]
553
100
  # @return [Boolean]
554
- def has_yard?(gemspec)
101
+ def has_yard? gemspec
555
102
  exist?(yard_gem_path(gemspec))
556
103
  end
557
104
 
558
105
  # @param gemspec [Gem::Specification]
559
106
  # @param hash [String, nil]
560
107
  # @return [String]
561
- def rbs_collection_path(gemspec, hash)
108
+ def rbs_collection_path gemspec, hash
562
109
  File.join(work_dir, 'rbs', "#{gemspec.name}-#{gemspec.version}-#{hash || 0}.ser")
563
110
  end
564
111
 
565
112
  # @param gemspec [Gem::Specification]
566
113
  # @return [String]
567
- def rbs_collection_path_prefix(gemspec)
114
+ def rbs_collection_path_prefix gemspec
568
115
  File.join(work_dir, 'rbs', "#{gemspec.name}-#{gemspec.version}-")
569
116
  end
570
117
 
571
118
  # @param gemspec [Gem::Specification]
572
119
  # @param hash [String, nil]
573
120
  # @return [Array<Pin::Base>, nil]
574
- def deserialize_rbs_collection_gem(gemspec, hash)
121
+ def deserialize_rbs_collection_gem gemspec, hash
575
122
  load(rbs_collection_path(gemspec, hash))
576
123
  end
577
124
 
@@ -579,20 +126,20 @@ module Solargraph
579
126
  # @param hash [String, nil]
580
127
  # @param pins [Array<Pin::Base>]n
581
128
  # @return [void]
582
- def serialize_rbs_collection_gem(gemspec, hash, pins)
129
+ def serialize_rbs_collection_gem gemspec, hash, pins
583
130
  save(rbs_collection_path(gemspec, hash), pins)
584
131
  end
585
132
 
586
133
  # @param gemspec [Gem::Specification]
587
134
  # @param hash [String, nil]
588
135
  # @return [String]
589
- def combined_path(gemspec, hash)
136
+ def combined_path gemspec, hash
590
137
  File.join(work_dir, 'combined', "#{gemspec.name}-#{gemspec.version}-#{hash || 0}.ser")
591
138
  end
592
139
 
593
140
  # @param gemspec [Gem::Specification]
594
141
  # @return [String]
595
- def combined_path_prefix(gemspec)
142
+ def combined_path_prefix gemspec
596
143
  File.join(work_dir, 'combined', "#{gemspec.name}-#{gemspec.version}-")
597
144
  end
598
145
 
@@ -600,7 +147,7 @@ module Solargraph
600
147
  # @param hash [String, nil]
601
148
  # @param pins [Array<Pin::Base>]
602
149
  # @return [void]
603
- def serialize_combined_gem(gemspec, hash, pins)
150
+ def serialize_combined_gem gemspec, hash, pins
604
151
  save(combined_path(gemspec, hash), pins)
605
152
  end
606
153
 
@@ -614,17 +161,39 @@ module Solargraph
614
161
  # @param gemspec [Gem::Specification]
615
162
  # @param hash [String, nil]
616
163
  # @return [Boolean]
617
- def has_rbs_collection?(gemspec, hash)
164
+ def has_rbs_collection? gemspec, hash
618
165
  exist?(rbs_collection_path(gemspec, hash))
619
166
  end
620
167
 
168
+ # @return [void]
169
+ def uncache_core
170
+ uncache(core_path)
171
+ end
172
+
173
+ # @return [void]
174
+ def uncache_stdlib
175
+ uncache(stdlib_path)
176
+ end
177
+
178
+ # @param gemspec [Gem::Specification]
179
+ # @param out [IO, StringIO, nil]
180
+ # @return [void]
181
+ def uncache_gem gemspec, out: nil
182
+ uncache(yardoc_path(gemspec), out: out)
183
+ uncache_by_prefix(rbs_collection_path_prefix(gemspec), out: out)
184
+ uncache(yard_gem_path(gemspec), out: out)
185
+ uncache_by_prefix(combined_path_prefix(gemspec), out: out)
186
+ end
187
+
621
188
  # @return [void]
622
189
  def clear
623
190
  FileUtils.rm_rf base_dir, secure: true
624
191
  end
625
192
 
193
+ private
194
+
626
195
  # @param file [String]
627
- # @sg-ignore Marshal.load evaluates to boolean here which is wrong
196
+ # @sg-ignore Marshal.load returns Object; we know it's Array<Pin::Base>
628
197
  # @return [Array<Solargraph::Pin::Base>, nil]
629
198
  def load file
630
199
  return nil unless File.file?(file)
@@ -635,6 +204,11 @@ module Solargraph
635
204
  nil
636
205
  end
637
206
 
207
+ # @param path [String]
208
+ def exist? *path
209
+ File.file? File.join(*path)
210
+ end
211
+
638
212
  # @param file [String]
639
213
  # @param pins [Array<Pin::Base>]
640
214
  # @return [void]
@@ -646,19 +220,28 @@ module Solargraph
646
220
  logger.debug { "Cache#save: Saved #{pins.length} pins to #{file}" }
647
221
  end
648
222
 
649
- def core?
650
- File.file?(core_path)
651
- end
652
-
653
- # @param out [StringIO, IO, nil]
654
- # @return [Array<Pin::Base>]
655
- def cache_core out: $stderr
656
- RbsMap::CoreMap.new.cache_core(out: out)
223
+ # @param path_segments [Array<String>]
224
+ # @return [void]
225
+ # @param [Object, nil] out
226
+ def uncache *path_segments, out: nil
227
+ path = File.join(*path_segments)
228
+ return unless File.exist?(path)
229
+ FileUtils.rm_rf path, secure: true
230
+ out&.puts "Clearing pin cache in #{path}"
657
231
  end
658
232
 
659
- # @param path [String]
660
- def exist? *path
661
- File.file? File.join(*path)
233
+ # @return [void]
234
+ # @param path_segments [Array<String>]
235
+ # @param [Object, nil] out
236
+ def uncache_by_prefix *path_segments, out: nil
237
+ path = File.join(*path_segments)
238
+ glob = "#{path}*"
239
+ out&.puts "Clearing pin cache in #{glob}"
240
+ Dir.glob(glob).each do |file|
241
+ next unless File.file?(file)
242
+ FileUtils.rm_rf file, secure: true
243
+ out&.puts "Clearing pin cache in #{file}"
244
+ end
662
245
  end
663
246
  end
664
247
  end