Class: RubyLsp::Requests::Support::RuboCopDiagnostic
- Inherits:
-
Object
- Object
- RubyLsp::Requests::Support::RuboCopDiagnostic
- Extended by:
- T::Sig
- Defined in:
- lib/ruby_lsp/requests/support/rubocop_diagnostic.rb
Constant Summary collapse
- RUBOCOP_TO_LSP_SEVERITY =
T.let({ convention: LanguageServer::Protocol::Constant::DiagnosticSeverity::INFORMATION, info: LanguageServer::Protocol::Constant::DiagnosticSeverity::INFORMATION, refactor: LanguageServer::Protocol::Constant::DiagnosticSeverity::INFORMATION, warning: LanguageServer::Protocol::Constant::DiagnosticSeverity::WARNING, error: LanguageServer::Protocol::Constant::DiagnosticSeverity::ERROR, fatal: LanguageServer::Protocol::Constant::DiagnosticSeverity::ERROR, }.freeze, T::Hash[Symbol, Integer])
Instance Attribute Summary collapse
-
#replacements ⇒ Object
readonly
Returns the value of attribute replacements.
Instance Method Summary collapse
- #correctable? ⇒ Boolean
- #in_range?(range) ⇒ Boolean
-
#initialize(offense, uri) ⇒ RuboCopDiagnostic
constructor
A new instance of RuboCopDiagnostic.
- #to_lsp_code_action ⇒ Object
- #to_lsp_diagnostic ⇒ Object
Constructor Details
#initialize(offense, uri) ⇒ RuboCopDiagnostic
Returns a new instance of RuboCopDiagnostic.
23 24 25 26 27 28 29 30 |
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 23 def initialize(offense, uri) @offense = offense @uri = uri @replacements = T.let( offense.correctable? ? offense_replacements : [], T::Array[LanguageServer::Protocol::Interface::TextEdit] ) end |
Instance Attribute Details
#replacements ⇒ Object (readonly)
Returns the value of attribute replacements.
20 21 22 |
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 20 def replacements @replacements end |
Instance Method Details
#correctable? ⇒ Boolean
33 34 35 |
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 33 def correctable? @offense.correctable? end |
#in_range?(range) ⇒ Boolean
38 39 40 |
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 38 def in_range?(range) range.cover?(@offense.line - 1) end |
#to_lsp_code_action ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 43 def to_lsp_code_action LanguageServer::Protocol::Interface::CodeAction.new( title: "Autocorrect #{@offense.cop_name}", kind: LanguageServer::Protocol::Constant::CodeActionKind::QUICK_FIX, edit: LanguageServer::Protocol::Interface::WorkspaceEdit.new( document_changes: [ LanguageServer::Protocol::Interface::TextDocumentEdit.new( text_document: LanguageServer::Protocol::Interface::OptionalVersionedTextDocumentIdentifier.new( uri: @uri, version: nil ), edits: @replacements ), ] ), is_preferred: true, ) end |
#to_lsp_diagnostic ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ruby_lsp/requests/support/rubocop_diagnostic.rb', line 63 def to_lsp_diagnostic if @offense.correctable? severity = RUBOCOP_TO_LSP_SEVERITY[@offense.severity.name] = @offense. else severity = LanguageServer::Protocol::Constant::DiagnosticSeverity::WARNING = "#{@offense.message}\n\nThis offense is not auto-correctable.\n" end LanguageServer::Protocol::Interface::Diagnostic.new( message: , source: "RuboCop", code: @offense.cop_name, severity: severity, range: LanguageServer::Protocol::Interface::Range.new( start: LanguageServer::Protocol::Interface::Position.new( line: @offense.line - 1, character: @offense.column ), end: LanguageServer::Protocol::Interface::Position.new( line: @offense.last_line - 1, character: @offense.last_column ) ) ) end |