Skip to content

Commit

Permalink
fix: consider variables before first group
Browse files Browse the repository at this point in the history
  • Loading branch information
seasick committed Jan 21, 2024
1 parent b8c1e05 commit 05372a8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/components/Customizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default function Customizer({ parameters, onChange }: Props) {
.map(([groupName, groupParams], idx) => (
<Accordion defaultExpanded={idx === 0} key={idx}>
<AccordionSummary expandIcon={<ArrowDropDownIcon />}>
{groupName}
{groupName || <i>Common Parameters</i>}
</AccordionSummary>
<AccordionDetails>
{groupParams.map((parameter) => {
Expand Down
22 changes: 14 additions & 8 deletions src/lib/openSCAD/parseParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) => {
Expand Down
9 changes: 9 additions & 0 deletions tests/__snapshots__/openSCADparseParameters.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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:",
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/openSCAD/customizer/example1.scad
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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();

0 comments on commit 05372a8

Please sign in to comment.