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(decls:) ⇒ Locator

Returns a new instance of Locator.



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

def initialize(decls:)
  @decls = decls
end

Instance Attribute Details

#declsObject (readonly)

Returns the value of attribute decls.



3
4
5
# File 'lib/rbs/locator.rb', line 3

def decls
  @decls
end

Instance Method Details

#bufferObject



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

def buffer
  decls[0].location&.buffer or raise
end

#find(line:, column:) ⇒ Object



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

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

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

  []
end

#find2(line:, column:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rbs/locator.rb', line 24

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



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

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::Alias
      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_loc(pos, location:, array:) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rbs/locator.rb', line 182

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



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 108

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

    case member
    when AST::Members::MethodDefinition
      member.types.each do |method_type|
        find_in_method_type(pos, array: array, method_type: 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



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

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rbs/locator.rb', line 166

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rbs/locator.rb', line 149

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



209
210
211
212
213
214
215
# File 'lib/rbs/locator.rb', line 209

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