Class: RubyLsp::Server

Inherits:
BaseServer show all
Extended by:
T::Sig
Defined in:
lib/ruby_lsp/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseServer

#new_worker, #pop_response, #run_shutdown, #send_empty_response, #send_log_message, #send_message, #start

Constructor Details

#initialize(test_mode: false) ⇒ Server



13
14
15
16
# File 'lib/ruby_lsp/server.rb', line 13

def initialize(test_mode: false)
  super
  @global_state = T.let(GlobalState.new, GlobalState)
end

Instance Attribute Details

#global_stateObject (readonly)

Returns the value of attribute global_state.



10
11
12
# File 'lib/ruby_lsp/server.rb', line 10

def global_state
  @global_state
end

Instance Method Details

#load_addonsObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ruby_lsp/server.rb', line 128

def load_addons
  errors = Addon.load_addons(@global_state, @outgoing_queue)

  if errors.any?
    send_log_message(
      "Error loading addons:\n\n#{errors.map(&:full_message).join("\n\n")}",
      type: Constant::MessageType::WARNING,
    )
  end

  errored_addons = Addon.addons.select(&:error?)

  if errored_addons.any?
    send_message(
      Notification.new(
        method: "window/showMessage",
        params: Interface::ShowMessageParams.new(
          type: Constant::MessageType::WARNING,
          message: "Error loading addons:\n\n#{errored_addons.map(&:formatted_errors).join("\n\n")}",
        ),
      ),
    )

    unless @test_mode
      send_log_message(
        errored_addons.map(&:errors_details).join("\n\n"),
        type: Constant::MessageType::WARNING,
      )
    end
  end
end

#process_message(message) ⇒ Object



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruby_lsp/server.rb', line 19

def process_message(message)
  case message[:method]
  when "initialize"
    send_log_message("Initializing Ruby LSP v#{VERSION}...")
    run_initialize(message)
  when "initialized"
    send_log_message("Finished initializing Ruby LSP!") unless @test_mode
    run_initialized
  when "textDocument/didOpen"
    text_document_did_open(message)
  when "textDocument/didClose"
    text_document_did_close(message)
  when "textDocument/didChange"
    text_document_did_change(message)
  when "textDocument/selectionRange"
    text_document_selection_range(message)
  when "textDocument/documentSymbol"
    text_document_document_symbol(message)
  when "textDocument/documentLink"
    text_document_document_link(message)
  when "textDocument/codeLens"
    text_document_code_lens(message)
  when "textDocument/semanticTokens/full"
    text_document_semantic_tokens_full(message)
  when "textDocument/foldingRange"
    text_document_folding_range(message)
  when "textDocument/semanticTokens/range"
    text_document_semantic_tokens_range(message)
  when "textDocument/formatting"
    text_document_formatting(message)
  when "textDocument/documentHighlight"
    text_document_document_highlight(message)
  when "textDocument/onTypeFormatting"
    text_document_on_type_formatting(message)
  when "textDocument/hover"
    text_document_hover(message)
  when "textDocument/inlayHint"
    text_document_inlay_hint(message)
  when "textDocument/codeAction"
    text_document_code_action(message)
  when "codeAction/resolve"
    code_action_resolve(message)
  when "textDocument/diagnostic"
    text_document_diagnostic(message)
  when "textDocument/completion"
    text_document_completion(message)
  when "completionItem/resolve"
    text_document_completion_item_resolve(message)
  when "textDocument/signatureHelp"
    text_document_signature_help(message)
  when "textDocument/definition"
    text_document_definition(message)
  when "textDocument/prepareTypeHierarchy"
    text_document_prepare_type_hierarchy(message)
  when "typeHierarchy/supertypes"
    type_hierarchy_supertypes(message)
  when "typeHierarchy/subtypes"
    type_hierarchy_subtypes(message)
  when "workspace/didChangeWatchedFiles"
    workspace_did_change_watched_files(message)
  when "workspace/symbol"
    workspace_symbol(message)
  when "rubyLsp/textDocument/showSyntaxTree"
    text_document_show_syntax_tree(message)
  when "rubyLsp/workspace/dependencies"
    workspace_dependencies(message)
  when "rubyLsp/workspace/addons"
    send_message(
      Result.new(
        id: message[:id],
        response:
          Addon.addons.map do |addon|
            { name: addon.name, errored: addon.error? }
          end,
      ),
    )
  when "$/cancelRequest"
    @mutex.synchronize { @cancelled_requests << message[:params][:id] }
  end
rescue StandardError, LoadError => e
  # If an error occurred in a request, we have to return an error response or else the editor will hang
  if message[:id]
    # If a document is deleted before we are able to process all of its enqueued requests, we will try to read it
    # from disk and it raise this error. This is expected, so we don't include the `data` attribute to avoid
    # reporting these to our telemetry
    if e.is_a?(Store::NonExistingDocumentError)
      send_message(Error.new(
        id: message[:id],
        code: Constant::ErrorCodes::INVALID_PARAMS,
        message: e.full_message,
      ))
    else
      send_message(Error.new(
        id: message[:id],
        code: Constant::ErrorCodes::INTERNAL_ERROR,
        message: e.full_message,
        data: {
          errorClass: e.class.name,
          errorMessage: e.message,
          backtrace: e.backtrace&.join("\n"),
        },
      ))
    end
  end

  send_log_message("Error processing #{message[:method]}: #{e.full_message}", type: Constant::MessageType::ERROR)
end