|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -require_relative "repl_completion/version" |
| 3 | +require_relative 'repl_completion/version' |
| 4 | +require_relative 'repl_completion/type_analyzer' |
| 5 | +require_relative 'repl_completion/result' |
4 | 6 |
|
5 | 7 | module ReplCompletion |
6 | | - class Error < StandardError; end |
7 | | - # Your code goes here... |
| 8 | + class << self |
| 9 | + attr_reader :last_analyze_error |
| 10 | + |
| 11 | + def rbs_load_error |
| 12 | + Types.rbs_load_error |
| 13 | + end |
| 14 | + |
| 15 | + def rbs_load_started? |
| 16 | + Types.rbs_load_started? |
| 17 | + end |
| 18 | + |
| 19 | + def rbs_loaded? |
| 20 | + !!Types.rbs_builder |
| 21 | + end |
| 22 | + |
| 23 | + def load_rbs |
| 24 | + Types.load_rbs_builder unless rbs_loaded? |
| 25 | + end |
| 26 | + |
| 27 | + def preload_rbs |
| 28 | + Types.preload_rbs_builder |
| 29 | + end |
| 30 | + |
| 31 | + def analyze(code, binding) |
| 32 | + preload_rbs |
| 33 | + begin |
| 34 | + verbose, $VERBOSE = $VERBOSE, nil |
| 35 | + result = analyze_code(code, binding) |
| 36 | + rescue Exception => e |
| 37 | + handle_error(e) |
| 38 | + ensure |
| 39 | + $VERBOSE = verbose |
| 40 | + end |
| 41 | + Result.new(result, binding) if result |
| 42 | + end |
| 43 | + |
| 44 | + def handle_error(e) |
| 45 | + @last_analyze_error = e |
| 46 | + end |
| 47 | + |
| 48 | + def info |
| 49 | + require 'rbs' |
| 50 | + prism_info = "Prism: #{Prism::VERSION}" |
| 51 | + rbs_info = "RBS: #{RBS::VERSION}" |
| 52 | + if rbs_load_error |
| 53 | + rbs_info << " #{rbs_load_error.inspect}" |
| 54 | + elsif !rbs_load_started? |
| 55 | + rbs_info << ' signatures not loaded' |
| 56 | + elsif !rbs_loaded? |
| 57 | + rbs_info << ' signatures loading' |
| 58 | + end |
| 59 | + "ReplCompletion: #{VERSION}, #{prism_info}, #{rbs_info}" |
| 60 | + end |
| 61 | + |
| 62 | + private |
| 63 | + |
| 64 | + def analyze_code(code, binding = Object::TOPLEVEL_BINDING) |
| 65 | + # Workaround for https://github.com/ruby/prism/issues/1592 |
| 66 | + return if code.match?(/%[qQ]\z/) |
| 67 | + |
| 68 | + ast = Prism.parse(code, scopes: [binding.local_variables]).value |
| 69 | + name = code[/(@@|@|\$)?\w*[!?=]?\z/] |
| 70 | + *parents, target_node = find_target ast, code.bytesize - name.bytesize |
| 71 | + return unless target_node |
| 72 | + |
| 73 | + calculate_scope = -> { TypeAnalyzer.calculate_target_type_scope(binding, parents, target_node).last } |
| 74 | + calculate_type_scope = ->(node) { TypeAnalyzer.calculate_target_type_scope binding, [*parents, target_node], node } |
| 75 | + |
| 76 | + case target_node |
| 77 | + when Prism::StringNode, Prism::InterpolatedStringNode |
| 78 | + call_node, args_node = parents.last(2) |
| 79 | + return unless call_node.is_a?(Prism::CallNode) && call_node.receiver.nil? |
| 80 | + return unless args_node.is_a?(Prism::ArgumentsNode) && args_node.arguments.size == 1 |
| 81 | + |
| 82 | + case call_node.name |
| 83 | + when :require |
| 84 | + [:require, name] |
| 85 | + when :require_relative |
| 86 | + [:require_relative, name] |
| 87 | + end |
| 88 | + when Prism::SymbolNode |
| 89 | + if parents.last.is_a? Prism::BlockArgumentNode # method(&:target) |
| 90 | + receiver_type, _scope = calculate_type_scope.call target_node |
| 91 | + [:call, name, receiver_type, false] |
| 92 | + else |
| 93 | + [:symbol, name] unless name.empty? |
| 94 | + end |
| 95 | + when Prism::CallNode |
| 96 | + return [:lvar_or_method, name, calculate_scope.call] if target_node.receiver.nil? |
| 97 | + |
| 98 | + self_call = target_node.receiver.is_a? Prism::SelfNode |
| 99 | + op = target_node.call_operator |
| 100 | + receiver_type, _scope = calculate_type_scope.call target_node.receiver |
| 101 | + receiver_type = receiver_type.nonnillable if op == '&.' |
| 102 | + [op == '::' ? :call_or_const : :call, name, receiver_type, self_call] |
| 103 | + when Prism::LocalVariableReadNode, Prism::LocalVariableTargetNode |
| 104 | + [:lvar_or_method, name, calculate_scope.call] |
| 105 | + when Prism::ConstantReadNode, Prism::ConstantTargetNode |
| 106 | + if parents.last.is_a? Prism::ConstantPathNode |
| 107 | + path_node = parents.last |
| 108 | + if path_node.parent # A::B |
| 109 | + receiver, scope = calculate_type_scope.call(path_node.parent) |
| 110 | + [:const, name, receiver, scope] |
| 111 | + else # ::A |
| 112 | + scope = calculate_scope.call |
| 113 | + [:const, name, Types::SingletonType.new(Object), scope] |
| 114 | + end |
| 115 | + else |
| 116 | + [:const, name, nil, calculate_scope.call] |
| 117 | + end |
| 118 | + when Prism::GlobalVariableReadNode, Prism::GlobalVariableTargetNode |
| 119 | + [:gvar, name, calculate_scope.call] |
| 120 | + when Prism::InstanceVariableReadNode, Prism::InstanceVariableTargetNode |
| 121 | + [:ivar, name, calculate_scope.call] |
| 122 | + when Prism::ClassVariableReadNode, Prism::ClassVariableTargetNode |
| 123 | + [:cvar, name, calculate_scope.call] |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + def find_target(node, position) |
| 128 | + location = ( |
| 129 | + case node |
| 130 | + when Prism::CallNode |
| 131 | + node.message_loc |
| 132 | + when Prism::SymbolNode |
| 133 | + node.value_loc |
| 134 | + when Prism::StringNode |
| 135 | + node.content_loc |
| 136 | + when Prism::InterpolatedStringNode |
| 137 | + node.closing_loc if node.parts.empty? |
| 138 | + end |
| 139 | + ) |
| 140 | + return [node] if location&.start_offset == position |
| 141 | + |
| 142 | + node.compact_child_nodes.each do |n| |
| 143 | + match = find_target(n, position) |
| 144 | + next unless match |
| 145 | + match.unshift node |
| 146 | + return match |
| 147 | + end |
| 148 | + |
| 149 | + [node] if node.location.start_offset == position |
| 150 | + end |
| 151 | + end |
8 | 152 | end |
0 commit comments