Skip to content

Commit

Permalink
clean: use RequestConfig class and switch off by default hints
Browse files Browse the repository at this point in the history
  • Loading branch information
snutij committed Dec 5, 2023
1 parent d9c8496 commit fbdf5c7
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 32 deletions.
4 changes: 2 additions & 2 deletions lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,8 @@ def initialize_request(options)

configured_hints = options.dig(:initializationOptions, :featuresConfiguration, :inlayHint)
configured_lenses = options.dig(:initializationOptions, :featuresConfiguration, :codeLens)
T.must(@store.features_configuration.dig(:inlayHint)).merge!(configured_hints) if configured_hints
T.must(@store.features_configuration.dig(:codeLens)).merge!(configured_lenses) if configured_lenses
T.must(@store.features_configuration.dig(:inlayHint)).configuration.merge!(configured_hints) if configured_hints
T.must(@store.features_configuration.dig(:codeLens)).configuration.merge!(configured_lenses) if configured_lenses

enabled_features = case configured_features
when Array
Expand Down
8 changes: 6 additions & 2 deletions lib/ruby_lsp/requests/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ module Requests
# [code lens](https://microsoft.github.io/language-server-protocol/specification#textDocument_codeLens)
# request informs the editor of runnable commands such as tests
#
# # Configuration
#
# To disable gem code lenses, set `rubyLsp.featuresConfiguration.codeLens.gemfileLinks` to `false`.
#
# # Example
#
# ```ruby
Expand Down Expand Up @@ -50,7 +54,7 @@ class CodeLens < ExtensibleListener
sig do
params(
uri: URI::Generic,
lenses_configuration: T::Hash[Symbol, T::Boolean],
lenses_configuration: RequestConfig,
dispatcher: Prism::Dispatcher,
).void
end
Expand Down Expand Up @@ -141,7 +145,7 @@ def on_call_node_enter(node)
end

if @path&.include?(GEMFILE_NAME) && name == :gem && arguments
return unless @lenses_configuration.dig(:gemfileLinks)
return unless @lenses_configuration.enabled?(:gemfileLinks)

first_argument = arguments.arguments.first
return unless first_argument.is_a?(Prism::StringNode)
Expand Down
14 changes: 11 additions & 3 deletions lib/ruby_lsp/requests/inlay_hints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ module Requests
# are labels added directly in the code that explicitly show the user something that might
# otherwise just be implied.
#
# # Configuration
#
# To enable rescue hints, set `rubyLsp.featuresConfiguration.inlayHint.implicitRescue` to `true`.
#
# To enable hash value hints, set `rubyLsp.featuresConfiguration.inlayHint.implicitHashValue` to `true`.
#
# To enable all hints, set `rubyLsp.featuresConfiguration.inlayHint.enableAll` to `true`.
#
# # Example
#
# ```ruby
Expand Down Expand Up @@ -42,7 +50,7 @@ class InlayHints < Listener
sig do
params(
range: T::Range[Integer],
hints_configuration: T::Hash[Symbol, T::Boolean],
hints_configuration: RequestConfig,
dispatcher: Prism::Dispatcher,
).void
end
Expand All @@ -58,7 +66,7 @@ def initialize(range, hints_configuration, dispatcher)

sig { params(node: Prism::RescueNode).void }
def on_rescue_node_enter(node)
return unless @hints_configuration.dig(:implicitRescue)
return unless @hints_configuration.enabled?(:implicitRescue)
return unless node.exceptions.empty?

loc = node.location
Expand All @@ -74,7 +82,7 @@ def on_rescue_node_enter(node)

sig { params(node: Prism::ImplicitNode).void }
def on_implicit_node_enter(node)
return unless @hints_configuration.dig(:implicitHashValue)
return unless @hints_configuration.enabled?(:implicitHashValue)
return unless visible?(node, @range)

node_value = node.value
Expand Down
18 changes: 10 additions & 8 deletions lib/ruby_lsp/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Store
sig { returns(URI::Generic) }
attr_accessor :workspace_uri

sig { returns(T::Hash[Symbol, T::Hash[Symbol, T::Boolean]]) }
sig { returns(T::Hash[Symbol, RequestConfig]) }
attr_accessor :features_configuration

sig { void }
Expand All @@ -33,15 +33,17 @@ def initialize
@workspace_uri = T.let(URI::Generic.from_path(path: Dir.pwd), URI::Generic)
@features_configuration = T.let(
{
codeLens: {
codeLens: RequestConfig.new({
enableAll: false,
gemfileLinks: true,
},
inlayHint: {
implicitRescue: true,
implicitHashValue: true,
},
}),
inlayHint: RequestConfig.new({
enableAll: false,
implicitRescue: false,
implicitHashValue: false,
}),
},
T::Hash[Symbol, T::Hash[Symbol, T::Boolean]],
T::Hash[Symbol, RequestConfig],
)
end

Expand Down
18 changes: 18 additions & 0 deletions lib/ruby_lsp/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,22 @@ def cancel
@cancelled = true
end
end

# A request configuration, to turn on/off features
class RequestConfig
extend T::Sig

sig { returns(T::Hash[Symbol, T::Boolean]) }
attr_accessor :configuration

sig { params(configuration: T::Hash[Symbol, T::Boolean]).void }
def initialize(configuration)
@configuration = configuration
end

sig { params(feature: Symbol).returns(T.nilable(T::Boolean)) }
def enabled?(feature)
@configuration[:enableAll] || @configuration[feature]
end
end
end
44 changes: 32 additions & 12 deletions test/executor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ def test_initialize_features_with_default_configuration
RubyLsp::Executor.new(@store, @message_queue)
.execute(method: "initialize", params: { initializationOptions: {} })

assert(@store.features_configuration.dig(:codeLens, :gemfileLinks))
assert(@store.features_configuration.dig(:inlayHint, :implicitRescue))
assert(@store.features_configuration.dig(:inlayHint, :implicitHashValue))
assert(@store.features_configuration.dig(:codeLens).enabled?(:gemfileLinks))
refute(@store.features_configuration.dig(:inlayHint).enabled?(:implicitRescue))
refute(@store.features_configuration.dig(:inlayHint).enabled?(:implicitHashValue))
end

def test_initialize_features_with_provided_configuration
Expand All @@ -239,16 +239,16 @@ def test_initialize_features_with_provided_configuration
gemfileLinks: false,
},
inlayHint: {
implicitRescue: false,
implicitHashValue: false,
implicitRescue: true,
implicitHashValue: true,
},
},
},
})

refute(@store.features_configuration.dig(:codeLens, :gemfileLinks))
refute(@store.features_configuration.dig(:inlayHint, :implicitRescue))
refute(@store.features_configuration.dig(:inlayHint, :implicitHashValue))
refute(@store.features_configuration.dig(:codeLens).enabled?(:gemfileLinks))
assert(@store.features_configuration.dig(:inlayHint).enabled?(:implicitRescue))
assert(@store.features_configuration.dig(:inlayHint).enabled?(:implicitHashValue))
end

def test_initialize_features_with_partially_provided_configuration
Expand All @@ -260,15 +260,35 @@ def test_initialize_features_with_partially_provided_configuration
gemfileLinks: false,
},
inlayHint: {
implicitHashValue: false,
implicitHashValue: true,
},
},
},
})

refute(@store.features_configuration.dig(:codeLens, :gemfileLinks))
assert(@store.features_configuration.dig(:inlayHint, :implicitRescue))
refute(@store.features_configuration.dig(:inlayHint, :implicitHashValue))
refute(@store.features_configuration.dig(:codeLens).enabled?(:gemfileLinks))
refute(@store.features_configuration.dig(:inlayHint).enabled?(:implicitRescue))
assert(@store.features_configuration.dig(:inlayHint).enabled?(:implicitHashValue))
end

def test_initialize_features_with_enable_all_configuration
RubyLsp::Executor.new(@store, @message_queue)
.execute(method: "initialize", params: {
initializationOptions: {
featuresConfiguration: {
codeLens: {
enableAll: true,
},
inlayHint: {
enableAll: true,
},
},
},
})

assert(@store.features_configuration.dig(:codeLens).enabled?(:gemfileLinks))
assert(@store.features_configuration.dig(:inlayHint).enabled?(:implicitRescue))
assert(@store.features_configuration.dig(:inlayHint).enabled?(:implicitHashValue))
end

def test_detects_rubocop_if_direct_dependency
Expand Down
4 changes: 2 additions & 2 deletions test/requests/code_lens_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_skip_gemfile_links
RUBY

dispatcher = Prism::Dispatcher.new
lenses_configuration = { gemfileLinks: false }
lenses_configuration = RubyLsp::RequestConfig.new({ gemfileLinks: false })
listener = RubyLsp::Requests::CodeLens.new(uri, lenses_configuration, dispatcher)
dispatcher.dispatch(document.tree)
response = listener.response
Expand Down Expand Up @@ -138,7 +138,7 @@ class Test < Minitest::Test; end
private

def default_lenses_configuration
{ gemfileLinks: true }
RubyLsp::RequestConfig.new({ gemfileLinks: true })
end

def create_code_lens_addon
Expand Down
6 changes: 3 additions & 3 deletions test/requests/inlay_hints_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def run_expectations(source)
document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri)

dispatcher = Prism::Dispatcher.new
hints_configuration = { implicitRescue: true, implicitHashValue: true }
hints_configuration = RubyLsp::RequestConfig.new({ implicitRescue: true, implicitHashValue: true })
listener = RubyLsp::Requests::InlayHints.new(params.first, hints_configuration, dispatcher)
dispatcher.dispatch(document.tree)
listener.response
Expand All @@ -30,7 +30,7 @@ def test_skip_implicit_hash_value
RUBY

dispatcher = Prism::Dispatcher.new
hints_configuration = { implicitRescue: true, implicitHashValue: false }
hints_configuration = RubyLsp::RequestConfig.new({ implicitRescue: true, implicitHashValue: false })
listener = RubyLsp::Requests::InlayHints.new(default_args.first, hints_configuration, dispatcher)
dispatcher.dispatch(document.tree)
assert_empty(listener.response)
Expand All @@ -45,7 +45,7 @@ def test_skip_implicit_rescue
RUBY

dispatcher = Prism::Dispatcher.new
hints_configuration = { implicitRescue: false, implicitHashValue: true }
hints_configuration = RubyLsp::RequestConfig.new({ implicitRescue: false, implicitHashValue: true })
listener = RubyLsp::Requests::InlayHints.new(default_args.first, hints_configuration, dispatcher)
dispatcher.dispatch(document.tree)
assert_empty(listener.response)
Expand Down

0 comments on commit fbdf5c7

Please sign in to comment.