@@ -56,67 +56,85 @@ async function handleNewJavaFiles(e: FileCreateEvent) {
56
56
}
57
57
}
58
58
59
- const formatNumber = ( num => num > 9 ? String ( num ) : `0${ num } ` ) ;
60
- for ( let i = 0 ; i < emptyFiles . length ; i ++ ) {
61
- const typeName : string = resolveTypeName ( textDocuments [ i ] . fileName ) ;
62
- const isPackageInfo = typeName === 'package-info' ;
63
- const isModuleInfo = typeName === 'module-info' ;
64
- const date = new Date ( ) ;
65
- const context : any = {
66
- fileName : path . basename ( textDocuments [ i ] . fileName ) ,
67
- packageName : "" ,
68
- typeName : typeName ,
69
- user : userInfo ( ) . username ,
70
- date : date . toLocaleDateString ( undefined , { month : "short" , day : "2-digit" , year : "numeric" } ) ,
71
- time : date . toLocaleTimeString ( ) ,
72
- year : date . getFullYear ( ) ,
73
- month : formatNumber ( date . getMonth ( ) + 1 ) ,
74
- shortmonth : date . toLocaleDateString ( undefined , { month : "short" } ) ,
75
- day : formatNumber ( date . getDate ( ) ) ,
76
- hour : formatNumber ( date . getHours ( ) ) ,
77
- minute : formatNumber ( date . getMinutes ( ) ) ,
78
- } ;
79
-
80
- if ( ! isModuleInfo ) {
81
- context . packageName = resolvePackageName ( sourcePaths , emptyFiles [ i ] . fsPath ) ;
82
- }
83
-
84
- const snippets : string [ ] = [ ] ;
85
- const fileHeader = getJavaConfiguration ( ) . get < string [ ] > ( "templates.fileHeader" ) ;
86
- if ( fileHeader && fileHeader . length ) {
87
- for ( const template of fileHeader ) {
88
- snippets . push ( stringInterpolate ( template , context ) ) ;
59
+ // See https://github.com/redhat-developer/vscode-java/issues/2939
60
+ // The client side listener should avoid inserting duplicated conents
61
+ // when the file creation event is triggered from a WorkspaceEdit.
62
+ // Given that the VS Code API doesn't provide a way to distinguish
63
+ // the event source, a workaround is to wait 100ms and let WorkspaceEdit
64
+ // to take effect first, then check if the workingcopy is filled with content.
65
+ const timeout = setTimeout ( async ( ) => {
66
+ const formatNumber = ( num => num > 9 ? String ( num ) : `0${ num } ` ) ;
67
+ for ( let i = 0 ; i < emptyFiles . length ; i ++ ) {
68
+ if ( textDocuments [ i ] . getText ( ) ) {
69
+ continue ;
89
70
}
90
- }
91
71
92
- if ( ! isModuleInfo ) {
93
- if ( context . packageName ) {
94
- snippets . push ( `package ${ context . packageName } ;` ) ;
95
- snippets . push ( "" ) ;
72
+ const typeName : string = resolveTypeName ( textDocuments [ i ] . fileName ) ;
73
+ const isPackageInfo = typeName === 'package-info' ;
74
+ const isModuleInfo = typeName === 'module-info' ;
75
+ const date = new Date ( ) ;
76
+ const context : any = {
77
+ fileName : path . basename ( textDocuments [ i ] . fileName ) ,
78
+ packageName : "" ,
79
+ typeName : typeName ,
80
+ user : userInfo ( ) . username ,
81
+ date : date . toLocaleDateString ( undefined , { month : "short" , day : "2-digit" , year : "numeric" } ) ,
82
+ time : date . toLocaleTimeString ( ) ,
83
+ year : date . getFullYear ( ) ,
84
+ month : formatNumber ( date . getMonth ( ) + 1 ) ,
85
+ shortmonth : date . toLocaleDateString ( undefined , { month : "short" } ) ,
86
+ day : formatNumber ( date . getDate ( ) ) ,
87
+ hour : formatNumber ( date . getHours ( ) ) ,
88
+ minute : formatNumber ( date . getMinutes ( ) ) ,
89
+ } ;
90
+
91
+ if ( ! isModuleInfo ) {
92
+ context . packageName = resolvePackageName ( sourcePaths , emptyFiles [ i ] . fsPath ) ;
96
93
}
97
- }
98
- if ( ! isPackageInfo ) {
99
- const typeComment = getJavaConfiguration ( ) . get < string [ ] > ( "templates.typeComment " ) ;
100
- if ( typeComment && typeComment . length ) {
101
- for ( const template of typeComment ) {
94
+
95
+ const snippets : string [ ] = [ ] ;
96
+ const fileHeader = getJavaConfiguration ( ) . get < string [ ] > ( "templates.fileHeader " ) ;
97
+ if ( fileHeader && fileHeader . length ) {
98
+ for ( const template of fileHeader ) {
102
99
snippets . push ( stringInterpolate ( template , context ) ) ;
103
100
}
104
101
}
105
102
106
- if ( isModuleInfo ) {
107
- snippets . push ( `module \${1:name} {` ) ;
108
- } else if ( ! serverReady || await isVersionLessThan ( emptyFiles [ i ] . toString ( ) , 14 ) ) {
109
- snippets . push ( `public \${1|class,interface,enum,abstract class,@interface|} ${ typeName } {` ) ;
110
- } else {
111
- snippets . push ( `public \${1|class ${ typeName } ,interface ${ typeName } ,enum ${ typeName } ,record ${ typeName } (),abstract class ${ typeName } ,@interface ${ typeName } |} {` ) ;
103
+ if ( ! isModuleInfo ) {
104
+ if ( context . packageName ) {
105
+ snippets . push ( `package ${ context . packageName } ;` ) ;
106
+ snippets . push ( "" ) ;
107
+ }
112
108
}
113
- snippets . push ( "\t${0}" ) ;
114
- snippets . push ( "}" ) ;
115
- snippets . push ( "" ) ;
109
+ if ( ! isPackageInfo ) {
110
+ const typeComment = getJavaConfiguration ( ) . get < string [ ] > ( "templates.typeComment" ) ;
111
+ if ( typeComment && typeComment . length ) {
112
+ for ( const template of typeComment ) {
113
+ snippets . push ( stringInterpolate ( template , context ) ) ;
114
+ }
115
+ }
116
+
117
+ if ( isModuleInfo ) {
118
+ snippets . push ( `module \${1:name} {` ) ;
119
+ } else if ( ! serverReady || await isVersionLessThan ( emptyFiles [ i ] . toString ( ) , 14 ) ) {
120
+ snippets . push ( `public \${1|class,interface,enum,abstract class,@interface|} ${ typeName } {` ) ;
121
+ } else {
122
+ snippets . push ( `public \${1|class ${ typeName } ,interface ${ typeName } ,enum ${ typeName } ,record ${ typeName } (),abstract class ${ typeName } ,@interface ${ typeName } |} {` ) ;
123
+ }
124
+ snippets . push ( "\t${0}" ) ;
125
+ snippets . push ( "}" ) ;
126
+ snippets . push ( "" ) ;
127
+ }
128
+ const textEditor = await window . showTextDocument ( textDocuments [ i ] ) ;
129
+ if ( textDocuments [ i ] . getText ( ) ) {
130
+ continue ;
131
+ }
132
+
133
+ textEditor . insertSnippet ( new SnippetString ( snippets . join ( "\n" ) ) ) ;
116
134
}
117
- const textEditor = await window . showTextDocument ( textDocuments [ i ] ) ;
118
- textEditor . insertSnippet ( new SnippetString ( snippets . join ( "\n" ) ) ) ;
119
- }
135
+
136
+ clearTimeout ( timeout ) ;
137
+ } , 100 ) ;
120
138
}
121
139
122
140
function getWillRenameHandler ( client : LanguageClient ) {
0 commit comments