Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» General code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Feb 4, 2025
1 parent 0c2ef1d commit c82c5e2
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 116 deletions.
8 changes: 5 additions & 3 deletions abm/abm.js
Original file line number Diff line number Diff line change
Expand Up @@ -880,9 +880,11 @@ function runSelectedAction() {
}
}

// Look for a script in buildroot/share/PlatformIO/scripts and run it.
// Exit if 'script' is not found or optional 'needs' are not met.
// Optional 'args' are added to the python command.
/**
* Look for a script in buildroot/share/PlatformIO/scripts and run it.
* Exit if 'script' is not found or optional 'needs' are not met.
* Optional 'args' are added to the python command.
*/
function runPython(script, needs, args) {
const script_file = path.join(marlin.workspaceRoot, 'buildroot', 'share', 'PlatformIO', 'scripts', script);
if (!fs.existsSync(script_file)) {
Expand Down
36 changes: 19 additions & 17 deletions abm/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,17 @@ function combinedSchema() {
/**
* Create two schemas for use in editor interaction, since we need to know if a change
* was made in Configuration.h that affects Configuration_adv.h directly or indirectly.
* s1 : Configuration.h schema
* s2 : Configuration_adv.h schema with Configuration.h + Conditionals_LCD.h precursor
* bas : Configuration.h schema
* adv : Configuration_adv.h schema with Configuration.h + Conditionals_LCD.h precursor
*/
const s1 = ConfigSchema.newSchemaFromText(config1),
s2 = ConfigSchema.newSchemaFromText(adv_combo, -prefix_lines);
const bas = ConfigSchema.newSchemaFromText(config1),
adv = ConfigSchema.newSchemaFromText(adv_combo, -prefix_lines);

return { basic: s1, advanced: s2 };
return { basic: bas, advanced: adv };
}

const schemas = combinedSchema();
abm.log("abmeditor.js", schemas);
abm.log("abm/editor.js", ConfigSchema.schemas);

// Utility function to get the name of a document from a full path.
const document_name = document => document.uri.fsPath.split(path.sep).pop();
Expand Down Expand Up @@ -193,14 +193,14 @@ class ConfigEditorProvider {
*/
function initWebview() {
// Get the name of the document.
abm.log(`initWebview: ${name}`);
abm.log(`ConfigEditorProvider.initWebview: ${name}`);

// Send the pre-parsed data to the web view.
wv.postMessage({ type: 'update', schema: myschema.bysec }); // editview.js:handleMessageToUI
wv.postMessage({ type: 'update', bysec: myschema.bysec }); // editview.js:handleMessageToUI

// Parse the text and send it to the webview.
//sch.importText(document.getText());
//wv.postMessage({ type: 'update', schema: sch.bysec });
//wv.postMessage({ type: 'update', bysec: sch.bysec });

// Originally the webview received the raw text.
//wv.postMessage({ type: 'update', text: document.getText() });
Expand All @@ -212,16 +212,16 @@ class ConfigEditorProvider {
*/
function updateWebview(external=false) {
// Get the name of the document.
abm.log(`updateWebview: ${name}`);
abm.log(`ConfigEditorProvider.updateWebview: ${name}`);

// Send the parsed data to the web view.
if (external)
wv.postMessage({ type: 'update', schema: myschema.bysec }); // editview.js:handleMessageToUI
wv.postMessage({ type: 'update', bysec: myschema.bysec }); // editview.js:handleMessageToUI

// If the second config file is also open, update it as well.
if (!is_adv && 'Configuration_adv.h' in webviews) {
abm.log("updateWebview >> Configuration_adv.h");
webviews['Configuration_adv.h'].postMessage({ type: 'update', schema: schemas.advanced.bysec });
webviews['Configuration_adv.h'].postMessage({ type: 'update', bysec: schemas.advanced.bysec });
}
}

Expand Down Expand Up @@ -327,16 +327,16 @@ class ConfigEditorProvider {

// Receive message from the webview via vscode.postMessage.
// The webview sends changes to apply to the underlying document.
function handleMessageFromUI(e) {
abm.log("(ConfigEditorProvider) handleMessageFromUI", e);
switch (e.type) {
function handleMessageFromUI(m) {
abm.log("ConfigEditorProvider.handleMessageFromUI", e);
switch (m.type) {
case 'change':
applyConfigChange(document, e.data); // Update the document text using the given data.
applyConfigChange(document, m.data); // Update the document text using the given data.
break;

case 'multi-change':
const edit = new vscode.WorkspaceEdit();
e.changes.forEach(d => {
m.changes.forEach(d => {
applyConfigChange(document, d.data, edit);
});
ws.applyEdit(edit);
Expand Down Expand Up @@ -367,6 +367,7 @@ class ConfigEditorProvider {
// Local path to script and css for the webview
const nonce = (0, abm.getNonce)(), // Use a nonce to whitelist which scripts can be run
jqueryUri = this.jsUri(webview, 'jquery-3.6.0.min.js'),
vsviewUri = this.jsUri(webview, 'vsview.js'),
schemaUri = this.jsUri(webview, 'schema.js'),
scriptUri = this.jsUri(webview, 'editview.js'),
gridUri = this.jsUri(webview, 'grid.js'),
Expand All @@ -380,6 +381,7 @@ class ConfigEditorProvider {
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="${cssUri}" rel="stylesheet" />
<script nonce="${nonce}" src="${jqueryUri}"></script>
<script nonce="${nonce}" src="${vsviewUri}"></script>
<script nonce="${nonce}" src="${schemaUri}"></script>
<script nonce="${nonce}" src="${scriptUri}"></script>
<script nonce="${nonce}" src="${gridUri}"></script>
Expand Down
4 changes: 4 additions & 0 deletions abm/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class InfoPanelProvider {
function updateWebview() {
wv.postMessage({ type: 'say', text: "hello" }); // infoview.js:handleMessageToUI
}

// Update the view now that the pane has been revealed.
updateWebview();
}

Expand All @@ -82,6 +84,7 @@ class InfoPanelProvider {
// Local path to script and css for the webview
const nonce = abm.getNonce(), // Use a nonce to whitelist which scripts can be run
jqueryUri = this.jsUri(webview, 'jquery-3.6.0.min.js'),
vsviewUri = this.jsUri(webview, 'vsview.js'),
scriptUri = this.jsUri(webview, 'infoview.js'),
cssUri = this.resourceUri(webview, 'css', 'infoview.css');

Expand All @@ -92,6 +95,7 @@ class InfoPanelProvider {
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src ${webview.cspSource}; style-src ${webview.cspSource}; script-src 'nonce-${nonce}'; ">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="${cssUri}" rel="stylesheet" />
<script nonce="${nonce}" src="${vsviewUri}"></script>
<script nonce="${nonce}" src="${jqueryUri}"></script>
<script nonce="${nonce}" src="${scriptUri}"></script>
<title>Marlin Info</title>
Expand Down
2 changes: 1 addition & 1 deletion abm/js/abmview.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var ABM = (function(){
const $a = $('<a>').attr('href', '#').text(m.val);
$a.click((e) => {
e.preventDefault();
vscode.postMessage({ command:'openfile', uri:m.uri });
_msg({ command:'openfile', uri:m.uri });
});
$dest.append($a);
}
Expand Down
48 changes: 23 additions & 25 deletions abm/js/editview.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,9 @@ $(function () {
String.prototype.camelToID = function () { return this.unbrace().replace(/([a-z])([A-Z0-9_])/g, '$1_$2').replace(/_/g, '-').toLowerCase(); }

Array.prototype.toggle = function (val, tf) {
const hv = this.includes(val);
if (tf && !hv)
this.push(val);
else if (!tf && hv) {
// Remove the value from the array
for (let i = this.length - 1; i >= 0; i--)
if (this[i] === val) this.splice(i, 1);
}
const idx = this.indexOf(val);
if (tf && idx === -1) this.push(val);
else if (!tf && idx !== -1) this.splice(idx, 1);
return this;
};

Expand All @@ -164,13 +159,17 @@ $(function () {
verbose = oldverbose;
}

// A copy of ConfigSchema for local usage
var schema = ConfigSchema.newSchema(),
config_filter = { terms: '', show_comments: true, show_disabled: true, collapsed: [] },
result_index = 0;
/**
* A new empty ConfigSchema for local usage
* Initialized using either:
* - Serialized 'bysec' data sent from the provider, or
* - Data for the revealed view restored using vscode.getState().
*/
var schema = ConfigSchema.newSchema();

// We need getState, setState, and postMessage.
const vscode = acquireVsCodeApi();
// The current filter state
var config_filter = { terms: '', show_comments: true, show_disabled: true, collapsed: [] },
result_index = 0;

// Ignore the next update message.
var ignore_update = false;
Expand All @@ -184,7 +183,7 @@ $(function () {
function end_multi_update() {
multi_update = false;
ignore_update = true;
vscode.postMessage({ type:'multi-change', changes }); // editor.js:handleMessageFromUI
_msg({ type:'multi-change', changes }); // editor.js:handleMessageFromUI
}

// A filter text box to filter the list of options.
Expand All @@ -204,7 +203,7 @@ $(function () {
// Global for the form.
var $form;

// Set up event handlers on the header form fields.
// Bind event handlers to the header form fields.
function initConfigFilterForm() {
// Make sure no forms are able to submit.
$('form').bind('submit', (e) => { return false; });
Expand Down Expand Up @@ -232,7 +231,7 @@ $(function () {
// A button to test sending messages to the extension.
const $button = $('#hello-button');
$button.find('button').bind('click', () => {
vscode.postMessage({ type: 'hello' }); // editor.js:handleMessageFromUI
_msg({ type: 'hello' }); // editor.js:handleMessageFromUI
});
}

Expand Down Expand Up @@ -292,7 +291,7 @@ $(function () {
changes.push(msg);
else {
ignore_update = true;
vscode.postMessage(msg); // editor.js:handleMessageFromUI
_msg(msg); // editor.js:handleMessageFromUI
}
}

Expand Down Expand Up @@ -722,13 +721,12 @@ $(function () {
function saveSectionCollapsed(sect_class, hide) {
log("saveSectionCollapsed", {sect_class, hide});

if (!('collapsed' in config_filter)) config_filter.collapsed = [];
config_filter.collapsed ??= [];

if (sect_class == 'all') {
config_filter.collapsed = [];
if (hide)
for (let sect in schema.bysec)
config_filter.collapsed.push(sect.sectID());
if (sect_class === 'all') {
config_filter.collapsed = hide
? Object.keys(schema.bysec).map(sect => sect.sectID())
: [];
}
else
config_filter.collapsed.toggle(sect_class, hide);
Expand Down Expand Up @@ -845,7 +843,7 @@ $(function () {
if (ignore_update) // This view caused the update? Ignore it.
ignore_update = false;
else
buildConfigFormWithData(message.schema); // Use the provided data to rebuild the form.
buildConfigFormWithData(message.bysec); // Use the provided data to rebuild the form.
break;

case 'text-update':
Expand Down
15 changes: 8 additions & 7 deletions abm/js/infoview.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ $(function () {
// Set up the webview with options and basic html.
}

// Update state from the stored data structure and filter.
// Save the current view state.
// Use this to restore the view when it is revealed.
function saveWebViewState() {
const data = { data: {} };
log('saveWebViewState', data);
Expand All @@ -42,21 +43,21 @@ $(function () {
* @param {object} message - The message object.
*/
var ignore_update = false; // Ignore the next update message.
function handleMessageToUI(message) {
log("handleMessageToUI", message);
switch (message.type) {
function handleMessageToUI(m) {
log("infoview.js : handleMessageToUI", m);
switch (m.type) {
// Update the whole form in response to an external change.
case 'info':
if (ignore_update) {
ignore_update = false;
return;
}
//drawInfo(message.data);
//drawInfo(m.data);
break;

// Display an error message
case 'error':
$('#error').text(message.text).show().click(() => { $('#error').hide(); });
$('#error').text(m.text).show().click(() => { $('#error').hide(); });
break;
}
}
Expand All @@ -73,7 +74,7 @@ $(function () {
initInfoView();

//
// Tab Revealed
// Info Panel Revealed
//
// If there is state data then we can skip the parser and build the form.
const state = vscode.getState();
Expand Down
Loading

0 comments on commit c82c5e2

Please sign in to comment.