Skip to content

Commit 38edb1f

Browse files
authored
Allow users to disable test related code lenses (#3632)
1 parent 33e24d1 commit 38edb1f

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

lib/ruby_lsp/global_state.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def initialize
6262
implicitRescue: false,
6363
implicitHashValue: false,
6464
}),
65+
codeLens: RequestConfig.new({
66+
enableAll: false,
67+
enableTestCodeLens: true,
68+
}),
6569
} #: Hash[Symbol, RequestConfig]
6670
end
6771

lib/ruby_lsp/requests/code_lens.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@ def initialize(global_state, document, dispatcher)
2727
@document = document
2828
@test_builder = ResponseBuilders::TestCollection.new #: ResponseBuilders::TestCollection
2929
uri = document.uri
30+
code_lens_config = global_state.feature_configuration(:codeLens)
31+
test_lenses_enabled = !code_lens_config || code_lens_config.enabled?(:enableTestCodeLens)
3032

3133
if global_state.enabled_feature?(:fullTestDiscovery)
32-
Listeners::TestStyle.new(@test_builder, global_state, dispatcher, uri)
33-
Listeners::SpecStyle.new(@test_builder, global_state, dispatcher, uri)
34+
if test_lenses_enabled
35+
Listeners::TestStyle.new(@test_builder, global_state, dispatcher, uri)
36+
Listeners::SpecStyle.new(@test_builder, global_state, dispatcher, uri)
37+
end
3438
else
3539
Listeners::CodeLens.new(@response_builder, global_state, uri, dispatcher)
3640
end
3741

3842
Addon.addons.each do |addon|
3943
addon.create_code_lens_listener(@response_builder, uri, dispatcher)
4044

41-
if global_state.enabled_feature?(:fullTestDiscovery)
45+
if global_state.enabled_feature?(:fullTestDiscovery) && test_lenses_enabled
4246
addon.create_discover_tests_listener(@test_builder, dispatcher, uri)
4347
end
4448
end

test/requests/discover_tests_test.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,49 @@ class TestCase; end
216216
end
217217
end
218218

219+
def test_does_not_collect_code_lenses_when_disabled
220+
source = <<~RUBY
221+
module Foo
222+
class MyTest < Test::Unit::TestCase
223+
def test_something; end
224+
225+
def test_something_else; end
226+
end
227+
end
228+
RUBY
229+
230+
with_server(source) do |server, uri|
231+
server.global_state.index.index_single(URI("/other_file.rb"), <<~RUBY)
232+
module Test
233+
module Unit
234+
class TestCase; end
235+
end
236+
end
237+
RUBY
238+
239+
state = server.global_state
240+
state.stubs(:enabled_feature?).returns(true)
241+
state.apply_options({
242+
initializationOptions: {
243+
featuresConfiguration: {
244+
codeLens: {
245+
enableTestCodeLens: false,
246+
},
247+
},
248+
},
249+
})
250+
251+
server.process_message(id: 1, method: "textDocument/codeLens", params: {
252+
textDocument: { uri: uri },
253+
})
254+
255+
# Discard the indexing log message
256+
server.pop_response
257+
items = get_response(server)
258+
assert_empty(items)
259+
end
260+
end
261+
219262
def test_ignores_minitest_tests_that_extend_active_support_declarative
220263
source = <<~RUBY
221264
class MyTest < ActiveSupport::TestCase

vscode/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,20 @@
342342
"type": "boolean"
343343
}
344344
}
345+
},
346+
"codeLens": {
347+
"description": "Customize code lens features",
348+
"type": "object",
349+
"properties": {
350+
"enableAll": {
351+
"type": "boolean"
352+
},
353+
"enableTestCodeLens": {
354+
"description": "Enable the run, run in terminal, debug code and other test related code lenses",
355+
"type": "boolean",
356+
"default": true
357+
}
358+
}
345359
}
346360
}
347361
},

0 commit comments

Comments
 (0)