From 9113482ff090c77515789b680d41360c1a4b3ac0 Mon Sep 17 00:00:00 2001 From: Stef Tervelde Date: Tue, 9 Dec 2025 18:43:58 +0100 Subject: [PATCH] Prioritize main sketch file when opening PDE files When opening PDE and Java files in a workspace folder, the main sketch file (as declared in sketch.properties or matching the folder name) is now moved to the front of the list and opened last to ensure it is focused. Also, text documents are opened with preserveFocus to avoid stealing focus from the user. --- client/src/setupPDEFiles.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/client/src/setupPDEFiles.ts b/client/src/setupPDEFiles.ts index fa8e740..8ebc6ff 100644 --- a/client/src/setupPDEFiles.ts +++ b/client/src/setupPDEFiles.ts @@ -12,6 +12,24 @@ export function setupPDEFiles() { async function OpenSketchFiles(folder: WorkspaceFolder) { // find all the .pde files in the folder const files = await workspace.findFiles(new RelativePattern(folder, '*.{pde,java}')); + let mainFile = files.find(file => file.fsPath.endsWith(`${folder.name}.pde`)); + // read the properties file if it exists + // and check if main has been declared + const [propertiesFile] = await workspace.findFiles(new RelativePattern(folder, 'sketch.properties')); + if (propertiesFile) { + const propertiesDoc = await workspace.openTextDocument(propertiesFile); + const propertiesText = propertiesDoc.getText(); + const mainMatch = propertiesText.match(/main\s*=\s*(\S+)/); + if (mainMatch) { + const mainFileName = mainMatch[1]; + mainFile = files.find(file => file.fsPath.endsWith(mainFileName)); + } + } + if (mainFile) { + // move the declared main file to the front + files.splice(files.indexOf(mainFile), 1); + files.unshift(mainFile); + } for (const file of files) { const doc = await workspace.openTextDocument(file); @@ -19,6 +37,8 @@ async function OpenSketchFiles(folder: WorkspaceFolder) { window.showErrorMessage(`Could not open sketch file: ${file.fsPath}`); return; } - await window.showTextDocument(doc, { preview: false }); + await window.showTextDocument(doc, { preview: false, preserveFocus: true }); } + if (mainFile) { await window.showTextDocument(mainFile, { preview: false }); } + } \ No newline at end of file