diff --git a/src/App.tsx b/src/App.tsx index d25ba8382d..5b1f8e696e 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -31,7 +31,6 @@ import { TextEditor } from 'components/TextEditor' import { Themes, getSystemTheme } from 'lib/theme' import { useEngineConnectionSubscriptions } from 'hooks/useEngineConnectionSubscriptions' import { engineCommandManager } from './lang/std/engineConnection' -import { kclManager } from 'lang/KclSinglton' import { useModelingContext } from 'hooks/useModelingContext' export function App() { @@ -82,26 +81,6 @@ export function App() { ? 'opacity-40' : '' - // Use file code loaded from disk - // on mount, and overwrite any locally-stored code - useEffect(() => { - if (isTauri() && loadedCode !== null) { - if (kclManager.engineCommandManager.engineConnection?.isReady()) { - // If the engine is ready, promptly execute the loaded code - kclManager.setCodeAndExecute(loadedCode) - } else { - // Otherwise, just set the code and wait for the connection to complete - kclManager.setCode(loadedCode) - } - } - return () => { - // Clear code on unmount if in desktop app - if (isTauri()) { - kclManager.setCode('') - } - } - }, [loadedCode]) - useEngineConnectionSubscriptions() const debounceSocketSend = throttle((message) => { diff --git a/src/Router.tsx b/src/Router.tsx index 56385fa7ec..ea499a39d9 100644 --- a/src/Router.tsx +++ b/src/Router.tsx @@ -42,7 +42,7 @@ import CommandBarProvider from 'components/CommandBar' import { TEST, VITE_KC_SENTRY_DSN } from './env' import * as Sentry from '@sentry/react' import ModelingMachineProvider from 'components/ModelingMachineProvider' -import { KclContextProvider } from 'lang/KclSinglton' +import { KclContextProvider, kclManager } from 'lang/KclSinglton' import FileMachineProvider from 'components/FileMachineProvider' import { sep } from '@tauri-apps/api/path' @@ -207,6 +207,7 @@ const router = createBrowserRouter( projectPath + sep + PROJECT_ENTRYPOINT ) const children = await readDir(projectPath, { recursive: true }) + kclManager.setCodeAndExecute(code, false) return { code, diff --git a/src/components/AvailableVarsHelpers.tsx b/src/components/AvailableVarsHelpers.tsx index 4905ca1f3c..39aac202e5 100644 --- a/src/components/AvailableVarsHelpers.tsx +++ b/src/components/AvailableVarsHelpers.tsx @@ -1,5 +1,5 @@ import { useEffect, useState, useRef } from 'react' -import { parse, BinaryPart, Value, executor } from '../lang/wasm' +import { parse, BinaryPart, Value } from '../lang/wasm' import { createIdentifier, createLiteral, @@ -10,6 +10,7 @@ import { findAllPreviousVariables, PrevVariable } from '../lang/queryAst' import { engineCommandManager } from '../lang/std/engineConnection' import { kclManager, useKclContext } from 'lang/KclSinglton' import { useModelingContext } from 'hooks/useModelingContext' +import { executeAst } from 'useStore' export const AvailableVars = ({ onVarClick, @@ -130,27 +131,29 @@ export function useCalc({ if (!programMemory || !selectionRange) return const varInfo = findAllPreviousVariables( kclManager.ast, - programMemory, + kclManager.programMemory, selectionRange ) setAvailableVarInfo(varInfo) - }, [kclManager.ast, programMemory, selectionRange]) + }, [kclManager.ast, kclManager.programMemory, selectionRange]) useEffect(() => { try { - const code = `const __result__ = ${value}\nshow(__result__)` + const code = `const __result__ = ${value}` const ast = parse(code) const _programMem: any = { root: {}, return: null } availableVarInfo.variables.forEach(({ key, value }) => { _programMem.root[key] = { type: 'userVal', value, __meta: [] } }) - - executor( + executeAst({ ast, - _programMem, engineCommandManager, - kclManager.defaultPlanes - ).then((programMemory) => { + defaultPlanes: kclManager.defaultPlanes, + useFakeExecutor: true, + programMemoryOverride: JSON.parse( + JSON.stringify(kclManager.programMemory) + ), + }).then(({ programMemory }) => { const resultDeclaration = ast.body.find( (a) => a.type === 'VariableDeclaration' && @@ -167,7 +170,7 @@ export function useCalc({ setCalcResult('NAN') setValueNode(null) } - }, [value]) + }, [value, availableVarInfo]) return { valueNode, @@ -212,7 +215,10 @@ export const CreateNewVariable = ({ }) => { return ( <> -