From 2a5433a2a76eb797bfdbc816363d036b46d31b76 Mon Sep 17 00:00:00 2001 From: Adam Chalmers Date: Tue, 28 Jan 2025 15:19:03 -0600 Subject: [PATCH] Fix edge treatment tests --- src/hooks/useEngineConnectionSubscriptions.ts | 19 +++++++++++---- src/lang/KclSingleton.ts | 8 ++++--- src/lang/modifyAst.ts | 23 ++++++++++++++----- src/lang/modifyAst/addEdgeTreatment.test.ts | 16 ++++++------- src/lang/modifyAst/addEdgeTreatment.ts | 4 ++-- src/lang/queryAst.ts | 11 +++++---- src/lang/std/sketch.ts | 12 ++++++++-- src/lib/selections.ts | 4 ++-- 8 files changed, 65 insertions(+), 32 deletions(-) diff --git a/src/hooks/useEngineConnectionSubscriptions.ts b/src/hooks/useEngineConnectionSubscriptions.ts index ec2a34761..ce1ee9e13 100644 --- a/src/hooks/useEngineConnectionSubscriptions.ts +++ b/src/hooks/useEngineConnectionSubscriptions.ts @@ -18,7 +18,7 @@ import { import { err, reportRejection } from 'lib/trap' import { DefaultPlaneStr, getFaceDetails } from 'clientSideScene/sceneEntities' import { getNodeFromPath, getNodePathFromSourceRange } from 'lang/queryAst' -import { CallExpression, defaultSourceRange } from 'lang/wasm' +import { CallExpression, CallExpressionKw, defaultSourceRange } from 'lang/wasm' import { EdgeCutInfo, ExtrudeFacePlane } from 'machines/modelingMachine' export function useEngineConnectionSubscriptions() { @@ -239,14 +239,23 @@ export function useEngineConnectionSubscriptions() { } } if (!chamferInfo) return null - const segmentCallExpr = getNodeFromPath( + const segmentCallExpr = getNodeFromPath< + CallExpression | CallExpressionKw + >( kclManager.ast, chamferInfo?.segment.codeRef.pathToNode || [], - 'CallExpression' + ['CallExpression', 'CallExpressionKw'] ) if (err(segmentCallExpr)) return null - if (segmentCallExpr.node.type !== 'CallExpression') return null - const sketchNodeArgs = segmentCallExpr.node.arguments + if ( + segmentCallExpr.node.type !== 'CallExpression' && + segmentCallExpr.node.type !== 'CallExpressionKw' + ) + return null + const sketchNodeArgs = + segmentCallExpr.node.type == 'CallExpression' + ? segmentCallExpr.node.arguments + : segmentCallExpr.node.arguments.map((la) => la.arg) const tagDeclarator = sketchNodeArgs.find( ({ type }) => type === 'TagDeclarator' ) diff --git a/src/lang/KclSingleton.ts b/src/lang/KclSingleton.ts index 07f282b2b..2b95184a4 100644 --- a/src/lang/KclSingleton.ts +++ b/src/lang/KclSingleton.ts @@ -12,6 +12,7 @@ import { EXECUTE_AST_INTERRUPT_ERROR_MESSAGE } from 'lib/constants' import { CallExpression, + CallExpressionKw, clearSceneAndBustCache, emptyExecState, ExecState, @@ -455,14 +456,15 @@ export class KclManager { Array.from(this.engineCommandManager.artifactGraph).forEach( ([commandId, artifact]) => { if (!('codeRef' in artifact)) return - const _node1 = getNodeFromPath>( + const _node1 = getNodeFromPath>( this.ast, artifact.codeRef.pathToNode, - 'CallExpression' + ['CallExpression', 'CallExpressionKw'] ) if (err(_node1)) return const { node } = _node1 - if (node.type !== 'CallExpression') return + if (node.type !== 'CallExpression' && node.type !== 'CallExpressionKw') + return const [oldStart, oldEnd] = artifact.codeRef.range if (oldStart === 0 && oldEnd === 0) return if (oldStart === node.start && oldEnd === node.end) return diff --git a/src/lang/modifyAst.ts b/src/lang/modifyAst.ts index f0ad0e98c..17f101341 100644 --- a/src/lang/modifyAst.ts +++ b/src/lang/modifyAst.ts @@ -34,7 +34,12 @@ import { ARG_INDEX_FIELD, LABELED_ARG_FIELD, } from './queryAst' -import { addTagForSketchOnFace, ARG_TAG, getConstraintInfo } from './std/sketch' +import { + addTagForSketchOnFace, + ARG_TAG, + getConstraintInfo, + getConstraintInfoKw, +} from './std/sketch' import { PathToNodeMap, isLiteralArrayOrStatic, @@ -1034,6 +1039,7 @@ export function giveSketchFnCallTag( return { tagDeclarator, isTagExisting } } + // We've handled CallExpressionKw above, so this has to be positional. const _node1 = getNodeFromPath(ast, path, 'CallExpression') if (err(_node1)) return _node1 const { node: primaryCallExp } = _node1 @@ -1175,17 +1181,22 @@ export function deleteSegmentFromPipeExpression( dependentRanges.forEach((range) => { const path = getNodePathFromSourceRange(_modifiedAst, range) - const callExp = getNodeFromPath>( + const callExp = getNodeFromPath>( _modifiedAst, path, - 'CallExpression', + ['CallExpression', 'CallExpressionKw'], true ) if (err(callExp)) return callExp - const constraintInfo = getConstraintInfo(callExp.node, code, path).find( - ({ sourceRange }) => isOverlap(sourceRange, range) - ) + const constraintInfo = + callExp.node.type === 'CallExpression' + ? getConstraintInfo(callExp.node, code, path).find(({ sourceRange }) => + isOverlap(sourceRange, range) + ) + : getConstraintInfoKw(callExp.node, code, path).find( + ({ sourceRange }) => isOverlap(sourceRange, range) + ) if (!constraintInfo) return if (!constraintInfo.argPosition) return diff --git a/src/lang/modifyAst/addEdgeTreatment.test.ts b/src/lang/modifyAst/addEdgeTreatment.test.ts index aec20babd..2c77c209f 100644 --- a/src/lang/modifyAst/addEdgeTreatment.test.ts +++ b/src/lang/modifyAst/addEdgeTreatment.test.ts @@ -63,18 +63,18 @@ const runGetPathToExtrudeForSegmentSelectionTest = async ( function getExtrudeExpression( ast: Program, pathToExtrudeNode: PathToNode - ): CallExpression | PipeExpression | undefined | Error { + ): CallExpression | CallExpressionKw | PipeExpression | undefined | Error { if (pathToExtrudeNode.length === 0) return undefined // no extrude node - const extrudeNodeResult = getNodeFromPath( - ast, - pathToExtrudeNode - ) + const extrudeNodeResult = getNodeFromPath< + CallExpression | CallExpressionKw + >(ast, pathToExtrudeNode) if (err(extrudeNodeResult)) { return extrudeNodeResult } return extrudeNodeResult.node } + function getExpectedExtrudeExpression( ast: Program, code: string, @@ -605,10 +605,10 @@ extrude001 = extrude(sketch001, length = -5) ) const pathToNode = getNodePathFromSourceRange(ast, range) if (err(pathToNode)) return - const callExp = getNodeFromPath( + const callExp = getNodeFromPath( ast, pathToNode, - 'CallExpression' + ['CallExpression', 'CallExpressionKw'] ) if (err(callExp)) return const edges = isTagUsedInEdgeTreatment({ ast, callExp: callExp.node }) @@ -640,7 +640,7 @@ extrude001 = extrude(sketch001, length = -5) const range = topLevelRange(start, start + lineOfInterest.length) const pathToNode = getNodePathFromSourceRange(ast, range) if (err(pathToNode)) return - const callExp = getNodeFromPath( + const callExp = getNodeFromPath( ast, pathToNode, 'CallExpression' diff --git a/src/lang/modifyAst/addEdgeTreatment.ts b/src/lang/modifyAst/addEdgeTreatment.ts index 3d489d791..5d3c72171 100644 --- a/src/lang/modifyAst/addEdgeTreatment.ts +++ b/src/lang/modifyAst/addEdgeTreatment.ts @@ -310,10 +310,10 @@ export function mutateAstWithTagForSketchSegment( astClone: Node, pathToSegmentNode: PathToNode ): { modifiedAst: Program; tag: string } | Error { - const segmentNode = getNodeFromPath( + const segmentNode = getNodeFromPath( astClone, pathToSegmentNode, - 'CallExpression' + ['CallExpression', 'CallExpressionKw'] ) if (err(segmentNode)) return segmentNode diff --git a/src/lang/queryAst.ts b/src/lang/queryAst.ts index ce0c87227..ed0cb0472 100644 --- a/src/lang/queryAst.ts +++ b/src/lang/queryAst.ts @@ -26,7 +26,7 @@ import { import { createIdentifier, splitPathAtLastIndex } from './modifyAst' import { getSketchSegmentFromSourceRange } from './std/sketchConstraints' import { getAngle } from '../lib/utils' -import { ARG_TAG, getFirstArg } from './std/sketch' +import { ARG_TAG, getArgForEnd, getFirstArg } from './std/sketch' import { getConstraintLevelFromSourceRange, getConstraintType, @@ -772,10 +772,10 @@ export function isLinesParallelAndConstrained( ast, secondaryLine?.codeRef?.range ) - const _secondaryNode = getNodeFromPath( + const _secondaryNode = getNodeFromPath( ast, secondaryPath, - 'CallExpression' + ['CallExpression', 'CallExpressionKw'] ) if (err(_secondaryNode)) return _secondaryNode const secondaryNode = _secondaryNode.node @@ -809,7 +809,10 @@ export function isLinesParallelAndConstrained( Math.abs(primaryAngle - secondaryAngleAlt) < EPSILON // is secondary line fully constrain, or has constrain type of 'angle' - const secondaryFirstArg = getFirstArg(secondaryNode) + const secondaryFirstArg = + secondaryNode.type === 'CallExpression' + ? getFirstArg(secondaryNode) + : getArgForEnd(secondaryNode) if (err(secondaryFirstArg)) return secondaryFirstArg const isAbsolute = false // ADAM: TODO diff --git a/src/lang/std/sketch.ts b/src/lang/std/sketch.ts index 14f145f99..9f80550ec 100644 --- a/src/lang/std/sketch.ts +++ b/src/lang/std/sketch.ts @@ -2012,7 +2012,10 @@ export function changeSketchArguments( sourceRangeOrPath.type === 'sourceRange' ? getNodePathFromSourceRange(_node, sourceRangeOrPath.sourceRange) : sourceRangeOrPath.pathToNode - const nodeMeta = getNodeFromPath(_node, thePath) + const nodeMeta = getNodeFromPath( + _node, + thePath + ) if (err(nodeMeta)) return nodeMeta const { node: callExpression, shallowPath } = nodeMeta @@ -2268,7 +2271,7 @@ export function replaceSketchLine({ */ function addTagToChamfer( tagInfo: AddTagInfo, - edgeCutMeta: EdgeCutInfo | null + edgeCutMeta: EdgeCutInfo ): | { modifiedAst: Node @@ -2406,6 +2409,11 @@ export function addTagForSketchOnFace( return addTagKw()(tagInfo) } if (expressionName === 'chamfer') { + if (edgeCutMeta === null) { + return new Error( + 'Cannot add tag to chamfer because no edge cut was provided' + ) + } return addTagToChamfer(tagInfo, edgeCutMeta) } if (expressionName in sketchLineHelperMapKw) { diff --git a/src/lib/selections.ts b/src/lib/selections.ts index 9b4c4ddba..647687603 100644 --- a/src/lib/selections.ts +++ b/src/lib/selections.ts @@ -443,10 +443,10 @@ function updateSceneObjectColors(codeBasedSelections: Selection[]) { Object.values(sceneEntitiesManager.activeSegments).forEach((segmentGroup) => { if (!SEGMENT_BODIES_PLUS_PROFILE_START.includes(segmentGroup?.name)) return - const nodeMeta = getNodeFromPath>( + const nodeMeta = getNodeFromPath>( updated, segmentGroup.userData.pathToNode, - 'CallExpression' + ['CallExpression', 'CallExpressionKw'] ) if (err(nodeMeta)) return const node = nodeMeta.node