2023-10-16 08:54:38 +11:00
|
|
|
import { Selections, toolTips } from '../../useStore'
|
|
|
|
import { Program, Value, VariableDeclarator } from '../../lang/wasm'
|
2023-03-02 21:19:11 +11:00
|
|
|
import {
|
|
|
|
getNodePathFromSourceRange,
|
|
|
|
getNodeFromPath,
|
2023-03-03 20:35:48 +11:00
|
|
|
} from '../../lang/queryAst'
|
2023-03-02 21:19:11 +11:00
|
|
|
import { isSketchVariablesLinked } from '../../lang/std/sketchConstraints'
|
|
|
|
import {
|
2023-03-07 15:45:59 +11:00
|
|
|
transformSecondarySketchLinesTagFirst,
|
2023-03-02 21:19:11 +11:00
|
|
|
getTransformInfos,
|
2023-10-16 08:54:38 +11:00
|
|
|
PathToNodeMap,
|
2023-03-02 21:19:11 +11:00
|
|
|
} from '../../lang/std/sketchcombos'
|
2023-10-11 13:36:54 +11:00
|
|
|
import { kclManager } from 'lang/KclSinglton'
|
2023-03-02 21:19:11 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
export function equalAngleInfo({
|
|
|
|
selectionRanges,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
}) {
|
|
|
|
const paths = selectionRanges.codeBasedSelections.map(({ range }) =>
|
|
|
|
getNodePathFromSourceRange(kclManager.ast, range)
|
|
|
|
)
|
|
|
|
const nodes = paths.map(
|
|
|
|
(pathToNode) => getNodeFromPath<Value>(kclManager.ast, pathToNode).node
|
|
|
|
)
|
|
|
|
const varDecs = paths.map(
|
|
|
|
(pathToNode) =>
|
|
|
|
getNodeFromPath<VariableDeclarator>(
|
|
|
|
kclManager.ast,
|
|
|
|
pathToNode,
|
|
|
|
'VariableDeclarator'
|
|
|
|
)?.node
|
|
|
|
)
|
|
|
|
const primaryLine = varDecs[0]
|
|
|
|
const secondaryVarDecs = varDecs.slice(1)
|
|
|
|
const isOthersLinkedToPrimary = secondaryVarDecs.every((secondary) =>
|
|
|
|
isSketchVariablesLinked(secondary, primaryLine, kclManager.ast)
|
|
|
|
)
|
|
|
|
const isAllTooltips = nodes.every(
|
|
|
|
(node) =>
|
|
|
|
node?.type === 'CallExpression' &&
|
|
|
|
toolTips.includes(node.callee.name as any)
|
|
|
|
)
|
2023-03-02 21:19:11 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
const transforms = getTransformInfos(
|
|
|
|
{
|
|
|
|
...selectionRanges,
|
|
|
|
codeBasedSelections: selectionRanges.codeBasedSelections.slice(1),
|
|
|
|
},
|
|
|
|
kclManager.ast,
|
|
|
|
'equalAngle'
|
|
|
|
)
|
2023-03-02 21:19:11 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
const enabled =
|
|
|
|
!!secondaryVarDecs.length &&
|
|
|
|
isAllTooltips &&
|
|
|
|
isOthersLinkedToPrimary &&
|
|
|
|
transforms.every(Boolean)
|
|
|
|
return { enabled, transforms }
|
|
|
|
}
|
2023-03-02 21:19:11 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
export function applyConstraintEqualAngle({
|
|
|
|
selectionRanges,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
}): {
|
|
|
|
modifiedAst: Program
|
|
|
|
pathToNodeMap: PathToNodeMap
|
|
|
|
} {
|
|
|
|
const { transforms } = equalAngleInfo({ selectionRanges })
|
|
|
|
const { modifiedAst, pathToNodeMap } = transformSecondarySketchLinesTagFirst({
|
|
|
|
ast: kclManager.ast,
|
|
|
|
selectionRanges,
|
|
|
|
transformInfos: transforms,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
})
|
|
|
|
return { modifiedAst, pathToNodeMap }
|
2023-03-02 21:19:11 +11:00
|
|
|
}
|