Class: RubyLsp::BaseServer Abstract
- Inherits:
-
Object
- Object
- RubyLsp::BaseServer
- Defined in:
- lib/ruby_lsp/base_server.rb
Overview
This class is abstract.
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(**options) ⇒ BaseServer
constructor
: (**untyped options) -> void.
-
#pop_response ⇒ Object
This method is only intended to be used in tests! Pops the latest response that would be sent to the client : -> untyped.
-
#process_message(message) ⇒ Object
abstract
: (Hash[Symbol, untyped] message) -> void.
-
#push_message(message) ⇒ Object
This method is only intended to be used in tests! Pushes a message to the incoming queue directly : (Hash[Symbol, untyped] message) -> void.
-
#run_shutdown ⇒ Object
: -> void.
-
#start ⇒ Object
: -> void.
-
#test_mode? ⇒ Boolean
: -> bool?.
Constructor Details
#initialize(**options) ⇒ BaseServer
: (**untyped options) -> void
8 9 10 11 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 8 def initialize(**) @reader = MessageReader.new([:reader] || $stdin) #: MessageReader @writer = MessageWriter.new([:writer] || $stdout) #: MessageWriter @test_mode = [:test_mode] #: bool? @setup_error = [:setup_error] #: StandardError? @install_error = [:install_error] #: StandardError? @incoming_queue = Thread::Queue.new #: Thread::Queue @outgoing_queue = Thread::Queue.new #: Thread::Queue @cancelled_requests = [] #: Array[Integer] @worker = new_worker #: Thread @current_request_id = 1 #: Integer @global_state = GlobalState.new #: GlobalState @store = Store.new(@global_state) #: Store @outgoing_dispatcher = Thread.new do unless @test_mode while ( = @outgoing_queue.pop) @global_state.synchronize { @writer.write(.to_hash) } end end end #: Thread Thread.main.priority = 1 # We read the initialize request in `exe/ruby-lsp` to be able to determine the workspace URI where Bundler should # be set up initialize_request = [:initialize_request] (initialize_request) if initialize_request end |
Instance Method Details
#pop_response ⇒ Object
This method is only intended to be used in tests! Pops the latest response that would be sent to the client : -> untyped
105 106 107 |
# File 'lib/ruby_lsp/base_server.rb', line 105 def pop_response @outgoing_queue.pop(timeout: 20) || raise("No message received from server") end |
#process_message(message) ⇒ Object
This method is abstract.
: (Hash[Symbol, untyped] message) -> void
117 118 119 |
# File 'lib/ruby_lsp/base_server.rb', line 117 def () raise AbstractMethodInvokedError end |
#push_message(message) ⇒ Object
This method is only intended to be used in tests! Pushes a message to the incoming queue directly : (Hash[Symbol, untyped] message) -> void
111 112 113 |
# File 'lib/ruby_lsp/base_server.rb', line 111 def () @incoming_queue << end |
#run_shutdown ⇒ Object
: -> void
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ruby_lsp/base_server.rb', line 127 def run_shutdown @incoming_queue.clear @outgoing_queue.clear @incoming_queue.close @outgoing_queue.close @cancelled_requests.clear @worker.terminate @outgoing_dispatcher.terminate @store.clear end |
#start ⇒ Object
: -> void
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 |
# File 'lib/ruby_lsp/base_server.rb', line 38 def start @reader. 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 @global_state.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 unless method.start_with?("textDocument/did") document = @store.get(parsed_uri) # If the client supports request delegation and we're working with an ERB document and there was # something to parse, then we have to maintain the client updated about the virtual state of the host # language source if document.parse! && @global_state.client_capabilities.supports_request_delegation && document.is_a?(ERBDocument) ( Notification.new( method: "delegate/textDocument/virtualState", params: { textDocument: { uri: uri, text: document.host_language_source, }, }, ), ) end end 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", "rubyLsp/diagnoseState", "$/cancelRequest" () when "shutdown" @global_state.synchronize do ("Shutting down Ruby LSP...") shutdown run_shutdown @writer.write(Result.new(id: [:id], response: nil).to_hash) end when "exit" exit(@incoming_queue.closed? ? 0 : 1) else @incoming_queue << end end end |
#test_mode? ⇒ Boolean
: -> bool?
122 123 124 |
# File 'lib/ruby_lsp/base_server.rb', line 122 def test_mode? @test_mode end |