Skip to content

Commit

Permalink
Add ListenerBasedRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Dec 18, 2023
1 parent e2b7b82 commit c5f913f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/ruby_lsp/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module RubyLsp

module Requests
autoload :BaseRequest, "ruby_lsp/requests/base_request"
autoload :ListenerBasedRequest, "ruby_lsp/requests/listener_based_request"
autoload :DocumentSymbol, "ruby_lsp/requests/document_symbol"
autoload :DocumentLink, "ruby_lsp/requests/document_link"
autoload :Hover, "ruby_lsp/requests/hover"
Expand Down
10 changes: 6 additions & 4 deletions lib/ruby_lsp/requests/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Requests
# class Test < Minitest::Test
# end
# ```
class CodeLens
class CodeLens < ListenerBasedRequest
extend T::Sig
extend T::Generic

Expand All @@ -39,18 +39,20 @@ class CodeLens
).void
end
def initialize(uri, lenses_configuration, dispatcher)
@listeners = T.let(
listeners = T.let(
[Listeners::CodeLens.new(uri, lenses_configuration, dispatcher)],
T::Array[Listener[ResponseType]],
)

Addon.addons.each do |addon|
addon_listener = addon.create_code_lens_listener(uri, dispatcher)
@listeners << addon_listener if addon_listener
listeners << addon_listener if addon_listener
end

super(listeners)
end

sig { returns(ResponseType) }
sig { override.returns(ResponseType) }
def response
result = []
@listeners.each do |listener|
Expand Down
10 changes: 6 additions & 4 deletions lib/ruby_lsp/requests/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Requests
# require "some_gem/file" # <- Request go to definition on this string will take you to the file
# Product.new # <- Request go to definition on this class name will take you to its declaration.
# ```
class Definition
class Definition < ListenerBasedRequest
extend T::Sig
extend T::Generic

Expand All @@ -47,19 +47,21 @@ def initialize(document, uri, index, position)
target = parent if target.is_a?(Prism::ConstantReadNode) && parent.is_a?(Prism::ConstantPathNode)

dispatcher = Prism::Dispatcher.new
@listeners = T.let(
listeners = T.let(
[Listeners::Definition.new(uri, nesting, index, dispatcher)],
T::Array[Listener[T.nilable(T.any(T::Array[Interface::Location], Interface::Location))]],
)
Addon.addons.each do |addon|
addon_listener = addon.create_definition_listener(uri, nesting, index, dispatcher)
@listeners << addon_listener if addon_listener
listeners << addon_listener if addon_listener
end

super(listeners)

dispatcher.dispatch_once(target)
end

sig { returns(ResponseType) }
sig { override.returns(ResponseType) }
def response
result = []

Expand Down
10 changes: 6 additions & 4 deletions lib/ruby_lsp/requests/document_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,28 @@ module Requests
# end
# end
# ```
class DocumentSymbol
class DocumentSymbol < ListenerBasedRequest
extend T::Sig
extend T::Generic

ResponseType = type_member { { fixed: T::Array[Interface::DocumentSymbol] } }

sig { params(dispatcher: Prism::Dispatcher).void }
def initialize(dispatcher)
@listeners = T.let(
listeners = T.let(
[Listeners::DocumentSymbol.new(dispatcher)],
T::Array[Listener[ResponseType]],
)

Addon.addons.each do |addon|
addon_listener = addon.create_document_symbol_listener(dispatcher)
@listeners << addon_listener if addon_listener
listeners << addon_listener if addon_listener
end

super(listeners)
end

sig { returns(ResponseType) }
sig { override.returns(ResponseType) }
def response
@listeners.flat_map(&:response).compact
end
Expand Down
12 changes: 6 additions & 6 deletions lib/ruby_lsp/requests/hover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Requests
# ```ruby
# String # -> Hovering over the class reference will show all declaration locations and the documentation
# ```
class Hover
class Hover < ListenerBasedRequest
extend T::Sig
extend T::Generic

Expand All @@ -40,23 +40,23 @@ def initialize(document, index, position)
target = parent
end

@listeners = T.let([], T::Array[Listener[ResponseType]])

# Don't need to instantiate any listeners if there's no target
return unless target

dispatcher = Prism::Dispatcher.new

@listeners = T.let([Listeners::Hover.new(nesting, index, dispatcher)], T::Array[Listener[ResponseType]])
listeners = T.let([Listeners::Hover.new(nesting, index, dispatcher)], T::Array[Listener[ResponseType]])
Addon.addons.each do |addon|
addon_listener = addon.create_hover_listener(nesting, index, dispatcher)
@listeners << addon_listener if addon_listener
listeners << addon_listener if addon_listener
end

super(listeners)

dispatcher.dispatch_once(target)
end

sig { returns(ResponseType) }
sig { override.returns(ResponseType) }
def response
responses = @listeners.map(&:response).compact

Expand Down
24 changes: 24 additions & 0 deletions lib/ruby_lsp/requests/listener_based_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# typed: strict
# frozen_string_literal: true

module RubyLsp
module Requests
# :nodoc:
class ListenerBasedRequest
extend T::Sig
extend T::Generic

ResponseType = type_member

abstract!

sig { params(listeners: T::Array[Listener[ResponseType]]).void }
def initialize(listeners)
@listeners = listeners
end

sig { abstract.returns(ResponseType) }
def response; end
end
end
end

0 comments on commit c5f913f

Please sign in to comment.