This commit is contained in:
Pierre Jacquier
2025-07-01 19:38:47 -04:00
parent 187925ff21
commit 9942a65612

View File

@ -10,7 +10,7 @@ import {
createVariableDeclaration, createVariableDeclaration,
findUniqueName, findUniqueName,
} from '@src/lang/create' } from '@src/lang/create'
import { getNodeFromPath, getExprsFromSelection, valueOrVariable } from '@src/lang/queryAst' import { createPathToNodeForLastVariable, createVariableExpressionsArray, getNodeFromPath, getVariableExprsFromSelection, valueOrVariable } from '@src/lang/queryAst'
import type { import type {
ArtifactGraph, ArtifactGraph,
CallExpressionKw, CallExpressionKw,
@ -28,7 +28,6 @@ import {
} from '@src/lang/modifyAst/boolean' } from '@src/lang/modifyAst/boolean'
import type { Selections } from '@src/lib/selections' import type { Selections } from '@src/lib/selections'
import { KclCommandValue } from '@src/lib/commandTypes' import { KclCommandValue } from '@src/lib/commandTypes'
import { createPathToNode, createSketchExpression } from './addSweep'
import { insertVariableAndOffsetPathToNode } from '../modifyAst' import { insertVariableAndOffsetPathToNode } from '../modifyAst'
import { KCL_DEFAULT_CONSTANT_PREFIXES } from '@src/lib/constants' import { KCL_DEFAULT_CONSTANT_PREFIXES } from '@src/lib/constants'
@ -54,20 +53,20 @@ export function setTranslate({
// 2. Prepare unlabeled and labeled arguments // 2. Prepare unlabeled and labeled arguments
// Map the sketches selection into a list of kcl expressions to be passed as unlabelled argument // Map the sketches selection into a list of kcl expressions to be passed as unlabelled argument
const objectsExprList = getExprsFromSelection( const variableExpressions = getVariableExprsFromSelection(
objects, objects,
modifiedAst, modifiedAst,
nodeToEdit nodeToEdit
) )
if (err(objectsExprList)) { if (err(variableExpressions)) {
return objectsExprList return variableExpressions
} }
// Extra labeled args expression // Extra labeled args expression
const globalExpr = global const globalExpr = global
? [createLabeledArg('global', createLiteral(global))] ? [createLabeledArg('global', createLiteral(global))]
: [] : []
const objectsExpr = createSketchExpression(objectsExprList) const objectsExpr = createVariableExpressionsArray(variableExpressions.exprs)
const call = createCallExpressionStdLibKw('translate', objectsExpr, [ const call = createCallExpressionStdLibKw('translate', objectsExpr, [
createLabeledArg('x', valueOrVariable(x)), createLabeledArg('x', valueOrVariable(x)),
createLabeledArg('y', valueOrVariable(y)), createLabeledArg('y', valueOrVariable(y)),
@ -102,13 +101,28 @@ export function setTranslate({
Object.assign(result.node, call) Object.assign(result.node, call)
pathToNode = nodeToEdit pathToNode = nodeToEdit
} else { } else {
const name = findUniqueName( const lastPathToNode: PathToNode | undefined =
modifiedAst, variableExpressions.paths.pop()
KCL_DEFAULT_CONSTANT_PREFIXES.TRANSLATE, if (objectsExpr === null && lastPathToNode) {
) const pipe = getNodeFromPath<PipeExpression>(
const declaration = createVariableDeclaration(name, call) modifiedAst,
modifiedAst.body.push(declaration) lastPathToNode,
pathToNode = createPathToNode(modifiedAst) 'PipeExpression'
)
if (err(pipe)) {
return pipe
}
pipe.node.body.push(call)
pathToNode = lastPathToNode
} else {
const name = findUniqueName(
modifiedAst,
KCL_DEFAULT_CONSTANT_PREFIXES.TRANSLATE,
)
const declaration = createVariableDeclaration(name, call)
modifiedAst.body.push(declaration)
pathToNode = createPathToNodeForLastVariable(modifiedAst)
}
} }
return { return {