Class: ThemeCheck::LanguageServer::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/theme_check/language_server/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(in_stream: STDIN, out_stream: STDOUT, err_stream: STDERR, should_raise_errors: false) ⇒ Server

Returns a new instance of Server.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/theme_check/language_server/server.rb', line 15

def initialize(
  in_stream: STDIN,
  out_stream: STDOUT,
  err_stream: STDERR,
  should_raise_errors: false
)
  validate!([in_stream, out_stream, err_stream])

  @handler = Handler.new(self)
  @in = in_stream
  @out = out_stream
  @err = err_stream

  @out.sync = true # do not buffer
  @err.sync = true # do not buffer

  @should_raise_errors = should_raise_errors
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



12
13
14
# File 'lib/theme_check/language_server/server.rb', line 12

def handler
  @handler
end

#should_raise_errorsObject (readonly)

Returns the value of attribute should_raise_errors.



13
14
15
# File 'lib/theme_check/language_server/server.rb', line 13

def should_raise_errors
  @should_raise_errors
end

Instance Method Details

#listenObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/theme_check/language_server/server.rb', line 34

def listen
  loop do
    process_request

  # support ctrl+c and stuff
  rescue SignalException, DoneStreaming
    cleanup
    return 0

  rescue Exception => e # rubocop:disable Lint/RescueException
    raise e if should_raise_errors
    log(e)
    log(e.backtrace)
    return 1
  end
end

#log(message) ⇒ Object



72
73
74
75
# File 'lib/theme_check/language_server/server.rb', line 72

def log(message)
  @err.puts(message)
  @err.flush
end

#send_response(response) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/theme_check/language_server/server.rb', line 51

def send_response(response)
  response_body = JSON.dump(response)
  log(JSON.pretty_generate(response)) if $DEBUG

  # Because programming is fun,
  #
  # Ruby on Windows turns \n into \r\n. Which means that \r\n
  # gets turned into \r\r\n. Which means that the protocol
  # breaks on windows unless we turn STDOUT into binary mode and
  # set the encoding manually (yuk!) or we do this little hack
  # here and put \n which gets transformed into \r\n on windows
  # only...
  #
  # Hours wasted: 8.
  eol = Gem.win_platform? ? "\n" : "\r\n"
  @out.write("Content-Length: #{response_body.bytesize}#{eol}")
  @out.write(eol)
  @out.write(response_body)
  @out.flush
end