Skip to content

Commit 398b412

Browse files
committed
feat: Added abbility to choose files with open dialog
1 parent 5062219 commit 398b412

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/extension.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as beet from './beet';
33
import * as config from './config';
44
import * as minecraft from './minecraft';
55
import * as utils from './utils';
6-
import * as path from 'path';
76

87
let SAVES: minecraft.Save[];
98

@@ -63,7 +62,7 @@ async function build() {
6362
case 1:
6463
configFile = configFiles[0];
6564
default:
66-
let selection = await utils.pickFile(configFiles, configFiles.map((f) => ({label: vscode.workspace.asRelativePath(f)})), "Pick beet config file");
65+
let selection = await utils.pickFile("Pick beet config file", configFiles);
6766
if(!selection) {
6867
return;
6968
}
@@ -88,8 +87,8 @@ async function linkWorld() {
8887
return;
8988
}
9089

91-
const items = await Promise.all(SAVES.map(async (s) => ({label: path.basename(s.uri.fsPath), description: (await s.getVersion()).name})));
92-
const selectedWorld = await utils.pickFile(SAVES.map(s => s.uri), items, "Pick world to link");
90+
const descriptions = await Promise.all(SAVES.map(async (s) => (await s.getVersion()).name));
91+
const selectedWorld = await utils.pickFile("Pick world to link", SAVES.map(s => s.uri), descriptions, {canSelectFiles: false, canSelectFolders: true});
9392
if(!selectedWorld) {
9493
return;
9594
}

src/utils.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode';
22
import * as fs from 'fs';
33
import * as util from 'util';
4+
import * as path from 'path';
45

56
export async function isDirectory(path: string) {
67
try {
@@ -19,19 +20,36 @@ export async function listDir(path: string) {
1920
}
2021
}
2122

22-
export async function pickFile<T extends vscode.QuickPickItem>(files: vscode.Uri[], items: T[], placeHolder: string): Promise<vscode.Uri | undefined> {
23+
export async function pickFile(placeHolder: string, files: vscode.Uri[], descriptions?: string[], openDialogOptions?: vscode.OpenDialogOptions): Promise<vscode.Uri | undefined> {
2324
if (files.length === 0) {
2425
return undefined;
2526
} else if(files.length === 1) {
2627
return files[0];
2728
}
2829

29-
let options: { [key: string]: (vscode.Uri) } = {};
30-
files.forEach((f, i) => {
31-
options[items[i].label] = f;
32-
});
30+
let items: vscode.QuickPickItem[] = [
31+
{
32+
label: "Select path ...",
33+
detail: "Select path with open dialog"
34+
}
35+
];
36+
37+
items = items.concat(<vscode.QuickPickItem[]> files.map((file, i) => ({
38+
label: path.basename(file.fsPath),
39+
detail: file.fsPath,
40+
description: descriptions? descriptions[i] : undefined
41+
})));
42+
43+
const selection = await vscode.window.showQuickPick(items, { placeHolder, matchOnDescription: true});
3344

34-
return vscode.window.showQuickPick(items, { placeHolder, matchOnDescription: true }).then((selection) => {
35-
return selection ? options[selection.label] : undefined;
45+
if(selection?.detail === "Select path with open dialog") {
46+
const selectedFiles = await vscode.window.showOpenDialog(openDialogOptions);
47+
return selectedFiles ? selectedFiles[0] : undefined;
48+
}
49+
50+
const options: { [key: string]: (vscode.Uri) } = {};
51+
files.forEach((f, i) => {
52+
options[files[i].fsPath] = f;
3653
});
54+
return selection?.detail ? options[selection.detail] : undefined;
3755
}

0 commit comments

Comments
 (0)