Class: Steep::Services::HoverProvider::Ruby
- Defined in:
- lib/steep/services/hover_provider/ruby.rb
Constant Summary collapse
- TypeContent =
_ = Struct.new(:node, :type, :location, keyword_init: true)
- VariableContent =
_ = Struct.new(:node, :name, :type, :location, keyword_init: true)
- TypeAssertionContent =
_ = Struct.new(:node, :original_type, :asserted_type, :location, keyword_init: true)
- MethodCallContent =
_ = Struct.new(:node, :method_call, :location, keyword_init: true)
- DefinitionContent =
_ = Struct.new(:node, :method_name, :method_type, :definition, :location, keyword_init: true)
- ConstantContent =
_ = Struct.new(:location, :full_name, :type, :decl, keyword_init: true) do # @implements ConstantContent def comments case when decl = class_decl decl.decls.map {|d| d.decl.comment } when decl = class_alias [decl.decl.comment] when decl = constant_decl [decl.decl.comment] else raise end.compact end def class_decl case decl when ::RBS::Environment::ClassEntry, ::RBS::Environment::ModuleEntry decl end end def class_alias case decl when ::RBS::Environment::ClassAliasEntry, ::RBS::Environment::ModuleAliasEntry decl end end def constant_decl if decl.is_a?(::RBS::Environment::ConstantEntry) decl end end def constant? constant_decl ? true : false end def class_or_module? (class_decl || class_alias) ? true : false end end
Instance Attribute Summary collapse
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Instance Method Summary collapse
- #content_for(target:, path:, line:, column:) ⇒ Object
-
#initialize(service:) ⇒ Ruby
constructor
A new instance of Ruby.
- #method_definition_for(factory, type_name, singleton_method: nil, instance_method: nil) ⇒ Object
- #method_name_from_method(context, builder:) ⇒ Object
- #project ⇒ Object
- #typecheck(target, path:, content:, line:, column:) ⇒ Object
Constructor Details
#initialize(service:) ⇒ Ruby
Returns a new instance of Ruby.
57 58 59 |
# File 'lib/steep/services/hover_provider/ruby.rb', line 57 def initialize(service:) @service = service end |
Instance Attribute Details
#service ⇒ Object (readonly)
Returns the value of attribute service.
55 56 57 |
# File 'lib/steep/services/hover_provider/ruby.rb', line 55 def service @service end |
Instance Method Details
#content_for(target:, path:, line:, column:) ⇒ Object
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 |
# File 'lib/steep/services/hover_provider/ruby.rb', line 113 def content_for(target:, path:, line:, column:) file = service.source_files[path] or return typing = typecheck(target, path: path, content: file.content, line: line, column: column) or return node, *parents = typing.source.find_nodes(line: line, column: column) if node && 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 when :assertion original_node, _ = node.children original_type = typing.type_of(node: original_node) asserted_type = typing.type_of(node: node) if original_type != asserted_type return TypeAssertionContent.new(node: node, original_type: original_type, asserted_type: asserted_type, location: node.location.expression) end end TypeContent.new( node: node, type: typing.type_of(node: node), location: node.location.expression ) end end |
#method_definition_for(factory, type_name, singleton_method: nil, instance_method: nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/steep/services/hover_provider/ruby.rb', line 65 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/steep/services/hover_provider/ruby.rb', line 93 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 |
#project ⇒ Object
61 62 63 |
# File 'lib/steep/services/hover_provider/ruby.rb', line 61 def project service.project end |
#typecheck(target, path:, content:, line:, column:) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/steep/services/hover_provider/ruby.rb', line 82 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.(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) rescue nil end |