# typed: strict# frozen_string_literal: truerequire"ruby_lsp/listeners/signature_help"moduleRubyLspmoduleRequests# ## The [signature help# request](https://microsoft.github.io/language-server-protocol/specification#textDocument_signatureHelp) 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## ```ruby# class Foo# def bar(a, b, c)# end## def baz# bar( # -> Signature help will show the parameters of `bar`# end# ```classSignatureHelp<RequestextendT::Sigclass<<selfextendT::Sigsig{returns(Interface::SignatureHelpOptions)}defprovider# Identifier characters are automatically included, such as A-Z, a-z, 0-9, _, * or :Interface::SignatureHelpOptions.new(trigger_characters: ["("," ",","],)endendsigdoparams(document: Document,index: RubyIndexer::Index,position: T::Hash[Symbol,T.untyped],context: T.nilable(T::Hash[Symbol,T.untyped]),dispatcher: Prism::Dispatcher,).voidenddefinitialize(document,index,position,context,dispatcher)super()target,parent,nesting=document.locate_node({line: position[:line],character: position[:character]},node_types: [Prism::CallNode],)target=adjust_for_nested_target(target,parent,position)@target=T.let(target,T.nilable(Prism::Node))@dispatcher=dispatcher@response_builder=T.let(ResponseBuilders::SignatureHelp.new,ResponseBuilders::SignatureHelp)Listeners::SignatureHelp.new(@response_builder,nesting,index,dispatcher)endsig{override.returns(T.nilable(Interface::SignatureHelp))}defperformreturnunless@target@dispatcher.dispatch_once(@target)@response_builder.responseendprivate# Adjust the target of signature help in the case where we have nested method calls. This is necessary so that we# select the right target in a situation like this:## foo(another_method_call)## In that case, we want to provide signature help for `foo` and not `another_method_call`.sigdoparams(target: T.nilable(Prism::Node),parent: T.nilable(Prism::Node),position: T::Hash[Symbol,T.untyped],).returns(T.nilable(Prism::Node))enddefadjust_for_nested_target(target,parent,position)# If the parent node is not a method call, then make no adjustmentsreturntargetunlessparent.is_a?(Prism::CallNode)# If the parent is a method call, but the target isn't, then return the parentreturnparentunlesstarget.is_a?(Prism::CallNode)# If both are method calls, we check the arguments of the inner method call. If there are no arguments, then# we're providing signature help for the outer method call.## If there are arguments, then we check if the arguments node covers the requested position. If it doesn't# cover, then we're providing signature help for the outer method call.arguments=target.argumentsarguments.nil?||!node_covers?(arguments,position)?parent:targetendsig{params(node: Prism::Node,position: T::Hash[Symbol,T.untyped]).returns(T::Boolean)}defnode_covers?(node,position)location=node.locationstart_line=location.start_line-1start_character=location.start_columnend_line=location.end_line-1end_character=location.end_columnstart_covered=start_line<position[:line]||(start_line==position[:line]&&start_character<=position[:character])end_covered=end_line>position[:line]||(end_line==position[:line]&&end_character>=position[:character])start_covered&&end_coveredendendendend