steep-activesupport-4 1.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.gitmodules +0 -0
  4. data/CHANGELOG.md +1032 -0
  5. data/LICENSE +21 -0
  6. data/README.md +260 -0
  7. data/Rakefile +227 -0
  8. data/Steepfile +68 -0
  9. data/bin/console +14 -0
  10. data/bin/generate-diagnostics-docs.rb +112 -0
  11. data/bin/mem_graph.rb +67 -0
  12. data/bin/mem_prof.rb +102 -0
  13. data/bin/output_rebaseline.rb +34 -0
  14. data/bin/output_test.rb +60 -0
  15. data/bin/rbs +20 -0
  16. data/bin/rbs-inline +19 -0
  17. data/bin/setup +9 -0
  18. data/bin/stackprof_test.rb +19 -0
  19. data/bin/steep +19 -0
  20. data/bin/steep-check.rb +251 -0
  21. data/bin/steep-prof +16 -0
  22. data/doc/narrowing.md +195 -0
  23. data/doc/shape.md +194 -0
  24. data/exe/steep +18 -0
  25. data/guides/README.md +5 -0
  26. data/guides/src/gem-rbs-collection/gem-rbs-collection.md +126 -0
  27. data/guides/src/getting-started/getting-started.md +163 -0
  28. data/guides/src/nil-optional/nil-optional.md +195 -0
  29. data/lib/steep/annotation_parser.rb +199 -0
  30. data/lib/steep/ast/annotation/collection.rb +172 -0
  31. data/lib/steep/ast/annotation.rb +137 -0
  32. data/lib/steep/ast/builtin.rb +104 -0
  33. data/lib/steep/ast/ignore.rb +148 -0
  34. data/lib/steep/ast/node/type_application.rb +88 -0
  35. data/lib/steep/ast/node/type_assertion.rb +81 -0
  36. data/lib/steep/ast/types/any.rb +35 -0
  37. data/lib/steep/ast/types/boolean.rb +45 -0
  38. data/lib/steep/ast/types/bot.rb +35 -0
  39. data/lib/steep/ast/types/class.rb +43 -0
  40. data/lib/steep/ast/types/factory.rb +557 -0
  41. data/lib/steep/ast/types/helper.rb +40 -0
  42. data/lib/steep/ast/types/instance.rb +42 -0
  43. data/lib/steep/ast/types/intersection.rb +93 -0
  44. data/lib/steep/ast/types/literal.rb +59 -0
  45. data/lib/steep/ast/types/logic.rb +84 -0
  46. data/lib/steep/ast/types/name.rb +128 -0
  47. data/lib/steep/ast/types/nil.rb +41 -0
  48. data/lib/steep/ast/types/proc.rb +117 -0
  49. data/lib/steep/ast/types/record.rb +79 -0
  50. data/lib/steep/ast/types/self.rb +43 -0
  51. data/lib/steep/ast/types/shared_instance.rb +11 -0
  52. data/lib/steep/ast/types/top.rb +35 -0
  53. data/lib/steep/ast/types/tuple.rb +60 -0
  54. data/lib/steep/ast/types/union.rb +97 -0
  55. data/lib/steep/ast/types/var.rb +65 -0
  56. data/lib/steep/ast/types/void.rb +35 -0
  57. data/lib/steep/cli.rb +401 -0
  58. data/lib/steep/diagnostic/deprecated/else_on_exhaustive_case.rb +20 -0
  59. data/lib/steep/diagnostic/deprecated/unknown_constant_assigned.rb +28 -0
  60. data/lib/steep/diagnostic/helper.rb +18 -0
  61. data/lib/steep/diagnostic/lsp_formatter.rb +78 -0
  62. data/lib/steep/diagnostic/result_printer2.rb +48 -0
  63. data/lib/steep/diagnostic/ruby.rb +1221 -0
  64. data/lib/steep/diagnostic/signature.rb +570 -0
  65. data/lib/steep/drivers/annotations.rb +52 -0
  66. data/lib/steep/drivers/check.rb +339 -0
  67. data/lib/steep/drivers/checkfile.rb +210 -0
  68. data/lib/steep/drivers/diagnostic_printer.rb +105 -0
  69. data/lib/steep/drivers/init.rb +66 -0
  70. data/lib/steep/drivers/langserver.rb +56 -0
  71. data/lib/steep/drivers/print_project.rb +113 -0
  72. data/lib/steep/drivers/stats.rb +203 -0
  73. data/lib/steep/drivers/utils/driver_helper.rb +143 -0
  74. data/lib/steep/drivers/utils/jobs_option.rb +26 -0
  75. data/lib/steep/drivers/vendor.rb +27 -0
  76. data/lib/steep/drivers/watch.rb +194 -0
  77. data/lib/steep/drivers/worker.rb +58 -0
  78. data/lib/steep/equatable.rb +23 -0
  79. data/lib/steep/expectations.rb +228 -0
  80. data/lib/steep/index/rbs_index.rb +350 -0
  81. data/lib/steep/index/signature_symbol_provider.rb +185 -0
  82. data/lib/steep/index/source_index.rb +167 -0
  83. data/lib/steep/interface/block.rb +103 -0
  84. data/lib/steep/interface/builder.rb +843 -0
  85. data/lib/steep/interface/function.rb +1090 -0
  86. data/lib/steep/interface/method_type.rb +330 -0
  87. data/lib/steep/interface/shape.rb +239 -0
  88. data/lib/steep/interface/substitution.rb +159 -0
  89. data/lib/steep/interface/type_param.rb +115 -0
  90. data/lib/steep/located_value.rb +20 -0
  91. data/lib/steep/method_name.rb +42 -0
  92. data/lib/steep/module_helper.rb +24 -0
  93. data/lib/steep/node_helper.rb +273 -0
  94. data/lib/steep/path_helper.rb +30 -0
  95. data/lib/steep/project/dsl.rb +268 -0
  96. data/lib/steep/project/group.rb +31 -0
  97. data/lib/steep/project/options.rb +63 -0
  98. data/lib/steep/project/pattern.rb +59 -0
  99. data/lib/steep/project/target.rb +92 -0
  100. data/lib/steep/project.rb +78 -0
  101. data/lib/steep/rake_task.rb +132 -0
  102. data/lib/steep/range_extension.rb +29 -0
  103. data/lib/steep/server/base_worker.rb +97 -0
  104. data/lib/steep/server/change_buffer.rb +73 -0
  105. data/lib/steep/server/custom_methods.rb +77 -0
  106. data/lib/steep/server/delay_queue.rb +45 -0
  107. data/lib/steep/server/interaction_worker.rb +492 -0
  108. data/lib/steep/server/lsp_formatter.rb +455 -0
  109. data/lib/steep/server/master.rb +912 -0
  110. data/lib/steep/server/target_group_files.rb +205 -0
  111. data/lib/steep/server/type_check_controller.rb +366 -0
  112. data/lib/steep/server/type_check_worker.rb +303 -0
  113. data/lib/steep/server/work_done_progress.rb +64 -0
  114. data/lib/steep/server/worker_process.rb +176 -0
  115. data/lib/steep/services/completion_provider.rb +802 -0
  116. data/lib/steep/services/content_change.rb +61 -0
  117. data/lib/steep/services/file_loader.rb +74 -0
  118. data/lib/steep/services/goto_service.rb +441 -0
  119. data/lib/steep/services/hover_provider/rbs.rb +88 -0
  120. data/lib/steep/services/hover_provider/ruby.rb +221 -0
  121. data/lib/steep/services/hover_provider/singleton_methods.rb +20 -0
  122. data/lib/steep/services/path_assignment.rb +46 -0
  123. data/lib/steep/services/signature_help_provider.rb +202 -0
  124. data/lib/steep/services/signature_service.rb +428 -0
  125. data/lib/steep/services/stats_calculator.rb +68 -0
  126. data/lib/steep/services/type_check_service.rb +394 -0
  127. data/lib/steep/services/type_name_completion.rb +236 -0
  128. data/lib/steep/signature/validator.rb +651 -0
  129. data/lib/steep/source/ignore_ranges.rb +69 -0
  130. data/lib/steep/source.rb +691 -0
  131. data/lib/steep/subtyping/cache.rb +30 -0
  132. data/lib/steep/subtyping/check.rb +1113 -0
  133. data/lib/steep/subtyping/constraints.rb +341 -0
  134. data/lib/steep/subtyping/relation.rb +101 -0
  135. data/lib/steep/subtyping/result.rb +324 -0
  136. data/lib/steep/subtyping/variable_variance.rb +89 -0
  137. data/lib/steep/test.rb +9 -0
  138. data/lib/steep/thread_waiter.rb +43 -0
  139. data/lib/steep/type_construction.rb +5183 -0
  140. data/lib/steep/type_inference/block_params.rb +416 -0
  141. data/lib/steep/type_inference/case_when.rb +303 -0
  142. data/lib/steep/type_inference/constant_env.rb +56 -0
  143. data/lib/steep/type_inference/context.rb +195 -0
  144. data/lib/steep/type_inference/logic_type_interpreter.rb +613 -0
  145. data/lib/steep/type_inference/method_call.rb +193 -0
  146. data/lib/steep/type_inference/method_params.rb +531 -0
  147. data/lib/steep/type_inference/multiple_assignment.rb +194 -0
  148. data/lib/steep/type_inference/send_args.rb +712 -0
  149. data/lib/steep/type_inference/type_env.rb +341 -0
  150. data/lib/steep/type_inference/type_env_builder.rb +138 -0
  151. data/lib/steep/typing.rb +321 -0
  152. data/lib/steep/version.rb +3 -0
  153. data/lib/steep.rb +369 -0
  154. data/manual/annotations.md +181 -0
  155. data/manual/ignore.md +20 -0
  156. data/manual/ruby-diagnostics.md +1879 -0
  157. data/sample/Steepfile +22 -0
  158. data/sample/lib/conference.rb +49 -0
  159. data/sample/lib/length.rb +35 -0
  160. data/sample/sig/conference.rbs +42 -0
  161. data/sample/sig/generics.rbs +15 -0
  162. data/sample/sig/length.rbs +34 -0
  163. data/steep-activesupport-4.gemspec +55 -0
  164. metadata +437 -0
@@ -0,0 +1,350 @@
1
+ module Steep
2
+ module Index
3
+ class RBSIndex
4
+ class TypeEntry
5
+ attr_reader :type_name
6
+ attr_reader :declarations
7
+ attr_reader :references
8
+
9
+ def initialize(type_name:)
10
+ @type_name = type_name
11
+ @declarations = Set[]
12
+ @references = Set[]
13
+ end
14
+
15
+ def add_declaration(decl)
16
+ case decl
17
+ when RBS::AST::Declarations::Class, RBS::AST::Declarations::Module
18
+ declarations << decl
19
+ when RBS::AST::Declarations::Interface
20
+ declarations << decl
21
+ when RBS::AST::Declarations::TypeAlias
22
+ declarations << decl
23
+ when RBS::AST::Declarations::ClassAlias, RBS::AST::Declarations::ModuleAlias
24
+ declarations << decl
25
+ else
26
+ raise "Unexpected type declaration: #{decl}"
27
+ end
28
+
29
+ self
30
+ end
31
+
32
+ def add_reference(ref)
33
+ case ref
34
+ when RBS::AST::Members::MethodDefinition
35
+ references << ref
36
+ when RBS::AST::Members::AttrAccessor, RBS::AST::Members::AttrReader, RBS::AST::Members::AttrWriter
37
+ references << ref
38
+ when RBS::AST::Members::InstanceVariable, RBS::AST::Members::ClassInstanceVariable, RBS::AST::Members::ClassVariable
39
+ references << ref
40
+ when RBS::AST::Members::Include, RBS::AST::Members::Extend
41
+ references << ref
42
+ when RBS::AST::Declarations::Module, RBS::AST::Declarations::Class
43
+ references << ref
44
+ when RBS::AST::Declarations::Constant, RBS::AST::Declarations::Global
45
+ references << ref
46
+ when RBS::AST::Declarations::TypeAlias
47
+ references << ref
48
+ when RBS::AST::Declarations::ClassAlias, RBS::AST::Declarations::ModuleAlias
49
+ references << ref
50
+ else
51
+ raise "Unexpected type reference: #{ref}"
52
+ end
53
+
54
+ self
55
+ end
56
+ end
57
+
58
+ class MethodEntry
59
+ attr_reader :method_name
60
+ attr_reader :declarations
61
+ attr_reader :references
62
+
63
+ def initialize(method_name:)
64
+ @method_name = method_name
65
+ @declarations = Set[]
66
+ @references = Set[]
67
+ end
68
+
69
+ def add_declaration(decl)
70
+ case decl
71
+ when RBS::AST::Members::MethodDefinition,
72
+ RBS::AST::Members::Alias,
73
+ RBS::AST::Members::AttrWriter,
74
+ RBS::AST::Members::AttrReader,
75
+ RBS::AST::Members::AttrAccessor
76
+ declarations << decl
77
+ else
78
+ raise "Unexpected method declaration: #{decl}"
79
+ end
80
+
81
+ self
82
+ end
83
+ end
84
+
85
+ class ConstantEntry
86
+ attr_reader :const_name
87
+ attr_reader :declarations
88
+
89
+ def initialize(const_name:)
90
+ @const_name = const_name
91
+ @declarations = Set[]
92
+ end
93
+
94
+ def add_declaration(decl)
95
+ case decl
96
+ when RBS::AST::Declarations::Constant
97
+ declarations << decl
98
+ else
99
+ raise
100
+ end
101
+
102
+ self
103
+ end
104
+ end
105
+
106
+ class GlobalEntry
107
+ attr_reader :global_name
108
+ attr_reader :declarations
109
+
110
+ def initialize(global_name:)
111
+ @global_name = global_name
112
+ @declarations = Set[]
113
+ end
114
+
115
+ def add_declaration(decl)
116
+ case decl
117
+ when RBS::AST::Declarations::Global
118
+ declarations << decl
119
+ else
120
+ raise
121
+ end
122
+
123
+ self
124
+ end
125
+ end
126
+
127
+ attr_reader :type_index
128
+ attr_reader :method_index
129
+ attr_reader :const_index
130
+ attr_reader :global_index
131
+
132
+ def initialize()
133
+ @type_index = {}
134
+ @method_index = {}
135
+ @const_index = {}
136
+ @global_index = {}
137
+ end
138
+
139
+ def entry(type_name: nil, method_name: nil, const_name: nil, global_name: nil)
140
+ case
141
+ when type_name
142
+ type_index[type_name] ||= TypeEntry.new(type_name: type_name)
143
+ when method_name
144
+ method_index[method_name] ||= MethodEntry.new(method_name: method_name)
145
+ when const_name
146
+ const_index[const_name] ||= ConstantEntry.new(const_name: const_name)
147
+ when global_name
148
+ global_index[global_name] ||= GlobalEntry.new(global_name: global_name)
149
+ else
150
+ raise
151
+ end
152
+ end
153
+
154
+ def each_entry(&block)
155
+ if block
156
+ type_index.each_value(&block)
157
+ method_index.each_value(&block)
158
+ const_index.each_value(&block)
159
+ global_index.each_value(&block)
160
+ else
161
+ enum_for(:each_entry)
162
+ end
163
+ end
164
+
165
+ def add_type_declaration(type_name, declaration)
166
+ entry(type_name: type_name).add_declaration(declaration)
167
+ end
168
+
169
+ def add_method_declaration(method_name, member)
170
+ entry(method_name: method_name).add_declaration(member)
171
+ end
172
+
173
+ def add_constant_declaration(const_name, decl)
174
+ entry(const_name: const_name).add_declaration(decl)
175
+ end
176
+
177
+ def add_global_declaration(global_name, decl)
178
+ entry(global_name: global_name).add_declaration(decl)
179
+ end
180
+
181
+ def each_declaration(type_name: nil, method_name: nil, const_name: nil, global_name: nil, &block)
182
+ if block
183
+ entry = __skip__ = entry(type_name: type_name, method_name: method_name, const_name: const_name, global_name: global_name)
184
+ entry.declarations.each(&block)
185
+ else
186
+ enum_for(:each_declaration, type_name: type_name, method_name: method_name, const_name: const_name, global_name: global_name)
187
+ end
188
+ end
189
+
190
+ def add_type_reference(type_name, ref)
191
+ entry(type_name: type_name).add_reference(ref)
192
+ end
193
+
194
+ def each_reference(type_name:, &block)
195
+ if block
196
+ case
197
+ when type_name
198
+ entry(type_name: type_name).references.each(&block)
199
+ end
200
+ else
201
+ enum_for(:each_reference, type_name: type_name)
202
+ end
203
+ end
204
+
205
+ class Builder
206
+ attr_reader :index
207
+
208
+ def initialize(index:)
209
+ @index = index
210
+ end
211
+
212
+ def member(type_name, member)
213
+ case member
214
+ when RBS::AST::Members::MethodDefinition
215
+ member.overloads.each do |overload|
216
+ overload.method_type.each_type do |type|
217
+ type_reference type, from: member
218
+ end
219
+ end
220
+
221
+ if member.instance?
222
+ method_name = InstanceMethodName.new(type_name: type_name, method_name: member.name)
223
+ index.add_method_declaration(method_name, member)
224
+ end
225
+
226
+ if member.singleton?
227
+ method_name = SingletonMethodName.new(type_name: type_name, method_name: member.name)
228
+ index.add_method_declaration(method_name, member)
229
+ end
230
+
231
+ when RBS::AST::Members::AttrAccessor, RBS::AST::Members::AttrReader, RBS::AST::Members::AttrWriter
232
+ type_reference member.type, from: member
233
+
234
+ if member.is_a?(RBS::AST::Members::AttrReader) || member.is_a?(RBS::AST::Members::AttrAccessor)
235
+ method_name = case member.kind
236
+ when :instance
237
+ InstanceMethodName.new(type_name: type_name, method_name: member.name)
238
+ when :singleton
239
+ SingletonMethodName.new(type_name: type_name, method_name: member.name)
240
+ else
241
+ raise
242
+ end
243
+ index.add_method_declaration(method_name, member)
244
+ end
245
+
246
+ if member.is_a?(RBS::AST::Members::AttrWriter) || member.is_a?(RBS::AST::Members::AttrAccessor)
247
+ method_name = case member.kind
248
+ when :instance
249
+ InstanceMethodName.new(type_name: type_name, method_name: "#{member.name}=".to_sym)
250
+ when :singleton
251
+ SingletonMethodName.new(type_name: type_name, method_name: "#{member.name}=".to_sym)
252
+ else
253
+ raise
254
+ end
255
+ index.add_method_declaration(method_name, member)
256
+ end
257
+
258
+ when RBS::AST::Members::InstanceVariable, RBS::AST::Members::ClassVariable, RBS::AST::Members::ClassInstanceVariable
259
+ type_reference member.type, from: member
260
+
261
+ when RBS::AST::Members::Include, RBS::AST::Members::Extend
262
+ index.add_type_reference member.name, member
263
+ member.args.each do |type|
264
+ type_reference type, from: member
265
+ end
266
+
267
+ when RBS::AST::Members::Alias
268
+ if member.instance?
269
+ new_name = InstanceMethodName.new(type_name: type_name, method_name: member.new_name)
270
+ index.add_method_declaration(new_name, member)
271
+ end
272
+
273
+ if member.singleton?
274
+ new_name = SingletonMethodName.new(type_name: type_name, method_name: member.new_name)
275
+ index.add_method_declaration(new_name, member)
276
+ end
277
+ end
278
+ end
279
+
280
+ def type_reference(type, from:)
281
+ case type
282
+ when RBS::Types::ClassInstance, RBS::Types::ClassSingleton, RBS::Types::Alias, RBS::Types::Interface
283
+ index.add_type_reference(type.name, from)
284
+ end
285
+
286
+ type.each_type do |ty|
287
+ type_reference ty, from: from
288
+ end
289
+ end
290
+
291
+ def env(env)
292
+ env.class_decls.each do |name, decl|
293
+ decl.decls.each do |d|
294
+ index.add_type_declaration(name, d.decl)
295
+
296
+ case d.decl
297
+ when RBS::AST::Declarations::Class
298
+ if super_class = d.decl.super_class
299
+ index.add_type_reference(super_class.name, d.decl)
300
+ super_class.args.each do |type|
301
+ type_reference(type, from: d.decl)
302
+ end
303
+ end
304
+ when RBS::AST::Declarations::Module
305
+ d.decl.self_types.each do |self_type|
306
+ index.add_type_reference(self_type.name, d.decl)
307
+ self_type.args.each do |type|
308
+ type_reference(type, from: d.decl)
309
+ end
310
+ end
311
+ end
312
+
313
+ d.decl.members.each do |member|
314
+ member(name, member)
315
+ end
316
+ end
317
+ end
318
+
319
+ env.class_alias_decls.each do |name, entry|
320
+ index.add_type_declaration(name, entry.decl)
321
+ index.add_type_reference(entry.decl.old_name, entry.decl)
322
+ end
323
+
324
+ env.interface_decls.each do |name, decl|
325
+ index.add_type_declaration(name, decl.decl)
326
+
327
+ decl.decl.members.each do |member|
328
+ member(name, member)
329
+ end
330
+ end
331
+
332
+ env.type_alias_decls.each do |name, decl|
333
+ index.add_type_declaration(name, decl.decl)
334
+ type_reference decl.decl.type, from: decl.decl
335
+ end
336
+
337
+ env.constant_decls.each do |name, decl|
338
+ index.add_constant_declaration(name, decl.decl)
339
+ type_reference decl.decl.type, from: decl.decl
340
+ end
341
+
342
+ env.global_decls.each do |name, decl|
343
+ index.add_global_declaration(name, decl.decl)
344
+ type_reference decl.decl.type, from: decl.decl
345
+ end
346
+ end
347
+ end
348
+ end
349
+ end
350
+ end
@@ -0,0 +1,185 @@
1
+ module Steep
2
+ module Index
3
+ class SignatureSymbolProvider
4
+ LSP = LanguageServer::Protocol
5
+
6
+ class SymbolInformation < Struct.new(:name, :kind, :container_name, :location, keyword_init: true)
7
+ end
8
+
9
+ attr_reader :project
10
+ attr_reader :indexes
11
+ attr_reader :assignment
12
+
13
+ def initialize(project:, assignment:)
14
+ @indexes = {}
15
+ @project = project
16
+ @assignment = assignment
17
+ end
18
+
19
+ def self.test_type_name(query, type_name)
20
+ case
21
+ when query == ""
22
+ true
23
+ else
24
+ type_name.to_s.upcase.include?(query.upcase)
25
+ end
26
+ end
27
+
28
+ class <<self
29
+ alias test_const_name test_type_name
30
+ alias test_global_name test_type_name
31
+ end
32
+
33
+ def self.test_method_name(query, method_name)
34
+ case
35
+ when query == ""
36
+ true
37
+ else
38
+ method_name.to_s.upcase.include?(query.upcase)
39
+ end
40
+ end
41
+
42
+ def assigned?(target, path)
43
+ if path.relative?
44
+ if project.targets.any? {|target| target.possible_signature_file?(path) }
45
+ path = project.absolute_path(path)
46
+ end
47
+ end
48
+
49
+ assignment =~ [target, path]
50
+ end
51
+
52
+ def query_symbol(query)
53
+ symbols = [] #: Array[SymbolInformation]
54
+
55
+ indexes.each do |target, index|
56
+ index.each_entry do |entry|
57
+ case entry
58
+ when RBSIndex::TypeEntry
59
+ next unless SignatureSymbolProvider.test_type_name(query, entry.type_name)
60
+
61
+ container_name = entry.type_name.namespace.relative!.to_s.delete_suffix("::")
62
+ name = entry.type_name.name.to_s
63
+
64
+ entry.declarations.each do |decl|
65
+ location = decl.location or next
66
+ next unless assigned?(target, Pathname(location.buffer.name))
67
+
68
+ case decl
69
+ when RBS::AST::Declarations::Class
70
+ symbols << SymbolInformation.new(
71
+ name: name,
72
+ location: location,
73
+ kind: LSP::Constant::SymbolKind::CLASS,
74
+ container_name: container_name
75
+ )
76
+ when RBS::AST::Declarations::Module
77
+ symbols << SymbolInformation.new(
78
+ name: name,
79
+ location: location,
80
+ kind: LSP::Constant::SymbolKind::MODULE,
81
+ container_name: container_name
82
+ )
83
+ when RBS::AST::Declarations::Interface
84
+ symbols << SymbolInformation.new(
85
+ name: name,
86
+ location: location,
87
+ kind: LSP::Constant::SymbolKind::INTERFACE,
88
+ container_name: container_name
89
+ )
90
+ when RBS::AST::Declarations::TypeAlias
91
+ symbols << SymbolInformation.new(
92
+ name: name,
93
+ location: location,
94
+ kind: LSP::Constant::SymbolKind::ENUM,
95
+ container_name: container_name
96
+ )
97
+ end
98
+ end
99
+ when RBSIndex::MethodEntry
100
+ next unless SignatureSymbolProvider.test_method_name(query, entry.method_name)
101
+
102
+ name = case entry.method_name
103
+ when InstanceMethodName
104
+ "##{entry.method_name.method_name}"
105
+ when SingletonMethodName
106
+ ".#{entry.method_name.method_name}"
107
+ else
108
+ raise
109
+ end
110
+ container_name = entry.method_name.type_name.relative!.to_s
111
+
112
+ entry.declarations.each do |decl|
113
+ location = decl.location or next
114
+ next unless assigned?(target, Pathname(location.buffer.name))
115
+
116
+ case decl
117
+ when RBS::AST::Members::MethodDefinition
118
+ symbols << SymbolInformation.new(
119
+ name: name,
120
+ location: location,
121
+ kind: LSP::Constant::SymbolKind::METHOD,
122
+ container_name: container_name
123
+ )
124
+ when RBS::AST::Members::Alias
125
+ symbols << SymbolInformation.new(
126
+ name: name,
127
+ location: location,
128
+ kind: LSP::Constant::SymbolKind::METHOD,
129
+ container_name: container_name
130
+ )
131
+ when RBS::AST::Members::AttrAccessor, RBS::AST::Members::AttrReader, RBS::AST::Members::AttrWriter
132
+ symbols << SymbolInformation.new(
133
+ name: name,
134
+ location: location,
135
+ kind: LSP::Constant::SymbolKind::PROPERTY,
136
+ container_name: container_name
137
+ )
138
+
139
+ if decl.ivar_name
140
+ symbols << SymbolInformation.new(
141
+ name: decl.ivar_name.to_s,
142
+ location: location,
143
+ kind: LSP::Constant::SymbolKind::FIELD,
144
+ container_name: container_name
145
+ )
146
+ end
147
+ end
148
+ end
149
+ when RBSIndex::ConstantEntry
150
+ next unless SignatureSymbolProvider.test_const_name(query, entry.const_name)
151
+
152
+ entry.declarations.each do |decl|
153
+ next unless decl.location
154
+ next unless assigned?(target, Pathname(decl.location.buffer.name))
155
+
156
+ symbols << SymbolInformation.new(
157
+ name: entry.const_name.name.to_s,
158
+ location: decl.location,
159
+ kind: LSP::Constant::SymbolKind::CONSTANT,
160
+ container_name: entry.const_name.namespace.relative!.to_s.delete_suffix("::")
161
+ )
162
+ end
163
+ when RBSIndex::GlobalEntry
164
+ next unless SignatureSymbolProvider.test_global_name(query, entry.global_name)
165
+
166
+ entry.declarations.each do |decl|
167
+ next unless decl.location
168
+ next unless assigned?(target, Pathname(decl.location.buffer.name))
169
+
170
+ symbols << SymbolInformation.new(
171
+ name: decl.name.to_s,
172
+ location: decl.location,
173
+ kind: LSP::Constant::SymbolKind::VARIABLE,
174
+ container_name: nil
175
+ )
176
+ end
177
+ end
178
+ end
179
+ end
180
+
181
+ symbols.uniq {|symbol| [symbol.name, symbol.location] }.sort_by {|symbol| symbol.name.to_s }
182
+ end
183
+ end
184
+ end
185
+ end
@@ -0,0 +1,167 @@
1
+ module Steep
2
+ module Index
3
+ class SourceIndex
4
+ class ConstantEntry
5
+ attr_reader :name
6
+
7
+ attr_reader :definitions
8
+ attr_reader :references
9
+
10
+ def initialize(name:)
11
+ @name = name
12
+
13
+ @definitions = Set[].compare_by_identity
14
+ @references = Set[].compare_by_identity
15
+ end
16
+
17
+ def add_definition(node)
18
+ case node.type
19
+ when :casgn, :const
20
+ @definitions << node
21
+ else
22
+ raise "Unexpected constant definition: #{node.type}"
23
+ end
24
+
25
+ self
26
+ end
27
+
28
+ def add_reference(node)
29
+ case node.type
30
+ when :const
31
+ @references << node
32
+ else
33
+ raise "Unexpected constant reference: #{node.type}"
34
+ end
35
+
36
+ self
37
+ end
38
+
39
+ def merge!(other)
40
+ definitions.merge(other.definitions)
41
+ references.merge(other.references)
42
+ self
43
+ end
44
+ end
45
+
46
+ class MethodEntry
47
+ attr_reader :name
48
+
49
+ attr_reader :definitions
50
+ attr_reader :references
51
+
52
+ def initialize(name:)
53
+ @name = name
54
+
55
+ @definitions = Set[].compare_by_identity
56
+ @references = Set[].compare_by_identity
57
+ end
58
+
59
+ def add_definition(node)
60
+ case node.type
61
+ when :def, :defs
62
+ @definitions << node
63
+ else
64
+ raise "Unexpected method definition: #{node.type}"
65
+ end
66
+
67
+ self
68
+ end
69
+
70
+ def add_reference(node)
71
+ case node.type
72
+ when :send, :block
73
+ @references << node
74
+ else
75
+ raise "Unexpected method reference: #{node.type}"
76
+ end
77
+
78
+ self
79
+ end
80
+
81
+ def merge!(other)
82
+ definitions.merge(other.definitions)
83
+ references.merge(other.references)
84
+ self
85
+ end
86
+ end
87
+
88
+ attr_reader :source
89
+ attr_reader :constant_index
90
+ attr_reader :method_index
91
+
92
+ attr_reader :parent
93
+ attr_reader :count
94
+ attr_reader :parent_count
95
+
96
+ def initialize(source:, parent: nil)
97
+ @source = source
98
+ @parent = parent
99
+ @parent_count = parent&.count
100
+
101
+ @count = @parent_count || 0
102
+
103
+ @constant_index = {}
104
+ @method_index = {}
105
+ end
106
+
107
+ def new_child
108
+ SourceIndex.new(source: source, parent: self)
109
+ end
110
+
111
+ def merge!(child)
112
+ raise unless child.parent == self
113
+ raise unless child.parent_count == count
114
+
115
+ constant_index.merge!(child.constant_index) do |_, entry, child_entry|
116
+ entry.merge!(child_entry)
117
+ end
118
+
119
+ method_index.merge!(child.method_index) do |_, entry, child_entry|
120
+ entry.merge!(child_entry)
121
+ end
122
+
123
+ @count = child.count + 1
124
+ end
125
+
126
+ def add_definition(constant: nil, method: nil, definition:)
127
+ @count += 1
128
+ entry(constant: constant, method: method).add_definition(definition)
129
+ self
130
+ end
131
+
132
+ def add_reference(constant: nil, method: nil, ref:)
133
+ @count += 1
134
+ entry(constant: constant, method: method).add_reference(ref)
135
+ self
136
+ end
137
+
138
+ def entry(constant: nil, method: nil)
139
+ case
140
+ when constant
141
+ constant_index[constant] ||= ConstantEntry.new(name: constant)
142
+ when method
143
+ method_index[method] ||= MethodEntry.new(name: method)
144
+ else
145
+ raise
146
+ end
147
+ end
148
+
149
+ def reference(constant_node: nil)
150
+ case
151
+ when constant_node
152
+ constant_index.each do |name, entry|
153
+ if entry.references.include?(constant_node)
154
+ return name
155
+ end
156
+
157
+ if entry.definitions.include?(constant_node)
158
+ return name
159
+ end
160
+ end
161
+
162
+ nil
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end