Class: Steep::Services::CompletionProvider::RBS

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, signature) ⇒ RBS

Returns a new instance of RBS.



7
8
9
10
# File 'lib/steep/services/completion_provider/rbs.rb', line 7

def initialize(path, signature)
  @path = path
  @signature = signature
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/steep/services/completion_provider/rbs.rb', line 5

def path
  @path
end

#projectObject (readonly)

Returns the value of attribute project.



5
6
7
# File 'lib/steep/services/completion_provider/rbs.rb', line 5

def project
  @project
end

#signatureObject (readonly)

Returns the value of attribute signature.



5
6
7
# File 'lib/steep/services/completion_provider/rbs.rb', line 5

def signature
  @signature
end

Instance Method Details

#run(line, column) ⇒ Object



12
13
14
15
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
62
63
64
65
66
67
68
69
70
# File 'lib/steep/services/completion_provider/rbs.rb', line 12

def run(line, column)
  context = nil #: RBS::Resolver::context

  case signature.status
  when Services::SignatureService::SyntaxErrorStatus, Services::SignatureService::AncestorErrorStatus
    if source = signature.latest_env.each_rbs_source.find { _1.buffer.name == path }
      dirs = source.directives
    else
      dirs = [] #: Array[RBS::AST::Directives::t]
    end
  else
    file = signature.files.fetch(path)
    file.is_a?(Services::SignatureService::RBSFileStatus) or raise
    source = file.source
    source.is_a?(::RBS::Source::RBS) or raise
    buffer = source.buffer
    dirs = source.directives
    decls = source.declarations

    locator = ::RBS::Locator.new(buffer: buffer, dirs: dirs, decls: decls)

    _hd, tail = locator.find2(line: line, column: column)
    tail ||= [] #: Array[RBS::Locator::component]

    tail.reverse_each do |t|
      case t
      when ::RBS::AST::Declarations::Module, ::RBS::AST::Declarations::Class
        if (last_type_name = context&.[](1)).is_a?(::RBS::TypeName)
          context = [context, last_type_name + t.name]
        else
          context = [context, t.name.absolute!]
        end
      end
    end
  end

  content =
    case file = signature.files.fetch(path)
    when ::RBS::Source::Ruby
      file.buffer.content
    when Services::SignatureService::RBSFileStatus
      file.content
    end
  buffer = ::RBS::Buffer.new(name: path, content: content)
  prefix = Services::CompletionProvider::TypeName::Prefix.parse(buffer, line: line, column: column)

  completion = Services::CompletionProvider::TypeName.new(env: signature.latest_env, context: context, dirs: dirs)
  type_names = completion.find_type_names(prefix)
  prefix_size = prefix ? prefix.size : 0

  [
    prefix_size,
    type_names.filter_map do |type_name|
      if (absolute_name, relative_name = completion.resolve_name_in_context(type_name))
        [absolute_name, relative_name]
      end
    end
  ]
end