Skip to content

Commit

Permalink
Merge pull request #1123 from Shopify/uk-add-rubocop-code-description
Browse files Browse the repository at this point in the history
Refactor RuboCop diagnostics for better encapsulation
  • Loading branch information
paracycle authored Oct 19, 2023
2 parents 7659d83 + 8e1bc9e commit 3ba35a5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lib/ruby_lsp/requests/support/rubocop_diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ class RuboCopDiagnostic
T::Hash[Symbol, Integer],
)

# Cache cops to attach URLs to diagnostics. Only built-in cops for now.
COP_TO_DOC_URL = T.let(
RuboCop::Cop::Registry.global.to_h,
T::Hash[String, [T.class_of(RuboCop::Cop::Base)]],
)

sig { params(offense: RuboCop::Cop::Offense, uri: URI::Generic).void }
def initialize(offense, uri)
@offense = offense
Expand Down Expand Up @@ -53,16 +47,6 @@ def to_lsp_code_action

sig { returns(Interface::Diagnostic) }
def to_lsp_diagnostic
severity = RUBOCOP_TO_LSP_SEVERITY[@offense.severity.name]
message = @offense.message

message += "\n\nThis offense is not auto-correctable.\n" unless @offense.correctable?

cop = COP_TO_DOC_URL[@offense.cop_name]&.first
if cop&.documentation_url
code_description = { href: cop.documentation_url }
end

Interface::Diagnostic.new(
message: message,
source: "RuboCop",
Expand All @@ -88,6 +72,24 @@ def to_lsp_diagnostic

private

sig { returns(String) }
def message
message = @offense.message
message += "\n\nThis offense is not auto-correctable.\n" unless @offense.correctable?
message
end

sig { returns(T.nilable(Integer)) }
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
Interface::CodeDescription.new(href: doc_url) if doc_url
end

sig { returns(T::Array[Interface::TextEdit]) }
def offense_replacements
@offense.corrector.as_replacements.map do |range, replacement|
Expand Down
19 changes: 19 additions & 0 deletions lib/ruby_lsp/requests/support/rubocop_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ def formatted_source
@options[:stdin]
end

class << self
extend T::Sig

sig { params(cop_name: String).returns(T.nilable(T.class_of(RuboCop::Cop::Base))) }
def find_cop_by_name(cop_name)
cop_registry[cop_name]&.first
end

private

sig { returns(T::Hash[String, [T.class_of(RuboCop::Cop::Base)]]) }
def cop_registry
@cop_registry ||= T.let(
RuboCop::Cop::Registry.global.to_h,
T.nilable(T::Hash[String, [T.class_of(RuboCop::Cop::Base)]]),
)
end
end

private

sig { params(_file: String, offenses: T::Array[RuboCop::Cop::Offense]).void }
Expand Down

0 comments on commit 3ba35a5

Please sign in to comment.