Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use of ${workspaceFolder} in more commands #1299

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow use of ${workspaceFolder} in more commands
  • Loading branch information
garymm committed May 3, 2024
commit f02467b8203b79181ae210c62befd5a527d420c6
23 changes: 12 additions & 11 deletions packages/ansible-language-server/src/utils/commandRunner.ts
Original file line number Diff line number Diff line change
@@ -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,18 @@ 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 interpreterPathFromSettings = replaceWorkspaceFolderInPath(
this.settings.python.interpreterPath,
this.context,
);

const interpreterPath = isEEEnabled ? "python3" : interpreterPathFromConfig;
const interpreterPath = isEEEnabled
? "python3"
: interpreterPathFromSettings;
if (executable.startsWith("ansible")) {
executablePath = isEEEnabled
? executable
: getAnsibleCommandExecPath(executable, this.settings);
: getAnsibleCommandExecPath(executable, this.settings, this.context);
} else {
executablePath = executable;
}
22 changes: 19 additions & 3 deletions packages/ansible-language-server/src/utils/execPath.ts
Original file line number Diff line number Diff line change
@@ -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,22 @@ 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 pathFromSettings =
name === "ansible-lint"
? settings.validation.lint.path
: path.join(path.dirname(settings.ansible.path), name);
return replaceWorkspaceFolderInPath(pathFromSettings, context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sure the vscode api has generic support for replacing all variables in the settings, please use that instead of trying to do it DIY.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been unable to find it.
This suggests it doesn't exist: microsoft/vscode#46471

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However if you can find it I'd be happy to use it!

}

export function replaceWorkspaceFolderInPath(
p: string,
context: WorkspaceFolderContext,
): string {
if (p.includes("${workspaceFolder}")) {
const workspaceFolder = URI.parse(context.workspaceFolder.uri).path;
p = p.replace("${workspaceFolder}", workspaceFolder);
}
return p;
}