From a37ab80b6e9bd10f651a15e477ba426048b81085 Mon Sep 17 00:00:00 2001 From: Gary Miguel Date: Fri, 3 May 2024 14:57:22 -0700 Subject: [PATCH] Allow use of ${workspaceFolder} in more commands --- .../src/utils/commandRunner.ts | 19 +++++++------- .../src/utils/execPath.ts | 25 ++++++++++++++++--- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/packages/ansible-language-server/src/utils/commandRunner.ts b/packages/ansible-language-server/src/utils/commandRunner.ts index 00e8ba3ab..a929c4c61 100644 --- a/packages/ansible-language-server/src/utils/commandRunner.ts +++ b/packages/ansible-language-server/src/utils/commandRunner.ts @@ -1,7 +1,10 @@ import { URI } from "vscode-uri"; import { Connection } from "vscode-languageserver"; import { withInterpreter, asyncExec } from "./misc"; -import { getAnsibleCommandExecPath } from "./execPath"; +import { + getAnsibleCommandExecPath, + replaceWorkspaceFolderInPath, +} from "./execPath"; import { WorkspaceFolderContext } from "../services/workspaceManager"; import { ExtensionSettings } from "../interfaces/extensionSettings"; @@ -33,20 +36,16 @@ export class CommandRunner { let command: string | undefined; let runEnv: NodeJS.ProcessEnv | undefined; const isEEEnabled = this.settings.executionEnvironment.enabled; - let interpreterPathFromConfig = this.settings.python.interpreterPath; - if (interpreterPathFromConfig.includes("${workspaceFolder}")) { - const workspaceFolder = URI.parse(this.context.workspaceFolder.uri).path; - interpreterPathFromConfig = interpreterPathFromConfig.replace( - "${workspaceFolder}", - workspaceFolder, - ); - } + const interpreterPathFromConfig = replaceWorkspaceFolderInPath( + this.settings.python.interpreterPath, + this.context, + ); const interpreterPath = isEEEnabled ? "python3" : interpreterPathFromConfig; if (executable.startsWith("ansible")) { executablePath = isEEEnabled ? executable - : getAnsibleCommandExecPath(executable, this.settings); + : getAnsibleCommandExecPath(executable, this.settings, this.context); } else { executablePath = executable; } diff --git a/packages/ansible-language-server/src/utils/execPath.ts b/packages/ansible-language-server/src/utils/execPath.ts index af9735a93..b57a4d466 100644 --- a/packages/ansible-language-server/src/utils/execPath.ts +++ b/packages/ansible-language-server/src/utils/execPath.ts @@ -1,6 +1,8 @@ // utils function to resolve executable path +import { URI } from "vscode-uri"; import * as path from "path"; import { ExtensionSettings } from "../interfaces/extensionSettings"; +import { WorkspaceFolderContext } from "../services/workspaceManager"; /** * A method to return the path to the provided executable @@ -11,8 +13,25 @@ import { ExtensionSettings } from "../interfaces/extensionSettings"; export function getAnsibleCommandExecPath( name: string, settings: ExtensionSettings, + context: WorkspaceFolderContext, ): string { - return name === "ansible-lint" - ? settings.validation.lint.path - : path.join(path.dirname(settings.ansible.path), name); + let pathFromConfig = + name === "ansible-lint" + ? settings.validation.lint.path + : path.join(path.dirname(settings.ansible.path), name); + return replaceWorkspaceFolderInPath(pathFromConfig, context); +} + +export function replaceWorkspaceFolderInPath( + pathFromSettings: string, + context: WorkspaceFolderContext, +): string { + if (pathFromSettings.includes("${workspaceFolder}")) { + const workspaceFolder = URI.parse(context.workspaceFolder.uri).path; + pathFromSettings = pathFromSettings.replace( + "${workspaceFolder}", + workspaceFolder, + ); + } + return pathFromSettings; }