Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Provide possibility to compile and verify sketches opened in active tab. #964

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@
"type": "object",
"title": "Arduino configuration",
"properties": {
"arduino.useActiveSketch": {
"type": "boolean",
"default": false,
"description": "Allows to verify or compile sketch opened in active tab."
},
"arduino.path": {
"type": "string",
"default": "",
Expand Down
6 changes: 6 additions & 0 deletions src/arduino/vscodeSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as vscode from "vscode";

const configKeys = {
USE_ACTIVE_SKETCH: "arduino.useActiveSketch",
ARDUINO_PATH: "arduino.path",
ARDUINO_COMMAND_PATH: "arduino.commandPath",
ADDITIONAL_URLS: "arduino.additionalUrls",
Expand All @@ -18,6 +19,7 @@ const configKeys = {
};

export interface IVscodeSettings {
useActiveSketch: boolean;
arduinoPath: string;
commandPath: string;
additionalUrls: string | string[];
Expand All @@ -43,6 +45,10 @@ export class VscodeSettings implements IVscodeSettings {
private constructor() {
}

public get useActiveSketch(): boolean {
return this.getConfigValue<boolean>(configKeys.USE_ACTIVE_SKETCH);
}

public get arduinoPath(): string {
return this.getConfigValue<string>(configKeys.ARDUINO_PATH);
}
Expand Down
23 changes: 23 additions & 0 deletions src/deviceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as constants from "./common/constants";
import * as util from "./common/util";
import * as Logger from "./logger/logger";

import { VscodeSettings } from "./arduino/vscodeSettings";
import { ARDUINO_CONFIG_FILE } from "./common/constants";
import { ArduinoWorkspace } from "./common/workspace";

Expand Down Expand Up @@ -96,6 +97,8 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {

private _programmer: string;

private _vscodeSettings = VscodeSettings.getInstance();

/**
* @constructor
*/
Expand All @@ -112,6 +115,9 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
this._sketchStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, constants.statusBarPriority.SKETCH);
this._sketchStatusBar.command = "arduino.setSketchFile";
this._sketchStatusBar.tooltip = "Sketch File";
vscode.window.onDidChangeActiveTextEditor(() => {
this.trySetOpenedFileAsSketch();
})
}
}

Expand Down Expand Up @@ -191,6 +197,10 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
}

public showStatusBar() {
if (this.trySetOpenedFileAsSketch()) {
return;
}

if (!this._sketch) {
return false;
}
Expand Down Expand Up @@ -361,4 +371,17 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
}
});
}

public trySetOpenedFileAsSketch() {
if (this._vscodeSettings.useActiveSketch) {
const openedFile = vscode.window.activeTextEditor.document.fileName
.slice(vscode.workspace.rootPath.length + 1);
if (/\.((ino)|(cpp)|c)$/.test(openedFile.trim())) {
this._sketch = openedFile;
this.saveContext();
return true;
}
}
return false;
};
}