| """ |
| Provides Python specific instantiation of the LanguageServer class. Contains various configurations and settings specific to Python. |
| """ |
|
|
| import logging |
| import os |
| import pathlib |
| import re |
| import threading |
|
|
| from overrides import override |
|
|
| from solidlsp.ls import SolidLanguageServer |
| from solidlsp.ls_config import LanguageServerConfig |
| from solidlsp.ls_logger import LanguageServerLogger |
| from solidlsp.lsp_protocol_handler.lsp_types import InitializeParams |
| from solidlsp.lsp_protocol_handler.server import ProcessLaunchInfo |
| from solidlsp.settings import SolidLSPSettings |
|
|
|
|
| class PyrightServer(SolidLanguageServer): |
| """ |
| Provides Python specific instantiation of the LanguageServer class using Pyright. |
| Contains various configurations and settings specific to Python. |
| """ |
|
|
| def __init__( |
| self, config: LanguageServerConfig, logger: LanguageServerLogger, repository_root_path: str, solidlsp_settings: SolidLSPSettings |
| ): |
| """ |
| Creates a PyrightServer instance. This class is not meant to be instantiated directly. |
| Use LanguageServer.create() instead. |
| """ |
| super().__init__( |
| config, |
| logger, |
| repository_root_path, |
| |
| |
| ProcessLaunchInfo(cmd="python -m pyright.langserver --stdio", cwd=repository_root_path), |
| "python", |
| solidlsp_settings, |
| ) |
|
|
| |
| self.analysis_complete = threading.Event() |
| self.found_source_files = False |
|
|
| @override |
| def is_ignored_dirname(self, dirname: str) -> bool: |
| return super().is_ignored_dirname(dirname) or dirname in ["venv", "__pycache__"] |
|
|
| @staticmethod |
| def _get_initialize_params(repository_absolute_path: str) -> InitializeParams: |
| """ |
| Returns the initialize params for the Pyright Language Server. |
| """ |
| |
| initialize_params: InitializeParams = { |
| "processId": os.getpid(), |
| "rootPath": repository_absolute_path, |
| "rootUri": pathlib.Path(repository_absolute_path).as_uri(), |
| "initializationOptions": { |
| "exclude": [ |
| "**/__pycache__", |
| "**/.venv", |
| "**/.env", |
| "**/build", |
| "**/dist", |
| "**/.pixi", |
| ], |
| "reportMissingImports": "error", |
| }, |
| "capabilities": { |
| "workspace": { |
| "workspaceEdit": {"documentChanges": True}, |
| "didChangeConfiguration": {"dynamicRegistration": True}, |
| "didChangeWatchedFiles": {"dynamicRegistration": True}, |
| "symbol": { |
| "dynamicRegistration": True, |
| "symbolKind": {"valueSet": list(range(1, 27))}, |
| }, |
| "executeCommand": {"dynamicRegistration": True}, |
| }, |
| "textDocument": { |
| "synchronization": {"dynamicRegistration": True, "willSave": True, "willSaveWaitUntil": True, "didSave": True}, |
| "hover": {"dynamicRegistration": True, "contentFormat": ["markdown", "plaintext"]}, |
| "signatureHelp": { |
| "dynamicRegistration": True, |
| "signatureInformation": { |
| "documentationFormat": ["markdown", "plaintext"], |
| "parameterInformation": {"labelOffsetSupport": True}, |
| }, |
| }, |
| "definition": {"dynamicRegistration": True}, |
| "references": {"dynamicRegistration": True}, |
| "documentSymbol": { |
| "dynamicRegistration": True, |
| "symbolKind": {"valueSet": list(range(1, 27))}, |
| "hierarchicalDocumentSymbolSupport": True, |
| }, |
| "publishDiagnostics": {"relatedInformation": True}, |
| }, |
| }, |
| "workspaceFolders": [ |
| {"uri": pathlib.Path(repository_absolute_path).as_uri(), "name": os.path.basename(repository_absolute_path)} |
| ], |
| } |
|
|
| return initialize_params |
|
|
| def _start_server(self): |
| """ |
| Starts the Pyright Language Server and waits for initial workspace analysis to complete. |
| |
| This prevents zombie processes by ensuring Pyright has finished its initial background |
| tasks before we consider the server ready. |
| |
| Usage: |
| ``` |
| async with lsp.start_server(): |
| # LanguageServer has been initialized and workspace analysis is complete |
| await lsp.request_definition(...) |
| await lsp.request_references(...) |
| # Shutdown the LanguageServer on exit from scope |
| # LanguageServer has been shutdown cleanly |
| ``` |
| """ |
|
|
| def execute_client_command_handler(params): |
| return [] |
|
|
| def do_nothing(params): |
| return |
|
|
| def window_log_message(msg): |
| """ |
| Monitor Pyright's log messages to detect when initial analysis is complete. |
| Pyright logs "Found X source files" when it finishes scanning the workspace. |
| """ |
| message_text = msg.get("message", "") |
| self.logger.log(f"LSP: window/logMessage: {message_text}", logging.INFO) |
|
|
| |
| |
| if re.search(r"Found \d+ source files?", message_text): |
| self.logger.log("Pyright workspace scanning complete", logging.INFO) |
| self.found_source_files = True |
| self.analysis_complete.set() |
| self.completions_available.set() |
|
|
| def check_experimental_status(params): |
| """ |
| Also listen for experimental/serverStatus as a backup signal |
| """ |
| if params.get("quiescent") == True: |
| self.logger.log("Received experimental/serverStatus with quiescent=true", logging.INFO) |
| if not self.found_source_files: |
| self.analysis_complete.set() |
| self.completions_available.set() |
|
|
| |
| self.server.on_request("client/registerCapability", do_nothing) |
| self.server.on_notification("language/status", do_nothing) |
| self.server.on_notification("window/logMessage", window_log_message) |
| self.server.on_request("workspace/executeClientCommand", execute_client_command_handler) |
| self.server.on_notification("$/progress", do_nothing) |
| self.server.on_notification("textDocument/publishDiagnostics", do_nothing) |
| self.server.on_notification("language/actionableNotification", do_nothing) |
| self.server.on_notification("experimental/serverStatus", check_experimental_status) |
|
|
| self.logger.log("Starting pyright-langserver server process", logging.INFO) |
| self.server.start() |
|
|
| |
| initialize_params = self._get_initialize_params(self.repository_root_path) |
|
|
| self.logger.log( |
| "Sending initialize request from LSP client to pyright server and awaiting response", |
| logging.INFO, |
| ) |
| init_response = self.server.send.initialize(initialize_params) |
| self.logger.log(f"Received initialize response from pyright server: {init_response}", logging.INFO) |
|
|
| |
| assert "textDocumentSync" in init_response["capabilities"] |
| assert "completionProvider" in init_response["capabilities"] |
| assert "definitionProvider" in init_response["capabilities"] |
|
|
| |
| self.server.notify.initialized({}) |
|
|
| |
| |
| self.logger.log("Waiting for Pyright to complete initial workspace analysis...", logging.INFO) |
| if self.analysis_complete.wait(timeout=5.0): |
| self.logger.log("Pyright initial analysis complete, server ready", logging.INFO) |
| else: |
| self.logger.log("Timeout waiting for Pyright analysis completion, proceeding anyway", logging.WARNING) |
| |
| self.analysis_complete.set() |
| self.completions_available.set() |
|
|