From 05372a8e1e7d53f5b0f92cf7c6a7c3c741a522a8 Mon Sep 17 00:00:00 2001 From: Clemens Solar Date: Sun, 21 Jan 2024 21:40:59 +0100 Subject: [PATCH] fix: consider variables before first group --- src/components/Customizer.tsx | 2 +- src/lib/openSCAD/parseParameter.ts | 22 ++++++++++++------- .../openSCADparseParameters.test.ts.snap | 9 ++++++++ .../openSCAD/customizer/example1.scad | 5 +++++ 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/components/Customizer.tsx b/src/components/Customizer.tsx index d5880bf..70da9d7 100644 --- a/src/components/Customizer.tsx +++ b/src/components/Customizer.tsx @@ -79,7 +79,7 @@ export default function Customizer({ parameters, onChange }: Props) { .map(([groupName, groupParams], idx) => ( }> - {groupName} + {groupName || Common Parameters} {groupParams.map((parameter) => { diff --git a/src/lib/openSCAD/parseParameter.ts b/src/lib/openSCAD/parseParameter.ts index 84b29e1..6d676b7 100644 --- a/src/lib/openSCAD/parseParameter.ts +++ b/src/lib/openSCAD/parseParameter.ts @@ -39,7 +39,13 @@ export default function parseParameters(script: string): Parameter[] { /^([a-z0-9A-Z_$]+)\s*=\s*([^;]+);[\t\f\cK ]*(\/\/[^\n]*)?/gm; // TODO: Use AST parser instead of regex const groupRegex = /^\/\*\s*\[([^\]]+)\]\s*\*\//gm; - const groupSections: { id: string; group: string; code: string }[] = []; + const groupSections: { id: string; group: string; code: string }[] = [ + { + id: '', + group: undefined, + code: script, + }, + ]; let tmpGroup; // Find groups @@ -59,13 +65,13 @@ export default function parseParameters(script: string): Parameter[] { group.code = script.substring(startIndex, endIndex); }); - // If there are no groups, add the entire script as a group - if (!groupSections.length) { - groupSections.push({ - id: '', - group: undefined, - code: script, - }); + // If we have more then one group, we need to adjust the code of the first group. + // It should onyl have the code that is above the first group. + if (groupSections.length > 1) { + groupSections[0].code = script.substring( + 0, + script.indexOf(groupSections[1].id) + ); } groupSections.forEach((groupSection) => { diff --git a/tests/__snapshots__/openSCADparseParameters.test.ts.snap b/tests/__snapshots__/openSCADparseParameters.test.ts.snap index a1b73d6..1438477 100644 --- a/tests/__snapshots__/openSCADparseParameters.test.ts.snap +++ b/tests/__snapshots__/openSCADparseParameters.test.ts.snap @@ -113,6 +113,15 @@ exports[`testing parameter parsing of openscad scripts testing dropDownBox.scad: exports[`testing parameter parsing of openscad scripts testing example1.scad: example1.scad 1`] = ` [ + { + "description": "A variable which comes first before the tabs", + "group": undefined, + "name": "VariableBeforeFirstTab", + "options": undefined, + "range": undefined, + "type": "number", + "value": 1, + }, { "description": "combo box for number", "group": "Drop down box:", diff --git a/tests/fixtures/openSCAD/customizer/example1.scad b/tests/fixtures/openSCAD/customizer/example1.scad index 9acd454..0d7ea39 100644 --- a/tests/fixtures/openSCAD/customizer/example1.scad +++ b/tests/fixtures/openSCAD/customizer/example1.scad @@ -1,5 +1,8 @@ // Copied from https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Customizer +// A variable which comes first before the tabs +VariableBeforeFirstTab = 1; + /* [Drop down box:] */ // combo box for number Numbers=2; // [0, 1, 2, 3] @@ -44,3 +47,5 @@ Vector1=[12]; //[0:2:50] Vector2=[12,34]; //[0:2:50] Vector3=[12,34,46]; //[0:2:50] Vector4=[12,34,46,24]; //[0:2:50] + +cube();