3
3
4
4
import * as vscode from "vscode" ;
5
5
import { IProjectCreationMetadata , IProjectCreationStep , StepResult } from "./types" ;
6
+ import { asyncDebounce } from "./utils" ;
6
7
7
8
export class SpecifySourcePackageNameStep implements IProjectCreationStep {
8
9
public static GET_NORMALIZED_PACKAGE_NAME = "getNormalizedPackageName" ;
@@ -11,12 +12,17 @@ export class SpecifySourcePackageNameStep implements IProjectCreationStep {
11
12
if ( ! metadata . client ) {
12
13
return StepResult . STOP ;
13
14
}
15
+ const getNormalizedPackageNameTrigger = asyncDebounce (
16
+ metadata . client . getNormalizedPackageName ,
17
+ 500 /** ms */ ,
18
+ metadata . client
19
+ ) ;
14
20
const disposables : vscode . Disposable [ ] = [ ] ;
15
21
// eslint-disable-next-line @typescript-eslint/no-unused-vars
16
22
const specifySourcePackageNamePromise = new Promise < StepResult > ( async ( resolve , _reject ) => {
17
23
const inputBox = vscode . window . createInputBox ( ) ;
18
24
const defaultName = metadata . sourcePackageName || "" ;
19
- const normalizedName = await metadata . client . getNormalizedPackageName ( defaultName ) ;
25
+ const normalizedName = await getNormalizedPackageNameTrigger ( defaultName ) ;
20
26
if ( ! normalizedName ) {
21
27
return resolve ( StepResult . STOP ) ;
22
28
}
@@ -25,7 +31,7 @@ export class SpecifySourcePackageNameStep implements IProjectCreationStep {
25
31
} )`;
26
32
inputBox . prompt = "Input source package name of your project." ;
27
33
inputBox . placeholder = "e.g. " + normalizedName ;
28
- inputBox . value = normalizedName ;
34
+ inputBox . value = normalizedName as string ;
29
35
inputBox . ignoreFocusOut = true ;
30
36
inputBox . enabled = true ;
31
37
if ( metadata . steps . length ) {
@@ -39,15 +45,26 @@ export class SpecifySourcePackageNameStep implements IProjectCreationStep {
39
45
) ;
40
46
}
41
47
disposables . push (
48
+ inputBox . onDidChangeValue ( async ( ) => {
49
+ const normalizedName = await getNormalizedPackageNameTrigger ( inputBox . value ) ;
50
+ if ( ! normalizedName ) {
51
+ return ;
52
+ } else if ( normalizedName !== inputBox . value ) {
53
+ this . setInputInvalid ( inputBox , normalizedName as string ) ;
54
+ } else {
55
+ this . setInputValid ( inputBox ) ;
56
+ }
57
+ } ) ,
42
58
inputBox . onDidAccept ( async ( ) => {
43
59
const normalizedName = await metadata . client . getNormalizedPackageName ( inputBox . value ) ;
44
- if ( normalizedName === inputBox . value ) {
60
+ if ( ! normalizedName ) {
61
+ return ;
62
+ } else if ( normalizedName !== inputBox . value ) {
63
+ this . setInputInvalid ( inputBox , normalizedName as string ) ;
64
+ } else {
45
65
metadata . sourcePackageName = inputBox . value ;
46
66
metadata . nextStep = undefined ;
47
67
resolve ( StepResult . NEXT ) ;
48
- } else {
49
- inputBox . enabled = false ;
50
- inputBox . validationMessage = `Invalid source package name, suggest name: ${ normalizedName } ` ;
51
68
}
52
69
} ) ,
53
70
inputBox . onDidHide ( ( ) => {
@@ -64,6 +81,20 @@ export class SpecifySourcePackageNameStep implements IProjectCreationStep {
64
81
disposables . forEach ( ( d ) => d . dispose ( ) ) ;
65
82
}
66
83
}
84
+
85
+ private setInputInvalid ( inputBox : vscode . InputBox , normalizedName : string ) {
86
+ if ( inputBox ) {
87
+ inputBox . enabled = false ;
88
+ inputBox . validationMessage = `Invalid source package name, suggest name: ${ normalizedName } ` ;
89
+ }
90
+ }
91
+
92
+ private setInputValid ( inputBox : vscode . InputBox ) {
93
+ if ( inputBox ) {
94
+ inputBox . enabled = true ;
95
+ inputBox . validationMessage = undefined ;
96
+ }
97
+ }
67
98
}
68
99
69
100
export const specifySourcePackageNameStep = new SpecifySourcePackageNameStep ( ) ;
0 commit comments