Class: RubyLsp::Requests::SignatureHelp

Inherits:
Listener
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/ruby_lsp/requests/signature_help.rb

Overview

Signature help demo

The signature help request displays information about the parameters of a method as you type an invocation.

Currently only supports methods invoked directly on self without taking inheritance into account.

Example

class Foo
 def bar(a, b, c)
 end

 def baz
   bar( # -> Signature help will show the parameters of `bar`
 end

Constant Summary collapse

ResponseType =
type_member { { fixed: T.nilable(T.any(Interface::SignatureHelp, T::Hash[Symbol, T.untyped])) } }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Listener

#response

Methods included from RubyLsp::Requests::Support::Common

#create_code_lens, #defined_in_gem?, #markdown_from_index_entries, #range_from_location, #range_from_node, #self_receiver?, #visible?

Constructor Details

#initialize(context, nesting, index, dispatcher) ⇒ SignatureHelp

Returns a new instance of SignatureHelp.



42
43
44
45
46
47
48
49
50
# File 'lib/ruby_lsp/requests/signature_help.rb', line 42

def initialize(context, nesting, index, dispatcher)
  @context = context
  @nesting = nesting
  @index = index
  @_response = T.let(nil, ResponseType)

  super(dispatcher)
  dispatcher.register(self, :on_call_node_enter)
end

Instance Attribute Details

#_response ⇒ Object (readonly)

Returns the value of attribute _response.



32
33
34
# File 'lib/ruby_lsp/requests/signature_help.rb', line 32

def _response
  @_response
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object



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
# File 'lib/ruby_lsp/requests/signature_help.rb', line 53

def on_call_node_enter(node)
  return if DependencyDetector.instance.typechecker
  return unless self_receiver?(node)

  message = node.message
  return unless message

  target_method = @index.resolve_method(message, @nesting.join("::"))
  return unless target_method

  parameters = target_method.parameters
  name = target_method.name

  # If the method doesn't have any parameters, there's no need to show signature help
  return if parameters.empty?

  label = "#{name}(#{parameters.map(&:decorated_name).join(", ")})"

  arguments_node = node.arguments
  arguments = arguments_node&.arguments || []
  active_parameter = (arguments.length - 1).clamp(0, parameters.length - 1)

  # If there are arguments, then we need to check if there's a trailing comma after the end of the last argument
  # to advance the active parameter to the next one
  if arguments_node &&
      node.slice.byteslice(arguments_node.location.end_offset - node.location.start_offset) == ","
    active_parameter += 1
  end

  @_response = Interface::SignatureHelp.new(
    signatures: [
      Interface::SignatureInformation.new(
        label: label,
        parameters: parameters.map { |param| Interface::ParameterInformation.new(label: param.name) },
        documentation: markdown_from_index_entries("", target_method),
      ),
    ],
    active_parameter: active_parameter,
  )
end