From 1bb9fe080a4fc33f4292b287568617c7d6360730 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Fri, 5 Apr 2024 13:31:32 -0400 Subject: [PATCH] Apply formatter selected in initialization options (#1886) --- lib/ruby_lsp/global_state.rb | 6 +++++- lib/ruby_lsp/server.rb | 4 +--- test/global_state_test.rb | 12 ++++++++++++ test/server_test.rb | 2 +- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/ruby_lsp/global_state.rb b/lib/ruby_lsp/global_state.rb index 1ed6d4622..c56f2f577 100644 --- a/lib/ruby_lsp/global_state.rb +++ b/lib/ruby_lsp/global_state.rb @@ -21,7 +21,7 @@ class GlobalState def initialize @workspace_uri = T.let(URI::Generic.from_path(path: Dir.pwd), URI::Generic) - @formatter = T.let(detect_formatter, String) + @formatter = T.let("auto", String) @test_library = T.let(detect_test_library, String) @typechecker = T.let(detect_typechecker, T::Boolean) @index = T.let(RubyIndexer::Index.new, RubyIndexer::Index) @@ -42,6 +42,10 @@ def active_formatter def apply_options(options) workspace_uri = options.dig(:workspaceFolders, 0, :uri) @workspace_uri = URI(workspace_uri) if workspace_uri + + specified_formatter = options.dig(:initializationOptions, :formatter) + @formatter = specified_formatter if specified_formatter + @formatter = detect_formatter if @formatter == "auto" end sig { returns(String) } diff --git a/lib/ruby_lsp/server.rb b/lib/ruby_lsp/server.rb index 318664988..ee01f6593 100644 --- a/lib/ruby_lsp/server.rb +++ b/lib/ruby_lsp/server.rb @@ -129,8 +129,6 @@ def run_initialize(message) progress = options.dig(:capabilities, :window, :workDoneProgress) @store.supports_progress = progress.nil? ? true : progress - formatter = options.dig(:initializationOptions, :formatter) || "auto" - configured_features = options.dig(:initializationOptions, :enabledFeatures) @store.experimental_features = options.dig(:initializationOptions, :experimentalFeaturesEnabled) || false @@ -177,7 +175,7 @@ def run_initialize(message) document_link_provider: document_link_provider, folding_range_provider: folding_ranges_provider, semantic_tokens_provider: semantic_tokens_provider, - document_formatting_provider: enabled_features["formatting"] && formatter != "none", + document_formatting_provider: enabled_features["formatting"] && @global_state.formatter != "none", document_highlight_provider: enabled_features["documentHighlights"], code_action_provider: code_action_provider, document_on_type_formatting_provider: on_type_formatting_provider, diff --git a/test/global_state_test.rb b/test/global_state_test.rb index dc67b6f17..fa36b9ce4 100644 --- a/test/global_state_test.rb +++ b/test/global_state_test.rb @@ -51,6 +51,18 @@ def test_direct_dependency_returns_false_outside_of_bundle refute(GlobalState.new.direct_dependency?(/^ruby-lsp/)) end + def test_apply_option_selects_formatter + state = GlobalState.new + state.apply_options({ initializationOptions: { formatter: "syntax_tree" } }) + assert_equal("syntax_tree", state.formatter) + end + + def test_applying_auto_formatter_invokes_detection + state = GlobalState.new + state.apply_options({ initializationOptions: { formatter: "auto" } }) + assert_equal("rubocop", state.formatter) + end + private def stub_dependencies(dependencies) diff --git a/test/server_test.rb b/test/server_test.rb index 232edb467..152ca8db6 100644 --- a/test/server_test.rb +++ b/test/server_test.rb @@ -141,7 +141,7 @@ def test_server_info_includes_version end def test_server_info_includes_formatter - @server.global_state.expects(:formatter).returns("rubocop") + @server.global_state.expects(:formatter).twice.returns("rubocop") capture_subprocess_io do @server.process_message({ id: 1,