1
1
import * as vscode from 'vscode' ;
2
2
import * as fs from 'fs' ;
3
3
import * as util from 'util' ;
4
+ import * as path from 'path' ;
4
5
5
6
export async function isDirectory ( path : string ) {
6
7
try {
@@ -19,19 +20,36 @@ export async function listDir(path: string) {
19
20
}
20
21
}
21
22
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 > {
23
24
if ( files . length === 0 ) {
24
25
return undefined ;
25
26
} else if ( files . length === 1 ) {
26
27
return files [ 0 ] ;
27
28
}
28
29
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 } ) ;
33
44
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 ;
36
53
} ) ;
54
+ return selection ?. detail ? options [ selection . detail ] : undefined ;
37
55
}
0 commit comments