solargraph 0.57.0 → 0.58.3

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 (216) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +2 -0
  3. data/.github/workflows/linting.yml +4 -2
  4. data/.github/workflows/plugins.yml +63 -28
  5. data/.github/workflows/rspec.yml +19 -4
  6. data/.github/workflows/typecheck.yml +2 -2
  7. data/.gitignore +1 -0
  8. data/.rubocop.yml +1 -1
  9. data/.rubocop_todo.yml +90 -1438
  10. data/CHANGELOG.md +39 -0
  11. data/Rakefile +1 -1
  12. data/bin/solargraph +3 -0
  13. data/lib/solargraph/api_map/cache.rb +110 -110
  14. data/lib/solargraph/api_map/constants.rb +279 -218
  15. data/lib/solargraph/api_map/index.rb +193 -169
  16. data/lib/solargraph/api_map/source_to_yard.rb +97 -94
  17. data/lib/solargraph/api_map/store.rb +384 -374
  18. data/lib/solargraph/api_map.rb +945 -951
  19. data/lib/solargraph/bench.rb +45 -45
  20. data/lib/solargraph/complex_type/type_methods.rb +228 -223
  21. data/lib/solargraph/complex_type/unique_type.rb +482 -475
  22. data/lib/solargraph/complex_type.rb +444 -427
  23. data/lib/solargraph/convention/active_support_concern.rb +1 -1
  24. data/lib/solargraph/convention/data_definition/data_assignment_node.rb +61 -61
  25. data/lib/solargraph/convention/data_definition/data_definition_node.rb +91 -91
  26. data/lib/solargraph/convention/data_definition.rb +105 -105
  27. data/lib/solargraph/convention/gemfile.rb +15 -15
  28. data/lib/solargraph/convention/gemspec.rb +23 -23
  29. data/lib/solargraph/convention/rakefile.rb +17 -17
  30. data/lib/solargraph/convention/struct_definition/struct_assignment_node.rb +61 -61
  31. data/lib/solargraph/convention/struct_definition/struct_definition_node.rb +102 -102
  32. data/lib/solargraph/convention/struct_definition.rb +164 -164
  33. data/lib/solargraph/convention.rb +78 -78
  34. data/lib/solargraph/converters/dd.rb +17 -17
  35. data/lib/solargraph/converters/dl.rb +15 -15
  36. data/lib/solargraph/converters/dt.rb +15 -15
  37. data/lib/solargraph/converters/misc.rb +1 -1
  38. data/lib/solargraph/diagnostics/require_not_found.rb +53 -53
  39. data/lib/solargraph/diagnostics/rubocop.rb +118 -118
  40. data/lib/solargraph/diagnostics/rubocop_helpers.rb +68 -66
  41. data/lib/solargraph/diagnostics/type_check.rb +55 -55
  42. data/lib/solargraph/diagnostics/update_errors.rb +41 -41
  43. data/lib/solargraph/doc_map.rb +439 -436
  44. data/lib/solargraph/environ.rb +1 -1
  45. data/lib/solargraph/equality.rb +34 -33
  46. data/lib/solargraph/gem_pins.rb +98 -94
  47. data/lib/solargraph/language_server/error_codes.rb +20 -20
  48. data/lib/solargraph/language_server/host/diagnoser.rb +89 -89
  49. data/lib/solargraph/language_server/host/dispatch.rb +130 -130
  50. data/lib/solargraph/language_server/host/message_worker.rb +112 -112
  51. data/lib/solargraph/language_server/host/sources.rb +99 -99
  52. data/lib/solargraph/language_server/host.rb +878 -872
  53. data/lib/solargraph/language_server/message/base.rb +97 -97
  54. data/lib/solargraph/language_server/message/client/register_capability.rb +15 -15
  55. data/lib/solargraph/language_server/message/completion_item/resolve.rb +60 -60
  56. data/lib/solargraph/language_server/message/extended/check_gem_version.rb +114 -114
  57. data/lib/solargraph/language_server/message/extended/document.rb +23 -23
  58. data/lib/solargraph/language_server/message/extended/document_gems.rb +32 -32
  59. data/lib/solargraph/language_server/message/extended/download_core.rb +19 -19
  60. data/lib/solargraph/language_server/message/extended/search.rb +20 -20
  61. data/lib/solargraph/language_server/message/initialize.rb +191 -191
  62. data/lib/solargraph/language_server/message/text_document/completion.rb +56 -56
  63. data/lib/solargraph/language_server/message/text_document/definition.rb +40 -40
  64. data/lib/solargraph/language_server/message/text_document/document_highlight.rb +16 -16
  65. data/lib/solargraph/language_server/message/text_document/document_symbol.rb +26 -26
  66. data/lib/solargraph/language_server/message/text_document/formatting.rb +148 -145
  67. data/lib/solargraph/language_server/message/text_document/hover.rb +58 -58
  68. data/lib/solargraph/language_server/message/text_document/prepare_rename.rb +11 -11
  69. data/lib/solargraph/language_server/message/text_document/references.rb +16 -16
  70. data/lib/solargraph/language_server/message/text_document/rename.rb +19 -19
  71. data/lib/solargraph/language_server/message/text_document/signature_help.rb +24 -24
  72. data/lib/solargraph/language_server/message/text_document/type_definition.rb +25 -25
  73. data/lib/solargraph/language_server/message/workspace/did_change_configuration.rb +35 -35
  74. data/lib/solargraph/language_server/message/workspace/did_change_watched_files.rb +40 -40
  75. data/lib/solargraph/language_server/message/workspace/did_change_workspace_folders.rb +26 -26
  76. data/lib/solargraph/language_server/message/workspace/workspace_symbol.rb +23 -23
  77. data/lib/solargraph/language_server/message.rb +94 -94
  78. data/lib/solargraph/language_server/progress.rb +1 -1
  79. data/lib/solargraph/language_server/request.rb +27 -25
  80. data/lib/solargraph/language_server/transport/data_reader.rb +74 -74
  81. data/lib/solargraph/language_server/uri_helpers.rb +49 -49
  82. data/lib/solargraph/library.rb +683 -683
  83. data/lib/solargraph/location.rb +82 -81
  84. data/lib/solargraph/logging.rb +37 -37
  85. data/lib/solargraph/page.rb +92 -93
  86. data/lib/solargraph/parser/comment_ripper.rb +69 -69
  87. data/lib/solargraph/parser/flow_sensitive_typing.rb +255 -255
  88. data/lib/solargraph/parser/node_processor/base.rb +92 -92
  89. data/lib/solargraph/parser/node_processor.rb +62 -62
  90. data/lib/solargraph/parser/parser_gem/class_methods.rb +149 -159
  91. data/lib/solargraph/parser/parser_gem/flawed_builder.rb +19 -19
  92. data/lib/solargraph/parser/parser_gem/node_chainer.rb +166 -166
  93. data/lib/solargraph/parser/parser_gem/node_methods.rb +486 -499
  94. data/lib/solargraph/parser/parser_gem/node_processors/and_node.rb +22 -21
  95. data/lib/solargraph/parser/parser_gem/node_processors/args_node.rb +59 -59
  96. data/lib/solargraph/parser/parser_gem/node_processors/begin_node.rb +15 -15
  97. data/lib/solargraph/parser/parser_gem/node_processors/block_node.rb +46 -46
  98. data/lib/solargraph/parser/parser_gem/node_processors/def_node.rb +53 -53
  99. data/lib/solargraph/parser/parser_gem/node_processors/defs_node.rb +37 -37
  100. data/lib/solargraph/parser/parser_gem/node_processors/if_node.rb +23 -23
  101. data/lib/solargraph/parser/parser_gem/node_processors/ivasgn_node.rb +40 -40
  102. data/lib/solargraph/parser/parser_gem/node_processors/lvasgn_node.rb +29 -29
  103. data/lib/solargraph/parser/parser_gem/node_processors/masgn_node.rb +59 -59
  104. data/lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb +98 -42
  105. data/lib/solargraph/parser/parser_gem/node_processors/orasgn_node.rb +17 -17
  106. data/lib/solargraph/parser/parser_gem/node_processors/resbody_node.rb +38 -38
  107. data/lib/solargraph/parser/parser_gem/node_processors/sclass_node.rb +52 -43
  108. data/lib/solargraph/parser/parser_gem/node_processors/send_node.rb +291 -292
  109. data/lib/solargraph/parser/parser_gem/node_processors/until_node.rb +29 -29
  110. data/lib/solargraph/parser/parser_gem/node_processors/while_node.rb +29 -29
  111. data/lib/solargraph/parser/parser_gem/node_processors.rb +70 -70
  112. data/lib/solargraph/parser/parser_gem.rb +12 -12
  113. data/lib/solargraph/parser/region.rb +69 -69
  114. data/lib/solargraph/parser/snippet.rb +17 -17
  115. data/lib/solargraph/parser.rb +23 -23
  116. data/lib/solargraph/pin/base.rb +729 -708
  117. data/lib/solargraph/pin/base_variable.rb +126 -124
  118. data/lib/solargraph/pin/block.rb +104 -103
  119. data/lib/solargraph/pin/breakable.rb +9 -9
  120. data/lib/solargraph/pin/callable.rb +231 -227
  121. data/lib/solargraph/pin/closure.rb +72 -76
  122. data/lib/solargraph/pin/common.rb +79 -79
  123. data/lib/solargraph/pin/constant.rb +45 -45
  124. data/lib/solargraph/pin/conversions.rb +123 -123
  125. data/lib/solargraph/pin/delegated_method.rb +120 -121
  126. data/lib/solargraph/pin/documenting.rb +114 -114
  127. data/lib/solargraph/pin/instance_variable.rb +34 -34
  128. data/lib/solargraph/pin/keyword.rb +20 -20
  129. data/lib/solargraph/pin/local_variable.rb +75 -79
  130. data/lib/solargraph/pin/method.rb +672 -656
  131. data/lib/solargraph/pin/method_alias.rb +34 -34
  132. data/lib/solargraph/pin/namespace.rb +115 -115
  133. data/lib/solargraph/pin/parameter.rb +275 -271
  134. data/lib/solargraph/pin/proxy_type.rb +39 -36
  135. data/lib/solargraph/pin/reference/override.rb +47 -47
  136. data/lib/solargraph/pin/reference/superclass.rb +15 -15
  137. data/lib/solargraph/pin/reference.rb +39 -48
  138. data/lib/solargraph/pin/search.rb +61 -58
  139. data/lib/solargraph/pin/signature.rb +61 -61
  140. data/lib/solargraph/pin/symbol.rb +53 -53
  141. data/lib/solargraph/pin/until.rb +18 -18
  142. data/lib/solargraph/pin/while.rb +18 -18
  143. data/lib/solargraph/pin.rb +44 -44
  144. data/lib/solargraph/pin_cache.rb +245 -245
  145. data/lib/solargraph/position.rb +132 -118
  146. data/lib/solargraph/range.rb +112 -108
  147. data/lib/solargraph/rbs_map/conversions.rb +823 -802
  148. data/lib/solargraph/rbs_map/core_fills.rb +84 -66
  149. data/lib/solargraph/rbs_map/core_map.rb +58 -54
  150. data/lib/solargraph/rbs_map/stdlib_map.rb +43 -43
  151. data/lib/solargraph/rbs_map.rb +163 -163
  152. data/lib/solargraph/server_methods.rb +16 -16
  153. data/lib/solargraph/shell.rb +363 -271
  154. data/lib/solargraph/source/chain/array.rb +37 -37
  155. data/lib/solargraph/source/chain/call.rb +337 -333
  156. data/lib/solargraph/source/chain/class_variable.rb +13 -13
  157. data/lib/solargraph/source/chain/constant.rb +26 -89
  158. data/lib/solargraph/source/chain/global_variable.rb +13 -13
  159. data/lib/solargraph/source/chain/hash.rb +34 -34
  160. data/lib/solargraph/source/chain/if.rb +28 -28
  161. data/lib/solargraph/source/chain/instance_variable.rb +13 -13
  162. data/lib/solargraph/source/chain/link.rb +109 -109
  163. data/lib/solargraph/source/chain/literal.rb +48 -48
  164. data/lib/solargraph/source/chain/or.rb +23 -23
  165. data/lib/solargraph/source/chain/q_call.rb +11 -11
  166. data/lib/solargraph/source/chain/variable.rb +13 -13
  167. data/lib/solargraph/source/chain/z_super.rb +30 -30
  168. data/lib/solargraph/source/chain.rb +291 -289
  169. data/lib/solargraph/source/change.rb +82 -82
  170. data/lib/solargraph/source/cursor.rb +166 -166
  171. data/lib/solargraph/source/encoding_fixes.rb +23 -23
  172. data/lib/solargraph/source/source_chainer.rb +194 -194
  173. data/lib/solargraph/source/updater.rb +55 -55
  174. data/lib/solargraph/source.rb +498 -498
  175. data/lib/solargraph/source_map/clip.rb +226 -234
  176. data/lib/solargraph/source_map/data.rb +34 -34
  177. data/lib/solargraph/source_map/mapper.rb +259 -261
  178. data/lib/solargraph/source_map.rb +212 -207
  179. data/lib/solargraph/type_checker/checks.rb +124 -124
  180. data/lib/solargraph/type_checker/param_def.rb +37 -37
  181. data/lib/solargraph/type_checker/problem.rb +32 -32
  182. data/lib/solargraph/type_checker/rules.rb +84 -70
  183. data/lib/solargraph/type_checker.rb +814 -752
  184. data/lib/solargraph/version.rb +5 -5
  185. data/lib/solargraph/workspace/config.rb +255 -237
  186. data/lib/solargraph/workspace/require_paths.rb +97 -98
  187. data/lib/solargraph/workspace.rb +220 -225
  188. data/lib/solargraph/yard_map/helpers.rb +44 -44
  189. data/lib/solargraph/yard_map/mapper/to_method.rb +130 -129
  190. data/lib/solargraph/yard_map/mapper/to_namespace.rb +31 -30
  191. data/lib/solargraph/yard_map/mapper.rb +79 -79
  192. data/lib/solargraph/yard_map/to_method.rb +89 -88
  193. data/lib/solargraph/yard_tags.rb +20 -20
  194. data/lib/solargraph/yardoc.rb +87 -64
  195. data/lib/solargraph.rb +105 -105
  196. data/rbs/fills/bundler/0/bundler.rbs +4271 -0
  197. data/rbs/fills/open3/0/open3.rbs +172 -0
  198. data/rbs/fills/rubygems/0/basic_specification.rbs +326 -0
  199. data/rbs/fills/rubygems/0/errors.rbs +364 -0
  200. data/rbs/fills/rubygems/0/spec_fetcher.rbs +107 -0
  201. data/rbs/fills/rubygems/0/specification.rbs +1753 -0
  202. data/rbs/shims/ast/0/node.rbs +5 -0
  203. data/rbs/shims/ast/2.4/.rbs_meta.yaml +9 -0
  204. data/rbs/shims/ast/2.4/ast.rbs +73 -0
  205. data/rbs/shims/parser/3.2.0.1/manifest.yaml +7 -0
  206. data/rbs/shims/parser/3.2.0.1/parser.rbs +201 -0
  207. data/rbs/shims/parser/3.2.0.1/polyfill.rbs +4 -0
  208. data/rbs_collection.yaml +4 -4
  209. data/solargraph.gemspec +15 -4
  210. metadata +71 -16
  211. data/lib/solargraph/parser/node_methods.rb +0 -97
  212. /data/rbs/fills/{tuple.rbs → tuple/tuple.rbs} +0 -0
  213. /data/{sig → rbs}/shims/parser/3.2.0.1/builders/default.rbs +0 -0
  214. /data/{sig → rbs}/shims/thor/1.2.0.1/.rbs_meta.yaml +0 -0
  215. /data/{sig → rbs}/shims/thor/1.2.0.1/manifest.yaml +0 -0
  216. /data/{sig → rbs}/shims/thor/1.2.0.1/thor.rbs +0 -0
@@ -1,708 +1,729 @@
1
- # frozen_string_literal: true
2
-
3
- module Solargraph
4
- module Pin
5
- # The base class for map pins.
6
- #
7
- class Base
8
- include Common
9
- include Conversions
10
- include Documenting
11
- include Logging
12
-
13
- # @return [YARD::CodeObjects::Base]
14
- attr_reader :code_object
15
-
16
- # @return [Solargraph::Location, nil]
17
- attr_reader :location
18
-
19
- # @return [Solargraph::Location, nil]
20
- attr_reader :type_location
21
-
22
- # @return [String]
23
- attr_reader :name
24
-
25
- # @return [String]
26
- attr_reader :path
27
-
28
- # @return [::Symbol]
29
- attr_accessor :source
30
-
31
- # @type [::Numeric, nil] A priority for determining if pins should be combined or not
32
- # A nil priority is considered the be the lowest. All code, yard & rbs pins have nil priority
33
- # Between 2 pins, the one with the higher priority gets chosen. If the priorities are equal, they are combined.
34
- attr_reader :combine_priority
35
-
36
- def presence_certain?
37
- true
38
- end
39
-
40
- # @param location [Solargraph::Location, nil]
41
- # @param type_location [Solargraph::Location, nil]
42
- # @param closure [Solargraph::Pin::Closure, nil]
43
- # @param name [String]
44
- # @param comments [String]
45
- # @param source [Symbol, nil]
46
- # @param docstring [YARD::Docstring, nil]
47
- # @param directives [::Array<YARD::Tags::Directive>, nil]
48
- # @param combine_priority [::Numeric, nil] See attr_reader for combine_priority
49
- def initialize location: nil, type_location: nil, closure: nil, source: nil, name: '', comments: '', docstring: nil, directives: nil, combine_priority: nil
50
- @location = location
51
- @type_location = type_location
52
- @closure = closure
53
- @name = name
54
- @comments = comments
55
- @source = source
56
- @identity = nil
57
- @docstring = docstring
58
- @directives = directives
59
- @combine_priority = combine_priority
60
-
61
- assert_source_provided
62
- assert_location_provided
63
- end
64
-
65
- # @return [void]
66
- def assert_location_provided
67
- return unless best_location.nil? && %i[yardoc source rbs].include?(source)
68
-
69
- Solargraph.assert_or_log(:best_location, "Neither location nor type_location provided - #{path} #{source} #{self.class}")
70
- end
71
-
72
- # @return [Pin::Closure, nil]
73
- def closure
74
- Solargraph.assert_or_log(:closure, "Closure not set on #{self.class} #{name.inspect} from #{source.inspect}") unless @closure
75
- # @type [Pin::Closure, nil]
76
- @closure
77
- end
78
-
79
- # @param other [self]
80
- # @param attrs [Hash{::Symbol => Object}]
81
- #
82
- # @return [self]
83
- def combine_with(other, attrs={})
84
- raise "tried to combine #{other.class} with #{self.class}" unless other.class == self.class
85
- priority_choice = choose_priority(other)
86
- return priority_choice unless priority_choice.nil?
87
-
88
- type_location = choose(other, :type_location)
89
- location = choose(other, :location)
90
- combined_name = combine_name(other)
91
- new_attrs = {
92
- location: location,
93
- type_location: type_location,
94
- name: combined_name,
95
- closure: choose_pin_attr_with_same_name(other, :closure),
96
- comments: choose_longer(other, :comments),
97
- source: :combined,
98
- docstring: choose(other, :docstring),
99
- directives: combine_directives(other),
100
- combine_priority: combine_priority
101
- }.merge(attrs)
102
- assert_same_macros(other)
103
- logger.debug { "Base#combine_with(path=#{path}) - other.comments=#{other.comments.inspect}, self.comments = #{self.comments}" }
104
- out = self.class.new(**new_attrs)
105
- out.reset_generated!
106
- out
107
- end
108
-
109
- # @param other [self]
110
- # @return [self, nil] Returns either the pin chosen based on priority or nil
111
- # A nil return means that the combination process must proceed
112
- def choose_priority(other)
113
- if combine_priority.nil? && !other.combine_priority.nil?
114
- return other
115
- elsif other.combine_priority.nil? && !combine_priority.nil?
116
- return self
117
- elsif !combine_priority.nil? && !other.combine_priority.nil?
118
- if combine_priority > other.combine_priority
119
- return self
120
- elsif combine_priority < other.combine_priority
121
- return other
122
- end
123
- end
124
-
125
- nil
126
- end
127
-
128
- # @param other [self]
129
- # @param attr [::Symbol]
130
- # @sg-ignore
131
- # @return [undefined]
132
- def choose_longer(other, attr)
133
- # @type [undefined]
134
- val1 = send(attr)
135
- # @type [undefined]
136
- val2 = other.send(attr)
137
- return val1 if val1 == val2
138
- return val2 if val1.nil?
139
- val1.length > val2.length ? val1 : val2
140
- end
141
-
142
- # @param other [self]
143
- # @return [::Array<YARD::Tags::Directive>, nil]
144
- def combine_directives(other)
145
- return self.directives if other.directives.empty?
146
- return other.directives if directives.empty?
147
- [directives + other.directives].uniq
148
- end
149
-
150
- # @param other [self]
151
- # @return [String]
152
- def combine_name(other)
153
- if needs_consistent_name? || other.needs_consistent_name?
154
- assert_same(other, :name)
155
- else
156
- choose(other, :name)
157
- end
158
- end
159
-
160
- # @return [void]
161
- def reset_generated!
162
- # @return_type doesn't go here as subclasses tend to assign it
163
- # themselves in constructors, and they will deal with setting
164
- # it in any methods that call this
165
- #
166
- # @docstring also doesn't go here, as there is code which
167
- # directly manipulates docstring without editing comments
168
- # (e.g., Api::Map::Store#index processes overrides that way
169
- #
170
- # Same with @directives, @macros, @maybe_directives, which
171
- # regenerate docstring
172
- @deprecated = nil
173
- reset_conversions
174
- end
175
-
176
- def needs_consistent_name?
177
- true
178
- end
179
-
180
- # @sg-ignore def should infer as symbol - "Not enough arguments to Module#protected"
181
- protected def equality_fields
182
- [name, location, type_location, closure, source]
183
- end
184
-
185
- # @param other [self]
186
- # @return [ComplexType]
187
- def combine_return_type(other)
188
- if return_type.undefined?
189
- other.return_type
190
- elsif other.return_type.undefined?
191
- return_type
192
- elsif dodgy_return_type_source? && !other.dodgy_return_type_source?
193
- other.return_type
194
- elsif other.dodgy_return_type_source? && !dodgy_return_type_source?
195
- return_type
196
- else
197
- all_items = return_type.items + other.return_type.items
198
- if all_items.any? { |item| item.selfy? } && all_items.any? { |item| item.rooted_tag == context.rooted_tag }
199
- # assume this was a declaration that should have said 'self'
200
- all_items.delete_if { |item| item.rooted_tag == context.rooted_tag }
201
- end
202
- ComplexType.new(all_items)
203
- end
204
- end
205
-
206
- def dodgy_return_type_source?
207
- # uses a lot of 'Object' instead of 'self'
208
- location&.filename&.include?('core_ext/object/')
209
- end
210
-
211
- # when choices are arbitrary, make sure the choice is consistent
212
- #
213
- # @param other [Pin::Base]
214
- # @param attr [::Symbol]
215
- #
216
- # @return [Object, nil]
217
- def choose(other, attr)
218
- results = [self, other].map(&attr).compact
219
- # true and false are different classes and can't be sorted
220
- return true if results.any? { |r| r == true || r == false }
221
- results.min
222
- rescue
223
- STDERR.puts("Problem handling #{attr} for \n#{self.inspect}\n and \n#{other.inspect}\n\n#{self.send(attr).inspect} vs #{other.send(attr).inspect}")
224
- raise
225
- end
226
-
227
- # @param other [self]
228
- # @param attr [::Symbol]
229
- # @sg-ignore
230
- # @return [undefined]
231
- def choose_node(other, attr)
232
- if other.object_id < attr.object_id
233
- other.send(attr)
234
- else
235
- send(attr)
236
- end
237
- end
238
-
239
- # @param other [self]
240
- # @param attr [::Symbol]
241
- # @sg-ignore
242
- # @return [undefined]
243
- def prefer_rbs_location(other, attr)
244
- if rbs_location? && !other.rbs_location?
245
- self.send(attr)
246
- elsif !rbs_location? && other.rbs_location?
247
- other.send(attr)
248
- else
249
- choose(other, attr)
250
- end
251
- end
252
-
253
- def rbs_location?
254
- type_location&.rbs?
255
- end
256
-
257
- # @param other [self]
258
- # @return [void]
259
- def assert_same_macros(other)
260
- return unless self.source == :yardoc && other.source == :yardoc
261
- assert_same_count(other, :macros)
262
- assert_same_array_content(other, :macros) { |macro| macro.tag.name }
263
- end
264
-
265
- # @param other [self]
266
- # @param attr [::Symbol]
267
- # @return [void]
268
- # @todo strong typechecking should complain when there are no block-related tags
269
- def assert_same_array_content(other, attr, &block)
270
- arr1 = send(attr)
271
- raise "Expected #{attr} on #{self} to be an Enumerable, got #{arr1.class}" unless arr1.is_a?(::Enumerable)
272
- # @type arr1 [::Enumerable]
273
- arr2 = other.send(attr)
274
- raise "Expected #{attr} on #{other} to be an Enumerable, got #{arr2.class}" unless arr2.is_a?(::Enumerable)
275
- # @type arr2 [::Enumerable]
276
-
277
- # @type [undefined]
278
- values1 = arr1.map(&block)
279
- # @type [undefined]
280
- values2 = arr2.map(&block)
281
- # @sg-ignore
282
- return arr1 if values1 == values2
283
- Solargraph.assert_or_log("combine_with_#{attr}".to_sym,
284
- "Inconsistent #{attr.inspect} values between \nself =#{inspect} and \nother=#{other.inspect}:\n\n self values = #{values1}\nother values =#{attr} = #{values2}")
285
- arr1
286
- end
287
-
288
- # @param other [self]
289
- # @param attr [::Symbol]
290
- #
291
- # @return [::Enumerable]
292
- def assert_same_count(other, attr)
293
- # @type [::Enumerable]
294
- arr1 = self.send(attr)
295
- raise "Expected #{attr} on #{self} to be an Enumerable, got #{arr1.class}" unless arr1.is_a?(::Enumerable)
296
- # @type [::Enumerable]
297
- arr2 = other.send(attr)
298
- raise "Expected #{attr} on #{other} to be an Enumerable, got #{arr2.class}" unless arr2.is_a?(::Enumerable)
299
- return arr1 if arr1.count == arr2.count
300
- Solargraph.assert_or_log("combine_with_#{attr}".to_sym,
301
- "Inconsistent #{attr.inspect} count value between \nself =#{inspect} and \nother=#{other.inspect}:\n\n self.#{attr} = #{arr1.inspect}\nother.#{attr} = #{arr2.inspect}")
302
- arr1
303
- end
304
-
305
- # @param other [self]
306
- # @param attr [::Symbol]
307
- #
308
- # @sg-ignore
309
- # @return [undefined]
310
- def assert_same(other, attr)
311
- return false if other.nil?
312
- val1 = send(attr)
313
- val2 = other.send(attr)
314
- return val1 if val1 == val2
315
- Solargraph.assert_or_log("combine_with_#{attr}".to_sym,
316
- "Inconsistent #{attr.inspect} values between \nself =#{inspect} and \nother=#{other.inspect}:\n\n self.#{attr} = #{val1.inspect}\nother.#{attr} = #{val2.inspect}")
317
- val1
318
- end
319
-
320
- # @param other [self]
321
- # @param attr [::Symbol]
322
- # @sg-ignore
323
- # @return [undefined]
324
- def choose_pin_attr_with_same_name(other, attr)
325
- # @type [Pin::Base, nil]
326
- val1 = send(attr)
327
- # @type [Pin::Base, nil]
328
- val2 = other.send(attr)
329
- raise "Expected pin for #{attr} on\n#{self.inspect},\ngot #{val1.inspect}" unless val1.nil? || val1.is_a?(Pin::Base)
330
- raise "Expected pin for #{attr} on\n#{other.inspect},\ngot #{val2.inspect}" unless val2.nil? || val2.is_a?(Pin::Base)
331
- if val1&.name != val2&.name
332
- Solargraph.assert_or_log("combine_with_#{attr}_name".to_sym,
333
- "Inconsistent #{attr.inspect} name values between \nself =#{inspect} and \nother=#{other.inspect}:\n\n self.#{attr} = #{val1.inspect}\nother.#{attr} = #{val2.inspect}")
334
- end
335
- choose_pin_attr(other, attr)
336
- end
337
-
338
- # @param other [self]
339
- # @param attr [::Symbol]
340
- #
341
- # @sg-ignore
342
- # @return [undefined]
343
- def choose_pin_attr(other, attr)
344
- # @type [Pin::Base, nil]
345
- val1 = send(attr)
346
- # @type [Pin::Base, nil]
347
- val2 = other.send(attr)
348
- if val1.class != val2.class
349
- # :nocov:
350
- Solargraph.assert_or_log("combine_with_#{attr}_class".to_sym,
351
- "Inconsistent #{attr.inspect} class values between \nself =#{inspect} and \nother=#{other.inspect}:\n\n self.#{attr} = #{val1.inspect}\nother.#{attr} = #{val2.inspect}")
352
- return val1
353
- # :nocov:
354
- end
355
- # arbitrary way of choosing a pin
356
- # @sg-ignore Need _1 support
357
- [val1, val2].compact.min_by { _1.best_location.to_s }
358
- end
359
-
360
- # @return [void]
361
- def assert_source_provided
362
- Solargraph.assert_or_log(:source, "source not provided - #{@path} #{@source} #{self.class}") if source.nil?
363
- end
364
-
365
- # @return [String]
366
- def comments
367
- @comments ||= ''
368
- end
369
-
370
- # @param generics_to_resolve [Enumerable<String>]
371
- # @param return_type_context [ComplexType, nil]
372
- # @param context [ComplexType]
373
- # @param resolved_generic_values [Hash{String => ComplexType}]
374
- # @return [self]
375
- def resolve_generics_from_context(generics_to_resolve, return_type_context = nil, resolved_generic_values: {})
376
- proxy return_type.resolve_generics_from_context(generics_to_resolve,
377
- return_type_context,
378
- resolved_generic_values: resolved_generic_values)
379
- end
380
-
381
- # @yieldparam [ComplexType]
382
- # @yieldreturn [ComplexType]
383
- # @return [self]
384
- def transform_types(&transform)
385
- proxy return_type.transform(&transform)
386
- end
387
-
388
- # Determine the concrete type for each of the generic type
389
- # parameters used in this method based on the parameters passed
390
- # into the its class and return a new method pin.
391
- #
392
- # @param definitions [Pin::Namespace] The module/class which uses generic types
393
- # @param context_type [ComplexType] The receiver type
394
- # @return [self]
395
- def resolve_generics definitions, context_type
396
- transform_types { |t| t.resolve_generics(definitions, context_type) if t }
397
- end
398
-
399
- def all_rooted?
400
- !return_type || return_type.all_rooted?
401
- end
402
-
403
- # @param generics_to_erase [::Array<String>]
404
- # @return [self]
405
- def erase_generics(generics_to_erase)
406
- return self if generics_to_erase.empty?
407
- transform_types { |t| t.erase_generics(generics_to_erase) }
408
- end
409
-
410
- # @return [String, nil]
411
- def filename
412
- return nil if location.nil?
413
- location.filename
414
- end
415
-
416
- # @return [Integer]
417
- def completion_item_kind
418
- LanguageServer::CompletionItemKinds::KEYWORD
419
- end
420
-
421
- # @return [Integer, nil]
422
- def symbol_kind
423
- nil
424
- end
425
-
426
- def to_s
427
- desc
428
- end
429
-
430
- # @return [Boolean]
431
- def variable?
432
- false
433
- end
434
-
435
- # @return [Location, nil]
436
- def best_location
437
- location || type_location
438
- end
439
-
440
- # True if the specified pin is a near match to this one. A near match
441
- # indicates that the pins contain mostly the same data. Any differences
442
- # between them should not have an impact on the API surface.
443
- #
444
- # @param other [Solargraph::Pin::Base, Object]
445
- # @return [Boolean]
446
- def nearly? other
447
- self.class == other.class &&
448
- name == other.name &&
449
- (closure == other.closure || (closure && closure.nearly?(other.closure))) &&
450
- (comments == other.comments ||
451
- (((maybe_directives? == false && other.maybe_directives? == false) || compare_directives(directives, other.directives)) &&
452
- compare_docstring_tags(docstring, other.docstring))
453
- )
454
- end
455
-
456
- # Pin equality is determined using the #nearly? method and also
457
- # requiring both pins to have the same location.
458
- #
459
- # @param other [self]
460
- def == other
461
- return false unless nearly? other
462
- comments == other.comments && location == other.location
463
- end
464
-
465
- # The pin's return type.
466
- #
467
- # @return [ComplexType]
468
- def return_type
469
- @return_type ||= ComplexType::UNDEFINED
470
- end
471
-
472
- # @return [YARD::Docstring]
473
- def docstring
474
- parse_comments unless @docstring
475
- @docstring ||= Solargraph::Source.parse_docstring('').to_docstring
476
- end
477
-
478
- # @return [::Array<YARD::Tags::Directive>]
479
- def directives
480
- parse_comments unless @directives
481
- @directives
482
- end
483
-
484
- # @return [::Array<YARD::Tags::MacroDirective>]
485
- def macros
486
- @macros ||= collect_macros
487
- end
488
-
489
- # Perform a quick check to see if this pin possibly includes YARD
490
- # directives. This method does not require parsing the comments.
491
- #
492
- # After the comments have been parsed, this method will return false if
493
- # no directives were found, regardless of whether it previously appeared
494
- # possible.
495
- #
496
- # @return [Boolean]
497
- def maybe_directives?
498
- return !@directives.empty? if defined?(@directives) && @directives
499
- @maybe_directives ||= comments.include?('@!')
500
- end
501
-
502
- # @return [Boolean]
503
- def deprecated?
504
- @deprecated ||= docstring.has_tag?('deprecated')
505
- end
506
-
507
- # Get a fully qualified type from the pin's return type.
508
- #
509
- # The relative type is determined from YARD documentation (@return,
510
- # @param, @type, etc.) and its namespaces are fully qualified using the
511
- # provided ApiMap.
512
- #
513
- # @param api_map [ApiMap]
514
- # @return [ComplexType]
515
- def typify api_map
516
- return_type.qualify(api_map, namespace)
517
- end
518
-
519
- # Infer the pin's return type via static code analysis.
520
- #
521
- # @param api_map [ApiMap]
522
- # @return [ComplexType]
523
- def probe api_map
524
- typify api_map
525
- end
526
-
527
- # @deprecated Use #typify and/or #probe instead
528
- # @param api_map [ApiMap]
529
- # @return [ComplexType]
530
- def infer api_map
531
- Solargraph::Logging.logger.warn "WARNING: Pin #infer methods are deprecated. Use #typify or #probe instead."
532
- type = typify(api_map)
533
- return type unless type.undefined?
534
- probe api_map
535
- end
536
-
537
- def proxied?
538
- @proxied ||= false
539
- end
540
-
541
- def probed?
542
- @probed ||= false
543
- end
544
-
545
- # @param api_map [ApiMap]
546
- # @return [self]
547
- def realize api_map
548
- return self if return_type.defined?
549
- type = typify(api_map)
550
- return proxy(type) if type.defined?
551
- type = probe(api_map)
552
- return self if type.undefined?
553
- result = proxy(type)
554
- result.probed = true
555
- result
556
- end
557
-
558
- # Return a proxy for this pin with the specified return type. Other than
559
- # the return type and the #proxied? setting, the proxy should be a clone
560
- # of the original.
561
- #
562
- # @param return_type [ComplexType]
563
- # @return [self]
564
- def proxy return_type
565
- result = dup
566
- result.return_type = return_type
567
- result.proxied = true
568
- result
569
- end
570
-
571
- # @deprecated
572
- # @return [String]
573
- def identity
574
- @identity ||= "#{closure&.path}|#{name}|#{location}"
575
- end
576
-
577
- # @return [String, nil]
578
- def to_rbs
579
- return_type.to_rbs
580
- end
581
-
582
- # @return [String]
583
- def type_desc
584
- rbs = to_rbs
585
- # RBS doesn't have a way to represent a Class<x> type
586
- rbs = return_type.rooted_tags if return_type.name == 'Class'
587
- if path
588
- if rbs
589
- path + ' ' + rbs
590
- else
591
- path
592
- end
593
- else
594
- rbs
595
- end
596
- end
597
-
598
- # @return [String]
599
- def inner_desc
600
- closure_info = closure&.desc
601
- binder_info = binder&.desc
602
- "name=#{name.inspect} return_type=#{type_desc}, context=#{context.rooted_tags}, closure=#{closure_info}, binder=#{binder_info}"
603
- end
604
-
605
- # @return [String]
606
- def desc
607
- "[#{inner_desc}]"
608
- end
609
-
610
- # @return [String]
611
- def inspect
612
- "#<#{self.class} `#{self.inner_desc}`#{all_location_text} via #{source.inspect}>"
613
- end
614
-
615
- # @return [String]
616
- def all_location_text
617
- if location.nil? && type_location.nil?
618
- ''
619
- elsif !location.nil? && type_location.nil?
620
- " at #{location.inspect})"
621
- elsif !type_location.nil? && location.nil?
622
- " at #{type_location.inspect})"
623
- else
624
- " at (#{location.inspect} and #{type_location.inspect})"
625
- end
626
- end
627
-
628
- # @return [void]
629
- def reset_generated!
630
- end
631
-
632
- protected
633
-
634
- # @return [Boolean]
635
- attr_writer :probed
636
-
637
- # @return [Boolean]
638
- attr_writer :proxied
639
-
640
- # @return [ComplexType]
641
- attr_writer :return_type
642
-
643
- attr_writer :docstring
644
-
645
- attr_writer :directives
646
-
647
- private
648
-
649
- # @return [void]
650
- def parse_comments
651
- # HACK: Avoid a NoMethodError on nil with empty overload tags
652
- if comments.nil? || comments.empty? || comments.strip.end_with?('@overload')
653
- @docstring = nil
654
- @directives = []
655
- else
656
- # HACK: Pass a dummy code object to the parser for plugins that
657
- # expect it not to be nil
658
- parse = Solargraph::Source.parse_docstring(comments)
659
- @docstring = parse.to_docstring
660
- @directives = parse.directives
661
- end
662
- end
663
-
664
- # True if two docstrings have the same tags, regardless of any other
665
- # differences.
666
- #
667
- # @param d1 [YARD::Docstring]
668
- # @param d2 [YARD::Docstring]
669
- # @return [Boolean]
670
- def compare_docstring_tags d1, d2
671
- return false if d1.tags.length != d2.tags.length
672
- d1.tags.each_index do |i|
673
- return false unless compare_tags(d1.tags[i], d2.tags[i])
674
- end
675
- true
676
- end
677
-
678
- # @param dir1 [::Array<YARD::Tags::Directive>]
679
- # @param dir2 [::Array<YARD::Tags::Directive>]
680
- # @return [Boolean]
681
- def compare_directives dir1, dir2
682
- return false if dir1.length != dir2.length
683
- dir1.each_index do |i|
684
- return false unless compare_tags(dir1[i].tag, dir2[i].tag)
685
- end
686
- true
687
- end
688
-
689
- # @param tag1 [YARD::Tags::Tag]
690
- # @param tag2 [YARD::Tags::Tag]
691
- # @return [Boolean]
692
- def compare_tags tag1, tag2
693
- tag1.class == tag2.class &&
694
- tag1.tag_name == tag2.tag_name &&
695
- tag1.text == tag2.text &&
696
- tag1.name == tag2.name &&
697
- tag1.types == tag2.types
698
- end
699
-
700
- # @return [::Array<YARD::Tags::Handlers::Directive>]
701
- def collect_macros
702
- return [] unless maybe_directives?
703
- parse = Solargraph::Source.parse_docstring(comments)
704
- parse.directives.select{ |d| d.tag.tag_name == 'macro' }
705
- end
706
- end
707
- end
708
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Solargraph
4
+ module Pin
5
+ # The base class for map pins.
6
+ #
7
+ class Base
8
+ include Common
9
+ include Conversions
10
+ include Documenting
11
+ include Logging
12
+
13
+ # @return [YARD::CodeObjects::Base]
14
+ attr_reader :code_object
15
+
16
+ # @return [Solargraph::Location, nil]
17
+ attr_reader :location
18
+
19
+ # @return [Solargraph::Location, nil]
20
+ attr_reader :type_location
21
+
22
+ # @return [String]
23
+ attr_reader :name
24
+
25
+ # @return [String]
26
+ attr_reader :path
27
+
28
+ # @return [::Symbol]
29
+ attr_accessor :source
30
+
31
+ # @type [::Numeric, nil] A priority for determining if pins should be combined or not
32
+ # A nil priority is considered the be the lowest. All code, yard & rbs pins have nil priority
33
+ # Between 2 pins, the one with the higher priority gets chosen. If the priorities are equal, they are combined.
34
+ attr_reader :combine_priority
35
+
36
+ def presence_certain?
37
+ true
38
+ end
39
+
40
+ # @param location [Solargraph::Location, nil]
41
+ # @param type_location [Solargraph::Location, nil]
42
+ # @param closure [Solargraph::Pin::Closure, nil]
43
+ # @param name [String]
44
+ # @param comments [String]
45
+ # @param source [Symbol, nil]
46
+ # @param docstring [YARD::Docstring, nil]
47
+ # @param directives [::Array<YARD::Tags::Directive>, nil]
48
+ # @param combine_priority [::Numeric, nil] See attr_reader for combine_priority
49
+ def initialize location: nil, type_location: nil, closure: nil, source: nil, name: '', comments: '', docstring: nil, directives: nil, combine_priority: nil
50
+ @location = location
51
+ @type_location = type_location
52
+ @closure = closure
53
+ @name = name
54
+ @comments = comments
55
+ @source = source
56
+ @identity = nil
57
+ @docstring = docstring
58
+ @directives = directives
59
+ @combine_priority = combine_priority
60
+
61
+ assert_source_provided
62
+ assert_location_provided
63
+ end
64
+
65
+ # @return [void]
66
+ def assert_location_provided
67
+ return unless best_location.nil? && %i[yardoc source rbs].include?(source)
68
+
69
+ Solargraph.assert_or_log(:best_location, "Neither location nor type_location provided - #{path} #{source} #{self.class}")
70
+ end
71
+
72
+ # @return [Pin::Closure, nil]
73
+ def closure
74
+ Solargraph.assert_or_log(:closure, "Closure not set on #{self.class} #{name.inspect} from #{source.inspect}") unless @closure
75
+ # @type [Pin::Closure, nil]
76
+ @closure
77
+ end
78
+
79
+ # @param other [self]
80
+ # @param attrs [Hash{::Symbol => Object}]
81
+ #
82
+ # @return [self]
83
+ def combine_with(other, attrs={})
84
+ raise "tried to combine #{other.class} with #{self.class}" unless other.class == self.class
85
+ priority_choice = choose_priority(other)
86
+ return priority_choice unless priority_choice.nil?
87
+
88
+ type_location = choose(other, :type_location)
89
+ location = choose(other, :location)
90
+ combined_name = combine_name(other)
91
+ new_attrs = {
92
+ location: location,
93
+ type_location: type_location,
94
+ name: combined_name,
95
+ closure: choose_pin_attr_with_same_name(other, :closure),
96
+ comments: choose_longer(other, :comments),
97
+ source: :combined,
98
+ docstring: choose(other, :docstring),
99
+ directives: combine_directives(other),
100
+ combine_priority: combine_priority
101
+ }.merge(attrs)
102
+ assert_same_macros(other)
103
+ logger.debug { "Base#combine_with(path=#{path}) - other.comments=#{other.comments.inspect}, self.comments = #{self.comments}" }
104
+ out = self.class.new(**new_attrs)
105
+ out.reset_generated!
106
+ out
107
+ end
108
+
109
+ # @param other [self]
110
+ # @return [self, nil] Returns either the pin chosen based on priority or nil
111
+ # A nil return means that the combination process must proceed
112
+ def choose_priority(other)
113
+ if combine_priority.nil? && !other.combine_priority.nil?
114
+ return other
115
+ elsif other.combine_priority.nil? && !combine_priority.nil?
116
+ return self
117
+ elsif !combine_priority.nil? && !other.combine_priority.nil?
118
+ if combine_priority > other.combine_priority
119
+ return self
120
+ elsif combine_priority < other.combine_priority
121
+ return other
122
+ end
123
+ end
124
+
125
+ nil
126
+ end
127
+
128
+ # @param other [self]
129
+ # @param attr [::Symbol]
130
+ # @sg-ignore
131
+ # @return [undefined]
132
+ def choose_longer(other, attr)
133
+ # @type [undefined]
134
+ val1 = send(attr)
135
+ # @type [undefined]
136
+ val2 = other.send(attr)
137
+ return val1 if val1 == val2
138
+ return val2 if val1.nil?
139
+ val1.length > val2.length ? val1 : val2
140
+ end
141
+
142
+ # @param other [self]
143
+ # @return [::Array<YARD::Tags::Directive>, nil]
144
+ def combine_directives(other)
145
+ return self.directives if other.directives.empty?
146
+ return other.directives if directives.empty?
147
+ [directives + other.directives].uniq
148
+ end
149
+
150
+ # @param other [self]
151
+ # @return [String]
152
+ def combine_name(other)
153
+ if needs_consistent_name? || other.needs_consistent_name?
154
+ assert_same(other, :name)
155
+ else
156
+ choose(other, :name)
157
+ end
158
+ end
159
+
160
+ # @return [void]
161
+ def reset_generated!
162
+ # @return_type doesn't go here as subclasses tend to assign it
163
+ # themselves in constructors, and they will deal with setting
164
+ # it in any methods that call this
165
+ #
166
+ # @docstring also doesn't go here, as there is code which
167
+ # directly manipulates docstring without editing comments
168
+ # (e.g., Api::Map::Store#index processes overrides that way
169
+ #
170
+ # Same with @directives, @macros, @maybe_directives, which
171
+ # regenerate docstring
172
+ @deprecated = nil
173
+ reset_conversions
174
+ end
175
+
176
+ def needs_consistent_name?
177
+ true
178
+ end
179
+
180
+ # @sg-ignore def should infer as symbol - "Not enough arguments to Module#protected"
181
+ protected def equality_fields
182
+ [name, location, type_location, closure, source]
183
+ end
184
+
185
+ # @param other [self]
186
+ # @return [ComplexType]
187
+ def combine_return_type(other)
188
+ if return_type.undefined?
189
+ other.return_type
190
+ elsif other.return_type.undefined?
191
+ return_type
192
+ elsif dodgy_return_type_source? && !other.dodgy_return_type_source?
193
+ other.return_type
194
+ elsif other.dodgy_return_type_source? && !dodgy_return_type_source?
195
+ return_type
196
+ else
197
+ all_items = return_type.items + other.return_type.items
198
+ if all_items.any? { |item| item.selfy? } && all_items.any? { |item| item.rooted_tag == context.reduce_class_type.rooted_tag }
199
+ # assume this was a declaration that should have said 'self'
200
+ all_items.delete_if { |item| item.rooted_tag == context.reduce_class_type.rooted_tag }
201
+ end
202
+ ComplexType.new(all_items)
203
+ end
204
+ end
205
+
206
+ def dodgy_return_type_source?
207
+ # uses a lot of 'Object' instead of 'self'
208
+ location&.filename&.include?('core_ext/object/')
209
+ end
210
+
211
+ # when choices are arbitrary, make sure the choice is consistent
212
+ #
213
+ # @param other [Pin::Base]
214
+ # @param attr [::Symbol]
215
+ #
216
+ # @return [Object, nil]
217
+ def choose(other, attr)
218
+ results = [self, other].map(&attr).compact
219
+ # true and false are different classes and can't be sorted
220
+ return true if results.any? { |r| r == true || r == false }
221
+ results.min
222
+ rescue
223
+ STDERR.puts("Problem handling #{attr} for \n#{self.inspect}\n and \n#{other.inspect}\n\n#{self.send(attr).inspect} vs #{other.send(attr).inspect}")
224
+ raise
225
+ end
226
+
227
+ # @param other [self]
228
+ # @param attr [::Symbol]
229
+ # @sg-ignore
230
+ # @return [undefined]
231
+ def choose_node(other, attr)
232
+ if other.object_id < attr.object_id
233
+ other.send(attr)
234
+ else
235
+ send(attr)
236
+ end
237
+ end
238
+
239
+ # @param other [self]
240
+ # @param attr [::Symbol]
241
+ # @sg-ignore
242
+ # @return [undefined]
243
+ def prefer_rbs_location(other, attr)
244
+ if rbs_location? && !other.rbs_location?
245
+ self.send(attr)
246
+ elsif !rbs_location? && other.rbs_location?
247
+ other.send(attr)
248
+ else
249
+ choose(other, attr)
250
+ end
251
+ end
252
+
253
+ def rbs_location?
254
+ type_location&.rbs?
255
+ end
256
+
257
+ # @param other [self]
258
+ # @return [void]
259
+ def assert_same_macros(other)
260
+ return unless self.source == :yardoc && other.source == :yardoc
261
+ assert_same_count(other, :macros)
262
+ # @param [YARD::Tags::MacroDirective]
263
+ assert_same_array_content(other, :macros) { |macro| macro.tag.name }
264
+ end
265
+
266
+ # @param other [self]
267
+ # @param attr [::Symbol]
268
+ # @return [void]
269
+ # @todo strong typechecking should complain when there are no block-related tags
270
+ def assert_same_array_content(other, attr, &block)
271
+ arr1 = send(attr)
272
+ raise "Expected #{attr} on #{self} to be an Enumerable, got #{arr1.class}" unless arr1.is_a?(::Enumerable)
273
+ # @type arr1 [::Enumerable]
274
+ arr2 = other.send(attr)
275
+ raise "Expected #{attr} on #{other} to be an Enumerable, got #{arr2.class}" unless arr2.is_a?(::Enumerable)
276
+ # @type arr2 [::Enumerable]
277
+
278
+ # @type [undefined]
279
+ values1 = arr1.map(&block)
280
+ # @type [undefined]
281
+ values2 = arr2.map(&block)
282
+ # @sg-ignore
283
+ return arr1 if values1 == values2
284
+ Solargraph.assert_or_log("combine_with_#{attr}".to_sym,
285
+ "Inconsistent #{attr.inspect} values between \nself =#{inspect} and \nother=#{other.inspect}:\n\n self values = #{values1}\nother values =#{attr} = #{values2}")
286
+ arr1
287
+ end
288
+
289
+ # @param other [self]
290
+ # @param attr [::Symbol]
291
+ #
292
+ # @return [::Enumerable]
293
+ def assert_same_count(other, attr)
294
+ # @type [::Enumerable]
295
+ arr1 = self.send(attr)
296
+ raise "Expected #{attr} on #{self} to be an Enumerable, got #{arr1.class}" unless arr1.is_a?(::Enumerable)
297
+ # @type [::Enumerable]
298
+ arr2 = other.send(attr)
299
+ raise "Expected #{attr} on #{other} to be an Enumerable, got #{arr2.class}" unless arr2.is_a?(::Enumerable)
300
+ return arr1 if arr1.count == arr2.count
301
+ Solargraph.assert_or_log("combine_with_#{attr}".to_sym,
302
+ "Inconsistent #{attr.inspect} count value between \nself =#{inspect} and \nother=#{other.inspect}:\n\n self.#{attr} = #{arr1.inspect}\nother.#{attr} = #{arr2.inspect}")
303
+ arr1
304
+ end
305
+
306
+ # @param other [self]
307
+ # @param attr [::Symbol]
308
+ #
309
+ # @sg-ignore
310
+ # @return [undefined]
311
+ def assert_same(other, attr)
312
+ return false if other.nil?
313
+ val1 = send(attr)
314
+ val2 = other.send(attr)
315
+ return val1 if val1 == val2
316
+ Solargraph.assert_or_log("combine_with_#{attr}".to_sym,
317
+ "Inconsistent #{attr.inspect} values between \nself =#{inspect} and \nother=#{other.inspect}:\n\n self.#{attr} = #{val1.inspect}\nother.#{attr} = #{val2.inspect}")
318
+ val1
319
+ end
320
+
321
+ # @param other [self]
322
+ # @param attr [::Symbol]
323
+ # @sg-ignore
324
+ # @return [undefined]
325
+ def choose_pin_attr_with_same_name(other, attr)
326
+ # @type [Pin::Base, nil]
327
+ val1 = send(attr)
328
+ # @type [Pin::Base, nil]
329
+ val2 = other.send(attr)
330
+ raise "Expected pin for #{attr} on\n#{self.inspect},\ngot #{val1.inspect}" unless val1.nil? || val1.is_a?(Pin::Base)
331
+ raise "Expected pin for #{attr} on\n#{other.inspect},\ngot #{val2.inspect}" unless val2.nil? || val2.is_a?(Pin::Base)
332
+ if val1&.name != val2&.name
333
+ Solargraph.assert_or_log("combine_with_#{attr}_name".to_sym,
334
+ "Inconsistent #{attr.inspect} name values between \nself =#{inspect} and \nother=#{other.inspect}:\n\n self.#{attr} = #{val1.inspect}\nother.#{attr} = #{val2.inspect}")
335
+ end
336
+ choose_pin_attr(other, attr)
337
+ end
338
+
339
+ # @param other [self]
340
+ # @param attr [::Symbol]
341
+ #
342
+ # @sg-ignore Missing @return tag for Solargraph::Pin::Base#choose_pin_attr
343
+ # @return [undefined]
344
+ def choose_pin_attr(other, attr)
345
+ # @type [Pin::Base, nil]
346
+ val1 = send(attr)
347
+ # @type [Pin::Base, nil]
348
+ val2 = other.send(attr)
349
+ if val1.class != val2.class
350
+ # :nocov:
351
+ Solargraph.assert_or_log("combine_with_#{attr}_class".to_sym,
352
+ "Inconsistent #{attr.inspect} class values between \nself =#{inspect} and \nother=#{other.inspect}:\n\n self.#{attr} = #{val1.inspect}\nother.#{attr} = #{val2.inspect}")
353
+ return val1
354
+ # :nocov:
355
+ end
356
+ # arbitrary way of choosing a pin
357
+ [val1, val2].compact.max_by do |closure|
358
+ [
359
+ # maximize number of gates, as types in other combined pins may
360
+ # depend on those gates
361
+ closure.gates.length,
362
+ # use basename so that results don't vary system to system
363
+ File.basename(closure.best_location.to_s)
364
+ ]
365
+ end
366
+ end
367
+
368
+ # @return [void]
369
+ def assert_source_provided
370
+ Solargraph.assert_or_log(:source, "source not provided - #{@path} #{@source} #{self.class}") if source.nil?
371
+ end
372
+
373
+ # @return [String]
374
+ def comments
375
+ @comments ||= ''
376
+ end
377
+
378
+ # @param generics_to_resolve [Enumerable<String>]
379
+ # @param return_type_context [ComplexType, nil]
380
+ # @param context [ComplexType]
381
+ # @param resolved_generic_values [Hash{String => ComplexType}]
382
+ # @return [self]
383
+ def resolve_generics_from_context(generics_to_resolve, return_type_context = nil, resolved_generic_values: {})
384
+ proxy return_type.resolve_generics_from_context(generics_to_resolve,
385
+ return_type_context,
386
+ resolved_generic_values: resolved_generic_values)
387
+ end
388
+
389
+ # @yieldparam [ComplexType]
390
+ # @yieldreturn [ComplexType]
391
+ # @return [self]
392
+ def transform_types(&transform)
393
+ proxy return_type.transform(&transform)
394
+ end
395
+
396
+ # Determine the concrete type for each of the generic type
397
+ # parameters used in this method based on the parameters passed
398
+ # into the its class and return a new method pin.
399
+ #
400
+ # @param definitions [Pin::Namespace] The module/class which uses generic types
401
+ # @param context_type [ComplexType] The receiver type
402
+ # @return [self]
403
+ def resolve_generics definitions, context_type
404
+ transform_types { |t| t.resolve_generics(definitions, context_type) if t }
405
+ end
406
+
407
+ def all_rooted?
408
+ !return_type || return_type.all_rooted?
409
+ end
410
+
411
+ # @param generics_to_erase [::Array<String>]
412
+ # @return [self]
413
+ def erase_generics(generics_to_erase)
414
+ return self if generics_to_erase.empty?
415
+ transform_types { |t| t.erase_generics(generics_to_erase) }
416
+ end
417
+
418
+ # @return [String, nil]
419
+ def filename
420
+ return nil if location.nil?
421
+ location.filename
422
+ end
423
+
424
+ # @return [Integer]
425
+ def completion_item_kind
426
+ LanguageServer::CompletionItemKinds::KEYWORD
427
+ end
428
+
429
+ # @return [Integer, nil]
430
+ def symbol_kind
431
+ nil
432
+ end
433
+
434
+ def to_s
435
+ desc
436
+ end
437
+
438
+ # @return [Boolean]
439
+ def variable?
440
+ false
441
+ end
442
+
443
+ # @return [Location, nil]
444
+ def best_location
445
+ location || type_location
446
+ end
447
+
448
+ # True if the specified pin is a near match to this one. A near match
449
+ # indicates that the pins contain mostly the same data. Any differences
450
+ # between them should not have an impact on the API surface.
451
+ #
452
+ # @param other [Solargraph::Pin::Base, Object]
453
+ # @return [Boolean]
454
+ def nearly? other
455
+ self.class == other.class &&
456
+ name == other.name &&
457
+ (closure == other.closure || (closure && closure.nearly?(other.closure))) &&
458
+ (comments == other.comments ||
459
+ (((maybe_directives? == false && other.maybe_directives? == false) || compare_directives(directives, other.directives)) &&
460
+ compare_docstring_tags(docstring, other.docstring))
461
+ )
462
+ end
463
+
464
+ # Pin equality is determined using the #nearly? method and also
465
+ # requiring both pins to have the same location.
466
+ #
467
+ # @param other [Object]
468
+ def == other
469
+ return false unless nearly? other
470
+ # @sg-ignore Should add more explicit type check on other
471
+ comments == other.comments && location == other.location
472
+ end
473
+
474
+ # The pin's return type.
475
+ #
476
+ # @return [ComplexType]
477
+ def return_type
478
+ @return_type ||= ComplexType::UNDEFINED
479
+ end
480
+
481
+ # @return [YARD::Docstring]
482
+ def docstring
483
+ parse_comments unless @docstring
484
+ @docstring ||= Solargraph::Source.parse_docstring('').to_docstring
485
+ end
486
+
487
+ # @return [::Array<YARD::Tags::Directive>]
488
+ def directives
489
+ parse_comments unless @directives
490
+ @directives
491
+ end
492
+
493
+ # @return [::Array<YARD::Tags::MacroDirective>]
494
+ def macros
495
+ @macros ||= collect_macros
496
+ end
497
+
498
+ # Perform a quick check to see if this pin possibly includes YARD
499
+ # directives. This method does not require parsing the comments.
500
+ #
501
+ # After the comments have been parsed, this method will return false if
502
+ # no directives were found, regardless of whether it previously appeared
503
+ # possible.
504
+ #
505
+ # @return [Boolean]
506
+ def maybe_directives?
507
+ return !@directives.empty? if defined?(@directives) && @directives
508
+ @maybe_directives ||= comments.include?('@!')
509
+ end
510
+
511
+ # @return [Boolean]
512
+ def deprecated?
513
+ @deprecated ||= docstring.has_tag?('deprecated')
514
+ end
515
+
516
+ # Get a fully qualified type from the pin's return type.
517
+ #
518
+ # The relative type is determined from YARD documentation (@return,
519
+ # @param, @type, etc.) and its namespaces are fully qualified using the
520
+ # provided ApiMap.
521
+ #
522
+ # @param api_map [ApiMap]
523
+ # @return [ComplexType]
524
+ def typify api_map
525
+ return_type.qualify(api_map, *(closure&.gates || ['']))
526
+ end
527
+
528
+ # Infer the pin's return type via static code analysis.
529
+ #
530
+ # @param api_map [ApiMap]
531
+ # @return [ComplexType]
532
+ def probe api_map
533
+ typify api_map
534
+ end
535
+
536
+ # @deprecated Use #typify and/or #probe instead
537
+ # @param api_map [ApiMap]
538
+ # @return [ComplexType]
539
+ def infer api_map
540
+ Solargraph::Logging.logger.warn "WARNING: Pin #infer methods are deprecated. Use #typify or #probe instead."
541
+ type = typify(api_map)
542
+ return type unless type.undefined?
543
+ probe api_map
544
+ end
545
+
546
+ def proxied?
547
+ @proxied ||= false
548
+ end
549
+
550
+ def probed?
551
+ @probed ||= false
552
+ end
553
+
554
+ # @param api_map [ApiMap]
555
+ # @return [self]
556
+ def realize api_map
557
+ return self if return_type.defined?
558
+ type = typify(api_map)
559
+ return proxy(type) if type.defined?
560
+ type = probe(api_map)
561
+ return self if type.undefined?
562
+ result = proxy(type)
563
+ result.probed = true
564
+ result
565
+ end
566
+
567
+ # Return a proxy for this pin with the specified return type. Other than
568
+ # the return type and the #proxied? setting, the proxy should be a clone
569
+ # of the original.
570
+ #
571
+ # @param return_type [ComplexType]
572
+ # @return [self]
573
+ def proxy return_type
574
+ result = dup
575
+ result.return_type = return_type
576
+ result.proxied = true
577
+ result
578
+ end
579
+
580
+ # @deprecated
581
+ # @return [String]
582
+ def identity
583
+ @identity ||= "#{closure&.path}|#{name}|#{location}"
584
+ end
585
+
586
+ # The namespaces available for resolving the current namespace. Each gate
587
+ # should be a fully qualified namespace or the root namespace (i.e., an
588
+ # empty string.)
589
+ #
590
+ # Example: Given the name 'Bar' and the gates ['Foo', ''],
591
+ # the fully qualified namespace should be 'Foo::Bar' or 'Bar'.
592
+ #
593
+ # @return [Array<String>]
594
+ def gates
595
+ @gates ||= closure&.gates || ['']
596
+ end
597
+
598
+ # @return [String, nil]
599
+ def to_rbs
600
+ return_type.to_rbs
601
+ end
602
+
603
+ # @return [String, nil]
604
+ def type_desc
605
+ rbs = to_rbs
606
+ # RBS doesn't have a way to represent a Class<x> type
607
+ rbs = return_type.rooted_tags if return_type.name == 'Class'
608
+ if path
609
+ if rbs
610
+ path + ' ' + rbs
611
+ else
612
+ path
613
+ end
614
+ else
615
+ rbs
616
+ end
617
+ end
618
+
619
+ # @return [String]
620
+ def inner_desc
621
+ closure_info = closure&.desc
622
+ binder_info = binder&.desc
623
+ "name=#{name.inspect} return_type=#{type_desc}, context=#{context.rooted_tags}, closure=#{closure_info}, binder=#{binder_info}"
624
+ end
625
+
626
+ # @return [String]
627
+ def desc
628
+ "[#{inner_desc}]"
629
+ end
630
+
631
+ # @return [String]
632
+ def inspect
633
+ "#<#{self.class} `#{self.inner_desc}`#{all_location_text} via #{source.inspect}>"
634
+ end
635
+
636
+ # @return [String]
637
+ def all_location_text
638
+ if location.nil? && type_location.nil?
639
+ ''
640
+ elsif !location.nil? && type_location.nil?
641
+ " at #{location.inspect})"
642
+ elsif !type_location.nil? && location.nil?
643
+ " at #{type_location.inspect})"
644
+ else
645
+ " at (#{location.inspect} and #{type_location.inspect})"
646
+ end
647
+ end
648
+
649
+ # @return [void]
650
+ def reset_generated!
651
+ end
652
+
653
+ protected
654
+
655
+ # @return [Boolean]
656
+ attr_writer :probed
657
+
658
+ # @return [Boolean]
659
+ attr_writer :proxied
660
+
661
+ # @return [ComplexType]
662
+ attr_writer :return_type
663
+
664
+ attr_writer :docstring
665
+
666
+ attr_writer :directives
667
+
668
+ private
669
+
670
+ # @return [void]
671
+ def parse_comments
672
+ # HACK: Avoid a NoMethodError on nil with empty overload tags
673
+ if comments.nil? || comments.empty? || comments.strip.end_with?('@overload')
674
+ @docstring = nil
675
+ @directives = []
676
+ else
677
+ # HACK: Pass a dummy code object to the parser for plugins that
678
+ # expect it not to be nil
679
+ parse = Solargraph::Source.parse_docstring(comments)
680
+ @docstring = parse.to_docstring
681
+ @directives = parse.directives
682
+ end
683
+ end
684
+
685
+ # True if two docstrings have the same tags, regardless of any other
686
+ # differences.
687
+ #
688
+ # @param d1 [YARD::Docstring]
689
+ # @param d2 [YARD::Docstring]
690
+ # @return [Boolean]
691
+ def compare_docstring_tags d1, d2
692
+ return false if d1.tags.length != d2.tags.length
693
+ d1.tags.each_index do |i|
694
+ return false unless compare_tags(d1.tags[i], d2.tags[i])
695
+ end
696
+ true
697
+ end
698
+
699
+ # @param dir1 [::Array<YARD::Tags::Directive>]
700
+ # @param dir2 [::Array<YARD::Tags::Directive>]
701
+ # @return [Boolean]
702
+ def compare_directives dir1, dir2
703
+ return false if dir1.length != dir2.length
704
+ dir1.each_index do |i|
705
+ return false unless compare_tags(dir1[i].tag, dir2[i].tag)
706
+ end
707
+ true
708
+ end
709
+
710
+ # @param tag1 [YARD::Tags::Tag]
711
+ # @param tag2 [YARD::Tags::Tag]
712
+ # @return [Boolean]
713
+ def compare_tags tag1, tag2
714
+ tag1.class == tag2.class &&
715
+ tag1.tag_name == tag2.tag_name &&
716
+ tag1.text == tag2.text &&
717
+ tag1.name == tag2.name &&
718
+ tag1.types == tag2.types
719
+ end
720
+
721
+ # @return [::Array<YARD::Tags::Handlers::Directive>]
722
+ def collect_macros
723
+ return [] unless maybe_directives?
724
+ parse = Solargraph::Source.parse_docstring(comments)
725
+ parse.directives.select{ |d| d.tag.tag_name == 'macro' }
726
+ end
727
+ end
728
+ end
729
+ end