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

add variable substitution for workspaceFolder #763

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
30 changes: 29 additions & 1 deletion src/utils/settingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import { workspace, WorkspaceConfiguration } from "vscode";
import { DescriptionConfiguration } from "../shared";
import { sep } from "path";


export function getWorkspaceConfiguration(): WorkspaceConfiguration {
return workspace.getConfiguration("leetcode");
Expand All @@ -13,7 +15,7 @@ export function shouldHideSolvedProblem(): boolean {
}

export function getWorkspaceFolder(): string {
return getWorkspaceConfiguration().get<string>("workspaceFolder", "");
return substitute(getWorkspaceConfiguration().get<string>("workspaceFolder", ""));
}

export function getEditorShortcuts(): string[] {
Expand Down Expand Up @@ -66,3 +68,29 @@ export interface IDescriptionConfiguration {
showInComment: boolean;
showInWebview: boolean;
}

function substitute<T>(val: T): T {
if (typeof val == 'string') {
val = val.replace(/\$\{(.*?)\}/g, (match, name) => {
const rep = replace(name);
return (rep === null) ? match : rep;
}) as unknown as T;
}
return val;
}

function replace(name: string): string | null {
if (name == 'pathSeparator') {
return sep as string;
}
const envPrefix = 'env:';
if (name.startsWith(envPrefix))
return process.env[name.substring(envPrefix.length)] || '';
const configPrefix = 'config:';
if (name.startsWith(configPrefix)) {
const config = workspace.getConfiguration().get(
name.substring(configPrefix.length));
return (typeof config == 'string') ? config : null;
}
return null;
}