diff --git a/src/extension.ts b/src/extension.ts index 0e6304c..0cbe83b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -6,8 +6,12 @@ import { LanguageClientOptions, ServerOptions, TransportKind, + WorkspaceFolder, } from "vscode-languageclient/node"; import { getHelmLsExecutable } from "./util/executable"; +import path from "path"; +import fs from "fs"; +import url from "url"; let client: LanguageClient; @@ -21,10 +25,27 @@ export async function activate(_: vscode.ExtensionContext) { console.log("Launching " + helmLsExecutable); + const workSpacePath = vscode.workspace.workspaceFolders?.[0].uri.path; + const filePath = vscode.window.activeTextEditor?.document.fileName; + var cwd: string + + console.log("Workspace path: " + workSpacePath, "File path: " + filePath); + if (workSpacePath && fs.existsSync(path.join(workSpacePath, "Chart.yaml"))) { + console.log("Setting cwd to " + workSpacePath); + cwd = workSpacePath + } + else if (filePath) { + console.log("Setting cwd to " + traversePathUpToChartYaml(filePath)); + cwd = traversePathUpToChartYaml(filePath) + } + const executable: Executable = { command: helmLsExecutable, args: ["serve"], transport: TransportKind.stdio, + options: { + cwd: cwd + } }; const serverOptions: ServerOptions = { @@ -35,6 +56,10 @@ export async function activate(_: vscode.ExtensionContext) { const clientOptions: LanguageClientOptions = { documentSelector: [{ scheme: "file", language: "helm" }], synchronize: {}, + workspaceFolder: { + uri: url.pathToFileURL(cwd), + name: vscode.workspace.workspaceFolders?.[0].name, + } }; client = new LanguageClient( @@ -54,3 +79,14 @@ export function deactivate(): Thenable | undefined { } return client.stop(); } + +function traversePathUpToChartYaml(directory: string): string { + if (fs.existsSync(path.join(directory, "Chart.yaml"))) { + return directory + } + const parent = path.dirname(directory) + if (parent === "/") { + return "" + } + return traversePathUpToChartYaml(parent) +}