Module: Rubocop::Lsp::Cli

Defined in:
lib/rubocop/lsp/cli.rb

Class Method Summary collapse

Class Method Details

.start(_argv) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rubocop/lsp/cli.rb', line 12

def self.start(_argv)
  handler = Handler.new

  handler.config do
    on("initialize") do
      store.clear

      respond_with_capabilities
    end

    on("shutdown") do
      store.clear

      nil
    end

    on("textDocument/didChange") do |request|
      uri = request.dig(:params, :textDocument, :uri)
      text = request.dig(:params, :contentChanges, 0, :text)

      collector = collect_diagnostics(uri: uri, text: text)
      send_diagnostics(collector)

      nil
    end

    on("textDocument/didOpen") do |request|
      uri = request.dig(:params, :textDocument, :uri)
      text = request.dig(:params, :textDocument, :text)

      collector = collect_diagnostics(uri: uri, text: text)
      send_diagnostics(collector)

      nil
    end

    on("textDocument/didClose") do |request|
      uri = request.dig(:params, :textDocument, :uri)

      clear_diagnostics(uri: uri)

      nil
    end

    on("textDocument/formatting") do |request|
      uri = request.dig(:params, :textDocument, :uri)

      collector = collect_diagnostics(uri: uri, text: store.text_for(uri))
      send_text_edits(collector)
    end

    on("textDocument/codeAction") do |request|
      uri = request.dig(:params, :textDocument, :uri)
      range = request.dig(:params, :range)
      start_line = range.dig(:start, :line)
      end_line = range.dig(:end, :line)

      line_range = Range.new(start_line, end_line)

      send_code_actions(uri, line_range)
    end
  end

  handler.start
end