|
| 1 | +from contextlib import asynccontextmanager |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import pathlib |
| 5 | +import shutil |
| 6 | +import stat |
| 7 | +from typing import AsyncIterator |
| 8 | +from multilspy.language_server import LanguageServer |
| 9 | +from multilspy.lsp_protocol_handler.server import ProcessLaunchInfo |
| 10 | +import json |
| 11 | +from multilspy.multilspy_utils import FileUtils, PlatformUtils |
| 12 | + |
| 13 | + |
| 14 | +class DartLanguageServer(LanguageServer): |
| 15 | + """ |
| 16 | + Provides Dart specific instantiation of the LanguageServer class. Contains various configurations and settings specific to Dart. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, config, logger, repository_root_path): |
| 20 | + """ |
| 21 | + Creates a DartServer instance. This class is not meant to be instantiated directly. Use LanguageServer.create() instead. |
| 22 | + """ |
| 23 | + |
| 24 | + executable_path = self.setup_runtime_dependencies(logger) |
| 25 | + super().__init__( |
| 26 | + config, |
| 27 | + logger, |
| 28 | + repository_root_path, |
| 29 | + ProcessLaunchInfo(cmd=executable_path, cwd=repository_root_path), |
| 30 | + "dart", |
| 31 | + ) |
| 32 | + |
| 33 | + def setup_runtime_dependencies(self, logger: "MultilspyLogger") -> str: |
| 34 | + platform_id = PlatformUtils.get_platform_id() |
| 35 | + |
| 36 | + with open(os.path.join(os.path.dirname(__file__), "runtime_dependencies.json"), "r") as f: |
| 37 | + d = json.load(f) |
| 38 | + del d["_description"] |
| 39 | + |
| 40 | + runtime_dependencies = d["runtimeDependencies"] |
| 41 | + runtime_dependencies = [ |
| 42 | + dependency for dependency in runtime_dependencies if dependency["platformId"] == platform_id.value |
| 43 | + ] |
| 44 | + |
| 45 | + assert len(runtime_dependencies) == 1 |
| 46 | + dependency = runtime_dependencies[0] |
| 47 | + |
| 48 | + dart_ls_dir = os.path.join(os.path.dirname(__file__), "static", "dart-language-server") |
| 49 | + dart_executable_path = os.path.join(dart_ls_dir, dependency["binaryName"]) |
| 50 | + |
| 51 | + if not os.path.exists(dart_ls_dir): |
| 52 | + os.makedirs(dart_ls_dir) |
| 53 | + FileUtils.download_and_extract_archive( |
| 54 | + logger, dependency["url"], dart_ls_dir, dependency["archiveType"] |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | + assert os.path.exists(dart_executable_path) |
| 59 | + os.chmod(dart_executable_path, stat.S_IEXEC) |
| 60 | + |
| 61 | + return f"{dart_executable_path} language-server --client-id multilspy.dart --client-version 1.2" |
| 62 | + |
| 63 | + |
| 64 | + def _get_initialize_params(self, repository_absolute_path: str): |
| 65 | + """ |
| 66 | + Returns the initialize params for the Dart Language Server. |
| 67 | + """ |
| 68 | + with open( |
| 69 | + os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r" |
| 70 | + ) as f: |
| 71 | + d = json.load(f) |
| 72 | + |
| 73 | + del d["_description"] |
| 74 | + |
| 75 | + d["processId"] = os.getpid() |
| 76 | + assert d["rootPath"] == "$rootPath" |
| 77 | + d["rootPath"] = repository_absolute_path |
| 78 | + |
| 79 | + assert d["rootUri"] == "$rootUri" |
| 80 | + d["rootUri"] = pathlib.Path(repository_absolute_path).as_uri() |
| 81 | + |
| 82 | + assert d["workspaceFolders"][0]["uri"] == "$uri" |
| 83 | + d["workspaceFolders"][0]["uri"] = pathlib.Path( |
| 84 | + repository_absolute_path |
| 85 | + ).as_uri() |
| 86 | + |
| 87 | + assert d["workspaceFolders"][0]["name"] == "$name" |
| 88 | + d["workspaceFolders"][0]["name"] = os.path.basename(repository_absolute_path) |
| 89 | + |
| 90 | + return d |
| 91 | + |
| 92 | + @asynccontextmanager |
| 93 | + async def start_server(self) -> AsyncIterator["DartLanguageServer"]: |
| 94 | + """ |
| 95 | + Start the language server and yield when the server is ready. |
| 96 | + """ |
| 97 | + |
| 98 | + async def execute_client_command_handler(params): |
| 99 | + return [] |
| 100 | + |
| 101 | + async def do_nothing(params): |
| 102 | + return |
| 103 | + |
| 104 | + async def check_experimental_status(params): |
| 105 | + pass |
| 106 | + |
| 107 | + async def window_log_message(msg): |
| 108 | + self.logger.log(f"LSP: window/logMessage: {msg}", logging.INFO) |
| 109 | + |
| 110 | + self.server.on_request("client/registerCapability", do_nothing) |
| 111 | + self.server.on_notification("language/status", do_nothing) |
| 112 | + self.server.on_notification("window/logMessage", window_log_message) |
| 113 | + self.server.on_request( |
| 114 | + "workspace/executeClientCommand", execute_client_command_handler |
| 115 | + ) |
| 116 | + self.server.on_notification("$/progress", do_nothing) |
| 117 | + self.server.on_notification("textDocument/publishDiagnostics", do_nothing) |
| 118 | + self.server.on_notification("language/actionableNotification", do_nothing) |
| 119 | + self.server.on_notification( |
| 120 | + "experimental/serverStatus", check_experimental_status |
| 121 | + ) |
| 122 | + |
| 123 | + async with super().start_server(): |
| 124 | + self.logger.log( |
| 125 | + "Starting dart-language-server server process", logging.INFO |
| 126 | + ) |
| 127 | + await self.server.start() |
| 128 | + initialize_params = self._get_initialize_params(self.repository_root_path) |
| 129 | + self.logger.log( |
| 130 | + "Sending initialize request to dart-language-server", |
| 131 | + logging.DEBUG, |
| 132 | + ) |
| 133 | + init_response = await self.server.send_request( |
| 134 | + "initialize", initialize_params |
| 135 | + ) |
| 136 | + self.logger.log( |
| 137 | + f"Received initialize response from dart-language-server: {init_response}", |
| 138 | + logging.INFO, |
| 139 | + ) |
| 140 | + |
| 141 | + self.server.notify.initialized({}) |
| 142 | + |
| 143 | + yield self |
| 144 | + |
| 145 | + await self.server.shutdown() |
| 146 | + await self.server.stop() |
0 commit comments