58 lines
1.5 KiB
Python
58 lines
1.5 KiB
Python
|
|
# Python imports
|
||
|
|
from concurrent.futures import ThreadPoolExecutor
|
||
|
|
|
||
|
|
# Lib imports
|
||
|
|
|
||
|
|
# Application imports
|
||
|
|
from .mixins.lsp_client_events_mixin import LSPClientEventsMixin
|
||
|
|
from .controllers.lsp_controller import LSPController
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
class LSPClientController(LSPClientEventsMixin):
|
||
|
|
def __init__(self):
|
||
|
|
super(LSPClientController, self).__init__()
|
||
|
|
|
||
|
|
self._cache_refresh_timeout_id: int = None
|
||
|
|
|
||
|
|
self.executor: ThreadPoolExecutor = ThreadPoolExecutor(max_workers = 1)
|
||
|
|
self.active_language_id: str = ""
|
||
|
|
self.clients: dict = {}
|
||
|
|
|
||
|
|
|
||
|
|
def create_client(
|
||
|
|
self,
|
||
|
|
lang_id: str = "python",
|
||
|
|
workspace_uri: str = "",
|
||
|
|
init_opts: dict = {}
|
||
|
|
) -> LSPController:
|
||
|
|
if lang_id in self.clients: return None
|
||
|
|
|
||
|
|
address = "127.0.0.1"
|
||
|
|
port = 9999
|
||
|
|
uri = f"ws://{address}:{port}/{lang_id}"
|
||
|
|
client = LSPController()
|
||
|
|
|
||
|
|
client.set_language(lang_id)
|
||
|
|
client.set_socket(uri)
|
||
|
|
client.start_client()
|
||
|
|
|
||
|
|
if not client.ws_client.wait_for_connection(timeout = 5.0):
|
||
|
|
logger.error(f"Failed to connect to LSP server for {lang_id}")
|
||
|
|
return None
|
||
|
|
|
||
|
|
self.clients[lang_id] = client
|
||
|
|
|
||
|
|
return client
|
||
|
|
|
||
|
|
def close_client(self, lang_id: str) -> bool:
|
||
|
|
if lang_id not in self.clients: return False
|
||
|
|
|
||
|
|
controller = self.clients.pop(lang_id)
|
||
|
|
controller.stop_client()
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
def get_active_client(self) -> LSPController:
|
||
|
|
return self.clients[self.active_language_id]
|