Class: Steep::Index::SignatureSymbolProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/index/signature_symbol_provider.rb

Defined Under Namespace

Classes: SymbolInformation

Constant Summary collapse

LSP =
LanguageServer::Protocol

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project:, assignment:) ⇒ SignatureSymbolProvider

Returns a new instance of SignatureSymbolProvider.



13
14
15
16
17
# File 'lib/steep/index/signature_symbol_provider.rb', line 13

def initialize(project:, assignment:)
  @indexes = {}
  @project = project
  @assignment = assignment
end

Instance Attribute Details

#assignmentObject (readonly)

Returns the value of attribute assignment.



11
12
13
# File 'lib/steep/index/signature_symbol_provider.rb', line 11

def assignment
  @assignment
end

#indexesObject (readonly)

Returns the value of attribute indexes.



10
11
12
# File 'lib/steep/index/signature_symbol_provider.rb', line 10

def indexes
  @indexes
end

#projectObject (readonly)

Returns the value of attribute project.



9
10
11
# File 'lib/steep/index/signature_symbol_provider.rb', line 9

def project
  @project
end

Class Method Details

.test_method_name(query, method_name) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/steep/index/signature_symbol_provider.rb', line 33

def self.test_method_name(query, method_name)
  case
  when query == ""
    true
  else
    method_name.to_s.upcase.include?(query.upcase)
  end
end

.test_type_name(query, type_name) ⇒ Object Also known as: test_const_name, test_global_name



19
20
21
22
23
24
25
26
# File 'lib/steep/index/signature_symbol_provider.rb', line 19

def self.test_type_name(query, type_name)
  case
  when query == ""
    true
  else
    type_name.to_s.upcase.include?(query.upcase)
  end
end

Instance Method Details

#assigned?(target, path) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
# File 'lib/steep/index/signature_symbol_provider.rb', line 42

def assigned?(target, path)
  if path.relative?
    if project.targets.any? {|target| target.possible_signature_file?(path) }
      path = project.absolute_path(path)
    end
  end

  assignment =~ [target, path]
end

#query_symbol(query) ⇒ Object



52
53
54
55
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
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
# File 'lib/steep/index/signature_symbol_provider.rb', line 52

def query_symbol(query)
  symbols = [] #: Array[SymbolInformation]

  indexes.each do |target, index|
    index.each_entry do |entry|
      case entry
      when RBSIndex::TypeEntry
        next unless SignatureSymbolProvider.test_type_name(query, entry.type_name)

        container_name = entry.type_name.namespace.relative!.to_s.delete_suffix("::")
        name = entry.type_name.name.to_s

        entry.declarations.each do |decl|
          location = decl.location or next
          next unless assigned?(target, Pathname(location.buffer.name))

          case decl
          when RBS::AST::Declarations::Class
            symbols << SymbolInformation.new(
              name: name,
              location: location,
              kind: LSP::Constant::SymbolKind::CLASS,
              container_name: container_name
            )
          when RBS::AST::Ruby::Declarations::ClassDecl
            symbols << SymbolInformation.new(
              name: name,
              location: location,
              kind: LSP::Constant::SymbolKind::CLASS,
              container_name: container_name
            )
          when RBS::AST::Declarations::Module
            symbols << SymbolInformation.new(
              name: name,
              location: location,
              kind: LSP::Constant::SymbolKind::MODULE,
              container_name: container_name
            )
          when RBS::AST::Ruby::Declarations::ModuleDecl
            symbols << SymbolInformation.new(
              name: name,
              location: location,
              kind: LSP::Constant::SymbolKind::MODULE,
              container_name: container_name
            )
          when RBS::AST::Declarations::Interface
            symbols << SymbolInformation.new(
              name: name,
              location: location,
              kind: LSP::Constant::SymbolKind::INTERFACE,
              container_name: container_name
            )
          when RBS::AST::Declarations::TypeAlias
            symbols << SymbolInformation.new(
              name: name,
              location: location,
              kind: LSP::Constant::SymbolKind::ENUM,
              container_name: container_name
            )
          end
        end
      when RBSIndex::MethodEntry
        next unless SignatureSymbolProvider.test_method_name(query, entry.method_name)

        name = case entry.method_name
               when InstanceMethodName
                 "##{entry.method_name.method_name}"
               when SingletonMethodName
                 ".#{entry.method_name.method_name}"
               else
                 raise
               end
        container_name = entry.method_name.type_name.relative!.to_s

        entry.declarations.each do |decl|
          location = decl.location or next
          next unless assigned?(target, Pathname(location.buffer.name))

          case decl
          when RBS::AST::Members::MethodDefinition
            symbols << SymbolInformation.new(
              name: name,
              location: location,
              kind: LSP::Constant::SymbolKind::METHOD,
              container_name: container_name
            )
          when RBS::AST::Members::Alias
            symbols << SymbolInformation.new(
              name: name,
              location: location,
              kind: LSP::Constant::SymbolKind::METHOD,
              container_name: container_name
            )
          when RBS::AST::Members::AttrAccessor, RBS::AST::Members::AttrReader, RBS::AST::Members::AttrWriter
            symbols << SymbolInformation.new(
              name: name,
              location: location,
              kind: LSP::Constant::SymbolKind::PROPERTY,
              container_name: container_name
            )

            if decl.ivar_name
              symbols << SymbolInformation.new(
                name: decl.ivar_name.to_s,
                location: location,
                kind: LSP::Constant::SymbolKind::FIELD,
                container_name: container_name
              )
            end
          when RBS::AST::Ruby::Members::DefMember
            symbols << SymbolInformation.new(
              name: name,
              location: location,
              kind: LSP::Constant::SymbolKind::METHOD,
              container_name: container_name
            )
          end
        end
      when RBSIndex::ConstantEntry
        next unless SignatureSymbolProvider.test_const_name(query, entry.const_name)

        entry.declarations.each do |decl|
          loc = decl.location or next
          next unless assigned?(target, Pathname(loc.buffer.name))

          symbols << SymbolInformation.new(
            name: entry.const_name.name.to_s,
            location: loc,
            kind: LSP::Constant::SymbolKind::CONSTANT,
            container_name: entry.const_name.namespace.relative!.to_s.delete_suffix("::")
          )
        end
      when RBSIndex::GlobalEntry
        next unless SignatureSymbolProvider.test_global_name(query, entry.global_name)

        entry.declarations.each do |decl|
          next unless decl.location
          next unless assigned?(target, Pathname(decl.location.buffer.name))

          symbols << SymbolInformation.new(
            name: decl.name.to_s,
            location: decl.location,
            kind: LSP::Constant::SymbolKind::VARIABLE,
            container_name: nil
          )
        end
      end
    end
  end

  symbols.uniq {|symbol| [symbol.name, symbol.location] }.sort_by {|symbol| symbol.name.to_s }
end