From 5a8d106b37c4127ae8bc6ab9cb213d0b6bdc9e3e Mon Sep 17 00:00:00 2001 From: Jason Tranchida Date: Thu, 29 Oct 2020 23:03:45 -0700 Subject: [PATCH] Quick pick selection of sketch files - Replace arduino.setSketchFile command with new arduino.selectSketch command which presents the user with a quick select containing all of the sketch files in the workspace. - Add "Arduino: Select Sketch" to the command palette - When picking sketches, filter out hardware, library, and build folders that may be under the workspace --- package.json | 5 +++++ src/deviceContext.ts | 2 +- src/extension.ts | 40 ++++++++++++++++++++++++++++------------ test/extension.test.ts | 2 +- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 20cf48a1..998643e3 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "onCommand:arduino.uploadUsingProgrammer", "onCommand:arduino.selectProgrammer", "onCommand:arduino.selectSerialPort", + "onCommand:arduino.selectSketch", "onCommand:arduino.changeBaudRate", "onCommand:arduino.addLibPath", "onCommand:arduino.openSerialMonitor", @@ -109,6 +110,10 @@ "command": "arduino.selectProgrammer", "title": "Arduino: Select Programmer" }, + { + "command": "arduino.selectSketch", + "title": "Arduino: Select Sketch" + }, { "command": "arduino.selectSerialPort", "title": "Arduino: Select Serial Port" diff --git a/src/deviceContext.ts b/src/deviceContext.ts index 5e24aea1..38045179 100644 --- a/src/deviceContext.ts +++ b/src/deviceContext.ts @@ -110,7 +110,7 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable { this._watcher.onDidDelete(() => this.loadContext()); this._vscodeWatcher.onDidDelete(() => this.loadContext()); this._sketchStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, constants.statusBarPriority.SKETCH); - this._sketchStatusBar.command = "arduino.setSketchFile"; + this._sketchStatusBar.command = "arduino.selectSketch"; this._sketchStatusBar.tooltip = "Sketch File"; } } diff --git a/src/extension.ts b/src/extension.ts index b6cf7a78..1f4408e5 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -172,24 +172,40 @@ export async function activate(context: vscode.ExtensionContext) { return { board: arduinoContextModule.default.boardManager.currentBoard.name }; }); - registerArduinoCommand("arduino.setSketchFile", async () => { + registerArduinoCommand("arduino.selectSketch", async () => { const sketchFileName = deviceContext.sketch; - const newSketchFileName = await vscode.window.showInputBox({ - placeHolder: sketchFileName, - validateInput: (value) => { - if (value && /\.((ino)|(cpp)|c)$/.test(value.trim())) { - return null; - } else { - return "Invalid sketch file name. Should be *.ino/*.cpp/*.c"; - } - }, - }); + + // Include any ino, cpp, or c files under the workspace folder + const includePattern = "**/*.{ino,cpp,c}"; + + // The sketchbook folder may contain hardware & library folders, any sketches under these paths + // should be excluded + const sketchbookPath = arduinoContextModule.default.arduinoApp.settings.sketchbookPath; + const excludePatterns = [ + path.relative(ArduinoWorkspace.rootPath, sketchbookPath + "/hardware/**"), + path.relative(ArduinoWorkspace.rootPath, sketchbookPath + "/libraries/**")]; + + // If an output path is specified, it should be excluded as well + if (deviceContext.output) { + const outputPath = path.relative(ArduinoWorkspace.rootPath, + path.resolve(ArduinoWorkspace.rootPath, deviceContext.output)); + excludePatterns.push(`${outputPath}/**`); + } + const excludePattern = `{${excludePatterns.join(",")}}`.replace("\\", "/"); + + const fileUris = await vscode.workspace.findFiles(includePattern, excludePattern); + const newSketchFileName = await vscode.window.showQuickPick(fileUris.map((fileUri) => + ({ + label: path.relative(ArduinoWorkspace.rootPath, fileUri.fsPath), + description: fileUri.fsPath, + })), + { placeHolder: sketchFileName }); if (!newSketchFileName) { return; } - deviceContext.sketch = newSketchFileName; + deviceContext.sketch = newSketchFileName.label; deviceContext.showStatusBar(); }); diff --git a/test/extension.test.ts b/test/extension.test.ts index 9c56fc41..131bcd3c 100644 --- a/test/extension.test.ts +++ b/test/extension.test.ts @@ -54,7 +54,7 @@ suite("Arduino: Extension Tests", () => { "arduino.showExampleExplorer", "arduino.loadPackages", "arduino.installBoard", - "arduino.setSketchFile", + "arduino.selectSketch", "arduino.cliUpload", "arduino.cliUploadUsingProgrammer", ];