Skip to content

Commit

Permalink
Apply formatter selected in initialization options
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Apr 4, 2024
1 parent 044568e commit 1b7c216
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/ruby_lsp/global_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) }
Expand Down
4 changes: 1 addition & 3 deletions lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions test/global_state_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1b7c216

Please sign in to comment.