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:
53
src/lang/std/sketchConstraints.ts
Normal file
53
src/lang/std/sketchConstraints.ts
Normal 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
|
||||
}
|
Reference in New Issue
Block a user