|
| 1 | +import { |
| 2 | + LanguageClient, |
| 3 | + LanguageClientOptions, |
| 4 | + ServerOptions, |
| 5 | +} from "vscode-languageclient/node"; |
| 6 | +import {Disposable, Uri, workspace} from "vscode"; |
| 7 | +import {CliWrapper} from "../cli/CliWrapper"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Manages the lifecycle of the DABs Language Server Protocol client. |
| 11 | + * Spawns `databricks bundle lsp` and connects via stdio to provide |
| 12 | + * deployment-aware features (document links, hover) for bundle YAML files. |
| 13 | + */ |
| 14 | +export class BundleLSPClient implements Disposable { |
| 15 | + private client: LanguageClient | undefined; |
| 16 | + private readonly cli: CliWrapper; |
| 17 | + |
| 18 | + constructor(cli: CliWrapper) { |
| 19 | + this.cli = cli; |
| 20 | + } |
| 21 | + |
| 22 | + async start(workspaceFolder: Uri, target?: string): Promise<void> { |
| 23 | + // Stop existing client if running. |
| 24 | + await this.stop(); |
| 25 | + |
| 26 | + const args = ["bundle", "lsp"]; |
| 27 | + if (target) { |
| 28 | + args.push("--target", target); |
| 29 | + } |
| 30 | + |
| 31 | + const serverOptions: ServerOptions = { |
| 32 | + command: this.cli.cliPath, |
| 33 | + args: args, |
| 34 | + options: { |
| 35 | + cwd: workspaceFolder.fsPath, |
| 36 | + }, |
| 37 | + }; |
| 38 | + |
| 39 | + const clientOptions: LanguageClientOptions = { |
| 40 | + documentSelector: [ |
| 41 | + { |
| 42 | + scheme: "file", |
| 43 | + language: "yaml", |
| 44 | + pattern: "**/databricks.yml", |
| 45 | + }, |
| 46 | + { |
| 47 | + scheme: "file", |
| 48 | + language: "yaml", |
| 49 | + pattern: "**/databricks.yaml", |
| 50 | + }, |
| 51 | + { |
| 52 | + scheme: "file", |
| 53 | + language: "yaml", |
| 54 | + pattern: "**/bundle.yml", |
| 55 | + }, |
| 56 | + { |
| 57 | + scheme: "file", |
| 58 | + language: "yaml", |
| 59 | + pattern: "**/bundle.yaml", |
| 60 | + }, |
| 61 | + ], |
| 62 | + workspaceFolder: { |
| 63 | + uri: workspaceFolder, |
| 64 | + name: workspace.name ?? "workspace", |
| 65 | + index: 0, |
| 66 | + }, |
| 67 | + }; |
| 68 | + |
| 69 | + this.client = new LanguageClient( |
| 70 | + "databricks-bundle-lsp", |
| 71 | + "Databricks Bundle LSP", |
| 72 | + serverOptions, |
| 73 | + clientOptions |
| 74 | + ); |
| 75 | + |
| 76 | + await this.client.start(); |
| 77 | + } |
| 78 | + |
| 79 | + async stop(): Promise<void> { |
| 80 | + if (this.client) { |
| 81 | + try { |
| 82 | + await this.client.stop(); |
| 83 | + } catch { |
| 84 | + // Client may already be stopped. |
| 85 | + } |
| 86 | + this.client = undefined; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + dispose(): void { |
| 91 | + this.stop(); |
| 92 | + } |
| 93 | +} |
0 commit comments