Cursors should stay after a code-mod (#113)

* setup to get path to nodes back from ast-mods

* fix cursor setting for constraint buttons that use transformSecondarySketchLinesTagFirst

* fix cursors for constraints that use transformAstSketchLines
This commit is contained in:
Kurt Hutten
2023-04-14 07:49:36 +10:00
committed by GitHub
parent 2fc68e7c82
commit 15699361a0
17 changed files with 410 additions and 312 deletions

View File

@ -93,7 +93,7 @@ export const Toolbar = () => {
ast,
pathToNode
)
updateAst(modifiedAst, pathToExtrudeArg)
updateAst(modifiedAst, { focusPath: pathToExtrudeArg })
}}
className="border m-1 px-1 rounded text-xs"
>
@ -111,7 +111,7 @@ export const Toolbar = () => {
pathToNode,
false
)
updateAst(modifiedAst, pathToExtrudeArg)
updateAst(modifiedAst, { focusPath: pathToExtrudeArg })
}}
className="border m-1 px-1 rounded text-xs"
>

View File

@ -11,17 +11,18 @@ import {
transformSecondarySketchLinesTagFirst,
getTransformInfos,
} from '../../lang/std/sketchcombos'
import { updateCursors } from '../../lang/util'
export const EqualAngle = () => {
const { guiMode, selectionRanges, ast, programMemory, updateAst } = useStore(
(s) => ({
const { guiMode, selectionRanges, ast, programMemory, updateAst, setCursor } =
useStore((s) => ({
guiMode: s.guiMode,
ast: s.ast,
updateAst: s.updateAst,
selectionRanges: s.selectionRanges,
programMemory: s.programMemory,
})
)
setCursor: s.setCursor,
}))
const [enableEqual, setEnableEqual] = useState(false)
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
useEffect(() => {
@ -72,18 +73,19 @@ export const EqualAngle = () => {
return (
<button
onClick={() =>
transformInfos &&
ast &&
updateAst(
onClick={async () => {
if (!(transformInfos && ast)) return
const { modifiedAst, pathToNodeMap } =
transformSecondarySketchLinesTagFirst({
ast,
selectionRanges,
transformInfos,
programMemory,
})?.modifiedAst
)
}
})
updateAst(modifiedAst, {
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
})
}}
className={`border m-1 px-1 rounded text-xs ${
enableEqual ? 'bg-gray-50 text-gray-800' : 'bg-gray-200 text-gray-400'
}`}

View File

@ -11,17 +11,18 @@ import {
transformSecondarySketchLinesTagFirst,
getTransformInfos,
} from '../../lang/std/sketchcombos'
import { updateCursors } from '../../lang/util'
export const EqualLength = () => {
const { guiMode, selectionRanges, ast, programMemory, updateAst } = useStore(
(s) => ({
const { guiMode, selectionRanges, ast, programMemory, updateAst, setCursor } =
useStore((s) => ({
guiMode: s.guiMode,
ast: s.ast,
updateAst: s.updateAst,
selectionRanges: s.selectionRanges,
programMemory: s.programMemory,
})
)
setCursor: s.setCursor,
}))
const [enableEqual, setEnableEqual] = useState(false)
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
useEffect(() => {
@ -72,18 +73,19 @@ export const EqualLength = () => {
return (
<button
onClick={() =>
transformInfos &&
ast &&
updateAst(
onClick={() => {
if (!(transformInfos && ast)) return
const { modifiedAst, pathToNodeMap } =
transformSecondarySketchLinesTagFirst({
ast,
selectionRanges,
transformInfos,
programMemory,
})?.modifiedAst
)
}
})
updateAst(modifiedAst, {
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
})
}}
className={`border m-1 px-1 rounded text-xs ${
enableEqual ? 'bg-gray-50 text-gray-800' : 'bg-gray-200 text-gray-400'
}`}

View File

@ -10,21 +10,22 @@ import {
getTransformInfos,
transformAstSketchLines,
} from '../../lang/std/sketchcombos'
import { updateCursors } from '../../lang/util'
export const HorzVert = ({
horOrVert,
}: {
horOrVert: 'vertical' | 'horizontal'
}) => {
const { guiMode, selectionRanges, ast, programMemory, updateAst } = useStore(
(s) => ({
const { guiMode, selectionRanges, ast, programMemory, updateAst, setCursor } =
useStore((s) => ({
guiMode: s.guiMode,
ast: s.ast,
updateAst: s.updateAst,
selectionRanges: s.selectionRanges,
programMemory: s.programMemory,
})
)
setCursor: s.setCursor,
}))
const [enableHorz, setEnableHorz] = useState(false)
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
useEffect(() => {
@ -51,19 +52,19 @@ export const HorzVert = ({
return (
<button
onClick={() =>
transformInfos &&
ast &&
updateAst(
transformAstSketchLines({
onClick={() => {
if (!transformInfos || !ast) return
const { modifiedAst, pathToNodeMap } = transformAstSketchLines({
ast,
selectionRanges,
transformInfos,
programMemory,
referenceSegName: '',
})?.modifiedAst
)
}
})
updateAst(modifiedAst, {
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
})
}}
className={`border m-1 px-1 rounded text-xs ${
enableHorz ? 'bg-gray-50 text-gray-800' : 'bg-gray-200 text-gray-400'
}`}

View File

@ -20,19 +20,20 @@ import {
import { GetInfoModal } from '../SetHorVertDistanceModal'
import { createVariableDeclaration } from '../../lang/modifyAst'
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
import { updateCursors } from '../../lang/util'
const getModalInfo = create(GetInfoModal as any)
export const Intersect = () => {
const { guiMode, selectionRanges, ast, programMemory, updateAst } = useStore(
(s) => ({
const { guiMode, selectionRanges, ast, programMemory, updateAst, setCursor } =
useStore((s) => ({
guiMode: s.guiMode,
ast: s.ast,
updateAst: s.updateAst,
selectionRanges: s.selectionRanges,
programMemory: s.programMemory,
})
)
setCursor: s.setCursor,
}))
const [enable, setEnable] = useState(false)
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
const [forecdSelectionRanges, setForcedSelectionRanges] =
@ -125,8 +126,8 @@ export const Intersect = () => {
return (
<button
onClick={async () => {
if (transformInfos && ast && forecdSelectionRanges) {
const { modifiedAst, tagInfo, valueUsedInTransform } =
if (!(transformInfos && ast && forecdSelectionRanges)) return
const { modifiedAst, tagInfo, valueUsedInTransform, pathToNodeMap } =
transformSecondarySketchLinesTagFirst({
ast: JSON.parse(JSON.stringify(ast)),
selectionRanges: forecdSelectionRanges,
@ -154,7 +155,9 @@ export const Intersect = () => {
initialVariableName: 'offset',
} as any)
if (segName === tagInfo?.tag && value === valueUsedInTransform) {
updateAst(modifiedAst)
updateAst(modifiedAst, {
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
})
} else {
// transform again but forcing certain values
const finalValue = removeDoubleNegatives(
@ -162,7 +165,7 @@ export const Intersect = () => {
sign,
variableName
)
const { modifiedAst: _modifiedAst } =
const { modifiedAst: _modifiedAst, pathToNodeMap } =
transformSecondarySketchLinesTagFirst({
ast,
selectionRanges: forecdSelectionRanges,
@ -180,8 +183,9 @@ export const Intersect = () => {
)
_modifiedAst.body = newBody
}
updateAst(_modifiedAst)
}
updateAst(_modifiedAst, {
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
})
}
}}
className={`border m-1 px-1 rounded text-xs ${

View File

@ -10,17 +10,18 @@ import {
getRemoveConstraintsTransforms,
transformAstSketchLines,
} from '../../lang/std/sketchcombos'
import { updateCursors } from '../../lang/util'
export const RemoveConstrainingValues = () => {
const { guiMode, selectionRanges, ast, programMemory, updateAst } = useStore(
(s) => ({
const { guiMode, selectionRanges, ast, programMemory, updateAst, setCursor } =
useStore((s) => ({
guiMode: s.guiMode,
ast: s.ast,
updateAst: s.updateAst,
selectionRanges: s.selectionRanges,
programMemory: s.programMemory,
})
)
setCursor: s.setCursor,
}))
const [enableHorz, setEnableHorz] = useState(false)
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
useEffect(() => {
@ -55,19 +56,19 @@ export const RemoveConstrainingValues = () => {
return (
<button
onClick={() =>
transformInfos &&
ast &&
updateAst(
transformAstSketchLines({
onClick={() => {
if (!transformInfos || !ast) return
const { modifiedAst, pathToNodeMap } = transformAstSketchLines({
ast,
selectionRanges,
transformInfos,
programMemory,
referenceSegName: '',
})?.modifiedAst
)
}
})
updateAst(modifiedAst, {
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
})
}}
className={`border m-1 px-1 rounded text-xs ${
enableHorz ? 'bg-gray-50 text-gray-800' : 'bg-gray-200 text-gray-400'
}`}

View File

@ -18,6 +18,7 @@ import {
createVariableDeclaration,
} from '../../lang/modifyAst'
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
import { updateCursors } from '../../lang/util'
const getModalInfo = create(SetAngleLengthModal as any)
@ -26,18 +27,14 @@ export const SetAbsDistance = ({
}: {
buttonType: 'xAbs' | 'yAbs' | 'snapToYAxis' | 'snapToXAxis'
}) => {
const {
guiMode,
selectionRanges: selections,
ast,
programMemory,
updateAst,
} = useStore((s) => ({
const { guiMode, selectionRanges, ast, programMemory, updateAst, setCursor } =
useStore((s) => ({
guiMode: s.guiMode,
ast: s.ast,
updateAst: s.updateAst,
selectionRanges: s.selectionRanges,
programMemory: s.programMemory,
setCursor: s.setCursor,
}))
const disType: ConstraintType =
buttonType === 'xAbs' || buttonType === 'yAbs'
@ -49,7 +46,7 @@ export const SetAbsDistance = ({
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
useEffect(() => {
if (!ast) return
const paths = selections.codeBasedSelections.map(({ range }) =>
const paths = selectionRanges.codeBasedSelections.map(({ range }) =>
getNodePathFromSourceRange(ast, range)
)
const nodes = paths.map(
@ -62,25 +59,25 @@ export const SetAbsDistance = ({
toolTips.includes(node.callee.name as any)
)
const theTransforms = getTransformInfos(selections, ast, disType)
const theTransforms = getTransformInfos(selectionRanges, ast, disType)
setTransformInfos(theTransforms)
const enableY =
disType === 'yAbs' &&
selections.otherSelections.length === 1 &&
selections.otherSelections[0] === 'x-axis' // select the x axis to set the distance from it i.e. y
selectionRanges.otherSelections.length === 1 &&
selectionRanges.otherSelections[0] === 'x-axis' // select the x axis to set the distance from it i.e. y
const enableX =
disType === 'xAbs' &&
selections.otherSelections.length === 1 &&
selections.otherSelections[0] === 'y-axis' // select the y axis to set the distance from it i.e. x
selectionRanges.otherSelections.length === 1 &&
selectionRanges.otherSelections[0] === 'y-axis' // select the y axis to set the distance from it i.e. x
const _enableHorz =
isAllTooltips &&
theTransforms.every(Boolean) &&
selections.codeBasedSelections.length === 1 &&
selectionRanges.codeBasedSelections.length === 1 &&
(enableX || enableY)
setEnableAngLen(_enableHorz)
}, [guiMode, selections])
}, [guiMode, selectionRanges])
if (guiMode.mode !== 'sketch') return null
const isAlign = buttonType === 'snapToYAxis' || buttonType === 'snapToXAxis'
@ -91,7 +88,7 @@ export const SetAbsDistance = ({
if (!(transformInfos && ast)) return
const { valueUsedInTransform } = transformAstSketchLines({
ast: JSON.parse(JSON.stringify(ast)),
selectionRanges: selections,
selectionRanges: selectionRanges,
transformInfos,
programMemory,
referenceSegName: '',
@ -108,9 +105,10 @@ export const SetAbsDistance = ({
? createIdentifier('_0')
: removeDoubleNegatives(valueNode, sign, variableName)
const { modifiedAst: _modifiedAst } = transformAstSketchLines({
const { modifiedAst: _modifiedAst, pathToNodeMap } =
transformAstSketchLines({
ast: JSON.parse(JSON.stringify(ast)),
selectionRanges: selections,
selectionRanges: selectionRanges,
transformInfos,
programMemory,
referenceSegName: '',
@ -126,7 +124,9 @@ export const SetAbsDistance = ({
_modifiedAst.body = newBody
}
updateAst(_modifiedAst)
updateAst(_modifiedAst, {
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
})
} catch (e) {
console.log('e', e)
}

View File

@ -19,19 +19,20 @@ import {
import { GetInfoModal } from '../SetHorVertDistanceModal'
import { createVariableDeclaration } from '../../lang/modifyAst'
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
import { updateCursors } from '../../lang/util'
const getModalInfo = create(GetInfoModal as any)
export const SetAngleBetween = () => {
const { guiMode, selectionRanges, ast, programMemory, updateAst } = useStore(
(s) => ({
const { guiMode, selectionRanges, ast, programMemory, updateAst, setCursor } =
useStore((s) => ({
guiMode: s.guiMode,
ast: s.ast,
updateAst: s.updateAst,
selectionRanges: s.selectionRanges,
programMemory: s.programMemory,
})
)
setCursor: s.setCursor,
}))
const [enable, setEnable] = useState(false)
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
useEffect(() => {
@ -83,8 +84,8 @@ export const SetAngleBetween = () => {
return (
<button
onClick={async () => {
if (transformInfos && ast) {
const { modifiedAst, tagInfo, valueUsedInTransform } =
if (!(transformInfos && ast)) return
const { modifiedAst, tagInfo, valueUsedInTransform, pathToNodeMap } =
transformSecondarySketchLinesTagFirst({
ast: JSON.parse(JSON.stringify(ast)),
selectionRanges,
@ -112,7 +113,9 @@ export const SetAngleBetween = () => {
initialVariableName: 'angle',
} as any)
if (segName === tagInfo?.tag && value === valueUsedInTransform) {
updateAst(modifiedAst)
updateAst(modifiedAst, {
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
})
} else {
const finalValue = removeDoubleNegatives(
valueNode as BinaryPart,
@ -120,7 +123,7 @@ export const SetAngleBetween = () => {
variableName
)
// transform again but forcing certain values
const { modifiedAst: _modifiedAst } =
const { modifiedAst: _modifiedAst, pathToNodeMap } =
transformSecondarySketchLinesTagFirst({
ast,
selectionRanges,
@ -138,8 +141,9 @@ export const SetAngleBetween = () => {
)
_modifiedAst.body = newBody
}
updateAst(_modifiedAst)
}
updateAst(_modifiedAst, {
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
})
}
}}
className={`border m-1 px-1 rounded text-xs ${

View File

@ -20,6 +20,7 @@ import {
import { GetInfoModal } from '../SetHorVertDistanceModal'
import { createLiteral, createVariableDeclaration } from '../../lang/modifyAst'
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
import { updateCursors } from '../../lang/util'
const getModalInfo = create(GetInfoModal as any)
@ -32,15 +33,15 @@ export const SetHorzVertDistance = ({
| 'alignEndsHorizontally'
| 'alignEndsVertically'
}) => {
const { guiMode, selectionRanges, ast, programMemory, updateAst } = useStore(
(s) => ({
const { guiMode, selectionRanges, ast, programMemory, updateAst, setCursor } =
useStore((s) => ({
guiMode: s.guiMode,
ast: s.ast,
updateAst: s.updateAst,
selectionRanges: s.selectionRanges,
programMemory: s.programMemory,
})
)
setCursor: s.setCursor,
}))
const constraint: ConstraintType =
buttonType === 'setHorzDistance' || buttonType === 'setVertDistance'
? buttonType
@ -105,8 +106,8 @@ export const SetHorzVertDistance = ({
return (
<button
onClick={async () => {
if (transformInfos && ast) {
const { modifiedAst, tagInfo, valueUsedInTransform } =
if (!(transformInfos && ast)) return
const { modifiedAst, tagInfo, valueUsedInTransform, pathToNodeMap } =
transformSecondarySketchLinesTagFirst({
ast: JSON.parse(JSON.stringify(ast)),
selectionRanges,
@ -136,17 +137,15 @@ export const SetHorzVertDistance = ({
constraint === 'setHorzDistance' ? 'xDis' : 'yDis',
} as any))
if (segName === tagInfo?.tag && value === valueUsedInTransform) {
updateAst(modifiedAst)
updateAst(modifiedAst, {
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
})
} else {
let finalValue = isAlign
? createLiteral(0)
: removeDoubleNegatives(
valueNode as BinaryPart,
sign,
variableName
)
: removeDoubleNegatives(valueNode as BinaryPart, sign, variableName)
// transform again but forcing certain values
const { modifiedAst: _modifiedAst } =
const { modifiedAst: _modifiedAst, pathToNodeMap } =
transformSecondarySketchLinesTagFirst({
ast,
selectionRanges,
@ -164,8 +163,9 @@ export const SetHorzVertDistance = ({
)
_modifiedAst.body = newBody
}
updateAst(_modifiedAst)
}
updateAst(_modifiedAst, {
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
})
}
}}
className={`border m-1 px-1 rounded text-xs ${

View File

@ -19,6 +19,7 @@ import {
} from '../../lang/modifyAst'
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
import { normaliseAngle } from '../../lib/utils'
import { updateCursors } from '../../lang/util'
const getModalInfo = create(SetAngleLengthModal as any)
@ -27,15 +28,15 @@ export const SetAngleLength = ({
}: {
angleOrLength: 'setAngle' | 'setLength'
}) => {
const { guiMode, selectionRanges, ast, programMemory, updateAst } = useStore(
(s) => ({
const { guiMode, selectionRanges, ast, programMemory, updateAst, setCursor } =
useStore((s) => ({
guiMode: s.guiMode,
ast: s.ast,
updateAst: s.updateAst,
selectionRanges: s.selectionRanges,
programMemory: s.programMemory,
})
)
setCursor: s.setCursor,
}))
const [enableAngLen, setEnableAngLen] = useState(false)
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
useEffect(() => {
@ -116,7 +117,8 @@ export const SetAngleLength = ({
])
}
const { modifiedAst: _modifiedAst } = transformAstSketchLines({
const { modifiedAst: _modifiedAst, pathToNodeMap } =
transformAstSketchLines({
ast: JSON.parse(JSON.stringify(ast)),
selectionRanges,
transformInfos,
@ -134,7 +136,9 @@ export const SetAngleLength = ({
_modifiedAst.body = newBody
}
updateAst(_modifiedAst)
updateAst(_modifiedAst, {
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
})
} catch (e) {
console.log('e', e)
}

View File

@ -668,7 +668,7 @@ function makeArrayExpression(
expression: ArrayExpression
lastIndex: number
} {
// should be called array opening brace '[' index
// should be called with index to an array opening brace '['
const openingBraceToken = tokens[index]
const firstElementToken = nextMeaningfulToken(tokens, index)
const { elements, lastIndex } = makeArrayElements(

View File

@ -536,10 +536,16 @@ export function giveSketchFnCallTag(
ast: Program,
range: Selection['range'],
tag?: string
): { modifiedAst: Program; tag: string; isTagExisting: boolean } {
): {
modifiedAst: Program
tag: string
isTagExisting: boolean
pathToNode: PathToNode
} {
const path = getNodePathFromSourceRange(ast, range)
const { node: primaryCallExp } = getNodeFromPath<CallExpression>(
ast,
getNodePathFromSourceRange(ast, range),
path,
'CallExpression'
)
const firstArg = getFirstArg(primaryCallExp)
@ -557,6 +563,7 @@ export function giveSketchFnCallTag(
modifiedAst: ast,
tag: tagStr,
isTagExisting,
pathToNode: path,
}
}

View File

@ -10,7 +10,6 @@ import {
VariableDeclaration,
ReturnStatement,
ArrayExpression,
Identifier,
} from './abstractSyntaxTree'
import { createIdentifier, splitPathAtLastIndex } from './modifyAst'
import { getSketchSegmentFromSourceRange } from './std/sketchConstraints'

View File

@ -1392,14 +1392,18 @@ export function replaceSketchLine({
from: [number, number]
createCallback: TransformCallback
referencedSegment?: Path
}): { modifiedAst: Program; valueUsedInTransform?: number } {
}): {
modifiedAst: Program
valueUsedInTransform?: number
pathToNode: PathToNode
} {
if (![...toolTips, 'intersect'].includes(fnName))
throw new Error('not a tooltip')
const _node = { ...node }
const thePath = getNodePathFromSourceRange(_node, sourceRange)
const { add } = sketchLineHelperMap[fnName]
const { modifiedAst, valueUsedInTransform } = add({
const { modifiedAst, valueUsedInTransform, pathToNode } = add({
node: _node,
previousProgramMemory: programMemory,
pathToNode: thePath,
@ -1409,7 +1413,7 @@ export function replaceSketchLine({
replaceExisting: true,
createCallback,
})
return { modifiedAst, valueUsedInTransform }
return { modifiedAst, valueUsedInTransform, pathToNode }
}
export function addTagForSketchOnFace(

View File

@ -25,7 +25,7 @@ import {
giveSketchFnCallTag,
} from '../modifyAst'
import { createFirstArg, getFirstArg, replaceSketchLine } from './sketch'
import { ProgramMemory } from '../executor'
import { PathToNode, ProgramMemory } from '../executor'
import { getSketchSegmentFromSourceRange } from './sketchConstraints'
import { getAngle, roundOff, normaliseAngle } from '../../lib/utils'
@ -785,6 +785,10 @@ const transformMap: TransformMap = {
tooltip: 'angledLine',
createNode: basicAngledLineCreateNode('len', 'ang', 'len'),
},
equalAngle: {
tooltip: 'angledLine',
createNode: basicAngledLineCreateNode('ang', 'len', 'len'),
},
},
},
angledLineOfXLength: {
@ -1319,6 +1323,8 @@ export function getRemoveConstraintsTransforms(
return theTransforms
}
type PathToNodeMap = { [key: number]: PathToNode }
export function transformSecondarySketchLinesTagFirst({
ast,
selectionRanges,
@ -1336,6 +1342,7 @@ export function transformSecondarySketchLinesTagFirst({
}): {
modifiedAst: Program
valueUsedInTransform?: number
pathToNodeMap: PathToNodeMap
tagInfo: {
tag: string
isTagExisting: boolean
@ -1344,14 +1351,13 @@ export function transformSecondarySketchLinesTagFirst({
// let node = JSON.parse(JSON.stringify(ast))
const primarySelection = selectionRanges.codeBasedSelections[0].range
const { modifiedAst, tag, isTagExisting } = giveSketchFnCallTag(
const { modifiedAst, tag, isTagExisting, pathToNode } = giveSketchFnCallTag(
ast,
primarySelection,
forceSegName
)
return {
...transformAstSketchLines({
const result = transformAstSketchLines({
ast: modifiedAst,
selectionRanges: {
...selectionRanges,
@ -1362,7 +1368,13 @@ export function transformSecondarySketchLinesTagFirst({
programMemory,
referenceSegName: tag,
forceValueUsedInTransform,
}),
})
const updatedPathToNodeMap = incrementPathToNodeMap(result.pathToNodeMap)
updatedPathToNodeMap[0] = pathToNode
return {
...result,
pathToNodeMap: updatedPathToNodeMap,
tagInfo: {
tag,
isTagExisting,
@ -1370,6 +1382,17 @@ export function transformSecondarySketchLinesTagFirst({
}
}
function incrementPathToNodeMap(
pathToNodeMap: PathToNodeMap,
increment = 1
): PathToNodeMap {
const newMap: PathToNodeMap = {}
Object.entries(pathToNodeMap).forEach(([key, path]) => {
newMap[Number(key) + increment] = path
})
return newMap
}
export function transformAstSketchLines({
ast,
selectionRanges,
@ -1386,10 +1409,15 @@ export function transformAstSketchLines({
referenceSegName: string
forceValueUsedInTransform?: Value
referencedSegmentRange?: Selection['range']
}): { modifiedAst: Program; valueUsedInTransform?: number } {
}): {
modifiedAst: Program
valueUsedInTransform?: number
pathToNodeMap: PathToNodeMap
} {
// deep clone since we are mutating in a loop, of which any could fail
let node = JSON.parse(JSON.stringify(ast))
let _valueUsedInTransform // TODO should this be an array?
const pathToNodeMap: PathToNodeMap = {}
selectionRanges.codeBasedSelections.forEach(({ range }, index) => {
const callBack = transformInfos?.[index].createNode
@ -1428,7 +1456,8 @@ export function transformAstSketchLines({
.segment
: sketchGroup.value.find((path) => path.name === _referencedSegmentName)
const { to, from } = seg
const { modifiedAst, valueUsedInTransform } = replaceSketchLine({
const { modifiedAst, valueUsedInTransform, pathToNode } = replaceSketchLine(
{
node: node,
programMemory,
sourceRange: range,
@ -1443,14 +1472,20 @@ export function transformAstSketchLines({
tag: callBackTag,
forceValueUsedInTransform,
}),
})
}
)
node = modifiedAst
pathToNodeMap[index] = pathToNode
if (typeof valueUsedInTransform === 'number') {
_valueUsedInTransform = valueUsedInTransform
}
})
return { modifiedAst: node, valueUsedInTransform: _valueUsedInTransform }
return {
modifiedAst: node,
valueUsedInTransform: _valueUsedInTransform,
pathToNodeMap,
}
}
function createSegLen(referenceSegName: string): Value {

28
src/lang/util.ts Normal file
View File

@ -0,0 +1,28 @@
import { Selections, StoreState } from '../useStore'
import { Program } from './abstractSyntaxTree'
import { PathToNode } from './executor'
import { getNodeFromPath } from './queryAst'
export function updateCursors(
setCursor: StoreState['setCursor'],
selectionRanges: Selections,
pathToNodeMap: { [key: number]: PathToNode }
): (newAst: Program) => void {
return (newAst: Program) => {
const newSelections: Selections = {
...selectionRanges,
codeBasedSelections: [],
}
Object.entries(pathToNodeMap).forEach(([index, path]) => {
const node = getNodeFromPath(newAst, path).node as any
const type = selectionRanges.codeBasedSelections[Number(index)].type
if (node) {
newSelections.codeBasedSelections.push({
range: [node.start, node.end],
type: type || 'default',
})
}
})
setCursor(newSelections)
}
}

View File

@ -88,7 +88,7 @@ export type GuiModes =
position: Position
}
interface StoreState {
export interface StoreState {
editorView: EditorView | null
setEditorView: (editorView: EditorView) => void
highlightRange: [number, number]
@ -105,7 +105,13 @@ interface StoreState {
resetLogs: () => void
ast: Program | null
setAst: (ast: Program | null) => void
updateAst: (ast: Program, focusPath?: PathToNode) => void
updateAst: (
ast: Program,
optionalParams?: {
focusPath?: PathToNode
callBack?: (ast: Program) => void
}
) => void
updateAstAsync: (ast: Program, focusPath?: PathToNode) => void
code: string
setCode: (code: string) => void
@ -185,11 +191,12 @@ export const useStore = create<StoreState>()(
setAst: (ast) => {
set({ ast })
},
updateAst: async (ast, focusPath) => {
updateAst: async (ast, { focusPath, callBack = () => {} } = {}) => {
const newCode = recast(ast)
const astWithUpdatedSource = abstractSyntaxTree(
await asyncLexer(newCode)
)
callBack(astWithUpdatedSource)
set({ ast: astWithUpdatedSource, code: newCode })
if (focusPath) {
@ -216,7 +223,7 @@ export const useStore = create<StoreState>()(
// setup a new update
pendingAstUpdates.push(
setTimeout(() => {
get().updateAst(ast, focusPath)
get().updateAst(ast, { focusPath })
}, 100) as unknown as number
)
},