* handle if graphSelections is empty * fix removeConstrainingValuesInfo by using pathToNodes if available instead of selectionRanges: current selection should not be required to remove constraints * selectionRanges not needed for removeConstrainingValuesInfo anymore * fix remove constraint unit test: pass line's pathToNode instead of argument to remove constraint
This commit is contained in:
@ -15,21 +15,15 @@ import type { Selection, Selections } from '@src/lib/selections'
|
||||
import { kclManager } from '@src/lib/singletons'
|
||||
import { err } from '@src/lib/trap'
|
||||
|
||||
export function removeConstrainingValuesInfo({
|
||||
selectionRanges,
|
||||
pathToNodes,
|
||||
}: {
|
||||
selectionRanges: Selections
|
||||
pathToNodes?: Array<PathToNode>
|
||||
}):
|
||||
export function removeConstrainingValuesInfo(pathToNodes: Array<PathToNode>):
|
||||
| {
|
||||
transforms: TransformInfo[]
|
||||
enabled: boolean
|
||||
updatedSelectionRanges: Selections
|
||||
}
|
||||
| Error {
|
||||
const _nodes = selectionRanges.graphSelections.map(({ codeRef }) => {
|
||||
const tmp = getNodeFromPath<Expr>(kclManager.ast, codeRef.pathToNode)
|
||||
const _nodes = pathToNodes.map((pathToNode) => {
|
||||
const tmp = getNodeFromPath<Expr>(kclManager.ast, pathToNode)
|
||||
if (tmp instanceof Error) return tmp
|
||||
return tmp.node
|
||||
})
|
||||
@ -37,8 +31,7 @@ export function removeConstrainingValuesInfo({
|
||||
if (err(_err1)) return _err1
|
||||
const nodes = _nodes as Expr[]
|
||||
|
||||
const updatedSelectionRanges = pathToNodes
|
||||
? {
|
||||
const updatedSelectionRanges = {
|
||||
otherSelections: [],
|
||||
graphSelections: nodes.map(
|
||||
(node): Selection => ({
|
||||
@ -49,7 +42,6 @@ export function removeConstrainingValuesInfo({
|
||||
})
|
||||
),
|
||||
}
|
||||
: selectionRanges
|
||||
const isAllTooltips = nodes.every(
|
||||
(node) =>
|
||||
node?.type === 'CallExpressionKw' &&
|
||||
@ -78,10 +70,12 @@ export function applyRemoveConstrainingValues({
|
||||
pathToNodeMap: PathToNodeMap
|
||||
}
|
||||
| Error {
|
||||
const constraint = removeConstrainingValuesInfo({
|
||||
selectionRanges,
|
||||
pathToNodes,
|
||||
pathToNodes =
|
||||
pathToNodes ||
|
||||
selectionRanges.graphSelections.map(({ codeRef }) => {
|
||||
return codeRef.pathToNode
|
||||
})
|
||||
const constraint = removeConstrainingValuesInfo(pathToNodes)
|
||||
if (err(constraint)) return constraint
|
||||
const { transforms, updatedSelectionRanges } = constraint
|
||||
|
||||
|
@ -333,6 +333,15 @@ export default class EditorManager {
|
||||
|
||||
createEditorSelection(selections: Selections) {
|
||||
let codeBasedSelections = []
|
||||
|
||||
// Handle empty graphSelections array to avoid runtime errors
|
||||
if (selections.graphSelections.length === 0) {
|
||||
const defaultCursor = EditorSelection.cursor(
|
||||
this._editorView?.state.doc.length || 0
|
||||
)
|
||||
return EditorSelection.create([defaultCursor], 0)
|
||||
}
|
||||
|
||||
for (const selection of selections.graphSelections) {
|
||||
const safeEnd = Math.min(
|
||||
selection.codeRef.range[1],
|
||||
|
@ -1252,18 +1252,11 @@ p3 = [342.51, 216.38],
|
||||
if (err(callExp)) {
|
||||
throw new Error('Failed to get CallExpressionKw node')
|
||||
}
|
||||
const constraintInfo = getConstraintInfoKw(
|
||||
callExp.node,
|
||||
codeManager.code,
|
||||
artifact.codeRef.pathToNode,
|
||||
filter
|
||||
)
|
||||
const constraint = constraintInfo[constraintIndex]
|
||||
|
||||
// Now that we're in sketchIdle state, test the "Constrain with named value" event
|
||||
actor.send({
|
||||
type: 'Constrain remove constraints',
|
||||
data: constraint.pathToNode,
|
||||
data: artifact.codeRef.pathToNode,
|
||||
})
|
||||
|
||||
// Wait for the state to change in response to the constraint
|
||||
|
@ -752,10 +752,13 @@ export const modelingMachine = setup({
|
||||
event,
|
||||
}) => {
|
||||
if (event.type !== 'Constrain remove constraints') return false
|
||||
const info = removeConstrainingValuesInfo({
|
||||
selectionRanges,
|
||||
pathToNodes: event.data && [event.data],
|
||||
|
||||
const pathToNodes = event.data
|
||||
? [event.data]
|
||||
: selectionRanges.graphSelections.map(({ codeRef }) => {
|
||||
return codeRef.pathToNode
|
||||
})
|
||||
const info = removeConstrainingValuesInfo(pathToNodes)
|
||||
if (err(info)) return false
|
||||
return info.enabled
|
||||
},
|
||||
|
Reference in New Issue
Block a user