1
+ import type { QuickPickOptions } from "@cursorless/common" ;
1
2
import * as vscode from "vscode" ;
2
- import type {
3
- QuickPickOptions ,
4
- UnknownValuesOptions ,
5
- } from "@cursorless/common" ;
6
-
7
- export async function vscodeShowQuickPick (
8
- items : readonly string [ ] ,
9
- options : QuickPickOptions | undefined ,
10
- ) {
11
- if ( options ?. unknownValues == null ) {
12
- return await vscode . window . showQuickPick ( items , options ) ;
13
- }
14
-
15
- const { unknownValues, ...rest } = options ;
16
- return await showQuickPickAllowingUnknown ( items , unknownValues , rest ) ;
17
- }
18
-
19
- interface CustomQuickPickItem extends vscode . QuickPickItem {
20
- value : string ;
21
- }
22
-
23
- const DEFAULT_NEW_VALUE_TEMPLATE = "Add new value '{}' →" ;
24
3
25
4
/**
26
5
* Show a quick pick that allows the user to enter a new value. We do this by
@@ -29,43 +8,58 @@ const DEFAULT_NEW_VALUE_TEMPLATE = "Add new value '{}' →";
29
8
* detect that it is an unknown value and handle that case.
30
9
*
31
10
* Based on https://stackoverflow.com/a/69842249
32
- * @param items
33
- * @param options
34
11
*/
35
- function showQuickPickAllowingUnknown (
36
- choices : readonly string [ ] ,
37
- unknownValues : UnknownValuesOptions ,
38
- options : vscode . QuickPickOptions ,
12
+
13
+ interface CustomQuickPickItem extends vscode . QuickPickItem {
14
+ value : string ;
15
+ }
16
+
17
+ export function vscodeShowQuickPick (
18
+ items : readonly string [ ] ,
19
+ options : QuickPickOptions | undefined ,
39
20
) {
40
21
return new Promise < string | undefined > ( ( resolve , _reject ) => {
41
22
const quickPick = vscode . window . createQuickPick < CustomQuickPickItem > ( ) ;
42
- const quickPickItems = choices . map ( ( choice ) => ( {
43
- label : choice ,
44
- value : choice ,
23
+
24
+ const quickPickItems = items . map ( ( item ) => ( {
25
+ label : item ,
26
+ value : item ,
45
27
} ) ) ;
28
+
46
29
quickPick . items = quickPickItems ;
47
30
48
- if ( options . title != null ) {
31
+ if ( options ? .title != null ) {
49
32
quickPick . title = options . title ;
50
33
}
51
34
52
- const { newValueTemplate = DEFAULT_NEW_VALUE_TEMPLATE } = unknownValues ;
35
+ if ( options ?. defaultValue != null ) {
36
+ quickPick . activeItems = quickPickItems . filter (
37
+ ( item ) => item . value === options . defaultValue ,
38
+ ) ;
39
+ }
40
+
41
+ if ( options ?. unknownValues ) {
42
+ const newValueTemplate =
43
+ typeof options . unknownValues === "string"
44
+ ? options . unknownValues
45
+ : "Add new value '{}' →" ;
53
46
54
- quickPick . onDidChangeValue ( ( ) => {
55
- quickPick . items = [
56
- ...quickPickItems ,
47
+ quickPick . onDidChangeValue ( ( ) => {
48
+ quickPick . items = [
49
+ ...quickPickItems ,
57
50
58
- // INJECT user values into proposed values
59
- ...( choices . includes ( quickPick . value )
60
- ? [ ]
61
- : [
62
- {
63
- label : newValueTemplate . replace ( "{}" , quickPick . value ) ,
64
- value : quickPick . value ,
65
- } ,
66
- ] ) ,
67
- ] ;
68
- } ) ;
51
+ // INJECT user values into proposed values
52
+ ...( items . includes ( quickPick . value ) || quickPick . value . trim ( ) === ""
53
+ ? [ ]
54
+ : [
55
+ {
56
+ label : newValueTemplate . replace ( "{}" , quickPick . value ) ,
57
+ value : quickPick . value ,
58
+ } ,
59
+ ] ) ,
60
+ ] ;
61
+ } ) ;
62
+ }
69
63
70
64
quickPick . onDidAccept ( ( ) => {
71
65
const selection = quickPick . activeItems [ 0 ] ;
0 commit comments