locateExtrudeDeclarator > locateVariableWithCallOrPipe

This commit is contained in:
max-mrgrsk
2025-05-10 18:28:40 +02:00
parent dc49105606
commit 66fba3b6fb
4 changed files with 36 additions and 36 deletions

View File

@ -16,7 +16,7 @@ import {
getNodeFromPath,
hasSketchPipeBeenExtruded,
traverse,
locateExtrudeDeclarator,
locateVariableWithCallOrPipe,
} from '@src/lang/queryAst'
import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils'
import type { Artifact } from '@src/lang/std/artifactGraph'
@ -165,12 +165,12 @@ export async function modifyAstWithEdgeTreatmentAndTag(
)
// Locate the extrude call
const locatedExtrudeDeclarator = locateExtrudeDeclarator(
const locatedExtrudeDeclarator = locateVariableWithCallOrPipe(
clonedAst,
pathToExtrudeNode
)
if (err(locatedExtrudeDeclarator)) return locatedExtrudeDeclarator
const { extrudeDeclarator } = locatedExtrudeDeclarator
const { variableDeclarator } = locatedExtrudeDeclarator
// Modify the extrude expression to include this edge treatment expression
// CallExpression - no edge treatment
@ -178,33 +178,33 @@ export async function modifyAstWithEdgeTreatmentAndTag(
let pathToEdgeTreatmentNode: PathToNode
if (extrudeDeclarator.init.type === 'CallExpressionKw') {
if (variableDeclarator.init.type === 'CallExpressionKw') {
// 1. case when no edge treatment exists
// modify ast with new edge treatment call by mutating the extrude node
extrudeDeclarator.init = createPipeExpression([
extrudeDeclarator.init,
variableDeclarator.init = createPipeExpression([
variableDeclarator.init,
edgeTreatmentCall,
])
// get path to the edge treatment node
pathToEdgeTreatmentNode = getPathToNodeOfEdgeTreatmentLiteral(
pathToExtrudeNode,
extrudeDeclarator,
variableDeclarator,
firstTag,
parameters
)
pathToEdgeTreatmentNodes.push(pathToEdgeTreatmentNode)
} else if (extrudeDeclarator.init.type === 'PipeExpression') {
} else if (variableDeclarator.init.type === 'PipeExpression') {
// 2. case when edge treatment exists or extrude in sketch pipe
// mutate the extrude node with the new edge treatment call
extrudeDeclarator.init.body.push(edgeTreatmentCall)
variableDeclarator.init.body.push(edgeTreatmentCall)
// get path to the edge treatment node
pathToEdgeTreatmentNode = getPathToNodeOfEdgeTreatmentLiteral(
pathToExtrudeNode,
extrudeDeclarator,
variableDeclarator,
firstTag,
parameters
)

View File

@ -1,7 +1,7 @@
import type { Node } from '@rust/kcl-lib/bindings/Node'
import type { PathToNode, Program } from '@src/lang/wasm'
import { locateExtrudeDeclarator } from '@src/lang/queryAst'
import { locateVariableWithCallOrPipe } from '@src/lang/queryAst'
import { err } from '@src/lib/trap'
export function deleteNodeInExtrudePipe(
@ -13,14 +13,14 @@ export function deleteNodeInExtrudePipe(
return new Error("Couldn't find node to delete in ast")
}
const lookup = locateExtrudeDeclarator(ast, node)
const lookup = locateVariableWithCallOrPipe(ast, node)
if (err(lookup)) {
return lookup
}
if (lookup.extrudeDeclarator.init.type !== 'PipeExpression') {
if (lookup.variableDeclarator.init.type !== 'PipeExpression') {
return new Error("Couldn't find node to delete in looked up extrusion")
}
lookup.extrudeDeclarator.init.body.splice(node[pipeIndex][0], 1)
lookup.variableDeclarator.init.body.splice(node[pipeIndex][0], 1)
}

View File

@ -7,7 +7,7 @@ import {
createPipeExpression,
createPipeSubstitution,
} from '@src/lang/create'
import { locateExtrudeDeclarator } from '@src/lang/queryAst'
import { locateVariableWithCallOrPipe } from '@src/lang/queryAst'
import type { PathToNode, Program } from '@src/lang/wasm'
import { COMMAND_APPEARANCE_COLOR_DEFAULT } from '@src/lib/commandBarConfigs/modelingCommandConfig'
import { err } from '@src/lib/trap'
@ -24,12 +24,12 @@ export function setAppearance({
const modifiedAst = structuredClone(ast)
// Locate the call (not necessarily an extrude here)
const result = locateExtrudeDeclarator(modifiedAst, nodeToEdit)
const result = locateVariableWithCallOrPipe(modifiedAst, nodeToEdit)
if (err(result)) {
return result
}
const declarator = result.extrudeDeclarator
const declarator = result.variableDeclarator
const call = createCallExpressionStdLibKw(
'appearance',
createPipeSubstitution(),

View File

@ -1175,36 +1175,36 @@ export function getSketchSelectionsFromOperation(
}
}
export function locateExtrudeDeclarator(
node: Program,
pathToExtrudeNode: PathToNode
): { extrudeDeclarator: VariableDeclarator; shallowPath: PathToNode } | Error {
const nodeOfExtrudeCall = getNodeFromPath<VariableDeclaration>(
node,
pathToExtrudeNode,
export function locateVariableWithCallOrPipe(
ast: Program,
pathToNode: PathToNode
): { variableDeclarator: VariableDeclarator; shallowPath: PathToNode } | Error {
const variableDeclarationNode = getNodeFromPath<VariableDeclaration>(
ast,
pathToNode,
'VariableDeclaration'
)
if (err(nodeOfExtrudeCall)) return nodeOfExtrudeCall
if (err(variableDeclarationNode)) return variableDeclarationNode
const { node: extrudeVarDecl } = nodeOfExtrudeCall
const extrudeDeclarator = extrudeVarDecl.declaration
if (!extrudeDeclarator) {
return new Error('Extrude Declarator not found.')
const { node: variableDecl } = variableDeclarationNode
const variableDeclarator = variableDecl.declaration
if (!variableDeclarator) {
return new Error('Variable Declarator not found.')
}
const extrudeInit = extrudeDeclarator?.init
if (!extrudeInit) {
return new Error('Extrude Init not found.')
const initializer = variableDeclarator?.init
if (!initializer) {
return new Error('Initializer not found.')
}
if (
extrudeInit.type !== 'CallExpressionKw' &&
extrudeInit.type !== 'PipeExpression'
initializer.type !== 'CallExpressionKw' &&
initializer.type !== 'PipeExpression'
) {
return new Error('Extrude must be a PipeExpression or CallExpressionKw')
return new Error('Initializer must be a PipeExpression or CallExpressionKw')
}
return { extrudeDeclarator, shallowPath: nodeOfExtrudeCall.shallowPath }
return { variableDeclarator, shallowPath: variableDeclarationNode.shallowPath }
}
export function findImportNodeAndAlias(