Class: SorbetView::Lsp::Transport
- Inherits:
-
Object
- Object
- SorbetView::Lsp::Transport
- Extended by:
- T::Sig
- Defined in:
- lib/sorbet_view/lsp/transport.rb
Overview
JSON-RPC over stdio transport for LSP
Instance Method Summary collapse
-
#initialize(input: $stdin, output: $stdout) ⇒ Transport
constructor
A new instance of Transport.
- #read_message ⇒ Object
- #send_error(id, code, message) ⇒ Object
- #send_notification(method_name, params) ⇒ Object
- #send_request(id, method_name, params) ⇒ Object
- #send_response(id, result) ⇒ Object
- #write_message(message) ⇒ Object
Constructor Details
#initialize(input: $stdin, output: $stdout) ⇒ Transport
Returns a new instance of Transport.
14 15 16 17 18 |
# File 'lib/sorbet_view/lsp/transport.rb', line 14 def initialize(input: $stdin, output: $stdout) @input = T.let(input, T.any(IO, StringIO)) @output = T.let(output, T.any(IO, StringIO)) @mutex = T.let(Mutex.new, Mutex) end |
Instance Method Details
#read_message ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/sorbet_view/lsp/transport.rb', line 23 def # Read headers content_length = nil loop do line = @input.gets return nil if line.nil? # EOF line = line.strip break if line.empty? # End of headers if line.start_with?('Content-Length:') content_length = line.split(':').last&.strip&.to_i end end return nil unless content_length # Read body body = @input.read(content_length) return nil if body.nil? JSON.parse(body) end |
#send_error(id, code, message) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/sorbet_view/lsp/transport.rb', line 68 def send_error(id, code, ) ({ 'jsonrpc' => '2.0', 'id' => id, 'error' => { 'code' => code, 'message' => } }) end |
#send_notification(method_name, params) ⇒ Object
78 79 80 |
# File 'lib/sorbet_view/lsp/transport.rb', line 78 def send_notification(method_name, params) ({ 'jsonrpc' => '2.0', 'method' => method_name, 'params' => params }) end |
#send_request(id, method_name, params) ⇒ Object
84 85 86 |
# File 'lib/sorbet_view/lsp/transport.rb', line 84 def send_request(id, method_name, params) ({ 'jsonrpc' => '2.0', 'id' => id, 'method' => method_name, 'params' => params }) end |
#send_response(id, result) ⇒ Object
62 63 64 |
# File 'lib/sorbet_view/lsp/transport.rb', line 62 def send_response(id, result) ({ 'jsonrpc' => '2.0', 'id' => id, 'result' => result }) end |
#write_message(message) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/sorbet_view/lsp/transport.rb', line 49 def () body = JSON.generate() header = "Content-Length: #{body.bytesize}\r\n\r\n" @mutex.synchronize do @output.write(header) @output.write(body) @output.flush end end |