class RuboCop::LSP::Routes

Routes for Language Server Protocol of RuboCop. @api private

Public Class Methods

new(server) click to toggle source
# File lib/rubocop/lsp/routes.rb, line 25
def initialize(server)
  @server = server

  @text_cache = {}
end

Private Class Methods

handle(name, &block) click to toggle source
# File lib/rubocop/lsp/routes.rb, line 19
def self.handle(name, &block)
  define_method(:"handle_#{name}", &block)
end

Public Instance Methods

for(name) click to toggle source
# File lib/rubocop/lsp/routes.rb, line 31
def for(name)
  name = "handle_#{name}"
  return unless respond_to?(name)

  method(name)
end
handle_method_missing(request) click to toggle source
# File lib/rubocop/lsp/routes.rb, line 167
def handle_method_missing(request)
  return unless request.key?(:id)

  @server.write(id: request[:id], result: nil)
end
handle_unsupported_method(request, method = request[:method]) click to toggle source
# File lib/rubocop/lsp/routes.rb, line 156
def handle_unsupported_method(request, method = request[:method])
  @server.write(
    id: request[:id],
    error: LanguageServer::Protocol::Interface::ResponseError.new(
      code: LanguageServer::Protocol::Constant::ErrorCodes::METHOD_NOT_FOUND,
      message: "Unsupported Method: #{method}"
    )
  )
  Logger.log("Unsupported Method: #{method}")
end

Private Instance Methods

diagnostic(file_uri, text) click to toggle source
# File lib/rubocop/lsp/routes.rb, line 205
def diagnostic(file_uri, text)
  @text_cache[file_uri] = text
  offenses = @server.offenses(remove_file_protocol_from(file_uri), text)
  diagnostics = offenses.map { |offense| to_diagnostic(offense) }

  {
    method: 'textDocument/publishDiagnostics',
    params: {
      uri: file_uri,
      diagnostics: diagnostics
    }
  }
end
extract_initialization_options_from(request) click to toggle source
# File lib/rubocop/lsp/routes.rb, line 175
def extract_initialization_options_from(request)
  safe_autocorrect = request.dig(:params, :initializationOptions, :safeAutocorrect)

  {
    safe_autocorrect: safe_autocorrect.nil? || safe_autocorrect == true,
    lint_mode: request.dig(:params, :initializationOptions, :lintMode) == true,
    layout_mode: request.dig(:params, :initializationOptions, :layoutMode) == true
  }
end
format_file(file_uri, command: nil) click to toggle source
# File lib/rubocop/lsp/routes.rb, line 185
def format_file(file_uri, command: nil)
  unless (text = @text_cache[file_uri])
    Logger.log("Format request arrived before text synchronized; skipping: `#{file_uri}'")

    return []
  end

  new_text = @server.format(remove_file_protocol_from(file_uri), text, command: command)

  return [] if new_text == text

  [
    newText: new_text,
    range: {
      start: { line: 0, character: 0 },
      end: { line: text.count("\n") + 1, character: 0 }
    }
  ]
end
remove_file_protocol_from(uri) click to toggle source
# File lib/rubocop/lsp/routes.rb, line 219
def remove_file_protocol_from(uri)
  uri.delete_prefix('file://')
end
to_diagnostic(offense) click to toggle source
# File lib/rubocop/lsp/routes.rb, line 223
def to_diagnostic(offense)
  code = offense[:cop_name]
  message = offense[:message]
  loc = offense[:location]
  rubocop_severity = offense[:severity]
  severity = Severity.find_by(rubocop_severity)

  {
    code: code, message: message, range: to_range(loc), severity: severity, source: 'rubocop'
  }
end
to_range(location) click to toggle source
# File lib/rubocop/lsp/routes.rb, line 235
def to_range(location)
  {
    start: { character: location[:start_column] - 1, line: location[:start_line] - 1 },
    end: { character: location[:last_column], line: location[:last_line] - 1 }
  }
end