Skip to content

Commit

Permalink
bugfix and refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHuey committed Nov 1, 2024
1 parent 957a905 commit 87dbc53
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 36 deletions.
10 changes: 7 additions & 3 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ const { dataObject } = require("./src/utils");
/**
* @param {vscode.ExtensionContext} context
*/
async function activate(context) {
function activate(context) {
console.log("Checking for data.json")
dataObject(); // To initialize the data.json file
console.log("Registering commands")
registerCommands();
// DO NOT MOVE THESE LINES ABOVE. THESE 2 LINES MUST BE FIRST
await init(context);
// DO NOT MOVE THESE LINES ABOVE. THEY MUST COME FIRST
console.log("Initializing other dependencies")
init(context);

console.log("Registering providers")
registerProviders(context);

console.log('"avr-utils" is now active!');
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"activationEvents": [
"onLanguage:avr-c",
"onLanguage:asm",
"onStartupFinished",
"onCommand:avr-utils.makeProject",
"onCommand:avr-utils.openMicrochipStudioProject"
],
Expand Down
1 change: 0 additions & 1 deletion src/commands/compileProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
toolchainDir,
dataObject,
currentExtension,
sendCursorTo,
} = require("../utils");
const { resetIncludeDir } = require("../providers/documentLinkProvider");
const { generateDiagnostics, clearDiagnostics } = require("../providers/diagnosticsProvider")
Expand Down
35 changes: 8 additions & 27 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ const selectDeviceButton = vscode.window.createStatusBarItem(vscode.StatusBarAli
*
* @param {vscode.ExtensionContext} context
*/
async function init(context) {
function init(context) {
vscode.commands.registerCommand("avr-utils.selectDevice", selectDevice);

getWorkspaceRelatedStuff(context);
setupWorkspaceProperties(context);
vscode.workspace.onDidChangeWorkspaceFolders((event) => {
if (event.removed.length > 0) {
workspaceNotYetAccessible = true;
context.subscriptions.pop();
context.subscriptions.pop();
if (vscode.workspace.workspaceFolders) getWorkspaceRelatedStuff(context);
if (vscode.workspace.workspaceFolders) setupWorkspaceProperties(context);
}
if (event.added.length > 0) {
getWorkspaceRelatedStuff(context);
setupWorkspaceProperties(context);
}
});

Expand All @@ -37,7 +37,6 @@ async function init(context) {
});

initButtons();
buttonVisibility();
}

/**
Expand All @@ -46,8 +45,10 @@ async function init(context) {
function showOrHideUI(editor) {
if (editor && (editor.document.languageId === 'avr-c' || editor.document.languageId === 'asm')) {
vscode.commands.executeCommand('setContext', 'avr-utils.isAvrC', true);
toggleButtons();
} else {
vscode.commands.executeCommand('setContext', 'avr-utils.isAvrC', false);
toggleButtons(true);
}
}

Expand All @@ -68,26 +69,6 @@ function initButtons() {
selectDeviceButton.command = "avr-utils.selectDevice";
}

async function buttonVisibility() {
while (true) {
await new Promise((r) => {
setTimeout(r, 500);
});
try {
if (vscode.window.activeTextEditor) {
if (vscode.window.activeTextEditor.document.languageId !== "avr-c" && vscode.window.activeTextEditor.document.languageId !== "asm") {
toggleButtons(true);
} else {
toggleButtons();
}
} else {
toggleButtons(true);
}
} finally {
}
}
}

function toggleButtons(hide = false) {
if (hide) {
if (_compileButtonVisible) compileButton.hide();
Expand Down Expand Up @@ -140,7 +121,7 @@ let workspaceNotYetAccessible = true;
/**
* @param {vscode.ExtensionContext} context
*/
async function getWorkspaceRelatedStuff(context) {
async function setupWorkspaceProperties(context) {
if (workspaceNotYetAccessible) {
if (vscode.workspace.workspaceFolders) {
initWorkspace(); // enables the "workspace" utils
Expand All @@ -149,7 +130,7 @@ async function getWorkspaceRelatedStuff(context) {
if (!fs.existsSync(pathtovscode)) {
fs.mkdirSync(pathtovscode);
}
context.subscriptions.push(createLinkProvider(/#(\s*?)include "(.*)"/, thisWorkspace().uri.fsPath));
// context.subscriptions.push(createLinkProvider(/#(\s*?)include "(.*)"/, thisWorkspace().uri.fsPath));
context.subscriptions.push(registerCompletions({ directory: thisWorkspace().uri.fsPath, triggers: ['"'], regex: /#include\s+"([^"]*)$/, end: "" }));
if (fs.existsSync(path.join(pathtovscode, "avr_project.json"))) {
let thejson = JSON.parse(fs.readFileSync(path.join(pathtovscode, "avr_project.json"), "utf8"));
Expand Down
1 change: 1 addition & 0 deletions src/providers/diagnosticsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ function clearDiagnostics() {
module.exports = {
generateDiagnostics,
clearDiagnostics,
diagnosticsCollection: globalDiagnostics,
}
3 changes: 2 additions & 1 deletion src/registerProviders.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { defaultCompletions } = require("./providers/completionsProvider");
const { includeDirProvider } = require("./providers/documentLinkProvider");
const { definitions } = require("./providers/definitionProvider");
const { diagnosticsCollection } = require("./providers/diagnosticsProvider");

/**@param {import("vscode").ExtensionContext} context */
function _(context) {
context.subscriptions.push(defaultCompletions, includeDirProvider, definitions);
context.subscriptions.push(defaultCompletions, includeDirProvider, definitions, diagnosticsCollection);
}

module.exports = _;
11 changes: 8 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,19 @@ async function changeWorkspace() {
* @param {Object} saveObject
*/
function dataObject(saveObject = null) {
const pathToDataFile = path.join(__dirname, "storage", "rdata.json");
if (saveObject) {
const oldObject = dataObject();
let oldObject = {};
try {
fs.statSync(pathToDataFile);
oldObject = dataObject();
} catch {}
let newObject = {...oldObject, ...saveObject};
fs.writeFileSync(path.join(__dirname, "storage", "data.json"), JSON.stringify(newObject), "utf-8");
fs.writeFileSync(pathToDataFile, JSON.stringify(newObject), "utf-8");
return;
}
try {
return JSON.parse(fs.readFileSync(path.join(__dirname, "storage", "data.json"), "utf8"));
return JSON.parse(fs.readFileSync(pathToDataFile, "utf8"));
} catch {
let temp = {toolchain_directory: `${path.join(homedir(), "Documents")}`};
dataObject(temp);
Expand Down

0 comments on commit 87dbc53

Please sign in to comment.