Class: Mui::Lsp::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/mui/lsp/manager.rb

Overview

Manages multiple LSP clients and coordinates between editor and servers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(editor:) ⇒ Manager

Returns a new instance of Manager.



9
10
11
12
13
14
15
16
17
# File 'lib/mui/lsp/manager.rb', line 9

def initialize(editor:)
  @editor = editor
  @server_configs = {}
  @clients = {}
  @text_syncs = {}
  @diagnostics_handler = Handlers::Diagnostics.new(editor: editor)
  @mutex = Mutex.new
  @pending_documents = {} # file_path => text, documents waiting for server startup
end

Instance Attribute Details

#diagnostics_handlerObject (readonly)

Returns the value of attribute diagnostics_handler.



7
8
9
# File 'lib/mui/lsp/manager.rb', line 7

def diagnostics_handler
  @diagnostics_handler
end

#editorObject (readonly)

Returns the value of attribute editor.



7
8
9
# File 'lib/mui/lsp/manager.rb', line 7

def editor
  @editor
end

Instance Method Details

#auto_start_for(file_path) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/mui/lsp/manager.rb', line 114

def auto_start_for(file_path)
  servers_to_start = []

  @mutex.synchronize do
    @server_configs.each do |name, config|
      next unless config.auto_start && config.handles_file?(file_path)
      next if @clients[name]&.running?

      servers_to_start << [name, find_project_root(file_path)]
    end
  end

  return false if servers_to_start.empty?

  # Start servers in background thread to avoid blocking editor startup
  Thread.new do
    servers_to_start.each do |name, root_path|
      start_server(name, root_path)
      # Send pending documents after server starts
      send_pending_documents(name)
    rescue StandardError => e
      @editor.message = "LSP auto-start error (#{name}): #{e.message}"
    end
  end

  true
end

#client_for(file_path) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/mui/lsp/manager.rb', line 80

def client_for(file_path)
  @mutex.synchronize do
    @server_configs.each do |name, config|
      return @clients[name] if config.handles_file?(file_path) && @clients[name]&.running?
    end
  end
  nil
end

#completion(file_path:, line:, character:) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/mui/lsp/manager.rb', line 247

def completion(file_path:, line:, character:)
  client = client_for(file_path)
  unless client
    @editor.message = server_unavailable_message(file_path)
    return
  end

  uri = TextDocumentSync.path_to_uri(file_path)
  handler = Handlers::Completion.new(editor: @editor, client: client)

  client.completion(uri: uri, line: line, character: character) do |result, error|
    handler.handle(result, error)
  end
end

#debug_infoObject



278
279
280
281
282
283
284
285
286
287
288
# File 'lib/mui/lsp/manager.rb', line 278

def debug_info
  @mutex.synchronize do
    @clients.transform_values do |client|
      {
        started: client.started?,
        initialized: client.initialized,
        last_stderr: client.last_stderr
      }
    end
  end
end

#definition(file_path:, line:, character:) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/mui/lsp/manager.rb', line 217

def definition(file_path:, line:, character:)
  client = client_for(file_path)
  unless client
    @editor.message = server_unavailable_message(file_path)
    return
  end

  uri = TextDocumentSync.path_to_uri(file_path)
  handler = Handlers::Definition.new(editor: @editor, client: client)

  client.definition(uri: uri, line: line, character: character) do |result, error|
    handler.handle(result, error)
  end
end

#did_change(file_path:, text:) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/mui/lsp/manager.rb', line 157

def did_change(file_path:, text:)
  uri = TextDocumentSync.path_to_uri(file_path)
  # Broadcast to all matching servers (each text_sync checks sync_on_change)
  text_syncs_for(file_path).each do |text_sync|
    text_sync.did_change(uri: uri, text: text)
  end
end

#did_close(file_path:) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/mui/lsp/manager.rb', line 190

def did_close(file_path:)
  @mutex.synchronize do
    @pending_documents.delete(file_path)
  end

  uri = TextDocumentSync.path_to_uri(file_path)
  # Broadcast to all matching servers
  text_syncs_for(file_path).each do |text_sync|
    text_sync.did_close(uri: uri)
  end
end

#did_open(file_path:, text:) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/mui/lsp/manager.rb', line 142

def did_open(file_path:, text:)
  # Store document for servers that are starting up
  @mutex.synchronize do
    @pending_documents[file_path] = text
  end

  auto_start_for(file_path)

  uri = TextDocumentSync.path_to_uri(file_path)
  # Broadcast to all matching servers that are already running
  text_syncs_for(file_path).each do |text_sync|
    text_sync.did_open(uri: uri, text: text)
  end
end

#did_save(file_path:, text: nil) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/mui/lsp/manager.rb', line 182

def did_save(file_path:, text: nil)
  uri = TextDocumentSync.path_to_uri(file_path)
  # Broadcast to all matching servers
  text_syncs_for(file_path).each do |text_sync|
    text_sync.did_save(uri: uri, text: text)
  end
end

#force_reopen(file_path:, text:) ⇒ Object

Force close and re-open document to reset LSP state



174
175
176
177
178
179
180
# File 'lib/mui/lsp/manager.rb', line 174

def force_reopen(file_path:, text:)
  uri = TextDocumentSync.path_to_uri(file_path)
  text_syncs_for(file_path).each do |text_sync|
    text_sync.did_close(uri: uri) if text_sync.open?(uri)
    text_sync.did_open(uri: uri, text: text)
  end
end

#hover(file_path:, line:, character:) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/mui/lsp/manager.rb', line 202

def hover(file_path:, line:, character:)
  client = client_for(file_path)
  unless client
    @editor.message = server_unavailable_message(file_path)
    return
  end

  uri = TextDocumentSync.path_to_uri(file_path)
  handler = Handlers::Hover.new(editor: @editor, client: client)

  client.hover(uri: uri, line: line, character: character) do |result, error|
    handler.handle(result, error)
  end
end

#notification_logObject



290
291
292
# File 'lib/mui/lsp/manager.rb', line 290

def notification_log
  @notification_log ||= []
end

#references(file_path:, line:, character:) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/mui/lsp/manager.rb', line 232

def references(file_path:, line:, character:)
  client = client_for(file_path)
  unless client
    @editor.message = server_unavailable_message(file_path)
    return
  end

  uri = TextDocumentSync.path_to_uri(file_path)
  handler = Handlers::References.new(editor: @editor, client: client)

  client.references(uri: uri, line: line, character: character) do |result, error|
    handler.handle(result, error)
  end
end

#register_server(config) ⇒ Object



19
20
21
22
23
# File 'lib/mui/lsp/manager.rb', line 19

def register_server(config)
  @mutex.synchronize do
    @server_configs[config.name] = config
  end
end

#registered_serversObject



274
275
276
# File 'lib/mui/lsp/manager.rb', line 274

def registered_servers
  @mutex.synchronize { @server_configs.keys }
end

#running_serversObject



262
263
264
265
266
# File 'lib/mui/lsp/manager.rb', line 262

def running_servers
  @mutex.synchronize do
    @clients.select { |_, client| client.running? }.keys
  end
end

#start_server(name, root_path = nil) ⇒ Object



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
# File 'lib/mui/lsp/manager.rb', line 25

def start_server(name, root_path = nil)
  config = @mutex.synchronize { @server_configs[name] }
  unless config
    @editor.message = "LSP Error: Unknown server: #{name}"
    return false
  end

  root_path ||= Dir.pwd

  client = Client.new(
    command: config.command,
    root_path: root_path,
    on_notification: method(:handle_notification)
  )

  text_sync = TextDocumentSync.new(
    client: client,
    server_config: config
  )

  client.start

  @mutex.synchronize do
    @clients[name] = client
    @text_syncs[name] = text_sync
  end

  # Check if initialized after a short delay
  @editor.message = if client.initialized
                      "LSP: #{name} ready"
                    else
                      "LSP: #{name} starting..."
                    end
  true
rescue StandardError => e
  @editor.message = "LSP Error: #{e.message}"
  false
end

#starting_serversObject



268
269
270
271
272
# File 'lib/mui/lsp/manager.rb', line 268

def starting_servers
  @mutex.synchronize do
    @clients.select { |_, client| client.started? && !client.initialized }.keys
  end
end

#stop_allObject



75
76
77
78
# File 'lib/mui/lsp/manager.rb', line 75

def stop_all
  names = @mutex.synchronize { @clients.keys.dup }
  names.each { |name| stop_server(name) }
end

#stop_server(name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/mui/lsp/manager.rb', line 64

def stop_server(name)
  client = @mutex.synchronize { @clients.delete(name) }
  text_sync = @mutex.synchronize { @text_syncs.delete(name) }

  return unless client

  text_sync&.close_all
  client.stop
  @editor.message = "LSP server stopped: #{name}"
end

#sync_now(file_path:, text:) ⇒ Object

Sync immediately without debounce (for completion requests)



166
167
168
169
170
171
# File 'lib/mui/lsp/manager.rb', line 166

def sync_now(file_path:, text:)
  uri = TextDocumentSync.path_to_uri(file_path)
  text_syncs_for(file_path).each do |text_sync|
    text_sync.did_change(uri: uri, text: text, debounce: false, force: true)
  end
end

#text_sync_for(file_path) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mui/lsp/manager.rb', line 89

def text_sync_for(file_path)
  @mutex.synchronize do
    @server_configs.each do |name, config|
      # Only return text_sync if the client is running
      next unless config.handles_file?(file_path)
      next unless @clients[name]&.running?
      return @text_syncs[name] if @text_syncs[name]
    end
  end
  nil
end

#text_syncs_for(file_path) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mui/lsp/manager.rb', line 101

def text_syncs_for(file_path)
  result = []
  @mutex.synchronize do
    @server_configs.each do |name, config|
      next unless config.handles_file?(file_path)
      next unless @clients[name]&.running?

      result << @text_syncs[name] if @text_syncs[name]
    end
  end
  result
end