Skip to content

Commit

Permalink
More places need to know how to tell if a call is absolute, and map i…
Browse files Browse the repository at this point in the history
…t to a tooltip if so
  • Loading branch information
adamchalmers committed Feb 27, 2025
1 parent 981d138 commit 8225bad
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
45 changes: 31 additions & 14 deletions src/lang/std/sketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2369,30 +2369,47 @@ export function getConstraintInfo(
)
}

/* Keyword functions like 'line' could be absolute or relative, depending on their parameters.
But tooltips like 'line' or 'lineTo' are either relative or absolute. Not both.
So, map (line, absolute) to lineTo, and (line, relative) to line. Etc.
*/
export function sketchFnNameToTooltip(
fnName: string,
isAbsolute: boolean
): ToolTip | undefined {
switch (fnName) {
case 'line':
return isAbsolute ? 'lineTo' : fnName
case 'xLine':
return isAbsolute ? 'xLineTo' : fnName
case 'yLine':
return isAbsolute ? 'yLineTo' : fnName
}
}

export function sketchFnIsAbsolute(
callExpression: Node<CallExpressionKw>
): boolean {
const fnName = callExpression.callee.name
return (
fnName === 'circleThreePoint' ||
findKwArg('endAbsolute', callExpression) !== undefined
)
}

export function getConstraintInfoKw(
callExpression: Node<CallExpressionKw>,
code: string,
pathToNode: PathToNode,
filterValue?: string
): ConstrainInfo[] {
const fnName = callExpression?.callee?.name || ''
const isAbsolute =
fnName === 'circleThreePoint' ||
findKwArg('endAbsolute', callExpression) !== undefined
if (!(fnName in sketchLineHelperMapKw)) return []
const correctFnName = (() => {
switch (fnName) {
case 'line':
return isAbsolute ? 'lineTo' : fnName
case 'xLine':
return isAbsolute ? 'xLineTo' : fnName
case 'yLine':
return isAbsolute ? 'yLineTo' : fnName
}
})()
const isAbsolute = sketchFnIsAbsolute(callExpression)
const correctFnName = sketchFnNameToTooltip(fnName, isAbsolute)
if (correctFnName === undefined) {
return []
}
if (!(correctFnName in sketchLineHelperMapKw)) return []
return sketchLineHelperMapKw[correctFnName].getConstraintInfo(
callExpression,
code,
Expand Down
18 changes: 14 additions & 4 deletions src/lang/std/sketchcombos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import { err } from 'lib/trap'
import { enginelessExecutor } from '../../lib/testHelpers'
import { codeRefFromRange } from './artifactGraph'
import { findKwArg } from 'lang/util'
import { ARG_END, ARG_END_ABSOLUTE } from './sketch'
import {
ARG_END,
ARG_END_ABSOLUTE,
sketchFnIsAbsolute,
sketchFnNameToTooltip,
} from './sketch'

beforeAll(async () => {
await initPromise
Expand Down Expand Up @@ -124,9 +129,14 @@ function getConstraintTypeFromSourceHelper2(
): ReturnType<typeof getConstraintType> | Error {
const ast = assertParse(code)

const arg = (ast.body[0] as any).expression.arguments[0] as Expr
const fnName = (ast.body[0] as any).expression.callee.name as ToolTip
return getConstraintType(arg, fnName, false)
const call = (ast.body[0] as any).expression
const arg = call.arguments[0] as Expr
const fnName = call.callee.name as ToolTip
const correctFnName = sketchFnNameToTooltip(fnName, sketchFnIsAbsolute(call))
if (correctFnName === undefined) {
return new Error(`could not map this ${fnName} call to a tooltip`)
}
return getConstraintType(arg, correctFnName, false)
}

function makeSelections(
Expand Down
11 changes: 5 additions & 6 deletions src/lang/std/sketchcombos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1755,17 +1755,16 @@ export function getConstraintType(
fnName: ToolTip,
isAbsolute: boolean
): LineInputsType | null {
if (fnName === 'xLine') return 'yRelative'
if (fnName === 'yLine') return 'xRelative'
if (fnName === 'xLineTo') return 'yAbsolute'
if (fnName === 'yLineTo') return 'xAbsolute'
// this function assumes that for two val sketch functions that one arg is locked down not both
// and for one val sketch functions that the arg is NOT locked down
// these conditions should have been checked previously.
// completely locked down or not locked down at all does not depend on the fnName so we can check that first
const isArr = isArray(val)
if (!isArr) {
if (fnName === 'xLine') return 'yRelative'
if (fnName === 'yLine') return 'xRelative'
if (fnName === 'xLineTo') return 'yAbsolute'
if (fnName === 'yLineTo') return 'xAbsolute'
} else {
if (isArr) {
const isFirstArgLockedDown = isNotLiteralArrayOrStatic(val[0])
if (fnName === 'line' && !isAbsolute)
return isFirstArgLockedDown ? 'xRelative' : 'yRelative'
Expand Down

0 comments on commit 8225bad

Please sign in to comment.