Class: Steep::Services::HoverProvider::Ruby

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/services/hover_provider/ruby.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service:) ⇒ Ruby

Returns a new instance of Ruby.



8
9
10
# File 'lib/steep/services/hover_provider/ruby.rb', line 8

def initialize(service:)
  @service = service
end

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



6
7
8
# File 'lib/steep/services/hover_provider/ruby.rb', line 6

def service
  @service
end

Instance Method Details

#content_for(target:, path:, line:, column:) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/steep/services/hover_provider/ruby.rb', line 67

def content_for(target:, path:, line:, column:)
  file = service.source_files[path] or return
  (typing, subtyping = typecheck(target, path: path, content: file.content, line: line, column: column)) or return
  locator = Locator::Ruby.new(typing.source)
  result = locator.find(line, column)

  case result
  when Locator::NodeResult
    node = result.node
    parents = result.parents

    case node.type
    when :lvar
      var_name = node.children[0]
      context = typing.cursor_context.context or raise
      var_type = context.type_env[var_name] || AST::Types::Any.instance()

      return VariableContent.new(
        node: node,
        name: var_name,
        type: var_type,
        location: node.location.name # steep:ignore NoMethod
      )

    when :lvasgn
      var_name, _rhs = node.children
      context = typing.cursor_context.context or raise
      type = context.type_env[var_name] || typing.type_of(node: node)

      return VariableContent.new(
        node: node,
        name: var_name,
        type: type,
        location: node.location.name # steep:ignore NoMethod
      )

    when :send, :csend
      result_node =
        case parents[0]&.type
        when :block, :numblock
          if node == parents.fetch(0).children[0]
            parents.fetch(0)
          else
            node
          end
        else
          node
        end

      case call = typing.call_of(node: result_node)
      when TypeInference::MethodCall::Typed, TypeInference::MethodCall::Error
        unless call.method_decls.empty?
          return MethodCallContent.new(
            node: result_node,
            method_call: call,
            location: node.location.selector # steep:ignore NoMethod
          )
        end
      end

    when :def, :defs
      context = typing.cursor_context.context or raise
      method_context = context.method_context

      if method_context && method_context.method
        if method_context.method_type
          return DefinitionContent.new(
            node: node,
            method_name: method_name_from_method(method_context, builder: context.factory.definition_builder),
            method_type: method_context.method_type,
            definition: method_context.method,
            location: node.loc.name # steep:ignore NoMethod
          )
        end
      end

    when :const, :casgn
      context = typing.cursor_context.context or raise

      type = typing.type_of(node: node)
      const_name = typing.source_index.reference(constant_node: node)

      if const_name
        entry = context.env.constant_entry(const_name) or return

        return ConstantContent.new(
          location: node.location.name, # steep:ignore NoMethod
          full_name: const_name,
          type: type,
          decl: entry
        )
      end
    end

    TypeContent.new(
      node: node,
      type: typing.type_of(node: node),
      location: node.location.expression
    )

  when Locator::TypeAssertionResult
    context = typing.cursor_context.context or raise
    pos = typing.source.buffer.loc_to_pos([line, column])

    nesting = context.module_context.nesting
    type_vars = context.variable_context.type_params.map { _1.name }

    if (name, location = result.locate_type_name(pos, nesting, subtyping, type_vars))
      if content = type_name_content(subtyping.factory.env, name, location)
        return content
      end
    end

    assertion_node = result.node.node
    original_node = assertion_node.children[0] or raise

    original_type = typing.type_of(node: original_node)
    asserted_type = typing.type_of(node: assertion_node)

    if original_type != asserted_type
      TypeAssertionContent.new(
        node: assertion_node,
        original_type: original_type,
        asserted_type: asserted_type,
        location: assertion_node.location.expression
      )
    else
      TypeContent.new(
        node: assertion_node,
        type: typing.type_of(node: assertion_node),
        location: assertion_node.location.expression
      )
    end

  when Locator::TypeApplicationResult
    begin
      context = typing.cursor_context.context or raise
      pos = typing.source.buffer.loc_to_pos([line, column])

      nesting = context.module_context.nesting
      type_vars = context.variable_context.type_params.map { _1.name }

      if (name, location = result.locate_type_name(pos, nesting, subtyping, type_vars))
        if content = type_name_content(subtyping.factory.env, name, location)
          return content
        end
      end

      nil
    rescue ::RBS::ParsingError
      return nil
    end
  end
end

#content_for_inline(target:, path:, line:, column:) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/steep/services/hover_provider/ruby.rb', line 255

def content_for_inline(target:, path:, line:, column:)
  signature = service.signature_services.fetch(target.name)
  source = signature.latest_env.sources.find do
    if _1.is_a?(::RBS::Source::Ruby)
      _1.buffer.name == path
    end
  end

  return unless source.is_a?(::RBS::Source::Ruby)

  locator = Locator::Inline.new(source)
  result = locator.find(line, column)

  case result
  when Locator::InlineTypeNameResult
    return type_name_content(signature.latest_env, result.type_name, result.location)
  else
    Steep.logger.fatal { { result: result.class }.inspect }
  end

  nil
end

#method_definition_for(factory, type_name, singleton_method: nil, instance_method: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/steep/services/hover_provider/ruby.rb', line 16

def method_definition_for(factory, type_name, singleton_method: nil, instance_method: nil)
  case
  when instance_method
    factory.definition_builder.build_instance(type_name).methods.fetch(instance_method)
  when singleton_method
    methods = factory.definition_builder.build_singleton(type_name).methods

    if singleton_method == :new
      methods[:new] || methods.fetch(:initialize)
    else
      methods.fetch(singleton_method)
    end
  else
    raise "One of the instance_method or singleton_method is required"
  end
end

#method_name_from_method(context, builder:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/steep/services/hover_provider/ruby.rb', line 47

def method_name_from_method(context, builder:)
  context.method or raise
  defined_in = context.method.defined_in or raise
  method_name = context.name or raise

  case
  when defined_in.class?
    case
    when builder.build_instance(defined_in).methods.key?(method_name)
      InstanceMethodName.new(type_name: defined_in, method_name: method_name)
    when builder.build_singleton(defined_in).methods.key?(method_name)
      SingletonMethodName.new(type_name: defined_in, method_name: method_name)
    else
      raise
    end
  else
    InstanceMethodName.new(type_name: defined_in, method_name: method_name)
  end
end

#projectObject



12
13
14
# File 'lib/steep/services/hover_provider/ruby.rb', line 12

def project
  service.project
end

#type_name_content(environment, type_name, location) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/steep/services/hover_provider/ruby.rb', line 222

def type_name_content(environment, type_name, location)
  case
  when type_name.class?
    if entry = environment.module_class_entry(type_name)
      decl = case entry
      when ::RBS::Environment::ModuleEntry, ::RBS::Environment::ClassEntry
        entry.primary_decl
      else
        entry.decl
      end

      ClassTypeContent.new(
        location: location,
        decl: decl
      )
    end
  when type_name.interface?
    if entry = environment.interface_decls.fetch(type_name, nil)
      InterfaceTypeContent.new(
        location: location,
        decl: entry.decl
      )
    end
  when type_name.alias?
    if entry = environment.type_alias_decls.fetch(type_name, nil)
      TypeAliasContent.new(
        location: location,
        decl: entry.decl
      )
    end
  end
end

#typecheck(target, path:, content:, line:, column:) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/steep/services/hover_provider/ruby.rb', line 33

def typecheck(target, path:, content:, line:, column:)
  subtyping = service.signature_services.fetch(target.name).current_subtyping or return
  source = Source.parse(content, path: path, factory: subtyping.factory)
  source = source.without_unrelated_defs(line: line, column: column)
  resolver = ::RBS::Resolver::ConstantResolver.new(builder: subtyping.factory.definition_builder)
  pos = source.buffer.loc_to_pos([line, column])
  [
    Services::TypeCheckService.type_check(source: source, subtyping: subtyping, constant_resolver: resolver, cursor: pos),
    subtyping
  ]
rescue
  nil
end