|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import shutil |
| 4 | +from functools import partial |
| 5 | +from subprocess import CalledProcessError |
| 6 | +from typing import Any, override |
| 7 | + |
| 8 | +import anyio |
| 9 | +from attrs import define |
| 10 | +from loguru import logger |
| 11 | + |
| 12 | +from lsp_client.capability.notification import ( |
| 13 | + WithNotifyDidChangeConfiguration, |
| 14 | +) |
| 15 | +from lsp_client.capability.request import ( |
| 16 | + WithRequestCallHierarchy, |
| 17 | + WithRequestCodeAction, |
| 18 | + WithRequestCompletion, |
| 19 | + WithRequestDeclaration, |
| 20 | + WithRequestDefinition, |
| 21 | + WithRequestDocumentSymbol, |
| 22 | + WithRequestHover, |
| 23 | + WithRequestReferences, |
| 24 | + WithRequestSignatureHelp, |
| 25 | + WithRequestTypeDefinition, |
| 26 | + WithRequestWorkspaceSymbol, |
| 27 | +) |
| 28 | +from lsp_client.capability.server_notification import ( |
| 29 | + WithReceiveLogMessage, |
| 30 | + WithReceiveLogTrace, |
| 31 | + WithReceivePublishDiagnostics, |
| 32 | + WithReceiveShowMessage, |
| 33 | +) |
| 34 | +from lsp_client.capability.server_request import ( |
| 35 | + WithRespondConfigurationRequest, |
| 36 | + WithRespondInlayHintRefresh, |
| 37 | + WithRespondShowDocumentRequest, |
| 38 | + WithRespondShowMessageRequest, |
| 39 | + WithRespondWorkspaceFoldersRequest, |
| 40 | +) |
| 41 | +from lsp_client.clients.base import PythonClientBase |
| 42 | +from lsp_client.server import DefaultServers, ServerInstallationError |
| 43 | +from lsp_client.server.container import ContainerServer |
| 44 | +from lsp_client.server.local import LocalServer |
| 45 | +from lsp_client.utils.types import lsp_type |
| 46 | + |
| 47 | +BasedpyrightContainerServer = partial( |
| 48 | + ContainerServer, image="ghcr.io/lsp-client/basedpyright:latest" |
| 49 | +) |
| 50 | + |
| 51 | + |
| 52 | +async def ensure_basedpyright_installed() -> None: |
| 53 | + if shutil.which("basedpyright-langserver"): |
| 54 | + return |
| 55 | + |
| 56 | + logger.warning( |
| 57 | + "basedpyright-langserver not found, attempting to install via npm..." |
| 58 | + ) |
| 59 | + |
| 60 | + try: |
| 61 | + await anyio.run_process(["npm", "install", "-g", "basedpyright"]) |
| 62 | + logger.info("Successfully installed basedpyright-langserver via npm") |
| 63 | + return |
| 64 | + except CalledProcessError as e: |
| 65 | + raise ServerInstallationError( |
| 66 | + "Could not install basedpyright-langserver. Please install it manually with 'npm install -g basedpyright' or 'pip install basedpyright'. " |
| 67 | + "See https://github.com/detachhead/basedpyright for more information." |
| 68 | + ) from e |
| 69 | + |
| 70 | + |
| 71 | +BasedpyrightLocalServer = partial( |
| 72 | + LocalServer, |
| 73 | + program="basedpyright-langserver", |
| 74 | + args=["--stdio"], |
| 75 | + ensure_installed=ensure_basedpyright_installed, |
| 76 | +) |
| 77 | + |
| 78 | + |
| 79 | +@define |
| 80 | +class BasedpyrightClient( |
| 81 | + PythonClientBase, |
| 82 | + WithNotifyDidChangeConfiguration, |
| 83 | + WithRequestCallHierarchy, |
| 84 | + WithRequestCodeAction, |
| 85 | + WithRequestCompletion, |
| 86 | + WithRequestDeclaration, |
| 87 | + WithRequestDefinition, |
| 88 | + WithRequestDocumentSymbol, |
| 89 | + WithRequestHover, |
| 90 | + WithRequestReferences, |
| 91 | + WithRequestSignatureHelp, |
| 92 | + WithRequestTypeDefinition, |
| 93 | + WithRequestWorkspaceSymbol, |
| 94 | + WithReceiveLogMessage, |
| 95 | + WithReceiveLogTrace, |
| 96 | + WithReceivePublishDiagnostics, |
| 97 | + WithReceiveShowMessage, |
| 98 | + WithRespondConfigurationRequest, |
| 99 | + WithRespondInlayHintRefresh, |
| 100 | + WithRespondShowDocumentRequest, |
| 101 | + WithRespondShowMessageRequest, |
| 102 | + WithRespondWorkspaceFoldersRequest, |
| 103 | +): |
| 104 | + """ |
| 105 | + - Language: Python |
| 106 | + - Homepage: https://github.com/detachhead/basedpyright |
| 107 | + - Doc: https://github.com/detachhead/basedpyright |
| 108 | + - Github: https://github.com/detachhead/basedpyright |
| 109 | + """ |
| 110 | + |
| 111 | + @classmethod |
| 112 | + @override |
| 113 | + def create_default_servers(cls) -> DefaultServers: |
| 114 | + return DefaultServers( |
| 115 | + local=BasedpyrightLocalServer(), |
| 116 | + container=BasedpyrightContainerServer(), |
| 117 | + ) |
| 118 | + |
| 119 | + @override |
| 120 | + def check_server_compatibility(self, info: lsp_type.ServerInfo | None) -> None: |
| 121 | + return |
| 122 | + |
| 123 | + @override |
| 124 | + def create_default_config(self) -> dict[str, Any] | None: |
| 125 | + """ |
| 126 | + https://github.com/detachhead/basedpyright#settings |
| 127 | + """ |
| 128 | + return { |
| 129 | + "basedpyright": { |
| 130 | + "analysis": { |
| 131 | + "autoImportCompletions": True, |
| 132 | + "autoSearchPaths": True, |
| 133 | + "diagnosticMode": "openFilesOnly", |
| 134 | + "indexing": True, |
| 135 | + "typeCheckingMode": "recommended", |
| 136 | + "inlayHints": { |
| 137 | + "variableTypes": True, |
| 138 | + "functionReturnTypes": True, |
| 139 | + "callArgumentNames": True, |
| 140 | + "pytestParameters": True, |
| 141 | + }, |
| 142 | + } |
| 143 | + } |
| 144 | + } |
0 commit comments