|
| 1 | +""" |
| 2 | +Provides JSON specific instantiation of the LanguageServer class using vscode-json-languageserver. |
| 3 | +Contains various configurations and settings specific to JSON files. |
| 4 | +""" |
| 5 | + |
| 6 | +import logging |
| 7 | +import os |
| 8 | +import pathlib |
| 9 | +import shutil |
| 10 | +from typing import Any |
| 11 | + |
| 12 | +from solidlsp.language_servers.common import RuntimeDependency, RuntimeDependencyCollection, build_npm_install_command |
| 13 | +from solidlsp.ls import LanguageServerDependencyProvider, LanguageServerDependencyProviderSinglePath, SolidLanguageServer |
| 14 | +from solidlsp.ls_config import LanguageServerConfig |
| 15 | +from solidlsp.lsp_protocol_handler.lsp_types import InitializeParams |
| 16 | +from solidlsp.settings import SolidLSPSettings |
| 17 | + |
| 18 | +log = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +class JsonLanguageServer(SolidLanguageServer): |
| 22 | + """ |
| 23 | + Provides JSON specific instantiation of the LanguageServer class using vscode-json-languageserver. |
| 24 | + Contains various configurations and settings specific to JSON files. |
| 25 | +
|
| 26 | + Note: Cross-file references are not supported for JSON (the language server only provides |
| 27 | + document symbols and hover). JSON is useful for getting a structured overview of JSON files |
| 28 | + and navigating their contents. |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self, config: LanguageServerConfig, repository_root_path: str, solidlsp_settings: SolidLSPSettings): |
| 32 | + """ |
| 33 | + Creates a JsonLanguageServer instance. This class is not meant to be instantiated directly. |
| 34 | + Use LanguageServer.create() instead. |
| 35 | + """ |
| 36 | + super().__init__( |
| 37 | + config, |
| 38 | + repository_root_path, |
| 39 | + None, |
| 40 | + "json", |
| 41 | + solidlsp_settings, |
| 42 | + ) |
| 43 | + |
| 44 | + def _create_dependency_provider(self) -> LanguageServerDependencyProvider: |
| 45 | + return self.DependencyProvider(self._custom_settings, self._ls_resources_dir) |
| 46 | + |
| 47 | + class DependencyProvider(LanguageServerDependencyProviderSinglePath): |
| 48 | + def _get_or_install_core_dependency(self) -> str: |
| 49 | + """ |
| 50 | + Setup runtime dependencies for JSON Language Server and return the path to the executable. |
| 51 | + """ |
| 52 | + is_node_installed = shutil.which("node") is not None |
| 53 | + assert is_node_installed, "node is not installed or isn't in PATH. Please install NodeJS and try again." |
| 54 | + is_npm_installed = shutil.which("npm") is not None |
| 55 | + assert is_npm_installed, "npm is not installed or isn't in PATH. Please install npm and try again." |
| 56 | + |
| 57 | + json_language_server_version = self._custom_settings.get("json_language_server_version", "1.3.4") |
| 58 | + npm_registry = self._custom_settings.get("npm_registry") |
| 59 | + |
| 60 | + deps = RuntimeDependencyCollection( |
| 61 | + [ |
| 62 | + RuntimeDependency( |
| 63 | + id="vscode-json-languageserver", |
| 64 | + description="vscode-json-languageserver package (Microsoft)", |
| 65 | + command=build_npm_install_command("vscode-json-languageserver", json_language_server_version, npm_registry), |
| 66 | + platform_id="any", |
| 67 | + ), |
| 68 | + ] |
| 69 | + ) |
| 70 | + |
| 71 | + json_ls_dir = os.path.join(self._ls_resources_dir, "json-lsp") |
| 72 | + json_executable_path = os.path.join(json_ls_dir, "node_modules", ".bin", "vscode-json-languageserver") |
| 73 | + |
| 74 | + if os.name == "nt": |
| 75 | + json_executable_path += ".cmd" |
| 76 | + |
| 77 | + if not os.path.exists(json_executable_path): |
| 78 | + log.info(f"JSON Language Server executable not found at {json_executable_path}. Installing...") |
| 79 | + deps.install(json_ls_dir) |
| 80 | + log.info("JSON language server dependencies installed successfully") |
| 81 | + |
| 82 | + if not os.path.exists(json_executable_path): |
| 83 | + raise FileNotFoundError( |
| 84 | + f"vscode-json-languageserver executable not found at {json_executable_path}, something went wrong with the installation." |
| 85 | + ) |
| 86 | + |
| 87 | + return json_executable_path |
| 88 | + |
| 89 | + def _create_launch_command(self, core_path: str) -> list[str]: |
| 90 | + return [core_path, "--stdio"] |
| 91 | + |
| 92 | + @staticmethod |
| 93 | + def _get_initialize_params(repository_absolute_path: str) -> InitializeParams: |
| 94 | + """ |
| 95 | + Returns the initialize params for the JSON Language Server. |
| 96 | + """ |
| 97 | + root_uri = pathlib.Path(repository_absolute_path).as_uri() |
| 98 | + initialize_params = { |
| 99 | + "locale": "en", |
| 100 | + "capabilities": { |
| 101 | + "textDocument": { |
| 102 | + "synchronization": {"didSave": True, "dynamicRegistration": True}, |
| 103 | + "completion": {"dynamicRegistration": True, "completionItem": {"snippetSupport": True}}, |
| 104 | + "definition": {"dynamicRegistration": True}, |
| 105 | + "references": {"dynamicRegistration": True}, |
| 106 | + "documentSymbol": { |
| 107 | + "dynamicRegistration": True, |
| 108 | + "hierarchicalDocumentSymbolSupport": True, |
| 109 | + "symbolKind": {"valueSet": list(range(1, 27))}, |
| 110 | + }, |
| 111 | + "hover": {"dynamicRegistration": True, "contentFormat": ["markdown", "plaintext"]}, |
| 112 | + }, |
| 113 | + "workspace": { |
| 114 | + "workspaceFolders": True, |
| 115 | + "didChangeConfiguration": {"dynamicRegistration": True}, |
| 116 | + }, |
| 117 | + }, |
| 118 | + "processId": os.getpid(), |
| 119 | + "rootPath": repository_absolute_path, |
| 120 | + "rootUri": root_uri, |
| 121 | + "workspaceFolders": [ |
| 122 | + { |
| 123 | + "uri": root_uri, |
| 124 | + "name": os.path.basename(repository_absolute_path), |
| 125 | + } |
| 126 | + ], |
| 127 | + } |
| 128 | + return initialize_params # type: ignore |
| 129 | + |
| 130 | + def _start_server(self) -> None: |
| 131 | + """ |
| 132 | + Starts the JSON Language Server, waits for the server to be ready and yields the LanguageServer instance. |
| 133 | + """ |
| 134 | + |
| 135 | + def register_capability_handler(params: Any) -> None: |
| 136 | + return |
| 137 | + |
| 138 | + def do_nothing(params: Any) -> None: |
| 139 | + return |
| 140 | + |
| 141 | + def window_log_message(msg: dict) -> None: |
| 142 | + log.info(f"LSP: window/logMessage: {msg}") |
| 143 | + |
| 144 | + self.server.on_request("client/registerCapability", register_capability_handler) |
| 145 | + self.server.on_notification("window/logMessage", window_log_message) |
| 146 | + self.server.on_notification("$/progress", do_nothing) |
| 147 | + self.server.on_notification("textDocument/publishDiagnostics", do_nothing) |
| 148 | + |
| 149 | + log.info("Starting JSON server process") |
| 150 | + self.server.start() |
| 151 | + initialize_params = self._get_initialize_params(self.repository_root_path) |
| 152 | + |
| 153 | + log.info("Sending initialize request from LSP client to LSP server and awaiting response") |
| 154 | + init_response = self.server.send.initialize(initialize_params) |
| 155 | + log.debug(f"Received initialize response from JSON server: {init_response}") |
| 156 | + |
| 157 | + if "documentSymbolProvider" in init_response.get("capabilities", {}): |
| 158 | + log.info("JSON server supports document symbols") |
| 159 | + else: |
| 160 | + log.warning("Warning: JSON server does not report document symbol support") |
| 161 | + |
| 162 | + self.server.notify.initialized({}) |
| 163 | + |
| 164 | + log.info("JSON server initialization complete") |
0 commit comments