Skip to content

Commit

Permalink
Replace unnecessarily similar response builders with CollectionRespon…
Browse files Browse the repository at this point in the history
…seBuilder
  • Loading branch information
st0012 committed Jan 30, 2024
1 parent d6bd600 commit a358606
Show file tree
Hide file tree
Showing 25 changed files with 70 additions and 223 deletions.
4 changes: 2 additions & 2 deletions lib/ruby_lsp/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def name; end
# Creates a new CodeLens listener. This method is invoked on every CodeLens request
sig do
overridable.params(
response_builder: ResponseBuilders::CodeLens,
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens],
uri: URI::Generic,
dispatcher: Prism::Dispatcher,
).void
Expand Down Expand Up @@ -147,7 +147,7 @@ def create_semantic_highlighting_listener(response_builder, dispatcher); end
# Creates a new Definition listener. This method is invoked on every Definition request
sig do
overridable.params(
response_builder: ResponseBuilders::Definition,
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::Location],
uri: URI::Generic,
nesting: T::Array[String],
index: RubyIndexer::Index,
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/listeners/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CodeLens

sig do
params(
response_builder: ResponseBuilders::CodeLens,
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens],
uri: URI::Generic,
lenses_configuration: RequestConfig,
dispatcher: Prism::Dispatcher,
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/listeners/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Completion

sig do
params(
response_builder: ResponseBuilders::Completion,
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::CompletionItem],
index: RubyIndexer::Index,
nesting: T::Array[String],
typechecker_enabled: T::Boolean,
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/listeners/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Definition

sig do
params(
response_builder: ResponseBuilders::Definition,
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::Location],
uri: URI::Generic,
nesting: T::Array[String],
index: RubyIndexer::Index,
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/listeners/document_highlight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class DocumentHighlight

sig do
params(
response_builder: ResponseBuilders::DocumentHighlight,
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::DocumentHighlight],
target: T.nilable(Prism::Node),
parent: T.nilable(Prism::Node),
dispatcher: Prism::Dispatcher,
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/listeners/document_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def gem_paths

sig do
params(
response_builder: ResponseBuilders::DocumentLink,
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::DocumentLink],
uri: URI::Generic,
comments: T::Array[Prism::Comment],
dispatcher: Prism::Dispatcher,
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/listeners/folding_ranges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class FoldingRanges

sig do
params(
response_builder: ResponseBuilders::FoldingRanges,
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::FoldingRange],
comments: T::Array[Prism::Comment],
dispatcher: Prism::Dispatcher,
).void
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/listeners/inlay_hints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class InlayHints

sig do
params(
response_builder: ResponseBuilders::InlayHints,
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::InlayHint],
range: T::Range[Integer],
hints_configuration: RequestConfig,
dispatcher: Prism::Dispatcher,
Expand Down
5 changes: 4 additions & 1 deletion lib/ruby_lsp/requests/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def provider
).void
end
def initialize(uri, lenses_configuration, dispatcher)
@response_builder = T.let(ResponseBuilders::CodeLens.new, ResponseBuilders::CodeLens)
@response_builder = T.let(
ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens].new,
ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens],
)
super()
Listeners::CodeLens.new(@response_builder, uri, lenses_configuration, dispatcher)

Expand Down
5 changes: 4 additions & 1 deletion lib/ruby_lsp/requests/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def initialize(document, index, position, typechecker_enabled, dispatcher)
char_position,
node_types: [Prism::CallNode, Prism::ConstantReadNode, Prism::ConstantPathNode],
)
@response_builder = T.let(ResponseBuilders::Completion.new, ResponseBuilders::Completion)
@response_builder = T.let(
ResponseBuilders::CollectionResponseBuilder[Interface::CompletionItem].new,
ResponseBuilders::CollectionResponseBuilder[Interface::CompletionItem],
)

Listeners::Completion.new(@response_builder, index, nesting, typechecker_enabled, dispatcher)

Expand Down
5 changes: 4 additions & 1 deletion lib/ruby_lsp/requests/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class Definition < Request
end
def initialize(document, index, position, dispatcher, typechecker_enabled)
super()
@response_builder = T.let(ResponseBuilders::Definition.new, ResponseBuilders::Definition)
@response_builder = T.let(
ResponseBuilders::CollectionResponseBuilder[Interface::Location].new,
ResponseBuilders::CollectionResponseBuilder[Interface::Location],
)

target, parent, nesting = document.locate_node(
position,
Expand Down
5 changes: 4 additions & 1 deletion lib/ruby_lsp/requests/document_highlight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ class DocumentHighlight < Request
def initialize(document, position, dispatcher)
super()
target, parent = document.locate_node(position)
@response_builder = T.let(ResponseBuilders::DocumentHighlight.new, ResponseBuilders::DocumentHighlight)
@response_builder = T.let(
ResponseBuilders::CollectionResponseBuilder[Interface::DocumentHighlight].new,
ResponseBuilders::CollectionResponseBuilder[Interface::DocumentHighlight],
)
Listeners::DocumentHighlight.new(@response_builder, target, parent, dispatcher)
end

Expand Down
5 changes: 4 additions & 1 deletion lib/ruby_lsp/requests/document_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ def provider
end
def initialize(uri, comments, dispatcher)
super()
@response_builder = T.let(ResponseBuilders::DocumentLink.new, ResponseBuilders::DocumentLink)
@response_builder = T.let(
ResponseBuilders::CollectionResponseBuilder[Interface::DocumentLink].new,
ResponseBuilders::CollectionResponseBuilder[Interface::DocumentLink],
)
Listeners::DocumentLink.new(@response_builder, uri, comments, dispatcher)
end

Expand Down
5 changes: 4 additions & 1 deletion lib/ruby_lsp/requests/folding_ranges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def provider
sig { params(comments: T::Array[Prism::Comment], dispatcher: Prism::Dispatcher).void }
def initialize(comments, dispatcher)
super()
@response_builder = T.let(ResponseBuilders::FoldingRanges.new, ResponseBuilders::FoldingRanges)
@response_builder = T.let(
ResponseBuilders::CollectionResponseBuilder[Interface::FoldingRange].new,
ResponseBuilders::CollectionResponseBuilder[Interface::FoldingRange],
)
@listener = T.let(
Listeners::FoldingRanges.new(@response_builder, comments, dispatcher),
Listeners::FoldingRanges,
Expand Down
5 changes: 4 additions & 1 deletion lib/ruby_lsp/requests/inlay_hints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def initialize(document, range, hints_configuration, dispatcher)
start_line = range.dig(:start, :line)
end_line = range.dig(:end, :line)

@response_builder = T.let(ResponseBuilders::InlayHints.new, ResponseBuilders::InlayHints)
@response_builder = T.let(
ResponseBuilders::CollectionResponseBuilder[Interface::InlayHint].new,
ResponseBuilders::CollectionResponseBuilder[Interface::InlayHint],
)
Listeners::InlayHints.new(@response_builder, start_line..end_line, hints_configuration, dispatcher)
end

Expand Down
10 changes: 2 additions & 8 deletions lib/ruby_lsp/response_builders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@

module RubyLsp
module ResponseBuilders
autoload :ResponseBuilder, "ruby_lsp/response_builders/response_builder"
autoload :CodeLens, "ruby_lsp/response_builders/code_lens"
autoload :Completion, "ruby_lsp/response_builders/completion"
autoload :Definition, "ruby_lsp/response_builders/definition"
autoload :DocumentHighlight, "ruby_lsp/response_builders/document_highlight"
autoload :DocumentLink, "ruby_lsp/response_builders/document_link"
autoload :CollectionResponseBuilder, "ruby_lsp/response_builders/collection_response_builder"
autoload :DocumentSymbol, "ruby_lsp/response_builders/document_symbol"
autoload :FoldingRanges, "ruby_lsp/response_builders/folding_ranges"
autoload :Hover, "ruby_lsp/response_builders/hover"
autoload :InlayHints, "ruby_lsp/response_builders/inlay_hints"
autoload :ResponseBuilder, "ruby_lsp/response_builders/response_builder"
autoload :SemanticHighlighting, "ruby_lsp/response_builders/semantic_highlighting"
autoload :SignatureHelp, "ruby_lsp/response_builders/signature_help"
end
Expand Down
28 changes: 0 additions & 28 deletions lib/ruby_lsp/response_builders/code_lens.rb

This file was deleted.

30 changes: 30 additions & 0 deletions lib/ruby_lsp/response_builders/collection_response_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# typed: strict
# frozen_string_literal: true

module RubyLsp
module ResponseBuilders
class CollectionResponseBuilder < ResponseBuilder
extend T::Sig
extend T::Helpers
extend T::Generic

ResponseType = type_member { { upper: Object } }

sig { void }
def initialize
super
@items = T.let([], T::Array[ResponseType])
end

sig { params(item: ResponseType).void }
def <<(item)
@items << item
end

sig { override.returns(T::Array[ResponseType]) }
def response
@items
end
end
end
end
28 changes: 0 additions & 28 deletions lib/ruby_lsp/response_builders/completion.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/ruby_lsp/response_builders/definition.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/ruby_lsp/response_builders/document_highlight.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/ruby_lsp/response_builders/document_link.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/ruby_lsp/response_builders/folding_ranges.rb

This file was deleted.

Loading

0 comments on commit a358606

Please sign in to comment.