Class: RBS::Locator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Locator.



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

#bufferObject (readonly)

Returns the value of attribute buffer.



5
6
7
# File 'lib/rbs/locator.rb', line 5

def buffer
  @buffer
end

#declsObject (readonly)

Returns the value of attribute decls.



5
6
7
# File 'lib/rbs/locator.rb', line 5

def decls
  @decls
end

#dirsObject (readonly)

Returns the value of attribute dirs.



5
6
7
# File 'lib/rbs/locator.rb', line 5

def dirs
  @dirs
end

Instance Method Details

#find(line:, column:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rbs/locator.rb', line 13

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:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rbs/locator.rb', line 29

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:) ⇒ Object



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

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) ⇒ Object



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

def find_in_directive(pos, dir, array)
  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:) ⇒ Object



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

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:) ⇒ Object



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

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:) ⇒ Object



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

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:) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rbs/locator.rb', line 187

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:) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rbs/locator.rb', line 170

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
      find_in_type(pos, type: upper_bound, array: array) or
        find_in_loc(pos, location: type_param.location, array: array)
    else
      find_in_loc(pos, location: type_param.location, array: array)
    end

    true
  else
    false
  end
end

#test_loc(pos, location:) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/rbs/locator.rb', line 230

def test_loc(pos, location:)
  if location
    location.range === pos
  else
    false
  end
end