|
| 1 | +""" |
| 2 | +Provides Deno-specific instantiation of the LanguageServer class, using the |
| 3 | +language server built into the Deno CLI (``deno lsp``). |
| 4 | +""" |
| 5 | + |
| 6 | +import logging |
| 7 | +import shutil |
| 8 | + |
| 9 | +from overrides import override |
| 10 | + |
| 11 | +from solidlsp.ls import LanguageServerDependencyProvider, LanguageServerDependencyProviderSinglePath, SolidLanguageServer |
| 12 | +from solidlsp.ls_config import LanguageServerConfig |
| 13 | +from solidlsp.settings import SolidLSPSettings |
| 14 | + |
| 15 | +log = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class DenoLanguageServer(SolidLanguageServer): |
| 19 | + """ |
| 20 | + Deno instantiation of the LanguageServer class, backed by ``deno lsp``. |
| 21 | +
|
| 22 | + Serves TypeScript/JavaScript in Deno projects. Unlike the plain |
| 23 | + typescript-language-server, ``deno lsp`` understands Deno-specific module |
| 24 | + resolution (``npm:`` / ``jsr:`` / ``https:`` imports) and the ``Deno.*`` |
| 25 | + global namespace. |
| 26 | +
|
| 27 | + This server overlaps the TypeScript server on file extensions and is therefore |
| 28 | + marked experimental: it is not auto-detected and must be selected explicitly via |
| 29 | + ``languages: [deno]`` in ``project.yml``. |
| 30 | + """ |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def supports_implementation_request(cls) -> bool: |
| 34 | + return True |
| 35 | + |
| 36 | + def __init__(self, config: LanguageServerConfig, repository_root_path: str, solidlsp_settings: SolidLSPSettings): |
| 37 | + super().__init__( |
| 38 | + config, |
| 39 | + repository_root_path, |
| 40 | + None, |
| 41 | + "typescript", |
| 42 | + solidlsp_settings, |
| 43 | + ) |
| 44 | + |
| 45 | + def _create_dependency_provider(self) -> LanguageServerDependencyProvider: |
| 46 | + return self.DependencyProvider(self._custom_settings, self._ls_resources_dir) |
| 47 | + |
| 48 | + @override |
| 49 | + def is_ignored_dirname(self, dirname: str) -> bool: |
| 50 | + # node_modules appears in Deno projects using npm compatibility; vendor/ holds |
| 51 | + # vendored remote dependencies. Neither should be indexed as project sources. |
| 52 | + return super().is_ignored_dirname(dirname) or dirname in ["node_modules", "vendor", "dist", "build"] |
| 53 | + |
| 54 | + class DependencyProvider(LanguageServerDependencyProviderSinglePath): |
| 55 | + def _get_or_install_core_dependency(self) -> str: |
| 56 | + """Return the path to the ``deno`` executable (the language server ships with it).""" |
| 57 | + deno_path = shutil.which("deno") |
| 58 | + if deno_path is None: |
| 59 | + raise FileNotFoundError( |
| 60 | + "The 'deno' executable was not found on PATH. Install Deno " |
| 61 | + "(https://docs.deno.com/runtime/getting_started/installation/) — it bundles " |
| 62 | + "the language server used here — and ensure 'deno' is on your PATH." |
| 63 | + ) |
| 64 | + return deno_path |
| 65 | + |
| 66 | + def _create_launch_command(self, core_path: str) -> list[str]: |
| 67 | + return [core_path, "lsp"] |
| 68 | + |
| 69 | + def _get_language_id_for_file(self, relative_file_path: str) -> str: |
| 70 | + # deno lsp relies on the correct languageId; .tsx/.jsx in particular must not |
| 71 | + # be sent as plain "typescript" or symbol ranges get truncated at JSX expressions. |
| 72 | + if relative_file_path.endswith(".tsx"): |
| 73 | + return "typescriptreact" |
| 74 | + if relative_file_path.endswith(".jsx"): |
| 75 | + return "javascriptreact" |
| 76 | + if relative_file_path.endswith((".js", ".mjs", ".cjs")): |
| 77 | + return "javascript" |
| 78 | + return "typescript" |
| 79 | + |
| 80 | + def _create_base_initialize_params(self) -> dict: |
| 81 | + return { |
| 82 | + "locale": "en", |
| 83 | + "capabilities": { |
| 84 | + "textDocument": { |
| 85 | + "synchronization": {"didSave": True, "dynamicRegistration": True}, |
| 86 | + "definition": {"dynamicRegistration": True}, |
| 87 | + "references": {"dynamicRegistration": True}, |
| 88 | + "documentSymbol": { |
| 89 | + "dynamicRegistration": True, |
| 90 | + "hierarchicalDocumentSymbolSupport": True, |
| 91 | + "symbolKind": {"valueSet": list(range(1, 27))}, |
| 92 | + }, |
| 93 | + "hover": {"dynamicRegistration": True, "contentFormat": ["markdown", "plaintext"]}, |
| 94 | + "completion": {"dynamicRegistration": True, "completionItem": {"snippetSupport": True}}, |
| 95 | + "rename": {"dynamicRegistration": True, "prepareSupport": True}, |
| 96 | + "publishDiagnostics": {"relatedInformation": True}, |
| 97 | + }, |
| 98 | + "workspace": { |
| 99 | + "workspaceFolders": True, |
| 100 | + "configuration": True, |
| 101 | + "didChangeConfiguration": {"dynamicRegistration": True}, |
| 102 | + "symbol": {"dynamicRegistration": True}, |
| 103 | + }, |
| 104 | + }, |
| 105 | + # deno lsp reads its settings from initializationOptions; enabling the server |
| 106 | + # and the linter mirrors the defaults of the official VS Code Deno extension. |
| 107 | + "initializationOptions": { |
| 108 | + "enable": True, |
| 109 | + "lint": True, |
| 110 | + "unstable": False, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + def _start_server(self) -> None: |
| 115 | + """Start the ``deno lsp`` process and drive the LSP initialize handshake.""" |
| 116 | + |
| 117 | + def register_capability_handler(params: dict) -> None: |
| 118 | + return |
| 119 | + |
| 120 | + def window_log_message(msg: dict) -> None: |
| 121 | + log.info(f"LSP: window/logMessage: {msg}") |
| 122 | + |
| 123 | + def do_nothing(params: dict) -> None: |
| 124 | + return |
| 125 | + |
| 126 | + def configuration_handler(params: dict) -> list: |
| 127 | + # deno lsp requests workspace/configuration during startup; return an empty |
| 128 | + # settings object per requested item so it proceeds with its defaults. |
| 129 | + return [{} for _ in params.get("items", [])] |
| 130 | + |
| 131 | + self.server.on_request("client/registerCapability", register_capability_handler) |
| 132 | + self.server.on_request("workspace/configuration", configuration_handler) |
| 133 | + self.server.on_notification("window/logMessage", window_log_message) |
| 134 | + self.server.on_notification("$/progress", do_nothing) |
| 135 | + self.server.on_notification("textDocument/publishDiagnostics", do_nothing) |
| 136 | + # Deno-specific notifications emitted after config discovery; no action needed. |
| 137 | + self.server.on_notification("deno/didRefreshDenoConfigurationTree", do_nothing) |
| 138 | + self.server.on_notification("deno/didChangeDenoConfiguration", do_nothing) |
| 139 | + |
| 140 | + log.info("Starting deno lsp server process") |
| 141 | + self.server.start() |
| 142 | + initialize_params = self._create_initialize_params() |
| 143 | + |
| 144 | + log.info("Sending initialize request from LSP client to deno lsp and awaiting response") |
| 145 | + init_response = self.server.send.initialize(initialize_params) |
| 146 | + |
| 147 | + assert "textDocumentSync" in init_response["capabilities"] |
| 148 | + assert "definitionProvider" in init_response["capabilities"] |
| 149 | + assert "documentSymbolProvider" in init_response["capabilities"] |
| 150 | + assert "referencesProvider" in init_response["capabilities"] |
| 151 | + |
| 152 | + self.server.notify.initialized({}) |
0 commit comments