Class: RubyLanguageServer::GoodCop

Inherits:
RuboCop::Runner
  • Object
show all
Defined in:
lib/ruby_language_server/good_cop.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path, initialization_error = nil) ⇒ GoodCop

Returns a new instance of GoodCop.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ruby_language_server/good_cop.rb', line 7

def initialize(config_path, initialization_error = nil)
  @initialization_error = initialization_error
  unless @initialization_error
    initialize_rubocop_ivars
    @config_store.options_config = config_path
    RubyLanguageServer.logger.debug("Rubocop config_path: #{config_path}")
    super({}, @config_store)
  end
rescue Exception => e
  RubyLanguageServer.logger.error(e)
  @initialization_error = "There was an issue loading the rubocop configuration file: #{e}.  Maybe you need to add some additional gems to the ide-ruby settings?"
end

Class Method Details

.instanceObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ruby_language_server/good_cop.rb', line 138

def instance
  return @instance if @instance

  @config_path ||= config_path
  config_path_timestamp = File.mtime(@config_path)
  if @cached_config_path_timestamp.nil? || @cached_config_path_timestamp < config_path_timestamp
    @cached_config_path_timestamp = config_path_timestamp
    @instance = new(@config_path)
  else
    @instance = new
  end
rescue StandardError => e
  @instance = new(@config_path, e.to_s)
end

Instance Method Details

#diagnostic_severity_for(severity) ⇒ Object

interface Diagnostic

/**
 * The range at which the message applies.
 */
range: Range;

/**
 * The diagnostic's severity. Can be omitted. If omitted it is up to the
 * client to interpret diagnostics as error, warning, info or hint.
 */
severity?: number;

/**
 * The diagnostic's code. Can be omitted.
 */
code?: number | string;

/**
 * A human-readable string describing the source of this
 * diagnostic, e.g. 'typescript' or 'super lint'.
 */
source?: string;

/**
 * The diagnostic's message.
 */
message: string;



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruby_language_server/good_cop.rb', line 68

def diagnostic_severity_for(severity)
  case severity.to_s
  when 'error', 'fatal'
    1
  when 'warning'
    2
  when 'refactor', 'convention'
    3
  else
    RubyLanguageServer.logger.error("Could not map severity for #{severity} - returning 2")
    2
  end
end

#diagnostics(text, filename = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruby_language_server/good_cop.rb', line 82

def diagnostics(text, filename = nil)
  return initialization_offenses unless @initialization_error.nil?

  maximum_severity = 4 # (ENV['LINT_LEVEL'] || 4).to_i
  enabled_offenses = offenses(text, filename).reject { |offense| offense.status == :disabled }
  enabled_offenses.map do |offense|
    {
      range: Location.position_hash(offense.location.line, offense.location.column, offense.location.last_line, offense.location.last_column),
      severity: diagnostic_severity_for(offense.severity),
      # code?: number | string;
      code: 'code',
      source: "RuboCop:#{offense.cop_name}",
      message: offense.message
    }
  end.select { |hash| hash[:severity] <= maximum_severity }
end