Skip to content

Commit

Permalink
Ensure we're handling client responses for window show message (#2814)
Browse files Browse the repository at this point in the history
### Motivation

When the server makes a request to the client, the response doesn't include the requested method by default. We can make it so that add-ons need to include it as part of the message properties, but normally only the ID can be used to reconcile request and response.

We need to let add-ons include the method name as part of the properties so that we can delegate appropriately.

### Implementation

Our regular `process_message` would just ignore any responses because `message[:method]` is always `nil` in a response. We need to extract it from `result` and define that add-ons must include the `method` as part of it.

### Automated Tests

Adapted the test.
  • Loading branch information
vinistock authored Nov 1, 2024
1 parent fc6f542 commit 34c38ce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ def process_message(message)
workspace_did_change_watched_files(message)
when "workspace/symbol"
workspace_symbol(message)
when "window/showMessageRequest"
window_show_message_request(message)
when "rubyLsp/textDocument/showSyntaxTree"
text_document_show_syntax_tree(message)
when "rubyLsp/workspace/dependencies"
Expand All @@ -108,6 +106,8 @@ def process_message(message)
)
when "$/cancelRequest"
@mutex.synchronize { @cancelled_requests << message[:params][:id] }
when nil
process_response(message) if message[:result]
end
rescue DelegateRequestError
send_message(Error.new(id: message[:id], code: DelegateRequestError::CODE, message: "DELEGATE_REQUEST"))
Expand Down Expand Up @@ -140,6 +140,15 @@ def process_message(message)
send_log_message("Error processing #{message[:method]}: #{e.full_message}", type: Constant::MessageType::ERROR)
end

# Process responses to requests that were sent to the client
sig { params(message: T::Hash[Symbol, T.untyped]).void }
def process_response(message)
case message.dig(:result, :method)
when "window/showMessageRequest"
window_show_message_request(message)
end
end

sig { params(include_project_addons: T::Boolean).void }
def load_addons(include_project_addons: true)
# If invoking Bundler.setup failed, then the load path will not be configured properly and trying to load add-ons
Expand Down Expand Up @@ -1193,11 +1202,14 @@ def process_indexing_configuration(indexing_options)

sig { params(message: T::Hash[Symbol, T.untyped]).void }
def window_show_message_request(message)
addon_name = message[:addon_name]
result = message[:result]
return unless result

addon_name = result[:addon_name]
addon = Addon.addons.find { |addon| addon.name == addon_name }
return unless addon

addon.handle_window_show_message_response(message[:title])
addon.handle_window_show_message_response(result[:title])
end
end
end
2 changes: 1 addition & 1 deletion test/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ def handle_window_show_message_response(title)
addon = RubyLsp::Addon.addons.find { |a| a.is_a?(klass) }
addon.expects(:handle_window_show_message_response).with("hello")

@server.process_message(method: "window/showMessageRequest", title: "hello", addon_name: "My Add-on")
@server.process_message(result: { method: "window/showMessageRequest", title: "hello", addon_name: "My Add-on" })
ensure
RubyLsp::Addon.addons.clear
RubyLsp::Addon.addon_classes.clear
Expand Down

0 comments on commit 34c38ce

Please sign in to comment.