Display numeric units in the variables pane (#6683)

This commit is contained in:
Jonathan Tran
2025-05-05 23:40:18 -04:00
committed by GitHub
parent 32db31e6c3
commit 1e056cfd8a
69 changed files with 602 additions and 163 deletions

View File

@ -16,6 +16,13 @@ describe('processMemory', () => {
return a - 2
}
otherVar = myFn(5)
nFeet = 2ft
nInches = 2in
nMm = 2mm
nDegrees = 2deg
nRadians = 2rad
nCount = 2_
nUnknown = 1rad * PI
theExtrude = startSketchOn(XY)
|> startProfile(at = [0, 0])
@ -32,8 +39,17 @@ describe('processMemory', () => {
const ast = assertParse(code)
const execState = await enginelessExecutor(ast)
const output = processMemory(execState.variables)
expect(output.myVar).toEqual(5)
expect(output.otherVar).toEqual(3)
expect(output.nFeet).toEqual('2: number(ft)')
expect(output.nInches).toEqual('2: number(in)')
expect(output.nMm).toEqual('2: number(mm)')
expect(output.nDegrees).toEqual('2: number(deg)')
expect(output.nRadians).toEqual('2: number(rad)')
expect(output.nCount).toEqual('2: number(Count)')
expect(output.nUnknown).toEqual(
'3.141592653589793 (number with unknown units)'
)
expect(output.myVar).toEqual('5 (no units, defaulting to mm or deg)')
expect(output.otherVar).toEqual('3 (no units, defaulting to mm or deg)')
expect(output.myFn).toEqual('__function__')
expect(output.theExtrude).toEqual([
{

View File

@ -11,7 +11,7 @@ import { useModelingContext } from '@src/hooks/useModelingContext'
import { useResolvedTheme } from '@src/hooks/useResolvedTheme'
import { useKclContext } from '@src/lang/KclProvider'
import type { VariableMap } from '@src/lang/wasm'
import { sketchFromKclValueOptional } from '@src/lang/wasm'
import { humanDisplayNumber, sketchFromKclValueOptional } from '@src/lang/wasm'
import { Reason, trap } from '@src/lib/trap'
export const MemoryPaneMenu = () => {
@ -81,31 +81,29 @@ export const MemoryPane = () => {
}
export const processMemory = (variables: VariableMap) => {
const processedMemory: any = {}
const processedMemory: Record<
string,
string | number | boolean | object | undefined
> = {}
for (const [key, val] of Object.entries(variables)) {
if (val === undefined) continue
if (
val.type === 'Sketch' ||
// @ts-ignore
val.type !== 'Function'
) {
const sk = sketchFromKclValueOptional(val, key)
if (val.type === 'Solid') {
processedMemory[key] = val.value.value.map(
({ ...rest }: ExtrudeSurface) => {
return rest
}
)
} else if (!(sk instanceof Reason)) {
processedMemory[key] = sk.paths.map(({ __geoMeta, ...rest }: Path) => {
const sk = sketchFromKclValueOptional(val, key)
if (val.type === 'Solid') {
processedMemory[key] = val.value.value.map(
({ ...rest }: ExtrudeSurface) => {
return rest
})
} else {
processedMemory[key] = val.value
}
//@ts-ignore
}
)
} else if (!(sk instanceof Reason)) {
processedMemory[key] = sk.paths.map(({ __geoMeta, ...rest }: Path) => {
return rest
})
} else if (val.type === 'Function') {
processedMemory[key] = `__function__`
processedMemory[key] = '__function__'
} else if (val.type === 'Number') {
processedMemory[key] = humanDisplayNumber(val.value, val.ty)
} else {
processedMemory[key] = val.value
}
}
return processedMemory