-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
server.py
516 lines (406 loc) · 18.9 KB
/
server.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
############################################################################
# Copyright(c) Open Law Library. All rights reserved. #
# See ThirdPartyNotices.txt in the project root for additional notices. #
# #
# Licensed under the Apache License, Version 2.0 (the "License") #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http: // www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
############################################################################
import asyncio
import json
import logging
import re
import sys
from concurrent.futures import Future, ThreadPoolExecutor
from threading import Event
from typing import Any, Callable, List, Optional, TextIO, TypeVar, Union
from pygls import IS_PYODIDE
from pygls.lsp import ConfigCallbackType, ShowDocumentCallbackType
from pygls.exceptions import PyglsError, JsonRpcException, FeatureRequestError
from lsprotocol.types import (
ClientCapabilities,
Diagnostic, MessageType, RegistrationParams,
ServerCapabilities, ShowDocumentParams,
TextDocumentSyncKind, UnregistrationParams,
WorkspaceApplyEditResponse, WorkspaceEdit,
WorkspaceConfigurationParams
)
from pygls.progress import Progress
from pygls.protocol import LanguageServerProtocol, default_converter
from pygls.workspace import Workspace
if not IS_PYODIDE:
from multiprocessing.pool import ThreadPool
logger = logging.getLogger(__name__)
F = TypeVar('F', bound=Callable)
async def aio_readline(loop, executor, stop_event, rfile, proxy):
"""Reads data from stdin in separate thread (asynchronously)."""
CONTENT_LENGTH_PATTERN = re.compile(rb'^Content-Length: (\d+)\r\n$')
# Initialize message buffer
message = []
content_length = 0
while not stop_event.is_set() and not rfile.closed:
# Read a header line
header = await loop.run_in_executor(executor, rfile.readline)
if not header:
break
message.append(header)
# Extract content length if possible
if not content_length:
match = CONTENT_LENGTH_PATTERN.fullmatch(header)
if match:
content_length = int(match.group(1))
logger.debug('Content length: %s', content_length)
# Check if all headers have been read (as indicated by an empty line \r\n)
if content_length and not header.strip():
# Read body
body = await loop.run_in_executor(executor, rfile.read, content_length)
if not body:
break
message.append(body)
# Pass message to language server protocol
proxy(b''.join(message))
# Reset the buffer
message = []
content_length = 0
class StdOutTransportAdapter:
"""Protocol adapter which overrides write method.
Write method sends data to stdout.
"""
def __init__(self, rfile, wfile):
self.rfile = rfile
self.wfile = wfile
def close(self):
self.rfile.close()
self.wfile.close()
def write(self, data):
self.wfile.write(data)
self.wfile.flush()
class PyodideTransportAdapter:
"""Protocol adapter which overrides write method.
Write method sends data to stdout.
"""
def __init__(self, wfile):
self.wfile = wfile
def close(self):
self.wfile.close()
def write(self, data):
self.wfile.write(data)
self.wfile.flush()
class WebSocketTransportAdapter:
"""Protocol adapter which calls write method.
Write method sends data via the WebSocket interface.
"""
def __init__(self, ws, loop):
self._ws = ws
self._loop = loop
def close(self) -> None:
"""Stop the WebSocket server."""
self._ws.close()
def write(self, data: Any) -> None:
"""Create a task to write specified data into a WebSocket."""
asyncio.ensure_future(self._ws.send(data))
class Server:
"""Class that represents async server. It can be started using TCP or IO.
Args:
protocol_cls(Protocol): Protocol implementation that must be derived
from `asyncio.Protocol`
converter_factory: Factory function to use when constructing a cattrs converter.
loop(AbstractEventLoop): asyncio event loop
max_workers(int, optional): Number of workers for `ThreadPool` and
`ThreadPoolExecutor`
sync_kind(TextDocumentSyncKind): Text document synchronization option
- None(0): no synchronization
- Full(1): replace whole text
- Incremental(2): replace text within a given range
Attributes:
_max_workers(int): Number of workers for thread pool executor
_server(Server): Server object which can be used to stop the process
_stop_event(Event): Event used for stopping `aio_readline`
_thread_pool(ThreadPool): Thread pool for executing methods decorated
with `@ls.thread()` - lazy instantiated
_thread_pool_executor(ThreadPoolExecutor): Thread pool executor
passed to `run_in_executor`
- lazy instantiated
"""
def __init__(self, protocol_cls, converter_factory, loop=None, max_workers=2,
sync_kind=TextDocumentSyncKind.Incremental):
if not issubclass(protocol_cls, asyncio.Protocol):
raise TypeError('Protocol class should be subclass of asyncio.Protocol')
self._max_workers = max_workers
self._server = None
self._stop_event = None
self._thread_pool = None
self._thread_pool_executor = None
self.sync_kind = sync_kind
if loop is None:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self._owns_loop = True
else:
self._owns_loop = False
self.loop = loop
self.lsp = protocol_cls(self, converter_factory())
def shutdown(self):
"""Shutdown server."""
logger.info('Shutting down the server')
self._stop_event.set()
if self._thread_pool:
self._thread_pool.terminate()
self._thread_pool.join()
if self._thread_pool_executor:
self._thread_pool_executor.shutdown()
if self._server:
self._server.close()
self.loop.run_until_complete(self._server.wait_closed())
if self._owns_loop and not self.loop.is_closed:
logger.info('Closing the event loop.')
self.loop.close()
def start_io(self, stdin: Optional[TextIO] = None, stdout: Optional[TextIO] = None):
"""Starts IO server."""
logger.info('Starting IO server')
self._stop_event = Event()
transport = StdOutTransportAdapter(stdin or sys.stdin.buffer,
stdout or sys.stdout.buffer)
self.lsp.connection_made(transport)
try:
self.loop.run_until_complete(
aio_readline(self.loop,
self.thread_pool_executor,
self._stop_event,
stdin or sys.stdin.buffer,
self.lsp.data_received))
except BrokenPipeError:
logger.error('Connection to the client is lost! Shutting down the server.')
except (KeyboardInterrupt, SystemExit):
pass
finally:
self.shutdown()
def start_pyodide(self):
logger.info('Starting Pyodide server')
# Note: We don't actually start anything running as the main event
# loop will be handled by the web platform.
transport = PyodideTransportAdapter(sys.stdout)
self.lsp.connection_made(transport)
self.lsp._send_only_body = True # Don't send headers within the payload
def start_tcp(self, host: str, port: int) -> None:
"""Starts TCP server."""
logger.info('Starting TCP server on %s:%s', host, port)
self._stop_event = Event()
self._server = self.loop.run_until_complete(
self.loop.create_server(self.lsp, host, port)
)
try:
self.loop.run_forever()
except (KeyboardInterrupt, SystemExit):
pass
finally:
self.shutdown()
def start_ws(self, host: str, port: int) -> None:
"""Starts WebSocket server."""
try:
from websockets.server import serve
except ImportError:
logger.error('Run `pip install pygls[ws]` to install `websockets`.')
sys.exit(1)
logger.info('Starting WebSocket server on {}:{}'.format(host, port))
self._stop_event = Event()
self.lsp._send_only_body = True # Don't send headers within the payload
async def connection_made(websocket, _):
"""Handle new connection wrapped in the WebSocket."""
self.lsp.transport = WebSocketTransportAdapter(websocket, self.loop)
async for message in websocket:
self.lsp._procedure_handler(
json.loads(message, object_hook=self.lsp._deserialize_message)
)
start_server = serve(connection_made, host, port, loop=self.loop)
self._server = start_server.ws_server
self.loop.run_until_complete(start_server)
try:
self.loop.run_forever()
except (KeyboardInterrupt, SystemExit):
pass
finally:
self._stop_event.set()
self.shutdown()
if not IS_PYODIDE:
@property
def thread_pool(self) -> ThreadPool:
"""Returns thread pool instance (lazy initialization)."""
if not self._thread_pool:
self._thread_pool = ThreadPool(processes=self._max_workers)
return self._thread_pool
@property
def thread_pool_executor(self) -> ThreadPoolExecutor:
"""Returns thread pool instance (lazy initialization)."""
if not self._thread_pool_executor:
self._thread_pool_executor = \
ThreadPoolExecutor(max_workers=self._max_workers)
return self._thread_pool_executor
class LanguageServer(Server):
"""A class that represents Language server using Language Server Protocol.
This class can be extended and it can be passed as a first argument to
registered commands/features.
Args:
name(str): Name of the server
version(str): Version of the server
protocol_cls(LanguageServerProtocol): LSP or any subclass of it
max_workers(int, optional): Number of workers for `ThreadPool` and
`ThreadPoolExecutor`
"""
lsp: LanguageServerProtocol
default_error_message = "Unexpected error in LSP server, see server's logs for details"
"""
The default error message sent to the user's editor when this server encounters an uncaught
exception.
"""
def __init__(
self,
name: str,
version: str,
loop=None,
protocol_cls=LanguageServerProtocol,
converter_factory=default_converter,
max_workers: int = 2
):
if not issubclass(protocol_cls, LanguageServerProtocol):
raise TypeError('Protocol class should be subclass of LanguageServerProtocol')
self.name = name
self.version = version
super().__init__(protocol_cls, converter_factory, loop, max_workers)
def apply_edit(
self, edit: WorkspaceEdit, label: Optional[str] = None
) -> WorkspaceApplyEditResponse:
"""Sends apply edit request to the client."""
return self.lsp.apply_edit(edit, label)
def command(self, command_name: str) -> Callable[[F], F]:
"""Decorator used to register custom commands.
Example:
@ls.command('myCustomCommand')
def my_cmd(ls, a, b, c):
pass
"""
return self.lsp.fm.command(command_name)
@property
def client_capabilities(self) -> ClientCapabilities:
"""Return client capabilities."""
return self.lsp.client_capabilities
def feature(
self, feature_name: str, options: Optional[Any] = None,
) -> Callable[[F], F]:
"""Decorator used to register LSP features.
Example:
@ls.feature('textDocument/completion', CompletionOptions(trigger_characters=['.']))
def completions(ls, params: CompletionParams):
return CompletionList(is_incomplete=False, items=[CompletionItem("Completion 1")])
"""
return self.lsp.fm.feature(feature_name, options)
def get_configuration(self, params: WorkspaceConfigurationParams,
callback: Optional[ConfigCallbackType] = None) -> Future:
"""Gets the configuration settings from the client."""
return self.lsp.get_configuration(params, callback)
def get_configuration_async(self, params: WorkspaceConfigurationParams) -> asyncio.Future:
"""Gets the configuration settings from the client. Should be called with `await`"""
return self.lsp.get_configuration_async(params)
def log_trace(self, message: str, verbose: Optional[str] = None) -> None:
"""Sends trace notification to the client."""
self.lsp.log_trace(message, verbose)
@property
def progress(self) -> Progress:
"""Gets the object to manage client's progress bar."""
return self.lsp.progress
def publish_diagnostics(
self,
uri: str,
diagnostics: Optional[List[Diagnostic]] = None,
version: Optional[int] = None,
**kwargs
):
"""
Sends diagnostic notification to the client.
"""
params = self.lsp._construct_publish_diagnostic_type(
uri,
diagnostics,
version,
**kwargs
)
self.lsp.publish_diagnostics(params, **kwargs)
def register_capability(self, params: RegistrationParams,
callback: Optional[Callable[[], None]] = None) -> Future:
"""Register a new capability on the client."""
return self.lsp.register_capability(params, callback)
def register_capability_async(self, params: RegistrationParams) -> asyncio.Future:
"""Register a new capability on the client. Should be called with `await`"""
return self.lsp.register_capability_async(params)
def semantic_tokens_refresh(self, callback: Optional[Callable[[], None]] = None) -> Future:
"""Request a refresh of all semantic tokens."""
return self.lsp.semantic_tokens_refresh(callback)
def semantic_tokens_refresh_async(self) -> asyncio.Future:
"""Request a refresh of all semantic tokens. Should be called with `await`"""
return self.lsp.semantic_tokens_refresh_async()
def send_notification(self, method: str, params: object = None) -> None:
"""Sends notification to the client."""
self.lsp.notify(method, params)
@property
def server_capabilities(self) -> ServerCapabilities:
"""Return server capabilities."""
return self.lsp.server_capabilities
def show_document(self, params: ShowDocumentParams,
callback: Optional[ShowDocumentCallbackType] = None) -> Future:
"""Display a particular document in the user interface."""
return self.lsp.show_document(params, callback)
def show_document_async(self, params: ShowDocumentParams) -> asyncio.Future:
"""Display a particular document in the user interface. Should be called with `await`"""
return self.lsp.show_document_async(params)
def show_message(self, message, msg_type=MessageType.Info) -> None:
"""Sends message to the client to display message."""
self.lsp.show_message(message, msg_type)
def show_message_log(self, message, msg_type=MessageType.Log) -> None:
"""Sends message to the client's output channel."""
self.lsp.show_message_log(message, msg_type)
def _report_server_error(self, error: Exception, source: Union[PyglsError, JsonRpcException]):
# Prevent recursive error reporting
try:
self.report_server_error(error, source)
except Exception:
logger.warning("Failed to report error to client")
def report_server_error(self, error: Exception, source: Union[PyglsError, JsonRpcException]):
"""
Sends error to the client for displaying.
By default this fucntion does not handle LSP request errors. This is because LSP requests
require direct responses and so already have a mechanism for including unexpected errors
in the response body.
All other errors are "out of band" in the sense that the client isn't explicitly waiting
for them. For example diagnostics are returned as notifications, not responses to requests,
and so can seemingly be sent at random. Also for example consider JSON RPC serialization
and deserialization, if a payload cannot be parsed then the whole request/response cycle
cannot be completed and so one of these "out of band" error messages is sent.
These "out of band" error messages are not a requirement of the LSP spec. Pygls simply
offers this behaviour as a recommended default. It is perfectly reasonble to override this
default.
"""
if source == FeatureRequestError:
return
self.show_message(self.default_error_message, msg_type=MessageType.Error)
def thread(self) -> Callable[[F], F]:
"""Decorator that mark function to execute it in a thread."""
return self.lsp.thread()
def unregister_capability(self, params: UnregistrationParams,
callback: Optional[Callable[[], None]] = None) -> Future:
"""Unregister a new capability on the client."""
return self.lsp.unregister_capability(params, callback)
def unregister_capability_async(self, params: UnregistrationParams) -> asyncio.Future:
"""Unregister a new capability on the client. Should be called with `await`"""
return self.lsp.unregister_capability_async(params)
@property
def workspace(self) -> Workspace:
"""Returns in-memory workspace."""
return self.lsp.workspace