From 7848d6317795c76f2aae780e9b98625c76b30d61 Mon Sep 17 00:00:00 2001 From: max Date: Mon, 23 Sep 2024 08:07:31 +0200 Subject: [PATCH] add multiple selections support for focusPath (#3944) --- src/lang/KclSingleton.ts | 45 +++++++++++++++++++-------------- src/lang/modifyAst/addFillet.ts | 13 +++++----- src/machines/modelingMachine.ts | 4 +-- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/lang/KclSingleton.ts b/src/lang/KclSingleton.ts index ec8f5a28a1..57c7ee613f 100644 --- a/src/lang/KclSingleton.ts +++ b/src/lang/KclSingleton.ts @@ -416,7 +416,7 @@ export class KclManager { ast: Program, execute: boolean, optionalParams?: { - focusPath?: PathToNode + focusPath?: Array zoomToFit?: boolean zoomOnRangeAndType?: { range: SourceRange @@ -435,27 +435,34 @@ export class KclManager { let returnVal: Selections | undefined = undefined if (optionalParams?.focusPath) { - const _node1 = getNodeFromPath( - astWithUpdatedSource, - optionalParams?.focusPath - ) - if (err(_node1)) return Promise.reject(_node1) - const { node } = _node1 - - const { start, end } = node - if (!start || !end) - return { - selections: undefined, - newAst: astWithUpdatedSource, - } returnVal = { - codeBasedSelections: [ - { + codeBasedSelections: [], + otherSelections: [], + } + + for (const path of optionalParams.focusPath) { + const getNodeFromPathResult = getNodeFromPath( + astWithUpdatedSource, + path + ) + if (err(getNodeFromPathResult)) + return Promise.reject(getNodeFromPathResult) + const { node } = getNodeFromPathResult + + const { start, end } = node + + if (!start || !end) + return { + selections: undefined, + newAst: astWithUpdatedSource, + } + + if (start && end) { + returnVal.codeBasedSelections.push({ type: 'default', range: [start, end], - }, - ], - otherSelections: [], + }) + } } } diff --git a/src/lang/modifyAst/addFillet.ts b/src/lang/modifyAst/addFillet.ts index 2f0218c84c..6d2a4edf25 100644 --- a/src/lang/modifyAst/addFillet.ts +++ b/src/lang/modifyAst/addFillet.ts @@ -65,7 +65,7 @@ export function modifyAstWithFilletAndTag( ast: Program, selection: Selections, radius: KclCommandValue -): { modifiedAst: Program; pathToFilletNode: PathToNode } | Error { +): { modifiedAst: Program; pathToFilletNode: Array } | Error { const astResult = insertRadiusIntoAst(ast, radius) if (err(astResult)) return astResult @@ -73,7 +73,8 @@ export function modifyAstWithFilletAndTag( const artifactGraph = engineCommandManager.artifactGraph let clonedAst = structuredClone(ast) - let lastPathToFilletNode: PathToNode = [] + const clonedAstForGetExtrude = structuredClone(ast) + let pathToFilletNodes: Array = [] for (const selectionRange of selection.codeBasedSelections) { const singleSelection = { @@ -82,7 +83,7 @@ export function modifyAstWithFilletAndTag( } const getPathToExtrudeForSegmentSelectionResult = getPathToExtrudeForSegmentSelection( - clonedAst, + clonedAstForGetExtrude, singleSelection, programMemory, artifactGraph @@ -101,9 +102,9 @@ export function modifyAstWithFilletAndTag( if (trap(addFilletResult)) return addFilletResult const { modifiedAst, pathToFilletNode } = addFilletResult clonedAst = modifiedAst - lastPathToFilletNode = pathToFilletNode + pathToFilletNodes.push(pathToFilletNode) } - return { modifiedAst: clonedAst, pathToFilletNode: lastPathToFilletNode } + return { modifiedAst: clonedAst, pathToFilletNode: pathToFilletNodes } } function insertRadiusIntoAst( @@ -166,7 +167,7 @@ export function getPathToExtrudeForSegmentSelection( async function updateAstAndFocus( modifiedAst: Program, - pathToFilletNode: PathToNode + pathToFilletNode: Array ) { const updatedAst = await kclManager.updateAst(modifiedAst, true, { focusPath: pathToFilletNode, diff --git a/src/machines/modelingMachine.ts b/src/machines/modelingMachine.ts index 172c329de6..2341c02caa 100644 --- a/src/machines/modelingMachine.ts +++ b/src/machines/modelingMachine.ts @@ -555,7 +555,7 @@ export const modelingMachine = setup({ store.videoElement?.pause() const updatedAst = await kclManager.updateAst(modifiedAst, true, { - focusPath: pathToExtrudeArg, + focusPath: [pathToExtrudeArg], zoomToFit: true, zoomOnRangeAndType: { range: selection.codeBasedSelections[0].range, @@ -602,7 +602,7 @@ export const modelingMachine = setup({ store.videoElement?.pause() const updatedAst = await kclManager.updateAst(modifiedAst, true, { - focusPath: pathToRevolveArg, + focusPath: [pathToRevolveArg], zoomToFit: true, zoomOnRangeAndType: { range: selection.codeBasedSelections[0].range,