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
# File 'lib/vimamsa/langservp.rb', line 20

def initialize(lang)
  @error = true
  clsp = conf(:custom_lsp)

  # Use LSP server specified by user if available
  @lang = lang
  lspconf = clsp[lang]
  if !lspconf.nil?
    @io = IO.popen(lspconf[:command], "r+")
  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

  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



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vimamsa/langservp.rb', line 113

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/vimamsa/langservp.rb', line 130

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



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/vimamsa/langservp.rb', line 72

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



125
126
127
128
# File 'lib/vimamsa/langservp.rb', line 125

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/vimamsa/langservp.rb', line 156

def open_file(fp, fc = nil)
  debug "open_file", 2
  fc = IO.read(fp) if fc.nil?
  fpuri = URI.join("file:///", fp).to_s

  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



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/vimamsa/langservp.rb', line 99

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