@@ -74,10 +74,24 @@ export function CodeSnippetInputDialog(
7474 }
7575 }
7676
77+ const body : InputHandler = new InputHandler ( tags ) ;
78+
79+ return showInputDialog ( tags , idx , codeSnippetWidget , code , body ) ;
80+ }
81+
82+ /**
83+ * This function creates the actual input form and processes the inputs given.
84+ */
85+ export function showInputDialog (
86+ tags : string [ ] ,
87+ idx : number ,
88+ codeSnippetWidget : CodeSnippetWidget ,
89+ code : string [ ] ,
90+ body : InputHandler
91+ ) : Promise < Contents . IModel | null > {
7792 return showCodeSnippetForm ( {
7893 title : 'Save Code Snippet' ,
79- body : new InputHandler ( tags ) ,
80- // focusNodeSelector: 'input',
94+ body : body ,
8195 buttons : [
8296 CodeSnippetForm . cancelButton ( ) ,
8397 CodeSnippetForm . okButton ( { label : 'Save' } )
@@ -88,15 +102,15 @@ export function CodeSnippetInputDialog(
88102 }
89103
90104 if ( validateForm ( result ) === false ) {
91- return CodeSnippetInputDialog ( codeSnippetWidget , code , idx ) ; // This works but it wipes out all the data they entered previously...
105+ showInputDialog ( tags , idx , codeSnippetWidget , code , body ) ;
92106 } else {
93107 if ( idx === - 1 ) {
94108 idx = codeSnippetWidget . codeSnippetWidgetModel . snippets . length ;
95109 }
96110
97111 const tags = result . value . slice ( 3 ) ;
98112 const newSnippet : ICodeSnippet = {
99- name : result . value [ 0 ] . replace ( ' ' , '' ) . toLowerCase ( ) ,
113+ name : result . value [ 0 ] . replace ( ' ' , '' ) ,
100114 description : result . value [ 1 ] ,
101115 language : result . value [ 2 ] ,
102116 code : code ,
@@ -106,13 +120,13 @@ export function CodeSnippetInputDialog(
106120 const contentsService = CodeSnippetContentsService . getInstance ( ) ;
107121 const currSnippets = codeSnippetWidget . codeSnippetWidgetModel . snippets ;
108122 for ( const snippet of currSnippets ) {
109- if ( snippet . name === newSnippet . name ) {
123+ if ( snippet . name . toLowerCase ( ) === newSnippet . name . toLowerCase ( ) ) {
110124 const result = saveOverWriteFile (
111125 codeSnippetWidget . codeSnippetWidgetModel ,
112126 snippet ,
113127 newSnippet
114128 ) ;
115-
129+ console . log ( 'uh reached here' ) ;
116130 result
117131 . then ( newSnippets => {
118132 codeSnippetWidget . renderCodeSnippetsSignal . emit ( newSnippets ) ;
@@ -142,7 +156,7 @@ function createNewSnippet(
142156 content : JSON . stringify ( newSnippet )
143157 }
144158 ) ;
145-
159+ console . log ( newSnippet . name ) ;
146160 request . then ( _ => {
147161 // add the new snippet to the snippet model
148162 codeSnippet . codeSnippetWidgetModel . addSnippet ( newSnippet , newSnippet . id ) ;
@@ -159,12 +173,12 @@ function createNewSnippet(
159173/**
160174 * Rename a file, asking for confirmation if it is overwriting another.
161175 */
162- async function saveOverWriteFile (
176+ export async function saveOverWriteFile (
163177 codeSnippetWidgetModel : CodeSnippetWidgetModel ,
164178 oldSnippet : ICodeSnippet ,
165179 newSnippet : ICodeSnippet
166180) : Promise < ICodeSnippet [ ] | null > {
167- const newPath = 'snippets/' + newSnippet . name + '.json' ;
181+ const newPath = 'snippets/' + oldSnippet . name + '.json' ;
168182
169183 return await shouldOverwrite ( newPath ) . then ( value => {
170184 if ( value ) {
@@ -182,7 +196,7 @@ async function saveOverWriteFile(
182196/**
183197 * Ask the user whether to overwrite a file.
184198 */
185- async function shouldOverwrite ( path : string ) : Promise < boolean > {
199+ export async function shouldOverwrite ( path : string ) : Promise < boolean > {
186200 const options = {
187201 title : 'Overwrite code snippet?' ,
188202 body : `"${ path } " already exists, overwrite?` ,
@@ -219,20 +233,18 @@ export function validateForm(
219233 message += 'Name must be filled out\n' ;
220234 status = false ;
221235 }
222- if ( name . match ( / [ ^ a - z 0 - 9 _ ] + / ) ) {
236+ if ( name . match ( / [ ^ a - z A - Z 0 - 9 _ ] + / ) ) {
237+ //allow lowercase, uppercase, alphanumeric, and underscore
223238 message += 'Wrong format of the name\n' ;
224239 status = false ;
225240 }
226- if ( description === '' ) {
227- message += 'Description must be filled out\n' ;
228- status = false ;
229- }
230241 if ( description . match ( / [ ^ a - z A - Z 0 - 9 _ , . ? ! ] + / ) ) {
242+ //alphanumeric but can include space or punctuation
231243 message += 'Wrong format of the description\n' ;
232244 status = false ;
233245 }
234246 if ( language === '' ) {
235- message += 'Language must be filled out' ;
247+ message += 'Language must be filled out\n ' ;
236248 status = false ;
237249 }
238250 if ( ! SUPPORTED_LANGUAGES . includes ( language ) ) {
@@ -302,7 +314,7 @@ class Private {
302314 const body = document . createElement ( 'form' ) ;
303315 const nameValidity = document . createElement ( 'p' ) ;
304316 nameValidity . textContent =
305- 'Name of the code snippet MUST be lowercased, alphanumeric, or composed of underscore(_)' ;
317+ 'Name of the code snippet MUST be alphanumeric, or composed of underscore(_)' ;
306318 nameValidity . className = CODE_SNIPPET_INPUTNAME_VALIDITY ;
307319
308320 const descriptionValidity = document . createElement ( 'p' ) ;
@@ -319,10 +331,9 @@ class Private {
319331 name . onblur = Private . handleOnBlur ;
320332
321333 const descriptionTitle = document . createElement ( 'label' ) ;
322- descriptionTitle . textContent = 'Description (required )' ;
334+ descriptionTitle . textContent = 'Description (optional )' ;
323335 const description = document . createElement ( 'input' ) ;
324336 description . className = CODE_SNIPPET_DIALOG_INPUT ;
325- description . required = true ;
326337 description . pattern = '[a-zA-Z0-9_ ,.?!]+' ;
327338 description . onblur = Private . handleOnBlur ;
328339
0 commit comments