Class: LanguageServer

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

Class Method Summary collapse

Class Method Details

.client_initialize(id, params) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/servers/language_server.rb', line 38

def self.client_initialize(id,params)
  return JSON.generate({
    jsonrpc: '2.0',
    result: {
      capabilities: {
        textDocumentSync:1,
        implementationProvider: "true"
      }
    },
    id: id
  })
end

.client_textDocument_didChange(id, params) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/servers/language_server.rb', line 59

def self.client_textDocument_didChange(id,params)
  uri = params["textDocument"]["uri"]
  version = params["textDocument"]["version"]
  code = params['contentChanges'][0]['text']
  return self.generate_diagnostics(uri,version,code)
  return
end

.client_textDocument_didOpen(id, params) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/servers/language_server.rb', line 51

def self.client_textDocument_didOpen(id,params)
  uri = params["textDocument"]["uri"]
  version = params["textDocument"]["version"]
  code = params['textDocument']['text']
  return self.generate_diagnostics(uri,version,code)
  return
end

.generate_diagnostics(uri, version, code) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/servers/language_server.rb', line 67

def self.generate_diagnostics(uri,version,code)
  result = RuleEngine.analyzeDocument(code) #convert to json

  diagnostics = []

  result.each do |sin|
    diagnostics.append({
                         range:{
                           start: { line: sin.begin_line-1, character: sin.begin_char },
                           end: { line: sin.end_line-1, character: sin.end_char }
                         },
                         severity: 2,
                         code: {
                           value:sin.type[:name],
                           target:sin.type[:solution]
                         },
                         source:'Puppet-sec-lint',
                         message: sin.type[:message]
                       })
  end

  return JSON.generate({
                         jsonrpc: '2.0',
                         method: 'textDocument/publishDiagnostics',
                         params: {
                           uri: uri,
                           version: version,
                           diagnostics: diagnostics
                         }
                       })
end

.start(port) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/servers/language_server.rb', line 11

def self.start(port)
  port ||= 5007
  server = TCPServer.open(port)

  loop {
    Thread.fork(server.accept) do |client|
      while line=client.gets
        length=Integer(line.scan(/\d/).join(''))
        line=client.read(length+2)
        request = JSON.parse(line)
        $logger.debug(line)

        method_name = request['method'].sub('/', '_')
        response = if self.respond_to? "client_"+method_name then self.send("client_"+method_name,request['id'],request['params']) end

        if not response.nil?
          client.flush
          client.print("Content-Length: "+response.length.to_s+"\r\n\r\n")
          client.print(response)
          $logger.debug(response)
        end
      end
      client.close
    end
  }
end