-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlsp_client.py
More file actions
264 lines (237 loc) · 9.58 KB
/
lsp_client.py
File metadata and controls
264 lines (237 loc) · 9.58 KB
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
from __future__ import annotations
import json
import time
from pathlib import Path
from typing import Any, Callable, Sequence
from .metrics import CallMetric, build_call_metric
from .transport import StdioJsonRpcTransport
class LspClient:
def __init__(
self,
command: Sequence[str],
timeout_seconds: float = 10.0,
*,
cwd: Path | None = None,
env: dict[str, str] | None = None,
trace: Callable[[str], None] | None = None,
) -> None:
self._transport = StdioJsonRpcTransport(
command,
cwd=None if cwd is None else str(cwd),
env=env,
request_handler=self._handle_server_request,
notification_handler=self._handle_server_notification,
trace=trace,
)
self._timeout_seconds = timeout_seconds
self._next_request_id = 1
self._metrics: list[CallMetric] = []
self._workspace_settings: dict[str, Any] = {}
self._trace = trace
@property
def metrics(self) -> list[CallMetric]:
return list(self._metrics)
@property
def stderr_lines(self) -> list[str]:
return self._transport.stderr_lines
def start(self) -> None:
self._transport.start()
def close(self) -> None:
self._transport.close()
def initialize(self, workspace_path: Path) -> dict[str, Any] | None:
return self.request(
"initialize",
{
"processId": None,
"clientInfo": {"name": "python-lsp-compare", "version": "0.1.0"},
"rootUri": workspace_path.as_uri(),
"capabilities": {
"workspace": {
"configuration": True,
"workspaceFolders": True,
"didChangeConfiguration": {"dynamicRegistration": False},
},
"textDocument": {
"hover": {"dynamicRegistration": False},
"completion": {"dynamicRegistration": False},
"documentSymbol": {"dynamicRegistration": False},
}
},
"workspaceFolders": [
{"uri": workspace_path.as_uri(), "name": workspace_path.name}
],
},
)
def initialized(self) -> None:
self.notify("initialized", {})
def did_change_configuration(self, settings: dict[str, Any], context: dict[str, Any] | None = None) -> None:
self._workspace_settings = settings
self._trace_message(
"workspace/didChangeConfiguration "
+ json.dumps(settings, ensure_ascii=True, default=str, separators=(",", ":"))
)
self.notify("workspace/didChangeConfiguration", {"settings": settings}, context=context)
def shutdown(self) -> dict[str, Any] | None:
return self.request("shutdown", None)
def exit(self) -> None:
self.notify("exit", None)
def did_open(
self,
uri: str,
text: str,
language_id: str = "python",
version: int = 1,
context: dict[str, Any] | None = None,
) -> None:
self.notify(
"textDocument/didOpen",
{
"textDocument": {
"uri": uri,
"languageId": language_id,
"version": version,
"text": text,
}
},
context=context,
)
def did_close(self, uri: str, context: dict[str, Any] | None = None) -> None:
self.notify("textDocument/didClose", {"textDocument": {"uri": uri}}, context=context)
def did_change(
self,
uri: str,
version: int,
changes: list[dict[str, Any]],
context: dict[str, Any] | None = None,
) -> None:
self.notify(
"textDocument/didChange",
{
"textDocument": {"uri": uri, "version": version},
"contentChanges": changes,
},
context=context,
)
def hover(self, uri: str, line: int, character: int, context: dict[str, Any] | None = None) -> Any:
return self.request(
"textDocument/hover",
{"textDocument": {"uri": uri}, "position": {"line": line, "character": character}},
context=context,
)
def completion(self, uri: str, line: int, character: int, context: dict[str, Any] | None = None) -> Any:
return self.request(
"textDocument/completion",
{
"textDocument": {"uri": uri},
"position": {"line": line, "character": character},
"context": {"triggerKind": 1},
},
context=context,
)
def document_symbols(self, uri: str, context: dict[str, Any] | None = None) -> Any:
return self.request("textDocument/documentSymbol", {"textDocument": {"uri": uri}}, context=context)
def definition(self, uri: str, line: int, character: int, context: dict[str, Any] | None = None) -> Any:
return self.request(
"textDocument/definition",
{"textDocument": {"uri": uri}, "position": {"line": line, "character": character}},
context=context,
)
def references(self, uri: str, line: int, character: int, context: dict[str, Any] | None = None) -> Any:
return self.request(
"textDocument/references",
{
"textDocument": {"uri": uri},
"position": {"line": line, "character": character},
"context": {"includeDeclaration": True},
},
context=context,
)
def request(self, method: str, params: dict[str, Any] | None, context: dict[str, Any] | None = None) -> Any:
request_id = self._next_request_id
self._next_request_id += 1
self._trace_message(f" transport: -> request id={request_id} method={method}")
started_at = time.time()
started_perf = time.perf_counter()
response = self._transport.send_request(request_id, method, params, timeout_seconds=self._timeout_seconds)
duration_ms = (time.perf_counter() - started_perf) * 1000
self._trace_message(f" transport: <- response id={request_id} method={method} ok={'result' in response.payload}")
payload = response.payload
bytes_sent = response.request_size
if "error" in payload:
self._metrics.append(
build_call_metric(
kind="request",
method=method,
duration_ms=duration_ms,
success=False,
started_at_unix=started_at,
bytes_sent=bytes_sent,
bytes_received=response.raw_size,
request_id=request_id,
error=payload["error"],
context=context,
)
)
raise RuntimeError(f"{method} failed: {payload['error']}")
self._metrics.append(
build_call_metric(
kind="request",
method=method,
duration_ms=duration_ms,
success=True,
started_at_unix=started_at,
bytes_sent=bytes_sent,
bytes_received=response.raw_size,
request_id=request_id,
result=payload.get("result"),
context=context,
)
)
return payload.get("result")
def notify(self, method: str, params: dict[str, Any] | None, context: dict[str, Any] | None = None) -> None:
started_at = time.time()
started_perf = time.perf_counter()
bytes_sent = self._transport.send_notification(method, params)
duration_ms = (time.perf_counter() - started_perf) * 1000
self._metrics.append(
build_call_metric(
kind="notification",
method=method,
duration_ms=duration_ms,
success=True,
started_at_unix=started_at,
bytes_sent=bytes_sent,
bytes_received=0,
context=context,
)
)
def _handle_server_request(self, payload: dict[str, Any]) -> Any:
method = payload.get("method")
if method == "workspace/configuration":
items = payload.get("params", {}).get("items", [])
sections = [item.get("section") for item in items]
result = [self._lookup_workspace_setting(section) for section in sections]
self._trace_message(
"workspace/configuration request "
+ json.dumps({"sections": sections, "result": result}, ensure_ascii=True, default=str, separators=(",", ":"))
)
return result
if method in {"client/registerCapability", "client/unregisterCapability", "window/workDoneProgress/create"}:
return None
if method == "workspace/applyEdit":
return {"applied": False}
return None
def _handle_server_notification(self, payload: dict[str, Any]) -> None:
return None
def _lookup_workspace_setting(self, section: str | None) -> Any:
if not section:
return self._workspace_settings
current: Any = self._workspace_settings
for part in section.split("."):
if not isinstance(current, dict) or part not in current:
return None
current = current[part]
return current
def _trace_message(self, message: str) -> None:
if self._trace is not None:
self._trace(message)