Class: Steep::Project::CompletionProvider
- Inherits:
-
Object
- Object
- Steep::Project::CompletionProvider
- Defined in:
- lib/steep/project/completion_provider.rb
Defined Under Namespace
Classes: InstanceVariableItem, LocalVariableItem, MethodNameItem, Position, Range
Instance Attribute Summary collapse
-
#modified_text ⇒ Object
readonly
Returns the value of attribute modified_text.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#source_text ⇒ Object
readonly
Returns the value of attribute source_text.
-
#subtyping ⇒ Object
readonly
Returns the value of attribute subtyping.
-
#typing ⇒ Object
readonly
Returns the value of attribute typing.
Instance Method Summary collapse
- #at_end?(pos, of:) ⇒ Boolean
- #index_for(string, line:, column:) ⇒ Object
-
#initialize(source_text:, path:, subtyping:) ⇒ CompletionProvider
constructor
A new instance of CompletionProvider.
- #instance_variable_items_for_context(context, position:, prefix:, items:) ⇒ Object
- #items_for_atmark(position:) ⇒ Object
- #items_for_dot(position:) ⇒ Object
- #items_for_trigger(position:) ⇒ Object
- #local_variable_items_for_context(context, position:, prefix:, items:) ⇒ Object
- #method_items_for_receiver_type(type, include_private:, prefix:, position:, items:) ⇒ Object
- #range_for(position, prefix: "") ⇒ Object
- #range_from_loc(loc) ⇒ Object
- #run(line:, column:) ⇒ Object
- #type_check!(text) ⇒ Object
- #word_name?(name) ⇒ Boolean
Constructor Details
#initialize(source_text:, path:, subtyping:) ⇒ CompletionProvider
22 23 24 25 26 |
# File 'lib/steep/project/completion_provider.rb', line 22 def initialize(source_text:, path:, subtyping:) @source_text = source_text @path = path @subtyping = subtyping end |
Instance Attribute Details
#modified_text ⇒ Object (readonly)
Returns the value of attribute modified_text.
18 19 20 |
# File 'lib/steep/project/completion_provider.rb', line 18 def modified_text @modified_text end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
16 17 18 |
# File 'lib/steep/project/completion_provider.rb', line 16 def path @path end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
19 20 21 |
# File 'lib/steep/project/completion_provider.rb', line 19 def source @source end |
#source_text ⇒ Object (readonly)
Returns the value of attribute source_text.
15 16 17 |
# File 'lib/steep/project/completion_provider.rb', line 15 def source_text @source_text end |
#subtyping ⇒ Object (readonly)
Returns the value of attribute subtyping.
17 18 19 |
# File 'lib/steep/project/completion_provider.rb', line 17 def subtyping @subtyping end |
#typing ⇒ Object (readonly)
Returns the value of attribute typing.
20 21 22 |
# File 'lib/steep/project/completion_provider.rb', line 20 def typing @typing end |
Instance Method Details
#at_end?(pos, of:) ⇒ Boolean
84 85 86 |
# File 'lib/steep/project/completion_provider.rb', line 84 def at_end?(pos, of:) of.last_line == pos.line && of.last_column == pos.column end |
#index_for(string, line:, column:) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/steep/project/completion_provider.rb', line 282 def index_for(string, line:, column:) index = 0 string.each_line.with_index do |s, i| if i+1 == line index += column break else index += s.size end end index end |
#instance_variable_items_for_context(context, position:, prefix:, items:) ⇒ Object
271 272 273 274 275 276 277 278 279 280 |
# File 'lib/steep/project/completion_provider.rb', line 271 def instance_variable_items_for_context(context, position:, prefix:, items:) range = range_for(position, prefix: prefix) context.type_env.ivar_types.map do |name, type| if name.to_s.start_with?(prefix) items << InstanceVariableItem.new(identifier: name, range: range, type: type) end end end |
#items_for_atmark(position:) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/steep/project/completion_provider.rb', line 209 def items_for_atmark(position:) # @ ← shift_pos = position-1 node, *parents = source.find_nodes(line: shift_pos.line, column: shift_pos.column) node ||= source.node return [] unless node context = typing.context_at(line: position.line, column: position.column) items = [] instance_variable_items_for_context(context, prefix: "", position: position, items: items) items end |
#items_for_dot(position:) ⇒ Object
180 181 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/steep/project/completion_provider.rb', line 180 def items_for_dot(position:) # foo. ← shift_pos = position-1 node, *parents = source.find_nodes(line: shift_pos.line, column: shift_pos.column) node ||= source.node return [] unless node if at_end?(shift_pos, of: node.loc) context = typing.context_at(line: position.line, column: position.column) receiver_type = case (type = typing.type_of(node: node)) when AST::Types::Self context.self_type else type end items = [] method_items_for_receiver_type(receiver_type, include_private: false, prefix: "", position: position, items: items) items else [] end end |
#items_for_trigger(position:) ⇒ Object
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 |
# File 'lib/steep/project/completion_provider.rb', line 96 def items_for_trigger(position:) node, *parents = source.find_nodes(line: position.line, column: position.column) node ||= source.node return [] unless node items = [] context = typing.context_at(line: position.line, column: position.column) case when node.type == :send && node.children[0] == nil && at_end?(position, of: node.loc.selector) # foo ← prefix = node.children[1].to_s method_items_for_receiver_type(context.self_type, include_private: true, prefix: prefix, position: position, items: items) local_variable_items_for_context(context, position: position, prefix: prefix, items: items) when node.type == :lvar && at_end?(position, of: node.loc) # foo ← (lvar) local_variable_items_for_context(context, position: position, prefix: node.children[0].name.to_s, items: items) when node.type == :send && node.children[0] && at_end?(position, of: node.loc.selector) # foo.ba ← receiver_type = case (type = typing.type_of(node: node.children[0])) when AST::Types::Self context.self_type else type end prefix = node.children[1].to_s method_items_for_receiver_type(receiver_type, include_private: false, prefix: prefix, position: position, items: items) when node.type == :const && node.children[0] == nil && at_end?(position, of: node.loc) # Foo ← (const) prefix = node.children[1].to_s method_items_for_receiver_type(context.self_type, include_private: false, prefix: prefix, position: position, items: items) when node.type == :send && at_end?(position, of: node.loc.dot) # foo.← ba receiver_type = case (type = typing.type_of(node: node.children[0])) when AST::Types::Self context.self_type else type end method_items_for_receiver_type(receiver_type, include_private: false, prefix: "", position: position, items: items) when node.type == :ivar && at_end?(position, of: node.loc) # @fo ← instance_variable_items_for_context(context, position: position, prefix: node.children[0].to_s, items: items) else method_items_for_receiver_type(context.self_type, include_private: true, prefix: "", position: position, items: items) local_variable_items_for_context(context, position: position, prefix: "", items: items) instance_variable_items_for_context(context, position: position, prefix: "", items: items) end items end |
#local_variable_items_for_context(context, position:, prefix:, items:) ⇒ Object
260 261 262 263 264 265 266 267 268 269 |
# File 'lib/steep/project/completion_provider.rb', line 260 def local_variable_items_for_context(context, position:, prefix:, items:) range = range_for(position, prefix: prefix) context.lvar_env.each do |name, type| if name.to_s.start_with?(prefix) items << LocalVariableItem.new(identifier: name, range: range, type: type) end end end |
#method_items_for_receiver_type(type, include_private:, prefix:, position:, items:) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/steep/project/completion_provider.rb', line 223 def method_items_for_receiver_type(type, include_private:, prefix:, position:, items:) range = range_for(position, prefix: prefix) definition = case type when AST::Types::Name::Instance type_name = subtyping.factory.type_name_1(type.name) subtyping.factory.definition_builder.build_instance(type_name) when AST::Types::Name::Class, AST::Types::Name::Module type_name = subtyping.factory.type_name_1(type.name) subtyping.factory.definition_builder.build_singleton(type_name) when AST::Types::Name::Interface type_name = subtyping.factory.type_name_1(type.name) interface = subtyping.factory.env.find_class(type_name) subtyping.factory.definition_builder.build_interface(type_name, interface) end if definition definition.methods.each do |name, method| if include_private || method.public? if name.to_s.start_with?(prefix) if word_name?(name.to_s) method.method_types.each do |method_type| items << MethodNameItem.new(identifier: name, range: range, definition: method, method_type: method_type) end end end end end end end |
#range_for(position, prefix: "") ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/steep/project/completion_provider.rb', line 88 def range_for(position, prefix: "") if prefix.empty? Range.new(start: position, end: position) else Range.new(start: position - prefix.size, end: position) end end |
#range_from_loc(loc) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/steep/project/completion_provider.rb', line 77 def range_from_loc(loc) Range.new( start: Position.new(line: loc.line, column: loc.column), end: Position.new(line: loc.last_line, column: loc.last_line) ) end |
#run(line:, column:) ⇒ Object
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 |
# File 'lib/steep/project/completion_provider.rb', line 40 def run(line:, column:) source_text = self.source_text.dup index = index_for(source_text, line:line, column: column) possible_trigger = source_text[index-1] Steep.logger.debug "possible_trigger: #{possible_trigger.inspect}" position = Position.new(line: line, column: column) begin Steep.logger.tagged "completion_provider#run(line: #{line}, column: #{column})" do Steep.measure "type_check!" do type_check!(source_text) end end Steep.measure "completion item collection" do items_for_trigger(position: position) end rescue Parser::SyntaxError => exn Steep.logger.error "recovering syntax error: #{exn.inspect}" case possible_trigger when "." source_text[index-1] = " " type_check!(source_text) items_for_dot(position: position) when "@" source_text[index-1] = " " type_check!(source_text) items_for_atmark(position: position) else [] end end end |
#type_check!(text) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/steep/project/completion_provider.rb', line 28 def type_check!(text) @modified_text = text Steep.measure "parsing" do @source = SourceFile.parse(text, path: path, factory: subtyping.factory) end Steep.measure "typechecking" do @typing = SourceFile.type_check(source, subtyping: subtyping) end end |
#word_name?(name) ⇒ Boolean
256 257 258 |
# File 'lib/steep/project/completion_provider.rb', line 256 def word_name?(name) name =~ /\w/ end |