Class: Steep::Services::GotoService
- Includes:
- ModuleHelper
- Defined in:
- lib/steep/services/goto_service.rb
Defined Under Namespace
Modules: SourceHelper Classes: ConstantQuery, MethodQuery, TypeNameQuery
Instance Attribute Summary collapse
-
#assignment ⇒ Object
readonly
Returns the value of attribute assignment.
-
#type_check ⇒ Object
readonly
Returns the value of attribute type_check.
Instance Method Summary collapse
- #constant_definition_in_rbs(name, locations:) ⇒ Object
- #constant_definition_in_ruby(name, locations:) ⇒ Object
- #definition(path:, line:, column:) ⇒ Object
- #each_type_name(type, &block) ⇒ Object
- #implementation(path:, line:, column:) ⇒ Object
-
#initialize(type_check:, assignment:) ⇒ GotoService
constructor
A new instance of GotoService.
- #method_locations(name, in_ruby:, in_rbs:, locations:) ⇒ Object
- #project ⇒ Object
- #query_at(path:, line:, column:) ⇒ Object
- #test_ast_location(loc, line:, column:) ⇒ Object
- #type_check_path(target:, path:, content:, line:, column:) ⇒ Object
- #type_definition(path:, line:, column:) ⇒ Object
- #type_name_locations(name, locations: []) ⇒ Object
Methods included from ModuleHelper
#module_name_from_node, #namespace_from_node
Constructor Details
#initialize(type_check:, assignment:) ⇒ GotoService
Returns a new instance of GotoService.
27 28 29 30 |
# File 'lib/steep/services/goto_service.rb', line 27 def initialize(type_check:, assignment:) @type_check = type_check @assignment = assignment end |
Instance Attribute Details
#assignment ⇒ Object (readonly)
Returns the value of attribute assignment.
25 26 27 |
# File 'lib/steep/services/goto_service.rb', line 25 def assignment @assignment end |
#type_check ⇒ Object (readonly)
Returns the value of attribute type_check.
25 26 27 |
# File 'lib/steep/services/goto_service.rb', line 25 def type_check @type_check end |
Instance Method Details
#constant_definition_in_rbs(name, locations:) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/steep/services/goto_service.rb', line 301 def constant_definition_in_rbs(name, locations:) project.targets.each do |target| signature = type_check.signature_services.fetch(target.name) env = signature.latest_env #: RBS::Environment case entry = env.constant_entry(name) when RBS::Environment::ConstantEntry if entry.decl.location locations << [target, entry.decl.location[:name]] end when RBS::Environment::ClassEntry, RBS::Environment::ModuleEntry entry.decls.each do |d| if d.decl.location locations << [target, d.decl.location[:name]] end end when RBS::Environment::ClassAliasEntry, RBS::Environment::ModuleAliasEntry if entry.decl.location locations << [target, entry.decl.location[:new_name]] end end end locations end |
#constant_definition_in_ruby(name, locations:) ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/steep/services/goto_service.rb', line 327 def constant_definition_in_ruby(name, locations:) type_check.source_files.each do |path, source| if typing = source.typing target = project.target_for_source_path(path) or raise entry = typing.source_index.entry(constant: name) entry.definitions.each do |node| case node.type when :const locations << [target, node.location.expression] when :casgn parent = node.children[0] location = if parent parent.location.expression.join(node.location.name) else node.location.name end locations << [target, location] end end end end locations end |
#definition(path:, line:, column:) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 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 |
# File 'lib/steep/services/goto_service.rb', line 56 def definition(path:, line:, column:) locations = [] #: Array[target_loc] queries = query_at(path: path, line: line, column: column) queries.uniq! queries.each do |query| case query when ConstantQuery constant_definition_in_rbs(query.name, locations: locations) if query.from_ruby? constant_definition_in_ruby(query.name, locations: locations) if query.from_rbs? when MethodQuery method_locations( query.name, locations: locations, in_ruby: query.from_rbs?, in_rbs: query.from_ruby? ) when TypeNameQuery type_name_locations(query.name, locations: locations) end end # Drop un-assigned paths here. # The path assignment makes sense only for `.rbs` files, because un-assigned `.rb` files are already skipped since they are not type checked. # locations.filter_map do |target, loc| case loc when RBS::Location if assignment =~ [target, loc.name] loc end else loc end end.uniq end |
#each_type_name(type, &block) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/steep/services/goto_service.rb', line 129 def each_type_name(type, &block) if block case type when AST::Types::Name::Instance, AST::Types::Name::Alias, AST::Types::Name::Singleton, AST::Types::Name::Interface yield type.name when AST::Types::Literal yield type.back_type.name when AST::Types::Nil yield RBS::TypeName.new(name: :NilClass, namespace: RBS::Namespace.root) when AST::Types::Boolean yield RBS::BuiltinNames::TrueClass.name yield RBS::BuiltinNames::FalseClass.name end type.each_child do |child| each_type_name(child, &block) end else enum_for :each_type_name, type end end |
#implementation(path:, line:, column:) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/steep/services/goto_service.rb', line 36 def implementation(path:, line:, column:) locations = [] #: Array[target_loc] queries = query_at(path: path, line: line, column: column) queries.uniq! queries.each do |query| case query when ConstantQuery constant_definition_in_ruby(query.name, locations: locations) when MethodQuery method_locations(query.name, locations: locations, in_ruby: true, in_rbs: false) when TypeNameQuery constant_definition_in_ruby(query.name, locations: locations) end end locations.map { _1[1] }.uniq end |
#method_locations(name, in_ruby:, in_rbs:, locations:) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/steep/services/goto_service.rb', line 353 def method_locations(name, in_ruby:, in_rbs:, locations:) if in_ruby type_check.source_files.each do |path, source| if typing = source.typing target = project.target_for_source_path(path) or raise entry = typing.source_index.entry(method: name) if entry.definitions.empty? if name.is_a?(SingletonMethodName) && name.method_name == :new initialize = InstanceMethodName.new(method_name: :initialize, type_name: name.type_name) entry = typing.source_index.entry(method: initialize) end end entry.definitions.each do |node| case node.type when :def locations << [target, node.location.name] when :defs locations << [target, node.location.name] end end end end end if in_rbs project.targets.each do |target| signature = type_check.signature_services.fetch(target.name) index = signature.latest_rbs_index entry = index.entry(method_name: name) if entry.declarations.empty? if name.is_a?(SingletonMethodName) && name.method_name == :new initialize = InstanceMethodName.new(method_name: :initialize, type_name: name.type_name) entry = index.entry(method_name: initialize) end end entry.declarations.each do |decl| case decl when RBS::AST::Members::MethodDefinition if decl.location locations << [target, decl.location[:name]] end when RBS::AST::Members::Alias if decl.location locations << [target, decl.location[:new_name]] end when RBS::AST::Members::AttrAccessor, RBS::AST::Members::AttrReader, RBS::AST::Members::AttrWriter if decl.location locations << [target, decl.location[:name]] end end end end end locations end |
#project ⇒ Object
32 33 34 |
# File 'lib/steep/services/goto_service.rb', line 32 def project type_check.project end |
#query_at(path:, line:, column:) ⇒ Object
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 221 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/steep/services/goto_service.rb', line 159 def query_at(path:, line:, column:) queries = [] #: Array[query] relative_path = project.relative_path(path) case when target = type_check.project.target_for_source_path(relative_path) source = type_check.source_files.fetch(relative_path, nil) or return [] typing, _signature = type_check_path(target: target, path: relative_path, content: source.content, line: line, column: column) if typing node, *parents = typing.source.find_nodes(line: line, column: column) if node && parents case node.type when :const, :casgn named_location = (_ = node.location) #: Parser::AST::_NamedLocation if test_ast_location(named_location.name, line: line, column: column) if name = typing.source_index.reference(constant_node: node) queries << ConstantQuery.new(name: name, from: :ruby) end end when :def, :defs named_location = (_ = node.location) #: Parser::AST::_NamedLocation if test_ast_location(named_location.name, line: line, column: column) if method_context = typing.cursor_context.context&.method_context if method = method_context.method method.defs.each do |defn| singleton_method = case defn.member when RBS::AST::Members::MethodDefinition defn.member.singleton? when RBS::AST::Members::Attribute defn.member.kind == :singleton end name = if singleton_method SingletonMethodName.new(type_name: defn.defined_in, method_name: method_context.name) else InstanceMethodName.new(type_name: defn.defined_in, method_name: method_context.name) end queries << MethodQuery.new(name: name, from: :ruby) end end end end when :send location = (_ = node.location) #: Parser::AST::_SelectorLocation if test_ast_location(location.selector, line: line, column: column) if (parent = parents[0]) && parent.type == :block && parent.children[0] == node node = parents[0] end case call = typing.call_of(node: node) when TypeInference::MethodCall::Typed, TypeInference::MethodCall::Error call.method_decls.each do |decl| queries << MethodQuery.new(name: decl.method_name, from: :ruby) end when TypeInference::MethodCall::Untyped # nop when TypeInference::MethodCall::NoMethodError # nop end end end end end when target_names = type_check.signature_file?(path) #: Array[Symbol] target_names.each do |target_name| signature_service = type_check.signature_services[target_name] #: SignatureService env = signature_service.latest_env buffer = env.buffers.find {|buf| buf.name.to_s == relative_path.to_s } or raise (dirs, decls = env.signatures[buffer]) or raise locator = RBS::Locator.new(buffer: buffer, dirs: dirs, decls: decls) last, nodes = locator.find2(line: line, column: column) nodes or raise case nodes[0] when RBS::AST::Declarations::Class, RBS::AST::Declarations::Module if last == :name queries << ConstantQuery.new(name: nodes[0].name, from: :rbs) end when RBS::AST::Declarations::Constant if last == :name queries << ConstantQuery.new(name: nodes[0].name, from: :rbs) end when RBS::AST::Members::MethodDefinition if last == :name parent_node = nodes[1] #: RBS::AST::Declarations::Class | RBS::AST::Declarations::Module | RBS::AST::Declarations::Interface type_name = parent_node.name method_name = nodes[0].name if nodes[0].instance? queries << MethodQuery.new( name: InstanceMethodName.new(type_name: type_name, method_name: method_name), from: :rbs ) end if nodes[0].singleton? queries << MethodQuery.new( name: SingletonMethodName.new(type_name: type_name, method_name: method_name), from: :rbs ) end end when RBS::AST::Members::Include, RBS::AST::Members::Extend, RBS::AST::Members::Prepend if last == :name queries << TypeNameQuery.new(name: nodes[0].name) end when RBS::Types::ClassInstance, RBS::Types::ClassSingleton, RBS::Types::Interface, RBS::Types::Alias if last == :name queries << TypeNameQuery.new(name: nodes[0].name) end when RBS::AST::Declarations::Class::Super, RBS::AST::Declarations::Module::Self if last == :name queries << TypeNameQuery.new(name: nodes[0].name) end end end end queries end |
#test_ast_location(loc, line:, column:) ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/steep/services/goto_service.rb', line 151 def test_ast_location(loc, line:, column:) return false if line < loc.line return false if line == loc.line && column < loc.column return false if loc.last_line < line return false if line == loc.last_line && loc.last_column < column true end |
#type_check_path(target:, path:, content:, line:, column:) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/steep/services/goto_service.rb', line 286 def type_check_path(target:, path:, content:, line:, column:) signature_service = type_check.signature_services.fetch(target.name) subtyping = signature_service.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) loc = source.buffer.loc_to_pos([line, column]) [ Services::TypeCheckService.type_check(source: source, subtyping: subtyping, constant_resolver: resolver, cursor: loc), signature_service ] rescue nil end |
#type_definition(path:, line:, column:) ⇒ Object
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 |
# File 'lib/steep/services/goto_service.rb', line 94 def type_definition(path:, line:, column:) locations = [] #: Array[target_loc] relative_path = project.relative_path(path) target = type_check.project.target_for_path(relative_path) or return [] source = type_check.source_files.fetch(relative_path) typing, signature = type_check_path(target: target, path: relative_path, content: source.content, line: line, column: column) typing or return [] signature or return [] node, *_parents = typing.source.find_nodes(line: line, column: column) node or return [] type = typing.type_of(node: node) subtyping = signature.current_subtyping or return [] each_type_name(type).uniq.each do |name| type_name_locations(name, locations: locations) end locations.filter_map do |target, loc| case loc when RBS::Location if assignment =~ [target, loc.name] loc end else loc end end.uniq end |
#type_name_locations(name, locations: []) ⇒ Object
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/steep/services/goto_service.rb', line 415 def type_name_locations(name, locations: []) project.targets.each do |target| signature = type_check.signature_services.fetch(target.name) index = signature.latest_rbs_index entry = index.entry(type_name: name) entry.declarations.each do |decl| case decl when RBS::AST::Declarations::Class, RBS::AST::Declarations::Module, RBS::AST::Declarations::Interface, RBS::AST::Declarations::TypeAlias if decl.location locations << [target, decl.location[:name]] end when RBS::AST::Declarations::AliasDecl if decl.location locations << [target, decl.location[:new_name]] end else raise end end end locations end |