@@ -30,10 +30,11 @@ const WIN32_DRIVE_REGEXP = /^[a-zA-Z]:\\/;
30
30
const prefix = '.arduinoIDE-unsaved' ;
31
31
32
32
@injectable ( )
33
- export class SketchesServiceImpl
34
- extends CoreClientAware
35
- implements SketchesService
36
- {
33
+ export class SketchesServiceImpl extends CoreClientAware
34
+ implements SketchesService {
35
+ private sketchSuffixIndex = 1 ;
36
+ private lastSketchBaseName : string ;
37
+
37
38
@inject ( ConfigService )
38
39
protected readonly configService : ConfigService ;
39
40
@@ -303,22 +304,31 @@ export class SketchesServiceImpl
303
304
monthNames [ today . getMonth ( ) ]
304
305
} ${ today . getDate ( ) } `;
305
306
const config = await this . configService . getConfiguration ( ) ;
306
- const user = FileUri . fsPath ( config . sketchDirUri ) ;
307
+ const sketchbookPath = FileUri . fsPath ( config . sketchDirUri ) ;
307
308
let sketchName : string | undefined ;
308
- for ( let i = 97 ; i < 97 + 26 ; i ++ ) {
309
- const sketchNameCandidate = `${ sketchBaseName } ${ String . fromCharCode ( i ) } ` ;
309
+
310
+ // If it's another day, reset the count of sketches created today
311
+ if ( this . lastSketchBaseName !== sketchBaseName ) this . sketchSuffixIndex = 1 ;
312
+
313
+ let nameFound = false ;
314
+ while ( ! nameFound ) {
315
+ const sketchNameCandidate = `${ sketchBaseName } ${ sketchIndexToLetters (
316
+ this . sketchSuffixIndex ++
317
+ ) } `;
310
318
// Note: we check the future destination folder (`directories.user`) for name collision and not the temp folder!
311
- if ( await promisify ( fs . exists ) ( path . join ( user , sketchNameCandidate ) ) ) {
312
- continue ;
319
+ const sketchExists = await promisify ( fs . exists ) (
320
+ path . join ( sketchbookPath , sketchNameCandidate )
321
+ ) ;
322
+ if ( ! sketchExists ) {
323
+ nameFound = true ;
324
+ sketchName = sketchNameCandidate ;
313
325
}
314
-
315
- sketchName = sketchNameCandidate ;
316
- break ;
317
326
}
318
327
319
328
if ( ! sketchName ) {
320
329
throw new Error ( 'Cannot create a unique sketch name' ) ;
321
330
}
331
+ this . lastSketchBaseName = sketchBaseName ;
322
332
323
333
const sketchDir = path . join ( parentPath , sketchName ) ;
324
334
const sketchFile = path . join ( sketchDir , `${ sketchName } .ino` ) ;
@@ -507,3 +517,23 @@ interface SketchContainerWithDetails extends SketchContainer {
507
517
readonly children : SketchContainerWithDetails [ ] ;
508
518
readonly sketches : SketchWithDetails [ ] ;
509
519
}
520
+
521
+ /*
522
+ * When a new sketch is created, add a suffix to distinguish it
523
+ * from other new sketches I created today.
524
+ * If 'sketch_jul8a' is already used, go with 'sketch_jul8b'.
525
+ * If 'sketch_jul8b' already used, go with 'sketch_jul8c'.
526
+ * When it reacheas 'sketch_jul8z', go with 'sketch_jul8aa',
527
+ * and so on.
528
+ */
529
+ function sketchIndexToLetters ( num : number ) : string {
530
+ let out = '' ;
531
+ let pow ;
532
+ do {
533
+ pow = Math . floor ( num / 26 ) ;
534
+ const mod = num % 26 ;
535
+ out = ( mod ? String . fromCharCode ( 96 + mod ) : ( -- pow , 'z' ) ) + out ;
536
+ num = pow ;
537
+ } while ( pow > 0 ) ;
538
+ return out ;
539
+ }
0 commit comments