Class: Rfmt::LSP::MessageIO
- Inherits:
-
Object
- Object
- Rfmt::LSP::MessageIO
- Defined in:
- lib/rfmt/lsp/message_io.rb
Overview
Reads and writes LSP JSON-RPC messages over an IO pair.
Constant Summary collapse
- HEADER_SEPARATOR =
"\r\n\r\n"- CONTENT_LENGTH =
/\AContent-Length:\s*(\d+)\z/i
Instance Method Summary collapse
-
#initialize(input: $stdin, output: $stdout) ⇒ MessageIO
constructor
A new instance of MessageIO.
- #read_message ⇒ Object
- #write_message(payload) ⇒ Object
Constructor Details
#initialize(input: $stdin, output: $stdout) ⇒ MessageIO
Returns a new instance of MessageIO.
12 13 14 15 |
# File 'lib/rfmt/lsp/message_io.rb', line 12 def initialize(input: $stdin, output: $stdout) @input = input @output = output end |
Instance Method Details
#read_message ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/rfmt/lsp/message_io.rb', line 17 def headers = read_headers return nil if headers.nil? content_length = headers.fetch('content-length').to_i JSON.parse(@input.read(content_length)) end |
#write_message(payload) ⇒ Object
25 26 27 28 29 |
# File 'lib/rfmt/lsp/message_io.rb', line 25 def (payload) body = JSON.generate(payload) @output.write("Content-Length: #{body.bytesize}#{HEADER_SEPARATOR}#{body}") @output.flush if @output.respond_to?(:flush) end |