diff --git a/src/lang/modifyAst.ts b/src/lang/modifyAst.ts index df999321c..04f2e8381 100644 --- a/src/lang/modifyAst.ts +++ b/src/lang/modifyAst.ts @@ -1209,3 +1209,41 @@ export function insertVariableAndOffsetPathToNode( } } } + +// Create an array expression for variables, +// or keep it null if all are PipeSubstitutions +export function createVariableExpressionsArray(sketches: Expr[]) { + let sketchesExpr: Expr | null = null + if (sketches.every((s) => s.type === 'PipeSubstitution')) { + // Keeping null so we don't even put it the % sign + } else if (sketches.length === 1) { + sketchesExpr = sketches[0] + } else { + sketchesExpr = createArrayExpression(sketches) + } + return sketchesExpr +} + +// Create a path to node to the last variable declaroator of an ast +// Optionally, can point to the first kwarg of the CallExpressionKw +export function createPathToNodeForLastVariable( + ast: Node, + toFirstKwarg = true +): PathToNode { + const argIndex = 0 // first kwarg for all sweeps here + const pathToCall: PathToNode = [ + ['body', ''], + [ast.body.length - 1, 'index'], + ['declaration', 'VariableDeclaration'], + ['init', 'VariableDeclarator'], + ] + if (toFirstKwarg) { + pathToCall.push( + ['arguments', 'CallExpressionKw'], + [argIndex, ARG_INDEX_FIELD], + ['arg', LABELED_ARG_FIELD] + ) + } + + return pathToCall +} diff --git a/src/lang/modifyAst/addSweep.ts b/src/lang/modifyAst/addSweep.ts index ba53bad50..3f39c2c50 100644 --- a/src/lang/modifyAst/addSweep.ts +++ b/src/lang/modifyAst/addSweep.ts @@ -8,7 +8,11 @@ import { createVariableDeclaration, findUniqueName, } from '@src/lang/create' -import { insertVariableAndOffsetPathToNode } from '@src/lang/modifyAst' +import { + createPathToNodeForLastVariable, + createVariableExpressionsArray, + insertVariableAndOffsetPathToNode, +} from '@src/lang/modifyAst' import { getEdgeTagCall, mutateAstWithTagForSketchSegment, @@ -16,8 +20,6 @@ import { import { getNodeFromPath, getVariableExprsFromSelection, - createVariableExpressionsArray, - createPathToNodeForLastVariable, valueOrVariable, } from '@src/lang/queryAst' import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils' diff --git a/src/lang/queryAst.ts b/src/lang/queryAst.ts index e07ead587..11486384a 100644 --- a/src/lang/queryAst.ts +++ b/src/lang/queryAst.ts @@ -2,11 +2,7 @@ import type { FunctionExpression } from '@rust/kcl-lib/bindings/FunctionExpressi import type { ImportStatement } from '@rust/kcl-lib/bindings/ImportStatement' import type { Node } from '@rust/kcl-lib/bindings/Node' import type { TypeDeclaration } from '@rust/kcl-lib/bindings/TypeDeclaration' -import { - createLocalName, - createPipeSubstitution, - createArrayExpression, -} from '@src/lang/create' +import { createLocalName, createPipeSubstitution } from '@src/lang/create' import type { ToolTip } from '@src/lang/langHelpers' import { splitPathAtLastIndex } from '@src/lang/modifyAst' import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils' @@ -1100,44 +1096,6 @@ export function getVariableExprsFromSelection( return { exprs, paths } } -// Create an array expression for variables, -// or keep it null if all are PipeSubstitutions -export function createVariableExpressionsArray(sketches: Expr[]) { - let sketchesExpr: Expr | null = null - if (sketches.every((s) => s.type === 'PipeSubstitution')) { - // Keeping null so we don't even put it the % sign - } else if (sketches.length === 1) { - sketchesExpr = sketches[0] - } else { - sketchesExpr = createArrayExpression(sketches) - } - return sketchesExpr -} - -// Create a path to node to the last variable declaroator of an ast -// Optionally, can point to the first kwarg of the CallExpressionKw -export function createPathToNodeForLastVariable( - ast: Node, - toFirstKwarg = true -): PathToNode { - const argIndex = 0 // first kwarg for all sweeps here - const pathToCall: PathToNode = [ - ['body', ''], - [ast.body.length - 1, 'index'], - ['declaration', 'VariableDeclaration'], - ['init', 'VariableDeclarator'], - ] - if (toFirstKwarg) { - pathToCall.push( - ['arguments', 'CallExpressionKw'], - [argIndex, ARG_INDEX_FIELD], - ['arg', LABELED_ARG_FIELD] - ) - } - - return pathToCall -} - // Go from the sketches argument in a KCL sweep call declaration // to a list of graph selections, useful for edit flows. // Somewhat of an inverse of getSketchExprsFromSelection.