|
| 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.request import ( |
| 13 | + WithRequestCallHierarchy, |
| 14 | + WithRequestDefinition, |
| 15 | + WithRequestDocumentSymbol, |
| 16 | + WithRequestHover, |
| 17 | + WithRequestImplementation, |
| 18 | + WithRequestReferences, |
| 19 | + WithRequestTypeDefinition, |
| 20 | + WithRequestWorkspaceSymbol, |
| 21 | +) |
| 22 | +from lsp_client.capability.server_notification import ( |
| 23 | + WithReceivePublishDiagnostics, |
| 24 | +) |
| 25 | +from lsp_client.capability.server_notification.log_message import WithReceiveLogMessage |
| 26 | +from lsp_client.client.abc import LSPClient |
| 27 | +from lsp_client.server.docker import DockerServer |
| 28 | +from lsp_client.server.local import LocalServer |
| 29 | +from lsp_client.utils.types import lsp_type |
| 30 | + |
| 31 | +TsserverLocalServer = partial( |
| 32 | + LocalServer, command=["typescript-language-server", "--stdio"] |
| 33 | +) |
| 34 | +TsserverDockerServer = partial(DockerServer, image="docker.io/lspcontainers/tsserver") |
| 35 | + |
| 36 | + |
| 37 | +@define |
| 38 | +class TsserverClient( |
| 39 | + LSPClient, |
| 40 | + WithRequestHover, |
| 41 | + WithRequestDefinition, |
| 42 | + WithRequestReferences, |
| 43 | + WithRequestImplementation, |
| 44 | + WithRequestTypeDefinition, |
| 45 | + WithRequestCallHierarchy, |
| 46 | + WithRequestDocumentSymbol, |
| 47 | + WithRequestWorkspaceSymbol, |
| 48 | + WithReceiveLogMessage, |
| 49 | + WithReceivePublishDiagnostics, |
| 50 | +): |
| 51 | + """ |
| 52 | + - Language: TypeScript, JavaScript |
| 53 | + - Homepage: https://github.com/typescript-language-server/typescript-language-server |
| 54 | + - Doc: https://github.com/typescript-language-server/typescript-language-server#readme |
| 55 | + - Github: https://github.com/typescript-language-server/typescript-language-server |
| 56 | + - VSCode Extension: Built-in TypeScript support in VS Code |
| 57 | + """ |
| 58 | + |
| 59 | + # Preferences for TypeScript/JavaScript language features |
| 60 | + suggest_complete_function_calls: bool = True |
| 61 | + include_automatic_optional_chain_completions: bool = True |
| 62 | + include_completions_for_module_exports: bool = True |
| 63 | + include_completions_with_insert_text: bool = True |
| 64 | + |
| 65 | + # Auto-import and path suggestions |
| 66 | + auto_import_suggestions: bool = True |
| 67 | + path_suggestions: bool = True |
| 68 | + |
| 69 | + # Code actions and refactorings |
| 70 | + enable_call_hierarchy: bool = True |
| 71 | + enable_semantic_highlighting: bool = True |
| 72 | + |
| 73 | + @override |
| 74 | + def get_language_id(self) -> lsp_type.LanguageKind: |
| 75 | + return lsp_type.LanguageKind.TypeScript |
| 76 | + |
| 77 | + @override |
| 78 | + def create_initialization_options(self) -> dict[str, Any]: |
| 79 | + return { |
| 80 | + "preferences": { |
| 81 | + "includeCompletionsForModuleExports": self.include_completions_for_module_exports, |
| 82 | + "includeAutomaticOptionalChainCompletions": self.include_automatic_optional_chain_completions, |
| 83 | + "includeCompletionsWithInsertText": self.include_completions_with_insert_text, |
| 84 | + }, |
| 85 | + "suggest": { |
| 86 | + "completeFunctionCalls": self.suggest_complete_function_calls, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + @override |
| 91 | + def check_server_compatibility(self, info: lsp_type.ServerInfo | None) -> None: |
| 92 | + return |
| 93 | + |
| 94 | + @override |
| 95 | + async def ensure_installed(self) -> None: |
| 96 | + if shutil.which("typescript-language-server"): |
| 97 | + return |
| 98 | + |
| 99 | + logger.warning( |
| 100 | + "typescript-language-server not found, attempting to install via npm..." |
| 101 | + ) |
| 102 | + |
| 103 | + try: |
| 104 | + await anyio.run_process( |
| 105 | + ["npm", "install", "-g", "typescript-language-server", "typescript"] |
| 106 | + ) |
| 107 | + logger.info("Successfully installed typescript-language-server via npm") |
| 108 | + return |
| 109 | + except CalledProcessError as e: |
| 110 | + raise RuntimeError( |
| 111 | + "Could not install typescript-language-server. Please install it manually with 'npm install -g typescript-language-server typescript'. " |
| 112 | + "See https://github.com/typescript-language-server/typescript-language-server for more information." |
| 113 | + ) from e |
0 commit comments