class Handler

Constants

Constant
Interface

Attributes

reader[R]
store[R]
writer[R]

Public Class Methods

new() click to toggle source
# File lib/rubocop/lsp/handler.rb, line 10
def initialize
  @writer = LanguageServer::Protocol::Transport::Stdio::Writer.new
  @reader = LanguageServer::Protocol::Transport::Stdio::Reader.new
  @handlers = {}
  @store = StateStore.new
end

Public Instance Methods

config(&blk) click to toggle source
# File lib/rubocop/lsp/handler.rb, line 17
def config(&blk)
  instance_exec(&blk)
end
start() click to toggle source
# File lib/rubocop/lsp/handler.rb, line 21
def start
  reader.read do |request|
    handle(request)
  end
end

Private Instance Methods

clear_diagnostics(uri:) click to toggle source
# File lib/rubocop/lsp/handler.rb, line 78
def clear_diagnostics(uri:)
  store.delete(uri)

  notify_diagnostics(uri: uri, diagnostics_response: [])
end
collect_diagnostics(uri:, text:) click to toggle source
# File lib/rubocop/lsp/handler.rb, line 112
def collect_diagnostics(uri:, text:)
  collector = DiagnosticCollector.new(uri: uri, text: text)
  collector.run

  store.set(uri: collector.uri, text: collector.text, diagnostics: collector.diagnostics)

  collector
end
handle(request) click to toggle source
# File lib/rubocop/lsp/handler.rb, line 39
def handle(request)
  log("Got request method: #{request[:method]}")
  handler = handler_for(request)
  result = handler.call(request) if handler
  writer.write(id: request[:id], result: result) if result
end
handler_for(request) click to toggle source
# File lib/rubocop/lsp/handler.rb, line 35
def handler_for(request)
  @handlers[request[:method]]
end
log(msg) click to toggle source
# File lib/rubocop/lsp/handler.rb, line 46
def log(msg)
  $stderr.puts(msg)
end
notify(method:, params: {}) click to toggle source
# File lib/rubocop/lsp/handler.rb, line 50
def notify(method:, params: {})
  log("In notify: #{writer}")
  writer.write(method: method, params: params)
end
notify_diagnostics(uri:, diagnostics_response:) click to toggle source
# File lib/rubocop/lsp/handler.rb, line 68
def notify_diagnostics(uri:, diagnostics_response:)
  notify(
    method: "textDocument/publishDiagnostics",
    params: Interface::PublishDiagnosticsParams.new(
      uri: uri,
      diagnostics: diagnostics_response
    )
  )
end
on(message, &blk) click to toggle source
# File lib/rubocop/lsp/handler.rb, line 31
def on(message, &blk)
  @handlers[message.to_s] = blk
end
respond_with_capabilities() click to toggle source
# File lib/rubocop/lsp/handler.rb, line 55
def respond_with_capabilities
  Interface::InitializeResult.new(
    capabilities: Interface::ServerCapabilities.new(
      text_document_sync: Interface::TextDocumentSyncOptions.new(
        change: Constant::TextDocumentSyncKind::FULL,
        open_close: true,
      ),
      document_formatting_provider: true,
      code_action_provider: true
    ),
  )
end
send_code_actions(uri, line_range) click to toggle source
# File lib/rubocop/lsp/handler.rb, line 91
def send_code_actions(uri, line_range)
  store.code_actions_for(uri, line_range)
end
send_diagnostics(collector) click to toggle source
# File lib/rubocop/lsp/handler.rb, line 84
def send_diagnostics(collector)
  notify_diagnostics(
    uri: collector.uri,
    diagnostics_response: collector.diagnostics.response
  )
end
send_text_edits(collector) click to toggle source
# File lib/rubocop/lsp/handler.rb, line 95
def send_text_edits(collector)
  return unless collector.formatted_text

  [
    Interface::TextEdit.new(
      range: Interface::Range.new(
        start: Interface::Position.new(line: 0, character: 0),
        end: Interface::Position.new(
          line: collector.text.size,
          character: collector.text.size
        )
      ),
      new_text: collector.formatted_text
    ),
  ]
end