Skip to content

Commit

Permalink
Handle responses for show window request and delegate to add-ons (#2803)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Oct 30, 2024
1 parent f81cf95 commit 33fd4e9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/ruby_lsp/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ def name; end
sig { abstract.returns(String) }
def version; end

# Handle a response from a window/showMessageRequest request. Add-ons must include the addon_name as part of the
# original request so that the response is delegated to the correct add-on and must override this method to handle
# the response
# https://microsoft.github.io/language-server-protocol/specification#window_showMessageRequest
sig { overridable.params(title: String).void }
def handle_window_show_message_response(title); end

# Creates a new CodeLens listener. This method is invoked on every CodeLens request
sig do
overridable.params(
Expand Down
16 changes: 15 additions & 1 deletion lib/ruby_lsp/global_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ class GlobalState
attr_reader :encoding

sig { returns(T::Boolean) }
attr_reader :supports_watching_files, :experimental_features, :supports_request_delegation, :top_level_bundle
attr_reader :supports_watching_files,
:experimental_features,
:supports_request_delegation,
:top_level_bundle,
:window_show_message_supports_extra_properties

sig { returns(T::Array[String]) }
attr_reader :supported_resource_operations
Expand Down Expand Up @@ -55,6 +59,7 @@ def initialize
end,
T::Boolean,
)
@window_show_message_supports_extra_properties = T.let(false, T::Boolean)
end

sig { params(addon_name: String).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
Expand Down Expand Up @@ -149,6 +154,15 @@ def apply_options(options)
supported_resource_operations = options.dig(:capabilities, :workspace, :workspaceEdit, :resourceOperations)
@supported_resource_operations = supported_resource_operations if supported_resource_operations

supports_additional_properties = options.dig(
:capabilities,
:window,
:showMessage,
:messageActionItem,
:additionalPropertiesSupport,
)
@window_show_message_supports_extra_properties = supports_additional_properties || false

notifications
end

Expand Down
11 changes: 11 additions & 0 deletions lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ 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 Down Expand Up @@ -1221,5 +1223,14 @@ def process_indexing_configuration(indexing_options)
# The index expects snake case configurations, but VS Code standardizes on camel case settings
configuration.apply_config(indexing_options.transform_keys { |key| key.to_s.gsub(/([A-Z])/, "_\\1").downcase })
end

sig { params(message: T::Hash[Symbol, T.untyped]).void }
def window_show_message_request(message)
addon_name = message[:addon_name]
addon = Addon.addons.find { |addon| addon.name == addon_name }
return unless addon

addon.handle_window_show_message_response(message[:title])
end
end
end
33 changes: 33 additions & 0 deletions test/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,39 @@ def foo
end
end

def test_show_window_responses_are_redirected_to_addons
klass = Class.new(RubyLsp::Addon) do
def activate(global_state, outgoing_queue)
@activated = true
@settings = global_state.settings_for_addon(name)
end

def deactivate; end

def name
"My Add-on"
end

def version
"0.1.0"
end

def handle_window_show_message_response(title)
end
end

begin
@server.load_addons
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")
ensure
RubyLsp::Addon.addons.clear
RubyLsp::Addon.addon_classes.clear
end
end

private

def with_uninstalled_rubocop(&block)
Expand Down

0 comments on commit 33fd4e9

Please sign in to comment.