2023-10-16 21:20:05 +11:00
|
|
|
import { toolTips } from '../../useStore'
|
2024-06-04 16:29:20 +10:00
|
|
|
import { Selection, Selections } from 'lib/selections'
|
|
|
|
import { PathToNode, Program, Value } from '../../lang/wasm'
|
2023-03-22 03:02:37 +11:00
|
|
|
import {
|
|
|
|
getNodePathFromSourceRange,
|
|
|
|
getNodeFromPath,
|
|
|
|
} from '../../lang/queryAst'
|
|
|
|
import {
|
2023-10-16 08:54:38 +11:00
|
|
|
PathToNodeMap,
|
2023-03-22 03:02:37 +11:00
|
|
|
getRemoveConstraintsTransforms,
|
|
|
|
transformAstSketchLines,
|
|
|
|
} from '../../lang/std/sketchcombos'
|
2024-03-22 16:55:30 +11:00
|
|
|
import { kclManager } from 'lib/singletons'
|
2023-03-22 03:02:37 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
export function removeConstrainingValuesInfo({
|
|
|
|
selectionRanges,
|
2024-06-04 16:29:20 +10:00
|
|
|
pathToNodes,
|
2023-10-16 08:54:38 +11:00
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
2024-06-04 16:29:20 +10:00
|
|
|
pathToNodes?: Array<PathToNode>
|
2023-10-16 08:54:38 +11:00
|
|
|
}) {
|
2024-06-04 16:29:20 +10:00
|
|
|
const paths =
|
|
|
|
pathToNodes ||
|
|
|
|
selectionRanges.codeBasedSelections.map(({ range }) =>
|
|
|
|
getNodePathFromSourceRange(kclManager.ast, range)
|
|
|
|
)
|
2023-10-16 08:54:38 +11:00
|
|
|
const nodes = paths.map(
|
|
|
|
(pathToNode) => getNodeFromPath<Value>(kclManager.ast, pathToNode).node
|
|
|
|
)
|
2024-06-04 16:29:20 +10:00
|
|
|
const updatedSelectionRanges = pathToNodes
|
|
|
|
? {
|
|
|
|
otherSelections: [],
|
|
|
|
codeBasedSelections: nodes.map(
|
|
|
|
(node): Selection => ({
|
|
|
|
range: [node.start, node.end],
|
|
|
|
type: 'default',
|
|
|
|
})
|
|
|
|
),
|
|
|
|
}
|
|
|
|
: selectionRanges
|
2023-10-16 08:54:38 +11:00
|
|
|
const isAllTooltips = nodes.every(
|
|
|
|
(node) =>
|
|
|
|
node?.type === 'CallExpression' &&
|
|
|
|
toolTips.includes(node.callee.name as any)
|
|
|
|
)
|
2023-03-22 03:02:37 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
try {
|
|
|
|
const transforms = getRemoveConstraintsTransforms(
|
2024-06-04 16:29:20 +10:00
|
|
|
updatedSelectionRanges,
|
2023-10-16 08:54:38 +11:00
|
|
|
kclManager.ast,
|
|
|
|
'removeConstrainingValues'
|
|
|
|
)
|
2023-03-22 03:02:37 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
const enabled = isAllTooltips && transforms.every(Boolean)
|
2024-06-04 16:29:20 +10:00
|
|
|
return { enabled, transforms, updatedSelectionRanges }
|
2023-10-16 08:54:38 +11:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
2024-06-04 16:29:20 +10:00
|
|
|
return { enabled: false, transforms: [], updatedSelectionRanges }
|
2023-10-16 08:54:38 +11:00
|
|
|
}
|
|
|
|
}
|
2023-03-22 03:02:37 +11:00
|
|
|
|
2023-10-16 08:54:38 +11:00
|
|
|
export function applyRemoveConstrainingValues({
|
|
|
|
selectionRanges,
|
2024-06-04 16:29:20 +10:00
|
|
|
pathToNodes,
|
2023-10-16 08:54:38 +11:00
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
2024-06-04 16:29:20 +10:00
|
|
|
pathToNodes?: Array<PathToNode>
|
2023-10-16 08:54:38 +11:00
|
|
|
}): {
|
|
|
|
modifiedAst: Program
|
|
|
|
pathToNodeMap: PathToNodeMap
|
|
|
|
} {
|
2024-06-04 16:29:20 +10:00
|
|
|
const { transforms, updatedSelectionRanges } = removeConstrainingValuesInfo({
|
|
|
|
selectionRanges,
|
|
|
|
pathToNodes,
|
|
|
|
})
|
2023-10-16 08:54:38 +11:00
|
|
|
return transformAstSketchLines({
|
|
|
|
ast: kclManager.ast,
|
2024-06-04 16:29:20 +10:00
|
|
|
selectionRanges: updatedSelectionRanges,
|
2023-10-16 08:54:38 +11:00
|
|
|
transformInfos: transforms,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
referenceSegName: '',
|
|
|
|
})
|
2023-03-22 03:02:37 +11:00
|
|
|
}
|