ruby-lsp 0.26.9 → 0.27.0.beta1

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.
@@ -5,9 +5,9 @@ module RubyLsp
5
5
  # A minimalistic type checker to try to resolve types that can be inferred without requiring a type system or
6
6
  # annotations
7
7
  class TypeInferrer
8
- #: (RubyIndexer::Index index) -> void
9
- def initialize(index)
10
- @index = index
8
+ #: (Rubydex::Graph) -> void
9
+ def initialize(graph)
10
+ @graph = graph
11
11
  end
12
12
 
13
13
  #: (NodeContext node_context) -> Type?
@@ -81,14 +81,13 @@ module RubyLsp
81
81
  receiver_name = RubyIndexer::Index.constant_name(receiver)
82
82
  return unless receiver_name
83
83
 
84
- resolved_receiver = @index.resolve(receiver_name, node_context.nesting)
85
- name = resolved_receiver&.first&.name
86
- return unless name
84
+ resolved_receiver = @graph.resolve_constant(receiver_name, node_context.nesting)
85
+ return unless resolved_receiver
87
86
 
88
- *parts, last = name.split("::")
89
- return Type.new("#{last}::<Class:#{last}>") if parts.empty?
87
+ *parts, last = resolved_receiver.name.split("::")
88
+ return Type.new("#{last}::<#{last}>") if parts.empty?
90
89
 
91
- Type.new("#{parts.join("::")}::#{last}::<Class:#{last}>")
90
+ Type.new("#{parts.join("::")}::#{last}::<#{last}>")
92
91
  when Prism::CallNode
93
92
  raw_receiver = receiver.message
94
93
 
@@ -96,12 +95,14 @@ module RubyLsp
96
95
  # When invoking `new`, we recursively infer the type of the receiver to get the class type its being invoked
97
96
  # on and then return the attached version of that type, since it's being instantiated.
98
97
  type = infer_receiver_for_call_node(receiver, node_context)
99
-
100
98
  return unless type
101
99
 
102
100
  # If the method `new` was overridden, then we cannot assume that it will return a new instance of the class
103
- new_method = @index.resolve_method("new", type.name)&.first
104
- return if new_method && new_method.owner&.name != "Class"
101
+ declaration = @graph[type.name] #: as Rubydex::Namespace?
102
+ return unless declaration
103
+
104
+ new_method = declaration.find_member("new()")
105
+ return if new_method && new_method.owner.name != "Class"
105
106
 
106
107
  type.attached
107
108
  elsif raw_receiver
@@ -121,11 +122,11 @@ module RubyLsp
121
122
  .map(&:capitalize)
122
123
  .join
123
124
 
124
- entries = @index.resolve(guessed_name, nesting) || @index.first_unqualified_const(guessed_name)
125
- name = entries&.first&.name
126
- return unless name
125
+ declaration = @graph.resolve_constant(guessed_name, nesting)
126
+ declaration ||= @graph.search(guessed_name).first
127
+ return unless declaration
127
128
 
128
- GuessedType.new(name)
129
+ GuessedType.new(declaration.name)
129
130
  end
130
131
 
131
132
  #: (NodeContext node_context) -> Type
@@ -142,24 +143,25 @@ module RubyLsp
142
143
  # If the class/module definition is using compact style (e.g.: `class Foo::Bar`), then we need to split the name
143
144
  # into its individual parts to build the correct singleton name
144
145
  parts = nesting.flat_map { |part| part.split("::") }
145
- Type.new("#{parts.join("::")}::<Class:#{parts.last}>")
146
+ Type.new("#{parts.join("::")}::<#{parts.last}>")
146
147
  end
147
148
 
148
149
  #: (NodeContext node_context) -> Type?
149
150
  def infer_receiver_for_class_variables(node_context)
150
151
  nesting_parts = node_context.nesting.dup
151
-
152
152
  return Type.new("Object") if nesting_parts.empty?
153
153
 
154
154
  nesting_parts.reverse_each do |part|
155
- break unless part.include?("<Class:")
155
+ break unless part.start_with?("<")
156
156
 
157
157
  nesting_parts.pop
158
158
  end
159
159
 
160
- receiver_name = nesting_parts.join("::")
161
- resolved_receiver = @index.resolve(receiver_name, node_context.nesting)&.first
162
- return unless resolved_receiver&.name
160
+ resolved_receiver = @graph.resolve_constant(
161
+ nesting_parts.last, #: as !nil
162
+ nesting_parts[0...-1], #: as !nil
163
+ )
164
+ return unless resolved_receiver
163
165
 
164
166
  Type.new(resolved_receiver.name)
165
167
  end
@@ -174,7 +176,7 @@ module RubyLsp
174
176
  @name = name
175
177
  end
176
178
 
177
- # Returns the attached version of this type by removing the `<Class:...>` part from its name
179
+ # Returns the attached version of this type by removing the `<...>` part from its name
178
180
  #: -> Type
179
181
  def attached
180
182
  Type.new(
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-lsp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.26.9
4
+ version: 0.27.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify
@@ -63,6 +63,20 @@ dependencies:
63
63
  - - "<"
64
64
  - !ruby/object:Gem::Version
65
65
  version: '5'
66
+ - !ruby/object:Gem::Dependency
67
+ name: rubydex
68
+ requirement: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: 0.1.0.beta1
73
+ type: :runtime
74
+ prerelease: false
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 0.1.0.beta1
66
80
  description: An opinionated language server for Ruby
67
81
  email:
68
82
  - ruby@shopify.com
@@ -168,6 +182,8 @@ files:
168
182
  - lib/ruby_lsp/response_builders/signature_help.rb
169
183
  - lib/ruby_lsp/response_builders/test_collection.rb
170
184
  - lib/ruby_lsp/ruby_document.rb
185
+ - lib/ruby_lsp/rubydex/definition.rb
186
+ - lib/ruby_lsp/rubydex/reference.rb
171
187
  - lib/ruby_lsp/scope.rb
172
188
  - lib/ruby_lsp/scripts/compose_bundle.rb
173
189
  - lib/ruby_lsp/scripts/compose_bundle_windows.rb