Class: Steep::Services::HoverProvider::RBS

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service:) ⇒ RBS

Returns a new instance of RBS.



8
9
10
# File 'lib/steep/services/hover_provider/rbs.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/rbs.rb', line 6

def service
  @service
end

Instance Method Details

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/steep/services/hover_provider/rbs.rb', line 16

def content_for(target:, path:, line:, column:)
  service = self.service.signature_services.fetch(target.name)

  env = service.latest_env
  source = env.each_rbs_source.find {|src| src.buffer.name == path } or return
  buffer = source.buffer
  dirs = source.directives
  decls = source.declarations

  locator = ::RBS::Locator.new(buffer: buffer, dirs: dirs, decls: decls)
  loc_key, path = locator.find2(line: line, column: column) || return
  head, *_tail = path

  case head
  when ::RBS::Types::Alias
    content_for_type_name(head.name, env: env, location: head.location || raise)

  when ::RBS::Types::ClassInstance, ::RBS::Types::ClassSingleton
    if loc_key == :name
      location = head.location&.[](:name) or raise
      content_for_type_name(head.name, env: env, location: location)
    end

  when ::RBS::Types::Interface
    location = head.location&.[](:name) or raise
    content_for_type_name(head.name, env: env, location: location)

  when ::RBS::AST::Declarations::ClassAlias, ::RBS::AST::Declarations::ModuleAlias
    if loc_key == :old_name
      location = head.location&.[](:old_name) or raise
      content_for_type_name(head.old_name, env: env, location: location)
    end

  when ::RBS::AST::Directives::Use::SingleClause
    if loc_key == :type_name
      location = head.location&.[](:type_name) or raise
      content_for_type_name(head.type_name.absolute!, env: env, location: location)
    end

  when ::RBS::AST::Directives::Use::WildcardClause
    if loc_key == :namespace
      location = head.location&.[](:namespace) or raise
      content_for_type_name(head.namespace.to_type_name.absolute!, env: env, location: location)
    end
  end
end

#content_for_type_name(type_name, env:, location:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/steep/services/hover_provider/rbs.rb', line 63

def content_for_type_name(type_name, env:, location:)
  case
  when type_name.alias?
    alias_decl = env.type_alias_decls[type_name]&.decl or return
    TypeAliasContent.new(location: location, decl: alias_decl)
  when type_name.interface?
    interface_decl = env.interface_decls[type_name]&.decl or return
    InterfaceTypeContent.new(location: location, decl: interface_decl)
  when type_name.class?
    class_entry = env.module_class_entry(type_name) or return

    case class_entry
    when ::RBS::Environment::ClassEntry, ::RBS::Environment::ModuleEntry
      class_decl = class_entry.primary_decl
    when ::RBS::Environment::ClassAliasEntry, ::RBS::Environment::ModuleAliasEntry
      class_decl = class_entry.decl
    end

    ClassTypeContent.new(location: location, decl: class_decl)
  end
end

#projectObject



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

def project
  service.project
end