diff --git a/src/components/FileMachineProvider.tsx b/src/components/FileMachineProvider.tsx index 306c6a73d..c4973e903 100644 --- a/src/components/FileMachineProvider.tsx +++ b/src/components/FileMachineProvider.tsx @@ -329,7 +329,7 @@ export const FileMachineProvider = ({ onSubmit: async (data) => { if (data.method === 'overwrite') { codeManager.updateCodeStateEditor(data.code) - await kclManager.executeCode({ zoomToFit: true }) + await kclManager.executeCode(true) await codeManager.writeToFile() } else if (data.method === 'newFile' && isDesktop()) { send({ diff --git a/src/components/FileTree.tsx b/src/components/FileTree.tsx index 3ed439c51..dfc813f4c 100644 --- a/src/components/FileTree.tsx +++ b/src/components/FileTree.tsx @@ -22,6 +22,7 @@ import usePlatform from 'hooks/usePlatform' import { FileEntry } from 'lib/project' import { useFileSystemWatcher } from 'hooks/useFileSystemWatcher' import { reportRejection } from 'lib/trap' +import { normalizeLineEndings } from 'lib/codeEditor' function getIndentationCSS(level: number) { return `calc(1rem * ${level + 1})` @@ -189,25 +190,23 @@ const FileTreeItem = ({ // Because subtrees only render when they are opened, that means this // only listens when they open. Because this acts like a useEffect, when // the ReactNodes are destroyed, so is this listener :) - /** Disabling this in favor of faster file writes until we fix file writing **/ - /* useFileSystemWatcher( - * async (eventType, path) => { - * // Prevents a cyclic read / write causing editor problems such as - * // misplaced cursor positions. - * if (codeManager.writeCausedByAppCheckedInFileTreeFileSystemWatcher) { - * codeManager.writeCausedByAppCheckedInFileTreeFileSystemWatcher = false - * return - * } - - * if (isCurrentFile && eventType === 'change') { - * let code = await window.electron.readFile(path, { encoding: 'utf-8' }) - * code = normalizeLineEndings(code) - * codeManager.updateCodeStateEditor(code) - * } - * fileSend({ type: 'Refresh' }) - * }, - * [fileOrDir.path] - * ) */ + useFileSystemWatcher( + async (eventType, path) => { + // Prevents a cyclic read / write causing editor problems such as + // misplaced cursor positions. + if (codeManager.writeCausedByAppCheckedInFileTreeFileSystemWatcher) { + codeManager.writeCausedByAppCheckedInFileTreeFileSystemWatcher = false + return + } + if (isCurrentFile && eventType === 'change') { + let code = await window.electron.readFile(path, { encoding: 'utf-8' }) + code = normalizeLineEndings(code) + codeManager.updateCodeStateEditor(code) + } + fileSend({ type: 'Refresh' }) + }, + [fileOrDir.path] + ) const showNewTreeEntry = newTreeEntry !== undefined && @@ -263,7 +262,7 @@ const FileTreeItem = ({ await codeManager.writeToFile() // Prevent seeing the model built one piece at a time when changing files - await kclManager.executeCode({ zoomToFit: true }) + await kclManager.executeCode(true) } else { // Let the lsp servers know we closed a file. onFileClose(currentFile?.path || null, project?.path || null) diff --git a/src/components/ProjectsContextProvider.tsx b/src/components/ProjectsContextProvider.tsx index 24931588d..8603031d3 100644 --- a/src/components/ProjectsContextProvider.tsx +++ b/src/components/ProjectsContextProvider.tsx @@ -124,7 +124,7 @@ const ProjectsContextWeb = ({ children }: { children: React.ReactNode }) => { clearImportSearchParams() codeManager.updateCodeStateEditor(input.code || '') await codeManager.writeToFile() - await kclManager.executeCode({ zoomToFit: true }) + await kclManager.executeCode(true) return { message: 'File overwritten successfully', diff --git a/src/components/SettingsAuthProvider.tsx b/src/components/SettingsAuthProvider.tsx index e7f6ad39c..8dcae757c 100644 --- a/src/components/SettingsAuthProvider.tsx +++ b/src/components/SettingsAuthProvider.tsx @@ -187,7 +187,7 @@ export const SettingsAuthProviderBase = ({ ) { // Unit changes requires a re-exec of code // eslint-disable-next-line @typescript-eslint/no-floating-promises - kclManager.executeCode({ zoomToFit: true }) + kclManager.executeCode(true) } else { // For any future logging we'd like to do // console.log( diff --git a/src/components/Stream.tsx b/src/components/Stream.tsx index 9f57f4d24..3ab1f7bd0 100644 --- a/src/components/Stream.tsx +++ b/src/components/Stream.tsx @@ -60,7 +60,7 @@ export const Stream = () => { */ function executeCodeAndPlayStream() { // eslint-disable-next-line @typescript-eslint/no-floating-promises - kclManager.executeCode({ zoomToFit: true }).then(async () => { + kclManager.executeCode(true).then(async () => { await videoRef.current?.play().catch((e) => { console.warn('Video playing was prevented', e, videoRef.current) }) diff --git a/src/lang/KclSingleton.ts b/src/lang/KclSingleton.ts index 85775275d..dba751c3b 100644 --- a/src/lang/KclSingleton.ts +++ b/src/lang/KclSingleton.ts @@ -457,7 +457,7 @@ export class KclManager { this._cancelTokens.set(key, true) }) } - async executeCode(opts?: { zoomToFit?: true }): Promise { + async executeCode(zoomToFit?: boolean): Promise { const ast = await this.safeParse(codeManager.code) if (!ast) { @@ -465,10 +465,10 @@ export class KclManager { return } - // zoomToFit = this.tryToZoomToFitOnCodeUpdate(ast, opts?.zoomToFit) + zoomToFit = this.tryToZoomToFitOnCodeUpdate(ast, zoomToFit) this.ast = { ...ast } - return this.executeAst(opts) + return this.executeAst({ zoomToFit }) } /** * This will override the zoom to fit to zoom into the model if the previous AST was empty. diff --git a/src/lang/codeManager.ts b/src/lang/codeManager.ts index 34958bfcb..7f240891c 100644 --- a/src/lang/codeManager.ts +++ b/src/lang/codeManager.ts @@ -157,7 +157,7 @@ export default class CodeManager { toast.error('Error saving file, please check file permissions') reject(err) }) - }, 10) + }, 1000) }) } else { safeLSSetItem(PERSIST_CODE_KEY, this.code) diff --git a/src/lang/kclSamples.test.ts b/src/lang/kclSamples.test.ts index a3e22744a..d59be26fa 100644 --- a/src/lang/kclSamples.test.ts +++ b/src/lang/kclSamples.test.ts @@ -50,7 +50,7 @@ process.chdir(DIR_KCL_SAMPLES) beforeAll(async () => { await initPromise -}, 5_000) +}) afterAll(async () => { try { @@ -78,6 +78,6 @@ describe('Test KCL Samples from public Github repository', () => { }, files.length * 1000 ) - }, 3_0000) + }) }) }) diff --git a/src/machines/modelingMachine.ts b/src/machines/modelingMachine.ts index 6f1e5dfa1..1c54e58df 100644 --- a/src/machines/modelingMachine.ts +++ b/src/machines/modelingMachine.ts @@ -368,14 +368,12 @@ export type ModelingMachineEvent = tool: SketchTool } } - // | { type: 'Finish rectangle' | 'Finish center rectangle' | 'Finish circle three point' | 'Finish circle' } | { type: 'Finish rectangle' } | { type: 'Finish center rectangle' } | { type: 'Finish circle' } | { type: 'Finish circle three point' } | { type: 'Artifact graph populated' } | { type: 'Artifact graph emptied' } -// | { type: 'xstate.done.actor.actor-circle-three-point' } export type MoveDesc = { line: number; snippet: string } diff --git a/src/routes/Onboarding/Introduction.tsx b/src/routes/Onboarding/Introduction.tsx index e451e7c7f..ada5d8ab3 100644 --- a/src/routes/Onboarding/Introduction.tsx +++ b/src/routes/Onboarding/Introduction.tsx @@ -94,7 +94,7 @@ function OnboardingWarningWeb(props: OnboardingResetWarningProps) { codeManager.updateCodeStateEditor(bracket) await codeManager.writeToFile() - await kclManager.executeCode({ zoomToFit: true }) + await kclManager.executeCode(true) props.setShouldShowWarning(false) } return () => { diff --git a/src/routes/Onboarding/Sketching.tsx b/src/routes/Onboarding/Sketching.tsx index 8d725f47b..7129ee196 100644 --- a/src/routes/Onboarding/Sketching.tsx +++ b/src/routes/Onboarding/Sketching.tsx @@ -8,7 +8,7 @@ export default function Sketching() { async function clearEditor() { // We do want to update both the state and editor here. codeManager.updateCodeStateEditor('') - await kclManager.executeCode({ zoomToFit: true }) + await kclManager.executeCode(true) } // eslint-disable-next-line @typescript-eslint/no-floating-promises diff --git a/src/routes/Onboarding/index.tsx b/src/routes/Onboarding/index.tsx index 52566f534..16d4f32e8 100644 --- a/src/routes/Onboarding/index.tsx +++ b/src/routes/Onboarding/index.tsx @@ -103,7 +103,7 @@ export function useDemoCode() { setTimeout( toSync(async () => { codeManager.updateCodeStateEditor(bracket) - await kclManager.executeCode({ zoomToFit: true }) + await kclManager.executeCode(true) await codeManager.writeToFile() }, reportRejection) )