Skip to content

Commit

Permalink
add multiple selections support for focusPath (#3944)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mrgrsk authored Sep 23, 2024
1 parent 619b059 commit 7848d63
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
45 changes: 26 additions & 19 deletions src/lang/KclSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export class KclManager {
ast: Program,
execute: boolean,
optionalParams?: {
focusPath?: PathToNode
focusPath?: Array<PathToNode>
zoomToFit?: boolean
zoomOnRangeAndType?: {
range: SourceRange
Expand All @@ -435,27 +435,34 @@ export class KclManager {
let returnVal: Selections | undefined = undefined

if (optionalParams?.focusPath) {
const _node1 = getNodeFromPath<any>(
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<any>(
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: [],
})
}
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/lang/modifyAst/addFillet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@ export function modifyAstWithFilletAndTag(
ast: Program,
selection: Selections,
radius: KclCommandValue
): { modifiedAst: Program; pathToFilletNode: PathToNode } | Error {
): { modifiedAst: Program; pathToFilletNode: Array<PathToNode> } | Error {
const astResult = insertRadiusIntoAst(ast, radius)
if (err(astResult)) return astResult

const programMemory = kclManager.programMemory
const artifactGraph = engineCommandManager.artifactGraph

let clonedAst = structuredClone(ast)
let lastPathToFilletNode: PathToNode = []
const clonedAstForGetExtrude = structuredClone(ast)
let pathToFilletNodes: Array<PathToNode> = []

for (const selectionRange of selection.codeBasedSelections) {
const singleSelection = {
Expand All @@ -82,7 +83,7 @@ export function modifyAstWithFilletAndTag(
}
const getPathToExtrudeForSegmentSelectionResult =
getPathToExtrudeForSegmentSelection(
clonedAst,
clonedAstForGetExtrude,
singleSelection,
programMemory,
artifactGraph
Expand All @@ -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(
Expand Down Expand Up @@ -166,7 +167,7 @@ export function getPathToExtrudeForSegmentSelection(

async function updateAstAndFocus(
modifiedAst: Program,
pathToFilletNode: PathToNode
pathToFilletNode: Array<PathToNode>
) {
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
focusPath: pathToFilletNode,
Expand Down
4 changes: 2 additions & 2 deletions src/machines/modelingMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7848d63

Please sign in to comment.