Constraint setup + Horizontal & Vertical implementation (#33)

* start of horizontal/vert constraint

* horz vert constraint working with variable

* quick fix

* add tests for horz constraint

* clean up
This commit is contained in:
Kurt Hutten
2023-02-21 10:50:45 +11:00
committed by GitHub
parent ea05f804cc
commit 4ec0401118
12 changed files with 1138 additions and 129 deletions

View File

@ -0,0 +1,53 @@
import { Range, TooTip } from '../../useStore'
import {
getNodePathFromSourceRange,
getNodeFromPath,
Program,
VariableDeclarator,
} from '../../lang/abstractSyntaxTree'
import { replaceSketchLine } from '../../lang/std/sketch'
import { ProgramMemory, SketchGroup } from '../../lang/executor'
import { TransformCallback } from '../../lang/std/stdTypes'
export function swapSketchHelper(
programMemory: ProgramMemory,
ast: Program,
range: Range,
newFnName: TooTip,
createCallback: TransformCallback
): { modifiedAst: Program } {
const path = getNodePathFromSourceRange(ast, range)
const varDec = getNodeFromPath<VariableDeclarator>(
ast,
path,
'VariableDeclarator'
).node
const varName = varDec.id.name
const sketchGroup = programMemory.root?.[varName]
if (!sketchGroup || sketchGroup.type !== 'sketchGroup')
throw new Error('not a sketch group')
const seg = getSketchSegmentIndexFromSourceRange(sketchGroup, range)
const { to, from } = seg
const { modifiedAst } = replaceSketchLine({
node: ast,
sourceRange: range,
programMemory,
fnName: newFnName,
to,
from,
createCallback,
})
return { modifiedAst }
}
function getSketchSegmentIndexFromSourceRange(
sketchGroup: SketchGroup,
[rangeStart, rangeEnd]: Range
): SketchGroup['value'][number] {
const line = sketchGroup.value.find(
({ __geoMeta: { sourceRange } }) =>
sourceRange[0] <= rangeStart && sourceRange[1] >= rangeEnd
)
if (!line) throw new Error('could not find matching line')
return line
}