Skip to content

Commit

Permalink
Rename Request#response to Request#perform (#1297)
Browse files Browse the repository at this point in the history
As @vinistock pointed out, using `#response` seems to imply that the
request was already completed before it's called. In ruby-lsp's case,
many requests are used in this pattern:

```rb
FooRequest.new(args).response
```

In this context, it feels like the computation is done in
`FooRequest#initialize` and `#response` is just a getter, which is a bit
misleading.

So this commit renames `#response` to `#perform` to make it more clear.
  • Loading branch information
st0012 authored Jan 11, 2024
1 parent 21bdbe1 commit 6c1da44
Show file tree
Hide file tree
Showing 38 changed files with 107 additions and 107 deletions.
44 changes: 22 additions & 22 deletions lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def run(request)
cached_response = document.cache_get(request[:method])
return cached_response if cached_response

# Run listeners for the document
# Run requests for the document
dispatcher = Prism::Dispatcher.new
folding_range = Requests::FoldingRanges.new(document.parse_result.comments, dispatcher)
document_symbol = Requests::DocumentSymbol.new(dispatcher)
Expand All @@ -107,13 +107,13 @@ def run(request)

# Store all responses retrieve in this round of visits in the cache and then return the response for the request
# we actually received
document.cache_set("textDocument/foldingRange", folding_range.response)
document.cache_set("textDocument/documentSymbol", document_symbol.response)
document.cache_set("textDocument/documentLink", document_link.response)
document.cache_set("textDocument/codeLens", code_lens.response)
document.cache_set("textDocument/foldingRange", folding_range.perform)
document.cache_set("textDocument/documentSymbol", document_symbol.perform)
document.cache_set("textDocument/documentLink", document_link.perform)
document.cache_set("textDocument/codeLens", code_lens.perform)
document.cache_set(
"textDocument/semanticTokens/full",
Requests::Support::SemanticTokenEncoder.new.encode(semantic_highlighting.response),
Requests::Support::SemanticTokenEncoder.new.encode(semantic_highlighting.perform),
)
document.cache_get(request[:method])
when "textDocument/semanticTokens/range"
Expand Down Expand Up @@ -147,7 +147,7 @@ def run(request)
document = @store.get(uri)
request = Requests::DocumentHighlight.new(document, request.dig(:params, :position), dispatcher)
dispatcher.dispatch(document.tree)
request.response
request.perform
when "textDocument/onTypeFormatting"
on_type_formatting(uri, request.dig(:params, :position), request.dig(:params, :ch))
when "textDocument/hover"
Expand All @@ -159,14 +159,14 @@ def run(request)
request.dig(:params, :position),
dispatcher,
document.typechecker_enabled?,
).response
).perform
when "textDocument/inlayHint"
hints_configurations = T.must(@store.features_configuration.dig(:inlayHint))
dispatcher = Prism::Dispatcher.new
document = @store.get(uri)
request = Requests::InlayHints.new(document, request.dig(:params, :range), hints_configurations, dispatcher)
dispatcher.visit(document.tree)
request.response
request.perform
when "textDocument/codeAction"
code_action(uri, request.dig(:params, :range), request.dig(:params, :context))
when "codeAction/resolve"
Expand Down Expand Up @@ -194,7 +194,7 @@ def run(request)
request.dig(:params, :position),
document.typechecker_enabled?,
dispatcher,
).response
).perform
when "textDocument/signatureHelp"
dispatcher = Prism::Dispatcher.new
document = @store.get(uri)
Expand All @@ -205,7 +205,7 @@ def run(request)
request.dig(:params, :position),
request.dig(:params, :context),
dispatcher,
).response
).perform
when "textDocument/definition"
dispatcher = Prism::Dispatcher.new
document = @store.get(uri)
Expand All @@ -215,7 +215,7 @@ def run(request)
request.dig(:params, :position),
dispatcher,
document.typechecker_enabled?,
).response
).perform
when "workspace/didChangeWatchedFiles"
did_change_watched_files(request.dig(:params, :changes))
when "workspace/symbol"
Expand Down Expand Up @@ -287,12 +287,12 @@ def perform_initial_indexing

sig { params(query: T.nilable(String)).returns(T::Array[Interface::WorkspaceSymbol]) }
def workspace_symbol(query)
Requests::WorkspaceSymbol.new(query, @index).response
Requests::WorkspaceSymbol.new(query, @index).perform
end

sig { params(uri: URI::Generic, range: T.nilable(T::Hash[Symbol, T.untyped])).returns({ ast: String }) }
def show_syntax_tree(uri, range)
{ ast: Requests::ShowSyntaxTree.new(@store.get(uri), range).response }
{ ast: Requests::ShowSyntaxTree.new(@store.get(uri), range).perform }
end

sig do
Expand Down Expand Up @@ -323,7 +323,7 @@ def text_document_did_close(uri)
end
def selection_range(uri, positions)
ranges = @store.cache_fetch(uri, "textDocument/selectionRange") do |document|
Requests::SelectionRanges.new(document).response
Requests::SelectionRanges.new(document).perform
end

# Per the selection range request spec (https://microsoft.github.io/language-server-protocol/specification#textDocument_selectionRange),
Expand All @@ -350,7 +350,7 @@ def formatting(uri)
path = uri.to_standardized_path
return unless path.nil? || path.start_with?(T.must(@store.workspace_uri.to_standardized_path))

Requests::Formatting.new(@store.get(uri), formatter: @store.formatter).response
Requests::Formatting.new(@store.get(uri), formatter: @store.formatter).perform
end

sig do
Expand All @@ -361,7 +361,7 @@ def formatting(uri)
).returns(T::Array[Interface::TextEdit])
end
def on_type_formatting(uri, position, character)
Requests::OnTypeFormatting.new(@store.get(uri), position, character).response
Requests::OnTypeFormatting.new(@store.get(uri), position, character).perform
end

sig do
Expand All @@ -374,14 +374,14 @@ def on_type_formatting(uri, position, character)
def code_action(uri, range, context)
document = @store.get(uri)

Requests::CodeActions.new(document, range, context).response
Requests::CodeActions.new(document, range, context).perform
end

sig { params(params: T::Hash[Symbol, T.untyped]).returns(Interface::CodeAction) }
def code_action_resolve(params)
uri = URI(params.dig(:data, :uri))
document = @store.get(uri)
result = Requests::CodeActionResolve.new(document, params).response
result = Requests::CodeActionResolve.new(document, params).perform

case result
when Requests::CodeActionResolve::Error::EmptySelection
Expand Down Expand Up @@ -415,7 +415,7 @@ def diagnostic(uri)
return unless path.nil? || path.start_with?(T.must(@store.workspace_uri.to_standardized_path))

response = @store.cache_fetch(uri, "textDocument/diagnostic") do |document|
Requests::Diagnostics.new(document).response
Requests::Diagnostics.new(document).perform
end

Interface::FullDocumentDiagnosticReport.new(kind: "full", items: response) if response
Expand All @@ -428,10 +428,10 @@ def semantic_tokens_range(uri, range)
end_line = range.dig(:end, :line)

dispatcher = Prism::Dispatcher.new
listener = Requests::SemanticHighlighting.new(dispatcher, range: start_line..end_line)
request = Requests::SemanticHighlighting.new(dispatcher, range: start_line..end_line)
dispatcher.visit(document.tree)

Requests::Support::SemanticTokenEncoder.new.encode(listener.response)
Requests::Support::SemanticTokenEncoder.new.encode(request.perform)
end

sig { params(id: String, title: String, percentage: Integer).void }
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/code_action_resolve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def initialize(document, code_action)
end

sig { override.returns(T.any(Interface::CodeAction, Error)) }
def response
def perform
return Error::EmptySelection if @document.source.empty?

source_range = @code_action.dig(:data, :range)
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/code_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def initialize(document, range, context)
end

sig { override.returns(T.nilable(T.all(T::Array[Interface::CodeAction], Object))) }
def response
def perform
diagnostics = @context[:diagnostics]

code_actions = diagnostics.flat_map do |diagnostic|
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def initialize(uri, lenses_configuration, dispatcher)
end

sig { override.returns(ResponseType) }
def response
def perform
@listeners.flat_map(&:response)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def initialize(document, index, position, typechecker_enabled, dispatcher)
end

sig { override.returns(ResponseType) }
def response
def perform
return [] unless @target

@dispatcher.dispatch_once(@target)
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def initialize(document, index, position, dispatcher, typechecker_enabled)
end

sig { override.returns(ResponseType) }
def response
def perform
@dispatcher.dispatch_once(@target)
result = []

Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/diagnostics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def initialize(document)
end

sig { override.returns(T.nilable(T.all(T::Array[Interface::Diagnostic], Object))) }
def response
def perform
# Running RuboCop is slow, so to avoid excessive runs we only do so if the file is syntactically valid
return syntax_error_diagnostics if @document.syntax_error?
return [] unless defined?(Support::RuboCopDiagnosticsRunner)
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/document_highlight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def initialize(document, position, dispatcher)
end

sig { override.returns(ResponseType) }
def response
def perform
@listener.response
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/document_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def initialize(uri, comments, dispatcher)
end

sig { override.returns(ResponseType) }
def response
def perform
@listener.response
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/document_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def initialize(dispatcher)
end

sig { override.returns(ResponseType) }
def response
def perform
@listeners.flat_map(&:response).compact
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/folding_ranges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def initialize(comments, dispatcher)
end

sig { override.returns(ResponseType) }
def response
def perform
@listener.response
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def initialize(document, formatter: "auto")
end

sig { override.returns(T.nilable(T.all(T::Array[Interface::TextEdit], Object))) }
def response
def perform
return if @formatter == "none"
return if @document.syntax_error?

Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/hover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def initialize(document, index, position, dispatcher, typechecker_enabled)
end

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

Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/inlay_hints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def initialize(document, range, hints_configuration, dispatcher)
end

sig { override.returns(ResponseType) }
def response
def perform
@listener.response
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/on_type_formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def initialize(document, position, trigger_character)
end

sig { override.returns(T.all(T::Array[Interface::TextEdit], Object)) }
def response
def perform
case @trigger_character
when "{"
handle_curly_brace if @document.syntax_error?
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Request
abstract!

sig { abstract.returns(T.anything) }
def response; end
def perform; end
end
end
end
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/selection_ranges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def initialize(document)
end

sig { override.returns(T.all(T::Array[Support::SelectionRange], Object)) }
def response
def perform
# [node, parent]
queue = [[@document.tree, nil]]

Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/semantic_highlighting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def initialize(dispatcher, range: nil)
end

sig { override.returns(ResponseType) }
def response
def perform
@listener.response
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/show_syntax_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def initialize(document, range)
end

sig { override.returns(String) }
def response
def perform
return ast_for_range if @range

output_string = +""
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/signature_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def initialize(document, index, position, context, dispatcher)
end

sig { override.returns(ResponseType) }
def response
def perform
return unless @target

@dispatcher.dispatch_once(@target)
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/workspace_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def initialize(query, index)
end

sig { override.returns(T::Array[Interface::WorkspaceSymbol]) }
def response
def perform
@index.fuzzy_search(@query).filter_map do |entry|
# If the project is using Sorbet, we let Sorbet handle symbols defined inside the project itself and RBIs, but
# we still return entries defined in gems to allow developers to jump directly to the source
Expand Down
4 changes: 2 additions & 2 deletions test/expectations/expectations_test_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def run_expectations(source)
params = @__params&.any? ? @__params : default_args
document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: URI("file:///fake.rb"))
dispatcher = Prism::Dispatcher.new
listener = #{handler_class}.new(dispatcher)
request = #{handler_class}.new(dispatcher)
dispatcher.dispatch(document.tree)
listener.response
request.perform
end
def assert_expectations(source, expected)
Expand Down
2 changes: 1 addition & 1 deletion test/requests/code_action_resolve_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def run_expectations(source)
params = @__params&.any? ? @__params : default_args
document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: URI("file:///fake.rb"))

RubyLsp::Requests::CodeActionResolve.new(document, params).response
RubyLsp::Requests::CodeActionResolve.new(document, params).perform
end

def assert_expectations(source, expected)
Expand Down
2 changes: 1 addition & 1 deletion test/requests/code_actions_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def run_expectations(source)
document,
params[:range],
params[:context],
).response
).perform
end

assert_empty(stdout)
Expand Down
4 changes: 2 additions & 2 deletions test/requests/code_actions_formatting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def assert_corrects_to_expected(diagnostic_code, code_action_title, source, expe
encoding: LanguageServer::Protocol::Constant::PositionEncodingKind::UTF16,
)

diagnostics = RubyLsp::Requests::Diagnostics.new(document).response
diagnostics = RubyLsp::Requests::Diagnostics.new(document).perform
diagnostic = T.must(T.must(diagnostics).find { |d| d.code == diagnostic_code })
range = diagnostic.range.to_hash.transform_values(&:to_hash)
result = RubyLsp::Requests::CodeActions.new(document, range, {
diagnostics: [JSON.parse(T.must(diagnostic).to_json, symbolize_names: true)],
}).response
}).perform

# CodeActions#run returns Array<CodeAction, Hash>. We're interested in the
# hashes here, so cast to untyped and only look at those.
Expand Down
Loading

0 comments on commit 6c1da44

Please sign in to comment.