Class: Steep::Services::SignatureHelpProvider

Inherits:
Object
  • Object
show all
Includes:
NodeHelper
Defined in:
lib/steep/services/signature_help_provider.rb

Constant Summary collapse

MethodCall =
TypeInference::MethodCall
Item =
_ = Struct.new(:method_type, :comment, :active_parameter) do
  # @implements Item

  def parameters
    if method_type.type.is_a?(RBS::Types::Function)
      arguments = [] #: Array[String]
      arguments.push(*method_type.type.required_positionals.map(&:to_s))
      arguments.push(*method_type.type.optional_positionals.map {|p| "?#{p}"})
      arguments.push("*#{method_type.type.rest_positionals}") if method_type.type.rest_positionals
      arguments.push(*method_type.type.trailing_positionals.map(&:to_s))
      arguments.push(*method_type.type.required_keywords.map {|name, param| "#{name}: #{param}" })
      arguments.push(*method_type.type.optional_keywords.map {|name, param| "?#{name}: #{param}" })
      arguments.push("**#{method_type.type.rest_keywords}") if method_type.type.rest_keywords
      arguments
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NodeHelper

#clone_node, #deconstruct_case_node, #deconstruct_case_node!, #deconstruct_if_node, #deconstruct_if_node!, #deconstruct_resbody_node, #deconstruct_resbody_node!, #deconstruct_rescue_node, #deconstruct_rescue_node!, #deconstruct_send_node, #deconstruct_send_node!, #deconstruct_sendish_and_block_nodes, #deconstruct_when_node, #deconstruct_when_node!, #deconstruct_whileish_node, #deconstruct_whileish_node!, #each_child_node, #each_descendant_node, #private_send?, #test_case_node, #test_if_node, #test_resbody_node, #test_rescue_node, #test_send_node, #test_when_node, #test_whileish_node, #value_node?

Constructor Details

#initialize(source:, subtyping:) ⇒ SignatureHelpProvider

Returns a new instance of SignatureHelpProvider.



32
33
34
35
36
# File 'lib/steep/services/signature_help_provider.rb', line 32

def initialize(source:, subtyping:)
  @source = source
  @subtyping = subtyping
  @buffer = source.buffer
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



26
27
28
# File 'lib/steep/services/signature_help_provider.rb', line 26

def buffer
  @buffer
end

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/steep/services/signature_help_provider.rb', line 26

def path
  @path
end

#sourceObject (readonly)

Returns the value of attribute source.



26
27
28
# File 'lib/steep/services/signature_help_provider.rb', line 26

def source
  @source
end

#subtypingObject (readonly)

Returns the value of attribute subtyping.



26
27
28
# File 'lib/steep/services/signature_help_provider.rb', line 26

def subtyping
  @subtyping
end

#typingObject (readonly)

Returns the value of attribute typing.



26
27
28
# File 'lib/steep/services/signature_help_provider.rb', line 26

def typing
  @typing
end

Instance Method Details

#active_parameter_for(method_type, argument_nodes, last_argument_nodes, node) ⇒ Object



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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/steep/services/signature_help_provider.rb', line 138

def active_parameter_for(method_type, argument_nodes, last_argument_nodes, node)
  return unless method_type
  return unless method_type.type.is_a?(RBS::Types::Function)

  positionals = method_type.type.required_positionals.size + method_type.type.optional_positionals.size + (method_type.type.rest_positionals ? 1 : 0) + method_type.type.trailing_positionals.size

  if argument_nodes.size == 1
    # Cursor is not on the argument (maybe on comma after argument)
    return 0 if last_argument_nodes.nil?  # No arguments

    case last_argument_nodes.fetch(-2).type
    when :splat
      method_type.type.required_positionals.size + method_type.type.optional_positionals.size + 1 if method_type.type.rest_positionals
    when :kwargs
      case last_argument_nodes.fetch(-3).type
      when :pair
        argname = last_argument_nodes.fetch(-3).children.first.children.first
        if method_type.type.required_keywords.key?(argname)
          positionals + method_type.type.required_keywords.keys.index(argname).to_i + 1
        elsif method_type.type.optional_keywords.key?(argname)
          positionals + method_type.type.required_keywords.size + method_type.type.optional_keywords.keys.index(argname).to_i + 1
        elsif method_type.type.rest_keywords
          positionals + method_type.type.required_keywords.size + method_type.type.optional_keywords.size
        end
      when :kwsplat
        positionals + method_type.type.required_keywords.size + method_type.type.optional_keywords.size if method_type.type.rest_keywords
      end
    else
      pos = (node.children[2...] || raise).index { |c| c.location == last_argument_nodes.fetch(-2).location }.to_i
      if method_type.type.rest_positionals
        [pos + 1, positionals - 1].min
      else
        [pos + 1, positionals].min
      end
    end
  else
    # Cursor is on the argument
    case argument_nodes.fetch(-2).type
    when :splat
      method_type.type.required_positionals.size + method_type.type.optional_positionals.size if method_type.type.rest_positionals
    when :kwargs
      if argument_nodes[-3]
        case argument_nodes.fetch(-3).type
        when :pair
          argname = argument_nodes.fetch(-3).children.first.children.first
          if method_type.type.required_keywords.key?(argname)
            positionals + method_type.type.required_keywords.keys.index(argname).to_i
          elsif method_type.type.optional_keywords.key?(argname)
            positionals + method_type.type.required_keywords.size + method_type.type.optional_keywords.keys.index(argname).to_i
          elsif method_type.type.rest_keywords
            positionals + method_type.type.required_keywords.size + method_type.type.optional_keywords.size
          end
        when :kwsplat
          positionals + method_type.type.required_keywords.size + method_type.type.optional_keywords.size if method_type.type.rest_keywords
        end
      end
    else
      pos = (node.children[2...] || raise).index { |c| c.location == argument_nodes.fetch(-2).location }.to_i
      [pos, positionals - 1].min
    end
  end
end

#envObject



28
29
30
# File 'lib/steep/services/signature_help_provider.rb', line 28

def env
  subtyping.factory.env
end

#last_argument_nodes_for(argument_nodes:, line:, column:) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/steep/services/signature_help_provider.rb', line 83

def last_argument_nodes_for(argument_nodes:, line:, column:)
  last = argument_nodes.last or raise
  return unless last.children[2]  # No arguments
  return argument_nodes if argument_nodes.size > 1  # Cursor is on the last argument

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

  while true
    pos -= 1
    line, column = buffer.pos_to_loc(pos)
    nodes = source.find_nodes(line: line, column: column)
    return unless nodes

    index = nodes.index { |n| n.type == :send || n.type == :csend }
    return nodes[..index] if index.to_i > 0
  end
end

#run(line:, column:) ⇒ Object



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
# File 'lib/steep/services/signature_help_provider.rb', line 38

def run(line:, column:)
  nodes = source.find_nodes(line: line, column: column)

  return unless nodes

  typing = type_check!(line: line, column: column)
  argument_nodes = [] #: Array[Parser::AST::Node]

  while true
    node = nodes.shift()
    parent = nodes.first

    node or return
    argument_nodes << node

    if node.type == :send || node.type == :csend
      pos = buffer.loc_to_pos([line, column])
      begin_loc = (_ = node.loc).begin #: Parser::Source::Range?
      end_loc = (_ = node.loc).end #: Parser::Source::Range?

      if begin_loc && end_loc
        if begin_loc.end_pos <= pos && pos <= end_loc.begin_pos
          # Given position is between open/close parens of args of send node

          if parent && (parent.type == :block || parent.type == :numblock) && node.equal?(parent.children[0])
            send_node = parent
          else
            send_node = node
          end

          last_argument_nodes = last_argument_nodes_for(argument_nodes: argument_nodes, line: line, column: column)
          return signature_help_for(send_node, argument_nodes, last_argument_nodes, typing)
        end
      end
    end
  end
end

#signature_help_for(node, argument, last_argument, typing) ⇒ Object



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
# File 'lib/steep/services/signature_help_provider.rb', line 101

def signature_help_for(node, argument, last_argument, typing)
  call = typing.call_of(node: node)
  context = typing.cursor_context.context or raise

  items = [] #: Array[Item]
  index = nil #: Integer?

  case call
  when MethodCall::Typed, MethodCall::Error
    type = call.receiver_type
    config = Interface::Builder::Config.new(self_type: context.self_type, variable_bounds: context.variable_context.upper_bounds)

    if shape = subtyping.builder.shape(type, config)
      shape = shape.public_shape if private_send?(node)

      if method = shape.methods[call.method_name]
        method.overloads.each.with_index do |overload, i|
          defn = overload.method_defs[0]

          active_parameter = active_parameter_for(defn&.type, argument, last_argument, node)
          items << Item.new(subtyping.factory.method_type_1(overload.method_type), defn&.comment, active_parameter)

          if call.is_a?(MethodCall::Typed)
            if call.method_decls.intersect?(overload.method_decls(call.method_name).to_set)
              index = i
            end
          end
        end
      end
    end
  when MethodCall::Untyped, MethodCall::NoMethodError
    return
  end

  [items, index]
end

#type_check!(line:, column:) ⇒ Object



76
77
78
79
80
81
# File 'lib/steep/services/signature_help_provider.rb', line 76

def type_check!(line:, column:)
  source = self.source.without_unrelated_defs(line: line, column: column)
  resolver = RBS::Resolver::ConstantResolver.new(builder: subtyping.factory.definition_builder)
  pos = self.source.buffer.loc_to_pos([line, column])
  TypeCheckService.type_check(source: source, subtyping: subtyping, constant_resolver: resolver, cursor: pos)
end