|
| 1 | +""" |
| 2 | +Provides mSL (mIRC Scripting Language) specific instantiation of the LanguageServer class. |
| 3 | +Uses a custom Python-based LSP server (pygls) for parsing .mrc files. |
| 4 | +
|
| 5 | +The LSP server script is shipped as ``msl_lsp_server.py`` alongside this module |
| 6 | +and launched as a subprocess using the current Python interpreter. |
| 7 | +""" |
| 8 | + |
| 9 | +import logging |
| 10 | +import os |
| 11 | +import pathlib |
| 12 | +import sys |
| 13 | +import threading |
| 14 | + |
| 15 | +from solidlsp.ls import ( |
| 16 | + SolidLanguageServer, |
| 17 | +) |
| 18 | +from solidlsp.ls_config import LanguageServerConfig |
| 19 | +from solidlsp.lsp_protocol_handler.lsp_types import InitializeParams |
| 20 | +from solidlsp.lsp_protocol_handler.server import ProcessLaunchInfo |
| 21 | +from solidlsp.settings import SolidLSPSettings |
| 22 | + |
| 23 | +log = logging.getLogger(__name__) |
| 24 | + |
| 25 | +_MSL_LSP_SCRIPT = os.path.join(os.path.dirname(__file__), "msl_lsp_server.py") |
| 26 | + |
| 27 | + |
| 28 | +class MslLanguageServer(SolidLanguageServer): |
| 29 | + """ |
| 30 | + Provides mSL (mIRC Scripting Language) specific instantiation of the LanguageServer class. |
| 31 | + Uses a Python-based LSP server for parsing .mrc files (aliases, events, menus, dialogs). |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self, config: LanguageServerConfig, repository_root_path: str, solidlsp_settings: SolidLSPSettings): |
| 35 | + """ |
| 36 | + Creates an MslLanguageServer instance. This class is not meant to be instantiated directly. |
| 37 | + Use LanguageServer.create() instead. |
| 38 | + """ |
| 39 | + process_launch_info = ProcessLaunchInfo(cmd=[sys.executable, _MSL_LSP_SCRIPT], cwd=repository_root_path) |
| 40 | + super().__init__( |
| 41 | + config, |
| 42 | + repository_root_path, |
| 43 | + process_launch_info=process_launch_info, |
| 44 | + language_id="msl", |
| 45 | + solidlsp_settings=solidlsp_settings, |
| 46 | + ) |
| 47 | + self.server_ready = threading.Event() |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def _get_initialize_params(repository_absolute_path: str) -> InitializeParams: |
| 51 | + """Returns the initialize params for the mSL Language Server.""" |
| 52 | + root_uri = pathlib.Path(repository_absolute_path).as_uri() |
| 53 | + initialize_params = { |
| 54 | + "locale": "en", |
| 55 | + "capabilities": { |
| 56 | + "textDocument": { |
| 57 | + "synchronization": {"didSave": True, "dynamicRegistration": True}, |
| 58 | + "documentSymbol": { |
| 59 | + "dynamicRegistration": True, |
| 60 | + "hierarchicalDocumentSymbolSupport": True, |
| 61 | + "symbolKind": {"valueSet": list(range(1, 27))}, |
| 62 | + }, |
| 63 | + "hover": {"dynamicRegistration": True, "contentFormat": ["markdown", "plaintext"]}, |
| 64 | + "references": {"dynamicRegistration": True}, |
| 65 | + "definition": {"dynamicRegistration": True}, |
| 66 | + }, |
| 67 | + "workspace": { |
| 68 | + "workspaceFolders": True, |
| 69 | + "symbol": {"dynamicRegistration": True}, |
| 70 | + }, |
| 71 | + }, |
| 72 | + "processId": os.getpid(), |
| 73 | + "rootPath": repository_absolute_path, |
| 74 | + "rootUri": root_uri, |
| 75 | + "workspaceFolders": [{"uri": root_uri, "name": os.path.basename(repository_absolute_path)}], |
| 76 | + } |
| 77 | + return initialize_params # type: ignore |
| 78 | + |
| 79 | + def _start_server(self) -> None: |
| 80 | + """Starts the mSL Language Server.""" |
| 81 | + |
| 82 | + def window_log_message(msg: dict) -> None: |
| 83 | + log.info(f"LSP: window/logMessage: {msg}") |
| 84 | + self.server_ready.set() |
| 85 | + |
| 86 | + def do_nothing(params: dict) -> None: |
| 87 | + pass |
| 88 | + |
| 89 | + self.server.on_notification("window/logMessage", window_log_message) |
| 90 | + self.server.on_notification("textDocument/publishDiagnostics", do_nothing) |
| 91 | + |
| 92 | + log.info("Starting mSL server process") |
| 93 | + self.server.start() |
| 94 | + initialize_params = self._get_initialize_params(self.repository_root_path) |
| 95 | + |
| 96 | + log.info("Sending initialize request to mSL LSP server") |
| 97 | + init_response = self.server.send.initialize(initialize_params) |
| 98 | + log.debug(f"Received initialize response: {init_response}") |
| 99 | + |
| 100 | + self.server.notify.initialized({}) |
| 101 | + |
| 102 | + # Wait briefly for server readiness |
| 103 | + if not self.server_ready.wait(timeout=2.0): |
| 104 | + log.info("Timeout waiting for mSL server ready signal, proceeding anyway") |
| 105 | + self.server_ready.set() |
| 106 | + |
| 107 | + log.info("mSL server initialization complete") |
0 commit comments