-
Notifications
You must be signed in to change notification settings - Fork 635
feat(parametric): add annotated design tree viewer #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aaravpatel0
wants to merge
6
commits into
Adam-CAD:master
Choose a base branch
from
aaravpatel0:feat/design-tree-viewer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aa7606f
feat(parametric): add annotated design tree viewer
aaravpatel0 432af30
fix(parametric): wire design tree selection to parameters
aaravpatel0 70ff580
chore(parametric): make design tree tests portable
aaravpatel0 2396d6f
fix(parametric): address design tree review feedback
aaravpatel0 1be6057
fix(parametric): handle invalid design tree parent links
aaravpatel0 2c23e86
fix(parametric): clarify design tree warning copy
aaravpatel0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
| import parseDesignTree from './parseDesignTree.ts'; | ||
|
|
||
| describe('parseDesignTree', () => { | ||
| it('parses a valid single node', () => { | ||
| const result = parseDesignTree( | ||
| '// @cadam-node {"id":"base","kind":"part","name":"Base","params":["width","height"],"moduleName":"base"}', | ||
| ); | ||
|
|
||
| assert.deepEqual(result, { | ||
| nodes: [ | ||
| { | ||
| id: 'base', | ||
| kind: 'part', | ||
| name: 'Base', | ||
| params: ['width', 'height'], | ||
| moduleName: 'base', | ||
| }, | ||
| ], | ||
| warnings: [], | ||
| }); | ||
| }); | ||
|
|
||
| it('parses valid parent and child nodes', () => { | ||
| const result = parseDesignTree(` | ||
| // @cadam-node {"id":"assembly","kind":"group","name":"Assembly"} | ||
| // @cadam-node {"id":"base-cut","kind":"operation","name":"Base cut","parentId":"assembly"} | ||
| `); | ||
|
|
||
| assert.deepEqual(result.nodes, [ | ||
| { id: 'assembly', kind: 'group', name: 'Assembly' }, | ||
| { | ||
| id: 'base-cut', | ||
| kind: 'operation', | ||
| name: 'Base cut', | ||
| parentId: 'assembly', | ||
| }, | ||
| ]); | ||
| assert.deepEqual(result.warnings, []); | ||
| }); | ||
|
|
||
| it('returns a warning for invalid JSON', () => { | ||
| const result = parseDesignTree('// @cadam-node {"id":"base","kind":"part"'); | ||
|
|
||
| assert.deepEqual(result.nodes, []); | ||
| assert.equal(result.warnings.length, 1); | ||
| assert.equal(result.warnings[0].code, 'invalid-json'); | ||
| }); | ||
|
|
||
| it('returns a warning for duplicate ids', () => { | ||
| const result = parseDesignTree(` | ||
| // @cadam-node {"id":"base","kind":"part"} | ||
| // @cadam-node {"id":"base","kind":"operation"} | ||
| `); | ||
|
|
||
| assert.deepEqual(result.nodes, [ | ||
| { id: 'base', kind: 'part', name: 'base' }, | ||
| ]); | ||
| assert.equal(result.warnings.length, 1); | ||
| assert.equal(result.warnings[0].code, 'duplicate-id'); | ||
| assert.equal(result.warnings[0].id, 'base'); | ||
| }); | ||
|
|
||
| it('returns a warning for missing id', () => { | ||
| const result = parseDesignTree('// @cadam-node {"kind":"part"}'); | ||
|
|
||
| assert.deepEqual(result.nodes, []); | ||
| assert.equal(result.warnings.length, 1); | ||
| assert.equal(result.warnings[0].code, 'missing-id'); | ||
| }); | ||
|
|
||
| it('returns a warning for missing kind', () => { | ||
| const result = parseDesignTree('// @cadam-node {"id":"base"}'); | ||
|
|
||
| assert.deepEqual(result.nodes, []); | ||
| assert.equal(result.warnings.length, 1); | ||
| assert.equal(result.warnings[0].code, 'missing-kind'); | ||
| assert.equal(result.warnings[0].id, 'base'); | ||
| }); | ||
|
|
||
| it('returns a warning for unknown kind', () => { | ||
| const result = parseDesignTree( | ||
| '// @cadam-node {"id":"base","kind":"sketch"}', | ||
| ); | ||
|
|
||
| assert.deepEqual(result.nodes, []); | ||
| assert.equal(result.warnings.length, 1); | ||
| assert.equal(result.warnings[0].code, 'unknown-kind'); | ||
| assert.equal(result.warnings[0].kind, 'sketch'); | ||
| }); | ||
|
|
||
| it('returns an empty result when there are no annotations', () => { | ||
| const result = parseDesignTree(` | ||
| width = 10; | ||
| module base() { | ||
| cube([width, 20, 5]); | ||
| } | ||
| `); | ||
|
|
||
| assert.deepEqual(result, { nodes: [], warnings: [] }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import type { | ||
| CadamDesignTreeNode, | ||
| CadamDesignTreeNodeKind, | ||
| CadamDesignTreeParseResult, | ||
| CadamDesignTreeParseWarning, | ||
| } from './types.ts'; | ||
|
|
||
| const CADAM_NODE_COMMENT_REGEX = /^\s*\/\/\s*@cadam-node\s+(.+?)\s*$/gm; | ||
| const KNOWN_NODE_KINDS = new Set<CadamDesignTreeNodeKind>([ | ||
| 'part', | ||
| 'operation', | ||
| 'group', | ||
| 'parameter', | ||
| ]); | ||
|
|
||
| type CadamNodePayload = { | ||
| id?: unknown; | ||
| kind?: unknown; | ||
| name?: unknown; | ||
| parentId?: unknown; | ||
| params?: unknown; | ||
| moduleName?: unknown; | ||
| }; | ||
|
|
||
| export default function parseDesignTree( | ||
| source: string, | ||
| ): CadamDesignTreeParseResult { | ||
| const nodes: CadamDesignTreeNode[] = []; | ||
| const warnings: CadamDesignTreeParseWarning[] = []; | ||
| const seenIds = new Set<string>(); | ||
|
|
||
| let match: RegExpExecArray | null; | ||
| while ((match = CADAM_NODE_COMMENT_REGEX.exec(source)) !== null) { | ||
| const raw = match[0]; | ||
| const json = match[1].trim(); | ||
| const line = lineNumberForIndex(source, match.index); | ||
| let payload: CadamNodePayload; | ||
|
|
||
| try { | ||
| const parsed: unknown = JSON.parse(json); | ||
| if (!isRecord(parsed)) { | ||
| warnings.push({ | ||
| code: 'invalid-json', | ||
| message: '@cadam-node payload must be a JSON object.', | ||
| line, | ||
| raw, | ||
| }); | ||
| continue; | ||
| } | ||
| payload = parsed; | ||
| } catch { | ||
| warnings.push({ | ||
| code: 'invalid-json', | ||
| message: '@cadam-node payload is not valid JSON.', | ||
| line, | ||
| raw, | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| const id = stringOrUndefined(payload.id); | ||
| if (!id) { | ||
| warnings.push({ | ||
| code: 'missing-id', | ||
| message: '@cadam-node payload is missing a string id.', | ||
| line, | ||
| raw, | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| const kind = stringOrUndefined(payload.kind); | ||
| if (!kind) { | ||
| warnings.push({ | ||
| code: 'missing-kind', | ||
| message: '@cadam-node payload is missing a string kind.', | ||
| line, | ||
| raw, | ||
| id, | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| if (!isKnownNodeKind(kind)) { | ||
| warnings.push({ | ||
| code: 'unknown-kind', | ||
| message: `@cadam-node kind "${kind}" is not supported.`, | ||
| line, | ||
| raw, | ||
| id, | ||
| kind, | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| if (seenIds.has(id)) { | ||
| warnings.push({ | ||
| code: 'duplicate-id', | ||
| message: `@cadam-node id "${id}" was already used.`, | ||
| line, | ||
| raw, | ||
| id, | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| seenIds.add(id); | ||
|
|
||
| const node: CadamDesignTreeNode = { | ||
| id, | ||
| kind, | ||
| name: stringOrUndefined(payload.name) ?? id, | ||
| }; | ||
| const parentId = stringOrUndefined(payload.parentId); | ||
| if (parentId) node.parentId = parentId; | ||
| const params = stringArrayOrUndefined(payload.params); | ||
| if (params) node.params = params; | ||
| const moduleName = stringOrUndefined(payload.moduleName); | ||
| if (moduleName) node.moduleName = moduleName; | ||
|
|
||
| nodes.push(node); | ||
| } | ||
|
|
||
| return { nodes, warnings }; | ||
| } | ||
|
|
||
| function isKnownNodeKind(kind: string): kind is CadamDesignTreeNodeKind { | ||
| return KNOWN_NODE_KINDS.has(kind as CadamDesignTreeNodeKind); | ||
| } | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| function isRecord(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === 'object' && value !== null && !Array.isArray(value); | ||
| } | ||
|
|
||
| function stringOrUndefined(value: unknown) { | ||
| if (typeof value !== 'string') return undefined; | ||
| const trimmed = value.trim(); | ||
| return trimmed.length > 0 ? trimmed : undefined; | ||
| } | ||
|
|
||
| function stringArrayOrUndefined(value: unknown) { | ||
| if (!Array.isArray(value)) return undefined; | ||
| const strings = value.filter( | ||
| (item): item is string => typeof item === 'string', | ||
| ); | ||
| return strings.length > 0 ? strings : undefined; | ||
| } | ||
|
greptile-apps[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| function lineNumberForIndex(source: string, index: number) { | ||
| return source.slice(0, index).split('\n').length; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.