Skip to content

Commit

Permalink
feat: Open env variables defined in other files than toml files
Browse files Browse the repository at this point in the history
  • Loading branch information
hverlin committed Dec 23, 2024
1 parent 5e9be2e commit 9c2f2ac
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
{
"view": "miseEnvsView",
"contents": "[Environment variables](https://mise.jdx.dev/environments.html) will be displayed here.",
"when": "config.mise.binPath && config.mise.enable"
"when": "config.mise.binPath && config.mise.enable && !mise.envProviderError"
},
{
"view": "miseEnvsView",
Expand Down
22 changes: 20 additions & 2 deletions src/miseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const STATE_DIR =
process.env.MISE_STATE_DIR ?? path.join(XDG_STATE_HOME, "mise");
const TRACKED_CONFIG_DIR = path.join(STATE_DIR, "tracked-configs");

const MIN_MISE_VERSION = [2024, 11, 32] as const;
const MIN_MISE_VERSION = [2024, 12, 2] as const;

function compareVersions(
a: readonly [number, number, number],
Expand Down Expand Up @@ -186,7 +186,7 @@ export class MiseService {
execution,
);

const p = new Promise((resolve, reject) => {
const p = new Promise((resolve) => {
const disposable = vscode.tasks.onDidEndTask((e) => {
if (e.execution.task === task) {
vscode.commands.executeCommand(MISE_RELOAD);
Expand Down Expand Up @@ -462,6 +462,24 @@ export class MiseService {
}
}

async getEnvWithInfo() {
if (!this.getMiseBinaryPath()) {
return [];
}

const { stdout } = await this.cache.execCmd({
command: "env --json-extended",
});

const parsed = JSON.parse(stdout) as Record<string, MiseEnvWithInfo>;
return Object.entries(parsed).map(([key, info]) => ({
name: key,
value: info.value,
tool: info?.tool,
source: info?.source ? expandPath(info.source) : undefined,
}));
}

async miseFmt() {
await this.execMiseCommand("fmt", { setMiseEnv: false });
}
Expand Down
58 changes: 41 additions & 17 deletions src/providers/envProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
MISE_COPY_ENV_VARIABLE_NAME,
MISE_COPY_ENV_VARIABLE_VALUE,
MISE_OPEN_ENV_VAR_DEFINITION,
MISE_OPEN_TOOL_DEFINITION,
MISE_SET_ENV_VARIABLE,
} from "../commands";
import {
Expand Down Expand Up @@ -34,7 +35,7 @@ export class MiseEnvsProvider implements vscode.TreeDataProvider<EnvItem> {
}

async getEnvItems() {
const envs = await this.miseService.getEnvs();
const envs = await this.miseService.getEnvWithInfo();
return envs.map((env) => new EnvItem(env));
}

Expand All @@ -58,9 +59,16 @@ export class MiseEnvsProvider implements vscode.TreeDataProvider<EnvItem> {
}

class EnvItem extends vscode.TreeItem {
constructor(public env: MiseEnv) {
constructor(public env: MiseEnvWithInfo) {
const label = env.value ? `${env.name}=${env.value}` : env.name;
super(label, vscode.TreeItemCollapsibleState.None);
this.tooltip = [
`${env.name}=${env.value}`,
env.source ? `source: ${env.source}` : "",
env.tool ? `Tool: ${env.tool}` : "",
]
.filter(Boolean)
.join("\n");

this.contextValue = "envItem";
this.command = {
Expand All @@ -79,7 +87,7 @@ export function registerEnvsCommands(
vscode.commands.registerCommand(
MISE_OPEN_ENV_VAR_DEFINITION,
async (name: string | undefined) => {
const possibleEnvs = await miseService.getEnvs();
const possibleEnvs = await miseService.getEnvWithInfo();
let selectedName = name;
if (!selectedName) {
selectedName = await vscode.window.showQuickPick(
Expand All @@ -93,20 +101,36 @@ export function registerEnvsCommands(
return;
}

const configs = (await miseService.getMiseConfigFiles()).filter((c) =>
c.path.endsWith(".toml"),
);

const documents = await Promise.all(
configs.map((config) =>
vscode.workspace.openTextDocument(config.path),
),
);
const needle = findEnvVarPosition(documents, env.name);
if (needle?.range) {
void vscode.window.showTextDocument(needle.document, {
selection: needle.range,
});
if (env.source) {
const document = await vscode.workspace.openTextDocument(env.source);
const needle = findEnvVarPosition([document], env.name);
if (needle?.range) {
void vscode.window.showTextDocument(needle.document, {
selection: needle.range,
});
}
} else if (env.tool) {
await vscode.commands.executeCommand(
MISE_OPEN_TOOL_DEFINITION,
env.tool,
);
} else {
const configs = (await miseService.getMiseConfigFiles()).filter((c) =>
c.path.endsWith(".toml"),
);

const documents = await Promise.all(
configs.map((config) =>
vscode.workspace.openTextDocument(config.path),
),
);
const needle = findEnvVarPosition(documents, env.name);

if (needle?.range) {
void vscode.window.showTextDocument(needle.document, {
selection: needle.range,
});
}
}
},
),
Expand Down
6 changes: 5 additions & 1 deletion src/providers/toolsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,12 @@ export function registerToolsCommands(
context.subscriptions.push(
vscode.commands.registerCommand(
MISE_OPEN_TOOL_DEFINITION,
async (tool: MiseTool | undefined) => {
async (tool: MiseTool | string | undefined) => {
let selectedTool = tool;
if (typeof selectedTool === "string") {
const tools = await miseService.getCurrentTools();
selectedTool = tools.find((t) => t.name === tool);
}
if (!selectedTool) {
const tools = await miseService.getCurrentTools();
const toolNames = tools.map(
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ type MiseEnv = {
name: string;
value: string;
};

type MiseEnvWithInfo = MiseEnv & {
source?: string;
tool?: string;
};
20 changes: 16 additions & 4 deletions src/utils/miseFileParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,22 @@ export function findEnvVarPosition(
envVarName: string,
) {
for (const document of documents) {
const parser = new TomlParser<MiseTomlType>(document.getText());
const range = parser.findRange(parser.parsed.env ?? {}, envVarName);
if (range) {
return { document, range };
if (document.fileName.endsWith("toml")) {
const parser = new TomlParser<MiseTomlType>(document.getText());
const range = parser.findRange(parser.parsed.env ?? {}, envVarName);
if (range) {
return { document, range };
}
} else {
const lines = document.getText().split("\n");
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line?.includes(envVarName)) {
const startPos = new vscode.Position(i, line.indexOf(envVarName));
const endPos = startPos.translate(0, envVarName.length);
return { document, range: new vscode.Range(startPos, endPos) };
}
}
}
}
return undefined;
Expand Down

0 comments on commit 9c2f2ac

Please sign in to comment.