-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
129 lines (114 loc) · 4.54 KB
/
extension.js
File metadata and controls
129 lines (114 loc) · 4.54 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//makes sure that user has these "requirements"
const vscode = require('vscode');
const cp = require('child_process');
const path = require('path');
function activate(context) {
console.log("MahkrabMaker activated");
//reconfigures when focus is switched to a C file
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(activeFile => {
if (isC(activeFile?.document)) { // uses isC function to fdind whether current file is c
console.log("MahkrabMaker: active editor changed to C file");
void configure();
}
})
);
//reconfigure when saving a cfile to local
context.subscriptions.push(
vscode.workspace.onDidSaveTextDocument(doc => {
if (isC(doc)) {
console.log("MahkrabMaker: C file saved");
void configure();
}
})
);
//configure immediately if the current editor is a C file
if (isC(vscode.window.activeTextEditor?.document)) {
console.log("MahkrabMaker: initial configure on activation (C file already active)");
void configure();
}
}
//returns a bool if file is c and has .c extension from the doc
function isC(doc) {
if (!doc) return false;
return doc.languageId === 'c' || doc.fileName?.endsWith('.c');
}
//uses async (and await) to allow btoh processes to run simultanously(asyncronous)
async function configure() {
const activeFile = vscode.window.activeTextEditor;
//if file is not c then configure is not called for this cycle
if (!activeFile || !isC(activeFile.document)) {
console.log("MahkrabMaker: configure skipped (no active C editor)");
return;
}
const doc = activeFile.document;
const ws = vscode.workspace.getWorkspaceFolder(doc.uri);
const cwd = ws ? ws.uri.fsPath : path.dirname(doc.fileName);
//reads the setting in the extension for where python is installed/or command to access(e.g. python, python3, users/bin/python)
const cfg = vscode.workspace.getConfiguration('mahkrab');
const python = cfg.get('pythonPath') || 'python';
//finds where to run the "main.py" file from
const scriptPath = path.join(__dirname, 'main.py');
//debugging locatinos necessary for extension
console.log("MahkrabMaker: preparing to call Python: ");
console.log(" python =", python);
console.log(" scriptPath =", scriptPath);
console.log(" activeFile =", doc.fileName);
console.log(" cwd =", cwd);
let out;
try {
out = cp.spawnSync(python, [scriptPath, '--file', doc.fileName, '--cwd', cwd], {
cwd,
encoding: 'utf8'
});
//shows the returned output, error, and status
console.log("MahkrabMaker: python stdout:\n\n\n", out.stdout);
console.log("\n\nMahkrabMaker: python stderr:", out.stderr);
if (out.status == 0) {
console.log("MahkrabMaker: python status: good")
}
else {
console.log("MahkrabMaker: python status: error")
}
} catch (e) {
return vscode.window.showErrorMessage(`MahkrabMaker: failed to start Python (${python}): ${e.message}`);
}
if (out.error) {
return vscode.window.showErrorMessage(`MahkrabMaker: Python error: ${out.error.message}`);
}
if (out.status !== 0) {
const stderr = (out.stderr || '').trim();
return vscode.window.showErrorMessage(`MahkrabMaker: main.py exited with ${out.status}${stderr ? `\n${stderr}` : ''}`);
}
let payload;
try {
payload = JSON.parse(out.stdout);
} catch (e) {
vscode.window.showErrorMessage(
`MahkrabMaker: main.py did not return valid JSON: ${e.message}`
);
vscode.window.showInformationMessage('MahkrabMaker: check "Extension Host" output for stdout/stderr details.');
return;
}
const full = String(payload?.full || '').trim();
if (!full) {
return vscode.window.showErrorMessage('MahkrabMaker: main.py did not include a "full" command.');
}
//update code runner settings
const codeRunner = vscode.workspace.getConfiguration('code-runner');
const current = codeRunner.get('executorMap') || {};
const updated = { ...current, c: full };
const target = vscode.workspace.workspaceFolders?.length
? vscode.ConfigurationTarget.Workspace
: vscode.ConfigurationTarget.Global;
try {
await codeRunner.update('executorMap', updated, target);
await codeRunner.update('runInTerminal', true, target);
console.log("MahkrabMaker: Code Runner executorMap.c updated");
vscode.window.showInformationMessage('MahkrabMaker: Code Runner command updated for C.');
} catch (e) {
vscode.window.showErrorMessage(`MahkrabMaker: failed to update Code Runner settings: ${e.message}`);
}
}
function deactivate() {}
module.exports = { activate, deactivate };