Skip to content

Commit 04c3d0c

Browse files
author
Alberto Iannaccone
authored
Fix sketch name duplicates (#887)
1 parent c9996df commit 04c3d0c

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

Diff for: arduino-ide-extension/src/node/sketches-service-impl.ts

+42-12
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ const WIN32_DRIVE_REGEXP = /^[a-zA-Z]:\\/;
3030
const prefix = '.arduinoIDE-unsaved';
3131

3232
@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+
3738
@inject(ConfigService)
3839
protected readonly configService: ConfigService;
3940

@@ -303,22 +304,31 @@ export class SketchesServiceImpl
303304
monthNames[today.getMonth()]
304305
}${today.getDate()}`;
305306
const config = await this.configService.getConfiguration();
306-
const user = FileUri.fsPath(config.sketchDirUri);
307+
const sketchbookPath = FileUri.fsPath(config.sketchDirUri);
307308
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+
)}`;
310318
// 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;
313325
}
314-
315-
sketchName = sketchNameCandidate;
316-
break;
317326
}
318327

319328
if (!sketchName) {
320329
throw new Error('Cannot create a unique sketch name');
321330
}
331+
this.lastSketchBaseName = sketchBaseName;
322332

323333
const sketchDir = path.join(parentPath, sketchName);
324334
const sketchFile = path.join(sketchDir, `${sketchName}.ino`);
@@ -507,3 +517,23 @@ interface SketchContainerWithDetails extends SketchContainer {
507517
readonly children: SketchContainerWithDetails[];
508518
readonly sketches: SketchWithDetails[];
509519
}
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

Comments
 (0)