Skip to content

Commit

Permalink
Show doc urls for extension cops
Browse files Browse the repository at this point in the history
RuboCop 1.64 added support for passing in a config, which will then construct
the correct url for non-buildin departments that have `DocumentationBaseURL` set.
  • Loading branch information
Earlopain committed May 23, 2024
1 parent 5650d4b commit 987c3d9
Show file tree
Hide file tree
Showing 7 changed files with 692 additions and 302 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ GEM
io-console (~> 0.5)
rexml (3.2.8)
strscan (>= 3.0.9)
rubocop (1.63.5)
rubocop (1.64.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
Expand Down
20 changes: 14 additions & 6 deletions lib/ruby_lsp/requests/support/rubocop_diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ def to_lsp_code_actions
code_actions
end

sig { returns(Interface::Diagnostic) }
def to_lsp_diagnostic
sig { params(config: RuboCop::Config).returns(Interface::Diagnostic) }
def to_lsp_diagnostic(config)
# highlighted_area contains the begin and end position of the first line
# This ensures that multiline offenses don't clutter the editor
highlighted = @offense.highlighted_area
Interface::Diagnostic.new(
message: message,
source: "RuboCop",
code: @offense.cop_name,
code_description: code_description,
code_description: code_description(config),
severity: severity,
range: Interface::Range.new(
start: Interface::Position.new(
Expand Down Expand Up @@ -80,9 +80,17 @@ def severity
RUBOCOP_TO_LSP_SEVERITY[@offense.severity.name]
end

sig { returns(T.nilable(Interface::CodeDescription)) }
def code_description
doc_url = RuboCopRunner.find_cop_by_name(@offense.cop_name)&.documentation_url
sig { params(config: RuboCop::Config).returns(T.nilable(Interface::CodeDescription)) }
def code_description(config)
cop = RuboCopRunner.find_cop_by_name(@offense.cop_name)
return unless cop

doc_url = if cop.method(:documentation_url).parameters.none?
# Rubocop < 1.64
cop.documentation_url
else
cop.documentation_url(config)
end
Interface::CodeDescription.new(href: doc_url) if doc_url
end

Expand Down
6 changes: 5 additions & 1 deletion lib/ruby_lsp/requests/support/rubocop_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def run_diagnostic(uri, document)
@diagnostic_runner.run(filename, document.source)

@diagnostic_runner.offenses.map do |offense|
Support::RuboCopDiagnostic.new(document, offense, uri).to_lsp_diagnostic
Support::RuboCopDiagnostic.new(
document,
offense,
uri,
).to_lsp_diagnostic(@diagnostic_runner.config_for_working_directory)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/ruby_lsp/requests/support/rubocop_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class ConfigurationError < StandardError; end
sig { returns(T::Array[RuboCop::Cop::Offense]) }
attr_reader :offenses

sig { returns(::RuboCop::Config) }
attr_reader :config_for_working_directory

DEFAULT_ARGS = T.let(
[
"--stderr", # Print any output to stderr so that our stdout does not get polluted
Expand Down Expand Up @@ -78,6 +81,7 @@ def initialize(*args)
args += DEFAULT_ARGS
rubocop_options = ::RuboCop::Options.new.parse(args).first
config_store = ::RuboCop::ConfigStore.new
@config_for_working_directory = T.let(config_store.for_pwd, ::RuboCop::Config)

super(rubocop_options, config_store)
end
Expand Down
Loading

0 comments on commit 987c3d9

Please sign in to comment.