Skip to content

Commit f372edc

Browse files
committed
Add TODO for sketch explorer context menu and refactor new sketch creation logic
1 parent 83ccf1d commit f372edc

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

client/src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export async function activate(context: ExtensionContext) {
3131
// TODO: Exporting to standalone application
3232
// TODO: Override the default save-as
3333
// TODO: limit running sketches when a sketch is open
34+
// TODO: Sketch explorer context menu (right click)
3435

3536
// Set up the selected Processing version, awaiting because we don't want to continue until we have a version
3637
await setupSelectedVersion(context);

client/src/setupCommands.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { basename, dirname, join } from 'path';
22
import { ExtensionContext, commands, Uri, window, workspace } from 'vscode';
33
import { state } from './extension';
44

5+
const sketchNumber = 0;
6+
57
export function setupCommands(context: ExtensionContext) {
68
const runSketch = commands.registerCommand('processing.sketch.run', (resource: Uri) => {
79
// TODO: Use VScode contexts to highlight run button when sketch is running, blocked until we do not run the sketch in a terminal
@@ -104,23 +106,20 @@ export function setupCommands(context: ExtensionContext) {
104106
});
105107

106108
const newSketch = commands.registerCommand('processing.sketch.new', async () => {
107-
const folder = await window.showOpenDialog({
108-
canSelectFiles: false,
109-
canSelectFolders: true,
110-
canSelectMany: false,
111-
title: "Select a folder to create a new sketch in",
112-
defaultUri: workspace.workspaceFolders && workspace.workspaceFolders[0]
113-
? Uri.joinPath(workspace.workspaceFolders[0].uri, '..')
114-
: undefined,
115-
});
116-
if (!folder || folder.length === 0) {
117-
return;
109+
const lastSketchNumberReset = context.globalState.get<number>('lastSketchNumberReset', 0);
110+
const now = Date.now();
111+
// Reset the sketch number if it has been more than 24 hours since the last reset
112+
if (now - lastSketchNumberReset > 24 * 60 * 60 * 1000) {
113+
context.globalState.update('sketchNumber', 0);
114+
context.globalState.update('lastSketchNumberReset', now);
118115
}
119-
// TODO: increment a,b,c if a sketch with the same name already exists
116+
117+
const sketchNumber = context.globalState.get<number>('sketchNumber', 0);
118+
120119
const sketchName = await window.showInputBox({
121120
prompt: "Enter the name of the new sketch",
122121
placeHolder: "Sketch name",
123-
value: `sketch_${new Date().toISOString().slice(2, 10).replace(/-/g, '')}a`,
122+
value: `sketch_${new Date().toISOString().slice(2, 10).replace(/-/g, '')}${numberToAlpha(sketchNumber)}`,
124123
validateInput: (value) => {
125124
if (!value || value.trim() === "") {
126125
return "Sketch name cannot be empty.";
@@ -132,20 +131,47 @@ export function setupCommands(context: ExtensionContext) {
132131
window.showErrorMessage("No sketch name provided.");
133132
return;
134133
}
134+
const folder = await window.showOpenDialog({
135+
canSelectFiles: false,
136+
canSelectFolders: true,
137+
canSelectMany: false,
138+
title: "Select a folder to create a new sketch in",
139+
defaultUri: workspace.workspaceFolders && workspace.workspaceFolders[0]
140+
? Uri.joinPath(workspace.workspaceFolders[0].uri, '..')
141+
: undefined,
142+
});
143+
if (!folder || folder.length === 0) {
144+
return;
145+
}
135146
const sketchPath = Uri.joinPath(folder[0], sketchName);
136147
try {
137148
await workspace.fs.createDirectory(sketchPath);
138149
const sketchFile = Uri.joinPath(sketchPath, `${sketchName}.pde`);
139150
await workspace.fs.writeFile(sketchFile, new Uint8Array());
140151
await commands.executeCommand('processing.sketch.open', sketchPath.fsPath);
141152
window.showInformationMessage(`New sketch created: ${sketchName}`);
153+
154+
context.globalState.update('sketchNumber', sketchNumber + 1);
142155
} catch (error) {
143156
window.showErrorMessage(`Could not create sketch: ${error instanceof Error ? error.message : error}`);
144157
console.error(error);
145158
}
159+
146160
});
147161

148162
// TODO: Add command to select Processing version and set the setting
149163

150164
context.subscriptions.push(runSketch, stopSketch, openSketch, newSketch);
151165
}
166+
167+
// Helper function to convert a number to alphabetical (e.g., 0 = a, 1 = b, ..., 25 = z, 26 = aa, etc.)
168+
function numberToAlpha(n: number): string {
169+
let s = '';
170+
n++;
171+
while (n > 0) {
172+
n--; // Adjust for 0-indexing
173+
s = String.fromCharCode(97 + (n % 26)) + s;
174+
n = Math.floor(n / 26);
175+
}
176+
return s;
177+
}

0 commit comments

Comments
 (0)