Class: LanguageServer::Protocol::Transport::Io::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/language_server/protocol/transport/io/writer.rb

Direct Known Subclasses

Stdio::Writer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Writer

Returns a new instance of Writer.



8
9
10
11
# File 'lib/language_server/protocol/transport/io/writer.rb', line 8

def initialize(io)
  @io = io
  io.binmode
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



6
7
8
# File 'lib/language_server/protocol/transport/io/writer.rb', line 6

def io
  @io
end

Instance Method Details

#write(response) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/language_server/protocol/transport/io/writer.rb', line 13

def write(response)
  response_str = response.merge(
    jsonrpc: "2.0"
  ).to_json

  headers = {
    "Content-Length" => response_str.bytesize
  }

  headers.each do |k, v|
    io.print "#{k}: #{v}\r\n"
  end

  io.print "\r\n"

  io.print response_str
  io.flush
end