-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathls_interact.py
297 lines (208 loc) · 6.72 KB
/
ls_interact.py
1
2
3
4
5
6
7
8
9
10
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
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
91
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
118
119
120
121
122
123
124
125
126
127
128
129
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import argparse
import common
import re
from common import Base
class Range:
def __init__(self, start_line, start_col, end_line, end_col):
self._sl = start_line
self._sc = start_col
self._el = end_line
self._ec = end_col
def to_lsp(self):
return {
'start': {
'line': self._sl - 1,
'character': self._sc - 1,
},
'end': {
'line': self._el - 1,
'character': self._ec - 1,
},
}
@property
def lsp_start_line(self):
return self._sl - 1
@property
def lsp_start_col(self):
return self._sc - 1
@property
def lsp_end_line(self):
return self._el - 1
@property
def lsp_end_col(self):
return self._ec - 1
class Initialize(Base):
def __init__(self, params):
super().__init__('initialize')
self._params = params
def get_params(self):
return self._params
class Initialized(Base):
def __init__(self):
super().__init__('initialized')
def get_params(self):
return {}
class Shutdown(Base):
def __init__(self):
super().__init__('shutdown')
def get_params(self):
return {}
class Exit(Base):
def __init__(self):
super().__init__('exit')
def get_params(self):
return {}
class DidOpenTextDocument(Base):
def __init__(self, path):
super().__init__('textDocument/didOpen')
self._path = path
def get_params(self):
with open(self._path) as f:
data = f.read()
obj = {}
obj['textDocument'] = {}
obj['textDocument']['uri'] = 'file://' + self._path
obj['textDocument']['languageId'] = 'cpp'
obj['textDocument']['version'] = 1
obj['textDocument']['text'] = data
# obj['metadata'] = {}
# obj['metadata']['extraFlags'] = ['-xc++']
return obj
class DidCloseTextDocument(Base):
def __init__(self, path):
super().__init__('textDocument/didClose')
self._path = path
def get_params(self):
obj = {}
obj['textDocument'] = {}
obj['textDocument']['uri'] = 'file://' + self._path
return obj
class DidChangeTextDocument(Base):
def __init__(self, path, text):
super().__init__('textDocument/didChange')
self._path = path
self._text = text
def get_params(self):
obj = {}
obj['textDocument'] = {}
obj['textDocument']['uri'] = 'file://' + self._path
obj['contentChanges'] = [{
'text': self._text,
}]
return obj
class GotoDefinition(Base):
def __init__(self, path, line, col):
super().__init__('textDocument/definition')
self._path = path
self._line = line
self._col = col
def get_params(self):
obj = {}
obj['textDocument'] = {}
obj['textDocument']['uri'] = 'file://' + self._path
obj['position'] = {}
obj['position']['line'] = self._line - 1
obj['position']['character'] = self._col - 1
return obj
class FindReferences(Base):
def __init__(self, path, line, col):
super().__init__('textDocument/references')
self._path = path
self._line = line
self._col = col
def get_params(self):
obj = {}
obj['textDocument'] = {}
obj['textDocument']['uri'] = 'file://' + self._path
obj['position'] = {}
obj['position']['line'] = self._line - 1
obj['position']['character'] = self._col - 1
return obj
class CodeLens(Base):
def __init__(self, path):
super().__init__('textDocument/codeLens')
self._path = path
def get_params(self):
obj = {}
obj['textDocument'] = {}
obj['textDocument']['uri'] = 'file://' + self._path
return obj
class CodeLensResolve(Base):
def __init__(self, lens):
super().__init__('codeLens/resolve')
self._lens = lens
def get_params(self):
return self._lens
class CodeAction(Base):
def __init__(self, path, range_, diags):
super().__init__('textDocument/codeAction')
self._path = path
if type(range_) == Range:
self._range = range_.to_lsp()
else:
self._range = range_
self._diags = diags
def get_params(self):
obj = {}
obj['textDocument'] = {}
obj['textDocument']['uri'] = 'file://' + self._path
obj['range'] = self._range
obj['context'] = {
'diagnostics': self._diags,
}
return obj
class Hover(Base):
def __init__(self, path, line, col):
super().__init__('textDocument/hover')
self._path = path
self._line = line
self._col = col
def get_params(self):
obj = {}
obj['textDocument'] = {}
obj['textDocument']['uri'] = 'file://' + self._path
obj['position'] = {}
obj['position']['line'] = self._line - 1
obj['position']['character'] = self._col - 1
return obj
class DidChangeConfiguration(Base):
def __init__(self, params):
super().__init__('workspace/didChangeConfiguration')
self._params = params
def get_params(self):
return self._params
class WorkspaceSymbol(Base):
def __init__(self, query):
super().__init__('workspace/symbol')
self._query = query
def get_params(self):
return {
'query': self._query
}
def run(callback, cmdline_args="", initialize_params={}):
argparser = argparse.ArgumentParser()
argparser.add_argument('server',
help=('server executable (may contain additional ' +
'args)'))
argparser.add_argument('--log', action='store_true',
help='print communication with the server')
argparser.add_argument('--log-pretty', action='store_true',
help='when --log is enabled, pretty-print the json')
args = argparser.parse_args()
m = re.match(r':(\d{1,5})', args.server)
if m:
port = int(m.group(1))
sock = common.connect_tool(port)
json_rpc = common.JsonRpc(sock, sock, args.log,
args.log_pretty)
else:
server = common.start_tool('{} {}'.format(args.server, cmdline_args))
json_rpc = common.JsonRpc(server.stdin, server.stdout, args.log,
args.log_pretty)
p = json_rpc.request(Initialize(initialize_params))
json_rpc.wait_for(p)
json_rpc.notify(Initialized())
callback(json_rpc)
p = json_rpc.request(Shutdown())
json_rpc.wait_for(p)
json_rpc.notify(Exit())