class Model::DiagnosticCollection

Attributes

diagnostics[R]
uri[R]

Public Class Methods

new(uri, offenses) click to toggle source
# File lib/rubocop/lsp/model/diagnostic_collection.rb, line 12
def initialize(uri, offenses)
  @uri = uri
  @diagnostics = offenses.map do |offense|
    Diagnostic.new(offense)
  end
end

Public Instance Methods

code_actions(line_range) click to toggle source
# File lib/rubocop/lsp/model/diagnostic_collection.rb, line 27
def code_actions(line_range)
  line_diagnostics = correctable_diagnostics.select do |diagnostic|
    line_range.include?(diagnostic.start.line)
  end

  actions = line_diagnostics.map do |diagnostic|
    code_action_from(
      diagnostic,
      title: "Autocorrect `#{diagnostic.cop_name}`",
      kind: Constant::CodeActionKind::QUICK_FIX
    )
  end

  if actions.any?
    actions << code_action_from(
      line_diagnostics,
      title: "Autocorrect all offenses on line",
      kind: "rubocop.fix"
    )
  end

  actions
end
correctable_diagnostics() click to toggle source
# File lib/rubocop/lsp/model/diagnostic_collection.rb, line 23
def correctable_diagnostics
  @correctable_diagnostics ||= diagnostics.select(&:correctable?)
end
response() click to toggle source
# File lib/rubocop/lsp/model/diagnostic_collection.rb, line 19
def response
  diagnostics.map(&:diagnostic_response)
end

Private Instance Methods

code_action_from(diagnostics, title:, kind:) click to toggle source
# File lib/rubocop/lsp/model/diagnostic_collection.rb, line 53
def code_action_from(diagnostics, title:, kind:)
  diagnostics = Array(diagnostics)

  Interface::CodeAction.new(
    title: title,
    kind: kind,
    edit: Interface::WorkspaceEdit.new(
      document_changes: [
        Interface::TextDocumentEdit.new(
          text_document: Interface::OptionalVersionedTextDocumentIdentifier.new(
            uri: uri,
            version: nil
          ),
          edits: diagnostics.flat_map(&:replacements)
        ),
      ]
    ),
    is_preferred: true,
  )
end