Class: RBS::Locator
- Inherits:
-
Object
- Object
- RBS::Locator
- Defined in:
- lib/rbs/locator.rb
Instance Attribute Summary collapse
-
#decls ⇒ Object
readonly
Returns the value of attribute decls.
Instance Method Summary collapse
- #buffer ⇒ Object
- #find(line:, column:) ⇒ Object
- #find2(line:, column:) ⇒ Object
- #find_in_decl(pos, decl:, array:) ⇒ Object
- #find_in_loc(pos, location:, array:) ⇒ Object
- #find_in_member(pos, member:, array:) ⇒ Object
- #find_in_method_type(pos, method_type:, array:) ⇒ Object
- #find_in_type(pos, type:, array:) ⇒ Object
-
#initialize(decls:) ⇒ Locator
constructor
A new instance of Locator.
- #test_loc(pos, location:) ⇒ Object
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
#decls ⇒ Object (readonly)
Returns the value of attribute decls.
3 4 5 |
# File 'lib/rbs/locator.rb', line 3 def decls @decls end |
Instance Method Details
#buffer ⇒ Object
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 107 108 109 110 111 112 113 114 115 116 117 118 |
# 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| if test_loc(pos, location: param.location) array.unshift(param) find_in_loc(pos, array: array, location: param.location) return true end 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| if test_loc(pos, location: param.location) array.unshift(param) find_in_loc(pos, array: array, location: param.location) return true end 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| if test_loc(pos, location: param.location) array.unshift(param) find_in_loc(pos, array: array, location: param.location) return true end 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
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 |
# File 'lib/rbs/locator.rb', line 173 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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rbs/locator.rb', line 120 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
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/rbs/locator.rb', line 143 def find_in_method_type(pos, method_type:, array:) if test_loc(pos, location: method_type.location) array.unshift(method_type) 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
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/rbs/locator.rb', line 157 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 |
#test_loc(pos, location:) ⇒ Object
200 201 202 203 204 205 206 |
# File 'lib/rbs/locator.rb', line 200 def test_loc(pos, location:) if location location.range === pos else false end end |