Skip to content

Commit 7d0344a

Browse files
authored
Forward initialization settings to addons (#2513)
1 parent 9372a1e commit 7d0344a

5 files changed

Lines changed: 65 additions & 1 deletion

File tree

lib/ruby_lsp/global_state.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def initialize
4040
@supports_watching_files = T.let(false, T::Boolean)
4141
@experimental_features = T.let(false, T::Boolean)
4242
@type_inferrer = T.let(TypeInferrer.new(@index, @experimental_features), TypeInferrer)
43+
@addon_settings = T.let({}, T::Hash[String, T.untyped])
44+
end
45+
46+
sig { params(addon_name: String).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
47+
def settings_for_addon(addon_name)
48+
@addon_settings[addon_name]
4349
end
4450

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

128+
addon_settings = options.dig(:initializationOptions, :addonSettings)
129+
if addon_settings
130+
addon_settings.transform_keys!(&:to_s)
131+
@addon_settings.merge!(addon_settings)
132+
end
133+
122134
notifications
123135
end
124136

test/addon_test.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module RubyLsp
77
class AddonTest < Minitest::Test
88
def setup
99
@addon = Class.new(Addon) do
10-
attr_reader :activated, :field
10+
attr_reader :activated, :field, :settings
1111

1212
def initialize
1313
@field = 123
@@ -16,6 +16,7 @@ def initialize
1616

1717
def activate(global_state, outgoing_queue)
1818
@activated = true
19+
@settings = global_state.settings_for_addon(name)
1920
end
2021

2122
def name
@@ -110,5 +111,25 @@ def test_raises_if_an_addon_cannot_be_found
110111
Addon.get("Invalid Addon")
111112
end
112113
end
114+
115+
def test_addons_receive_settings
116+
global_state = GlobalState.new
117+
global_state.apply_options({
118+
initializationOptions: {
119+
addonSettings: {
120+
"My Addon" => { something: false },
121+
},
122+
},
123+
})
124+
125+
outgoing_queue = Thread::Queue.new
126+
Addon.load_addons(global_state, outgoing_queue)
127+
128+
addon = Addon.get("My Addon")
129+
130+
assert_equal({ something: false }, T.unsafe(addon).settings)
131+
ensure
132+
T.must(outgoing_queue).close
133+
end
113134
end
114135
end

test/global_state_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,20 @@ def test_type_checker_is_detected_based_on_transitive_sorbet_static
209209
assert_predicate(state, :has_type_checker)
210210
end
211211

212+
def test_addon_settings_are_stored
213+
global_state = GlobalState.new
214+
215+
global_state.apply_options({
216+
initializationOptions: {
217+
addonSettings: {
218+
"Ruby LSP Rails" => { runtimeServerEnabled: false },
219+
},
220+
},
221+
})
222+
223+
assert_equal({ runtimeServerEnabled: false }, global_state.settings_for_addon("Ruby LSP Rails"))
224+
end
225+
212226
private
213227

214228
def stub_direct_dependencies(dependencies)

vscode/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,22 @@
294294
}
295295
}
296296
},
297+
"rubyLsp.addonSettings": {
298+
"description": "Settings that will be forwarded to configure the behavior of Ruby LSP addons. Keys are addon names, values are objects of settings",
299+
"type": "object",
300+
"examples": [
301+
{
302+
"Ruby LSP Rails": {
303+
"something": true
304+
}
305+
},
306+
{
307+
"Standard Ruby": {
308+
"something": true
309+
}
310+
}
311+
]
312+
},
297313
"rubyLsp.rubyVersionManager": {
298314
"type": "object",
299315
"properties": {

vscode/src/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ function collectClientOptions(
184184
formatter: configuration.get("formatter"),
185185
linters: configuration.get("linters"),
186186
indexing: configuration.get("indexing"),
187+
addonSettings: configuration.get("addonSettings"),
187188
},
188189
};
189190
}

0 commit comments

Comments
 (0)