-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetupPDEFiles.ts
More file actions
44 lines (38 loc) · 1.51 KB
/
setupPDEFiles.ts
File metadata and controls
44 lines (38 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { RelativePattern, window, workspace, WorkspaceFolder } from 'vscode';
export function setupPDEFiles() {
workspace.onDidChangeWorkspaceFolders(event => {
event.added.forEach(OpenSketchFiles);
});
workspace.workspaceFolders?.forEach(OpenSketchFiles);
}
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);
if (!doc) {
window.showErrorMessage(`Could not open sketch file: ${file.fsPath}`);
return;
}
await window.showTextDocument(doc, { preview: false, preserveFocus: true });
}
if (mainFile) { await window.showTextDocument(mainFile, { preview: false }); }
}