Class: LangSrv

Inherits:
Object
  • Object
show all
Defined in:
lib/vimamsa/langservp.rb

Constant Summary collapse

@@languages =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lang) ⇒ LangSrv

Returns a new instance of LangSrv.



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vimamsa/langservp.rb', line 20

def initialize(lang)
  @error = true

  # Use LSP server specified by user if available
  @lang = lang

  lspconf = nil
  ret = cnf.lsp.server?.find { |k, v| v[:languages].include?(@lang) }
  lspconf = ret[1] unless ret.nil?

  if !lspconf.nil?
    error = false
    begin
      @io = IO.popen(lspconf[:command], "r+")
    rescue Errno::ENOENT => e
      pp e
      error = true
    rescue StandardError => e
      debug "StandardError @io = IO.popen(lspconf[:command] ...", 2
      pp e
      error = true
    end
    if error or @io.nil?
      message("Could not start lsp server #{lspconf[:name]}")
      error = true
      return nil
    end
  else
    return nil
  end
  @writer = LSP::Transport::Io::Writer.new(@io)
  @reader = LSP::Transport::Io::Reader.new(@io)
  @id = 0

  wf = []
  for c in conf(:workspace_folders)
    wf << LSP::Interface::WorkspaceFolder.new(uri: c[:uri], name: c[:name])
  end
  debug "WORKSPACE FOLDERS", 2
  debug wf.inspect, 2

  pid = Process.pid

  if lspconf[:name] == "phpactor"
    initp = LSP::Interface::InitializeParams.new(
      process_id: pid,
      root_uri: lspconf[:rooturi],
      workspace_folders: wf,
      capabilities: { 'workspace': { 'workspaceFolders': true } },
    )
  else
    initp = LSP::Interface::InitializeParams.new(
      process_id: pid,
      root_uri: "null",
      workspace_folders: wf,
      capabilities: { 'workspace': { 'workspaceFolders': true } },
    )
  end
  @resp = {}

  @writer.write(id: new_id, params: initp, method: "initialize")

  @lst = Thread.new {
    @reader.read do |r|
      @resp[r[:id]] = r
      pp r
      # exit
    end
  }
  @error = false
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



6
7
8
# File 'lib/vimamsa/langservp.rb', line 6

def error
  @error
end

Class Method Details

.get(lang) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/vimamsa/langservp.rb', line 8

def self.get(lang)
  if @@languages[lang].nil?
    @@languages[lang] = LangSrv.new(lang)
    @@languages[lang] = nil if @@languages[lang].error
  end
  return @@languages[lang]
end

Instance Method Details

#add_workspacesObject

TODO



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/vimamsa/langservp.rb', line 133

def add_workspaces() # TODO
  # https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_workspaceFolders
  debug "Add workspaces", 2
  a = [LSP::Interface::WorkspaceFolder.new(uri: "file:///...", name: "vimamsa")]
  id = new_id
  # @writer.write(id: id, params: a, method: "textDocument/definition")
  # @writer.write(id: id, params: a, method: "workspace/workspaceFolders")
  @writer.write(id: id, params: a, method: "workspace/didChangeWorkspaceFolders")
  r = wait_for_response(id)
  pp r
end

#get_definition(fpuri, lpos, cpos) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/vimamsa/langservp.rb', line 150

def get_definition(fpuri, lpos, cpos)
  a = LSP::Interface::DefinitionParams.new(
    position: LSP::Interface::Position.new(line: lpos, character: cpos),
    text_document: LSP::Interface::TextDocumentIdentifier.new(uri: fpuri),
  )
  id = new_id
  pp a
  @writer.write(id: id, params: a, method: "textDocument/definition")
  r = wait_for_response(id)
  return nil if r.nil?
  # Ripl.start :binding => binding
  pp r
  line = HSafe.new(r)[:result][0][:range][:start][:line].val
  uri = HSafe.new(r)[:result][0][:uri].val

  if !uri.nil? and !line.nil?
    puts "LINE:" + line.to_s
    puts "URI:" + uri
    fpath = URI.parse(uri).path
    line = line + 1
    return [fpath, line]
  end

  return nil
end

#handle_delta(delta, fpath, version) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/vimamsa/langservp.rb', line 92

def handle_delta(delta, fpath, version)
  fpuri = URI.join("file:///", fpath).to_s

  # delta[0]: char position
  # delta[1]: INSERT or DELETE
  # delta[2]: number of chars affected
  # delta[3]: text to add in case of insert

  changes = nil
  if delta[1] == INSERT
    changes = [{ 'rangeLength': 0, 'range': { 'start': { 'line': delta[4][0], 'character': delta[4][1] }, 'end': { 'line': delta[4][0], 'character': delta[4][1] } }, 'text': delta[3] }]
  elsif delta[1] == DELETE
    changes = [{ 'rangeLength': delta[2], 'range': { 'start': { 'line': delta[4][0], 'character': delta[4][1] }, 'end': { 'line': delta[5][0], 'character': delta[5][1] } }, 'text': "" }]
  end
  debug changes.inspect, 2

  if !changes.nil?
    a = LSP::Interface::DidChangeTextDocumentParams.new(
      text_document: LSP::Interface::VersionedTextDocumentIdentifier.new(uri: fpuri, version: version),
      content_changes: changes,
    )
    id = new_id
    pp a
    @writer.write(id: id, params: a, method: "textDocument/didChange")
  end
end

#handle_responsesObject



145
146
147
148
# File 'lib/vimamsa/langservp.rb', line 145

def handle_responses()
  #TODO
  # r = @resp.delete_at(0)
end

#new_idObject



16
17
18
# File 'lib/vimamsa/langservp.rb', line 16

def new_id()
  return @id += 1
end

#open_file(fp, fc = nil) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/vimamsa/langservp.rb', line 176

def open_file(fp, fc = nil)
  debug "open_file", 2
  fc = IO.read(fp) if fc.nil?

  encoded_filepath = URI.encode_www_form_component(fp)
  fpuri = URI.parse("file://#{encoded_filepath}")

  a = LSP::Interface::DidOpenTextDocumentParams.new(
    text_document: LSP::Interface::TextDocumentItem.new(
      uri: fpuri,
      text: fc,
      language_id: "c++",
      version: 1,
    ),
  )

  @writer.write(method: "textDocument/didOpen", params: a)
end

#wait_for_response(id) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/vimamsa/langservp.rb', line 119

def wait_for_response(id)
  t = Time.now
  debug "Waiting for response id:#{id}"
  while @resp[id].nil?
    sleep 0.03
    if Time.now - t > 5
      debug "Timeout LSP call id:#{id}"
      return nil
    end
  end
  debug "End waiting id:#{id}"
  return @resp[id]
end