forked from posit-dev/positron
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start the LSP via a request to the kernel positron.lsp comm (posit-de…
- Loading branch information
1 parent
566aa9a
commit ba798e8
Showing
5 changed files
with
122 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# | ||
# Copyright (C) 2023 Posit Software, PBC. All rights reserved. | ||
# | ||
|
||
import urllib.parse | ||
from typing import Tuple | ||
|
||
from .positron_jedilsp import POSITRON | ||
|
||
|
||
class LSPService: | ||
""" | ||
LSPService manages the positron.lsp comm and cooridinates starting the LSP. | ||
""" | ||
|
||
def __init__(self, kernel): # noqa: F821 | ||
self.kernel = kernel | ||
self.lsp_comm = None | ||
|
||
def on_comm_open(self, comm, open_msg) -> None: | ||
""" | ||
Setup positron.lsp comm to receive messages. | ||
""" | ||
self.lsp_comm = comm | ||
comm.on_msg(self.receive_message) | ||
self.receive_open(open_msg) | ||
|
||
def receive_open(self, msg) -> None: | ||
""" | ||
Start the LSP on the requested port. | ||
""" | ||
data = msg["content"]["data"] | ||
|
||
client_address = data.get("client_address", None) | ||
if client_address is not None: | ||
host, port = self.split_address(client_address) | ||
if host is not None and port is not None: | ||
POSITRON.start(lsp_host=host, lsp_port=port, kernel=self.kernel) | ||
return | ||
|
||
raise ValueError("Invalid client_address in LSP open message") | ||
|
||
def receive_message(self, msg) -> None: | ||
""" | ||
Handle messages received from the client via the positron.lsp comm. | ||
""" | ||
pass | ||
|
||
def shutdown(self) -> None: | ||
if self.lsp_comm is not None: | ||
try: | ||
self.lsp_comm.close() | ||
except Exception: | ||
pass | ||
|
||
def split_address(self, client_address: str) -> Tuple[str | None, int | None]: | ||
""" | ||
Split an address of the form "host:port" into a tuple of (host, port). | ||
""" | ||
result = urllib.parse.urlsplit("//" + client_address) | ||
return (result.hostname, result.port) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.