Class: RubyLsp::BaseServer
- Inherits:
-
Object
- Object
- RubyLsp::BaseServer
- Extended by:
- T::Helpers, T::Sig
- Defined in:
- lib/ruby_lsp/base_server.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(test_mode: false) ⇒ BaseServer
constructor
A new instance of BaseServer.
- #new_worker ⇒ Object
- #pop_response ⇒ Object
- #process_message(message) ⇒ Object
- #run_shutdown ⇒ Object
- #send_empty_response(id) ⇒ Object
- #send_log_message(message, type: Constant::MessageType::LOG) ⇒ Object
- #send_message(message) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(test_mode: false) ⇒ BaseServer
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ruby_lsp/base_server.rb', line 12 def initialize(test_mode: false) @test_mode = T.let(test_mode, T::Boolean) @writer = T.let(Transport::Stdio::Writer.new, Transport::Stdio::Writer) @reader = T.let(Transport::Stdio::Reader.new, Transport::Stdio::Reader) @incoming_queue = T.let(Thread::Queue.new, Thread::Queue) @outgoing_queue = T.let(Thread::Queue.new, Thread::Queue) @cancelled_requests = T.let([], T::Array[Integer]) @mutex = T.let(Mutex.new, Mutex) @worker = T.let(new_worker, Thread) @current_request_id = T.let(1, Integer) @store = T.let(Store.new, Store) @outgoing_dispatcher = T.let( Thread.new do unless test_mode while ( = @outgoing_queue.pop) @mutex.synchronize { @writer.write(.to_hash) } end end end, Thread, ) Thread.main.priority = 1 end |
Instance Method Details
#new_worker ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ruby_lsp/base_server.rb', line 114 def new_worker Thread.new do while ( = T.let(@incoming_queue.pop, T.nilable(T::Hash[Symbol, T.untyped]))) id = [:id] # Check if the request was cancelled before trying to process it @mutex.synchronize do if id && @cancelled_requests.include?(id) (Result.new(id: id, response: nil)) @cancelled_requests.delete(id) next end end () end end end |
#pop_response ⇒ Object
103 104 105 |
# File 'lib/ruby_lsp/base_server.rb', line 103 def pop_response @outgoing_queue.pop end |
#process_message(message) ⇒ Object
108 |
# File 'lib/ruby_lsp/base_server.rb', line 108 def (); end |
#run_shutdown ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ruby_lsp/base_server.rb', line 89 def run_shutdown @incoming_queue.clear @outgoing_queue.clear @incoming_queue.close @outgoing_queue.close @cancelled_requests.clear @worker.join @outgoing_dispatcher.join @store.clear end |
#send_empty_response(id) ⇒ Object
145 146 147 |
# File 'lib/ruby_lsp/base_server.rb', line 145 def send_empty_response(id) (Result.new(id: id, response: nil)) end |
#send_log_message(message, type: Constant::MessageType::LOG) ⇒ Object
150 151 152 |
# File 'lib/ruby_lsp/base_server.rb', line 150 def (, type: Constant::MessageType::LOG) (Notification.(, type: Constant::MessageType::LOG)) end |
#send_message(message) ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'lib/ruby_lsp/base_server.rb', line 134 def () # When we're shutting down the server, there's a small race condition between closing the thread queues and # finishing remaining requests. We may close the queue in the middle of processing a request, which will then fail # when trying to send a response back return if @outgoing_queue.closed? @outgoing_queue << @current_request_id += 1 if .is_a?(Request) end |
#shutdown ⇒ Object
111 |
# File 'lib/ruby_lsp/base_server.rb', line 111 def shutdown; end |
#start ⇒ Object
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 |
# File 'lib/ruby_lsp/base_server.rb', line 38 def start @reader.read do || method = [:method] # We must parse the document under a mutex lock or else we might switch threads and accept text edits in the # source. Altering the source reference during parsing will put the parser in an invalid internal state, since # it started parsing with one source but then it changed in the middle. We don't want to do this for text # synchronization notifications @mutex.synchronize do uri = .dig(:params, :textDocument, :uri) if uri begin parsed_uri = URI(uri) [:params][:textDocument][:uri] = parsed_uri # We don't want to try to parse documents on text synchronization notifications @store.get(parsed_uri).parse unless method.start_with?("textDocument/did") rescue Store::NonExistingDocumentError # If we receive a request for a file that no longer exists, we don't want to fail end end end # The following requests need to be executed in the main thread directly to avoid concurrency issues. Everything # else is pushed into the incoming queue case method when "initialize", "initialized", "textDocument/didOpen", "textDocument/didClose", "textDocument/didChange" () when "shutdown" ("Shutting down Ruby LSP...") shutdown @mutex.synchronize do run_shutdown @writer.write(Result.new(id: [:id], response: nil).to_hash) end when "exit" @mutex.synchronize do status = @incoming_queue.closed? ? 0 : 1 ("Shutdown complete with status #{status}") exit(status) end else @incoming_queue << end end end |