Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward initialization settings to addons #2513

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/ruby_lsp/global_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def initialize
@supports_watching_files = T.let(false, T::Boolean)
@experimental_features = T.let(false, T::Boolean)
@type_inferrer = T.let(TypeInferrer.new(@index, @experimental_features), TypeInferrer)
@addon_settings = T.let({}, T::Hash[String, T.untyped])
end

sig { params(addon_name: String).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
def settings_for_addon(addon_name)
@addon_settings[addon_name]
end

sig { params(identifier: String, instance: Requests::Support::Formatter).void }
Expand Down Expand Up @@ -119,6 +125,12 @@ def apply_options(options)
@experimental_features = options.dig(:initializationOptions, :experimentalFeaturesEnabled) || false
@type_inferrer.experimental_features = @experimental_features

addon_settings = options.dig(:initializationOptions, :addonSettings)
if addon_settings
addon_settings.transform_keys!(&:to_s)
@addon_settings.merge!(addon_settings)
end

notifications
end

Expand Down
23 changes: 22 additions & 1 deletion test/addon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module RubyLsp
class AddonTest < Minitest::Test
def setup
@addon = Class.new(Addon) do
attr_reader :activated, :field
attr_reader :activated, :field, :settings

def initialize
@field = 123
Expand All @@ -16,6 +16,7 @@ def initialize

def activate(global_state, outgoing_queue)
@activated = true
@settings = global_state.settings_for_addon(name)
end

def name
Expand Down Expand Up @@ -110,5 +111,25 @@ def test_raises_if_an_addon_cannot_be_found
Addon.get("Invalid Addon")
end
end

def test_addons_receive_settings
global_state = GlobalState.new
global_state.apply_options({
initializationOptions: {
addonSettings: {
"My Addon" => { something: false },
},
},
})

outgoing_queue = Thread::Queue.new
Addon.load_addons(global_state, outgoing_queue)

addon = Addon.get("My Addon")

assert_equal({ something: false }, T.unsafe(addon).settings)
ensure
T.must(outgoing_queue).close
end
end
end
14 changes: 14 additions & 0 deletions test/global_state_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ def test_type_checker_is_detected_based_on_transitive_sorbet_static
assert_predicate(state, :has_type_checker)
end

def test_addon_settings_are_stored
global_state = GlobalState.new

global_state.apply_options({
initializationOptions: {
addonSettings: {
"Ruby LSP Rails" => { runtimeServerEnabled: false },
},
},
})

assert_equal({ runtimeServerEnabled: false }, global_state.settings_for_addon("Ruby LSP Rails"))
end

private

def stub_direct_dependencies(dependencies)
Expand Down
16 changes: 16 additions & 0 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@
}
}
},
"rubyLsp.addonSettings": {
"description": "Settings that will be forwarded to configure the behavior of Ruby LSP addons. Keys are addon names, values are objects of settings",
"type": "object",
"examples": [
{
"Ruby LSP Rails": {
"something": true
}
},
{
"Standard Ruby": {
"something": true
}
}
]
},
"rubyLsp.rubyVersionManager": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions vscode/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ function collectClientOptions(
formatter: configuration.get("formatter"),
linters: configuration.get("linters"),
indexing: configuration.get("indexing"),
addonSettings: configuration.get("addonSettings"),
},
};
}
Expand Down
Loading