Class: Lsp::FileLanguageServer

Inherits:
Object
  • Object
show all
Defined in:
lib/lsp/file_language_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(implementation, input = $stdin, output = $stdout, logger: Logger.new(nil)) ⇒ FileLanguageServer

Returns a new instance of FileLanguageServer.



6
7
8
9
10
11
# File 'lib/lsp/file_language_server.rb', line 6

def initialize(implementation, input = $stdin, output = $stdout, logger: Logger.new(nil))
  @implementation = implementation
  @input = input
  @output = output
  @logger = logger
end

Instance Attribute Details

#implementationObject (readonly)

Returns the value of attribute implementation.



71
72
73
# File 'lib/lsp/file_language_server.rb', line 71

def implementation
  @implementation
end

#inputObject (readonly)

Returns the value of attribute input.



71
72
73
# File 'lib/lsp/file_language_server.rb', line 71

def input
  @input
end

#loggerObject (readonly)

Returns the value of attribute logger.



71
72
73
# File 'lib/lsp/file_language_server.rb', line 71

def logger
  @logger
end

#outputObject (readonly)

Returns the value of attribute output.



71
72
73
# File 'lib/lsp/file_language_server.rb', line 71

def output
  @output
end

Instance Method Details

#build_message(hash) ⇒ Object



66
67
68
69
# File 'lib/lsp/file_language_server.rb', line 66

def build_message(hash)
  json = hash.to_json
  "Content-Length: #{json.size}\r\n\r\n#{json}"
end

#prepareObject



50
51
52
# File 'lib/lsp/file_language_server.rb', line 50

def prepare
  implementation.language_server = self
end

#response(id, result, error) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lsp/file_language_server.rb', line 54

def response(id, result, error)
  payload = {
    jsonrpc: "2.0",
    id: id,
    result: result,
    error: error,
  }
  output.write(build_message(payload))
  output.flush
  logger.info("SEND #{payload}")
end

#startObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lsp/file_language_server.rb', line 13

def start
  prepare

  loop do
    headers = {}
    loop do
      header_line = input.readline.strip
      if header_line.empty?
        if headers.empty?
          redo
        else
          break
        end
      end
      header_name, header_value = header_line.split(":", 2)
      headers[header_name] = header_value.strip
    end

    body_raw = input.read(headers["Content-Length"].to_i)
    body_json = JSON.parse(body_raw, symbolize_names: true)
    logger.info("RECV #{body_json}")
    if body_json.key?(:id)
      implementation.request(
        body_json.fetch(:id),
        body_json.fetch(:method),
        body_json.fetch(:params))
    else
      implementation.notify(
        body_json.fetch(:method),
        body_json.fetch(:params))
    end
  end
rescue EOFError
rescue => e
  logger.error("Server failed because of #{e}")
end