Class: RBS::Locator

Inherits:
Object
  • Object
show all
Defined in:
sig/locator.rbs,
lib/rbs/locator.rb

Overview

Locator helps finding RBS elements based on locations in the RBS source text.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer:, dirs:, decls:) ⇒ Locator

Returns a new instance of Locator.

Parameters:

  • buffer: (Buffer)
  • decls: (Array[AST::Declarations::t])
  • dirs: (Array[AST::Directives::t])


7
8
9
10
11
# File 'lib/rbs/locator.rb', line 7

def initialize(buffer:, dirs:, decls:)
  @buffer = buffer
  @dirs = dirs
  @decls = decls
end

Instance Attribute Details

#bufferBuffer (readonly)

The buffer the location points to

Returns:



18
19
20
# File 'sig/locator.rbs', line 18

def buffer
  @buffer
end

#declsArray[AST::Declarations::t] (readonly)

Array of top-level declarations.

Returns:

  • (Array[AST::Declarations::t])


22
23
24
# File 'sig/locator.rbs', line 22

def decls
  @decls
end

#dirsArray[AST::Directives::t] (readonly)

Array of directives.

Returns:

  • (Array[AST::Directives::t])


26
27
28
# File 'sig/locator.rbs', line 26

def dirs
  @dirs
end

Instance Method Details

#find(line:, column:) ⇒ Array[component]

Returns list of components. Inner component comes first.

Parameters:

  • line: (Integer)
  • column: (Integer)

Returns:

  • (Array[component])


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'sig/locator.rbs', line 33

def find(line:, column:)
  pos = buffer.loc_to_pos([line, column])

  dirs.each do |dir|
    array = [] #: Array[component]
    find_in_directive(pos, dir, array) and return array
  end

  decls.each do |decl|
    array = [] #: Array[component]
    find_in_decl(pos, decl: decl, array: array) and return array
  end

  []
end

#find2(line:, column:) ⇒ [Symbol?, Array[component]]?

Returns pair of the inner most symbol and outer components. It ensures the array starts with a AST/type component.

Parameters:

  • line: (Integer)
  • column: (Integer)

Returns:

  • ([Symbol?, Array[component]], nil)


38
39
40
41
42
43
44
45
46
47
48
49
# File 'sig/locator.rbs', line 38

def find2(line:, column:)
  path = find(line: line, column: column)

  return if path.empty?

  hd, *tl = path
  if hd.is_a?(Symbol)
    [hd, tl]
  else
    [nil, path]
  end
end

#find_in_decl(pos, decl:, array:) ⇒ Boolean

Parameters:

  • pos (Integer)
  • decl: (AST::Declarations::t)
  • array: (Array[component])

Returns:

  • (Boolean)


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
# File 'lib/rbs/locator.rb', line 60

def find_in_decl(pos, decl:, array:)
  if test_loc(pos, location: decl.location)
    array.unshift(decl)

    case decl
    when AST::Declarations::Class
      decl.type_params.each do |param|
        find_in_type_param(pos, type_param: param, array: array) and return true
      end

      if super_class = decl.super_class
        if test_loc(pos, location: super_class.location)
          array.unshift(super_class)
          find_in_loc(pos, array: array, location: super_class.location)
          return true
        end
      end

      decl.each_decl do |decl_|
        find_in_decl(pos, decl: decl_, array: array) and return true
      end

      decl.each_member do |member|
        find_in_member(pos, array: array, member: member) and return true
      end

    when AST::Declarations::Module
      decl.type_params.each do |param|
        find_in_type_param(pos, type_param: param, array: array) and return true
      end

      decl.self_types.each do |self_type|
        if test_loc(pos, location: self_type.location)
          array.unshift(self_type)
          find_in_loc(pos, array: array, location: self_type.location)
          return true
        end
      end

      decl.each_decl do |decl_|
        find_in_decl(pos, decl: decl_, array: array) and return true
      end

      decl.each_member do |member|
        find_in_member(pos, array: array, member: member) and return true
      end

    when AST::Declarations::Interface
      decl.type_params.each do |param|
        find_in_type_param(pos, type_param: param, array: array) and return true
      end

      decl.members.each do |member|
        find_in_member(pos, array: array, member: member) and return true
      end

    when AST::Declarations::Constant, AST::Declarations::Global
      find_in_type(pos, array: array, type: decl.type) and return true

    when AST::Declarations::TypeAlias
      find_in_type(pos, array: array, type: decl.type) and return true
    end

    find_in_loc(pos, location: decl.location, array: array)

    true
  else
    false
  end
end

#find_in_directive(pos, dir, array) ⇒ Boolean

Parameters:

  • pos (Integer)
  • (AST::Directives::t)
  • (Array[component])

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rbs/locator.rb', line 42

def find_in_directive(pos, dir, array)
  return false unless dir.is_a?(AST::Directives::Use)

  if test_loc(pos, location: dir.location)
    array.unshift(dir)

    dir.clauses.each do |clause|
      if test_loc(pos, location: clause.location)
        array.unshift(clause)
        find_in_loc(pos, location: clause.location, array: array)
        return true
      end
    end
  end

  false
end

#find_in_loc(pos, location:, array:) ⇒ Boolean

Parameters:

  • pos (Integer)
  • location: (Location[untyped, untyped], nil)
  • array: (Array[component])

Returns:

  • (Boolean)


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
# File 'lib/rbs/locator.rb', line 212

def find_in_loc(pos, location:, array:)
  if test_loc(pos, location: location)
    if location.is_a?(Location)
      location.each_optional_key do |key|
        if loc = location[key]
          if loc.range === pos
            array.unshift(key)
            return true
          end
        end
      end

      location.each_required_key do |key|
        loc = location[key] or raise
        if loc.range === pos
          array.unshift(key)
          return true
        end
      end
    end

    true
  else
    false
  end
end

#find_in_member(pos, member:, array:) ⇒ Boolean

Parameters:

  • pos (Integer)
  • member: (AST::Members::t)
  • array: (Array[component])

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rbs/locator.rb', line 131

def find_in_member(pos, member:, array:)
  if test_loc(pos, location: member.location)
    array.unshift(member)

    case member
    when AST::Members::MethodDefinition
      member.overloads.each do |overload|
        find_in_method_type(pos, array: array, method_type: overload.method_type) and return true
      end
    when AST::Members::InstanceVariable, AST::Members::ClassInstanceVariable, AST::Members::ClassVariable
      find_in_type(pos, array: array, type: member.type) and return true
    when AST::Members::AttrReader, AST::Members::AttrWriter, AST::Members::AttrAccessor
      find_in_type(pos, array: array, type: member.type) and return true
    end

    find_in_loc(pos, location: member.location, array: array)

    true
  else
    false
  end
end

#find_in_method_type(pos, method_type:, array:) ⇒ Boolean

Parameters:

  • pos (Integer)
  • method_type: (MethodType)
  • array: (Array[component])

Returns:

  • (Boolean)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rbs/locator.rb', line 154

def find_in_method_type(pos, method_type:, array:)
  if test_loc(pos, location: method_type.location)
    array.unshift(method_type)

    method_type.type_params.each do |param|
      find_in_type_param(pos, type_param: param, array: array) and return true
    end

    method_type.each_type do |type|
      find_in_type(pos, array: array, type: type) and break
    end

    true
  else
    false
  end
end

#find_in_type(pos, type:, array:) ⇒ Boolean

Parameters:

  • pos (Integer)
  • type: (Types::t)
  • array: (Array[component])

Returns:

  • (Boolean)


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rbs/locator.rb', line 196

def find_in_type(pos, type:, array:)
  if test_loc(pos, location: type.location)
    array.unshift(type)

    type.each_type do |type_|
      find_in_type(pos, array: array, type: type_) and return true
    end

    find_in_loc(pos, array: array, location: type.location)

    true
  else
    false
  end
end

#find_in_type_param(pos, type_param:, array:) ⇒ Boolean

Parameters:

  • pos (Integer)
  • type_param: (AST::TypeParam)
  • array: (Array[component])

Returns:

  • (Boolean)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/rbs/locator.rb', line 172

def find_in_type_param(pos, type_param:, array:)
  if test_loc(pos, location: type_param.location)
    array.unshift(type_param)

    if upper_bound = type_param.upper_bound_type
      find_in_type(pos, type: upper_bound, array: array) and return true
    end

    if lower_bound = type_param.lower_bound_type
      find_in_type(pos, type: lower_bound, array: array) and return true
    end

    if default_type = type_param.default_type
      find_in_type(pos, type: default_type, array: array) and return true
    end

    find_in_loc(pos, location: type_param.location, array: array)

    true
  else
    false
  end
end

#test_loc(pos, location:) ⇒ Boolean

Parameters:

  • pos (Integer)
  • location: (Location[untyped, untyped], nil)

Returns:

  • (Boolean)


239
240
241
242
243
244
245
# File 'lib/rbs/locator.rb', line 239

def test_loc(pos, location:)
  if location
    location.start_pos <= pos && pos <= location.end_pos
  else
    false
  end
end