-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
start of fixing changing files and cleaning up after execute #897
Changes from all commits
7e317c7
77c4f9c
f493da8
71b87cf
c2528c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,15 +17,7 @@ | |
import { processCodeMirrorRanges } from 'lib/selections' | ||
import { LanguageServerClient } from 'editor/lsp' | ||
import kclLanguage from 'editor/lsp/language' | ||
import { isTauri } from 'lib/isTauri' | ||
import { useParams } from 'react-router-dom' | ||
import { writeTextFile } from '@tauri-apps/api/fs' | ||
import { toast } from 'react-hot-toast' | ||
import { | ||
EditorView, | ||
addLineHighlight, | ||
lineHighlightField, | ||
} from 'editor/highlightextension' | ||
import { EditorView, lineHighlightField } from 'editor/highlightextension' | ||
import { roundOff } from 'lib/utils' | ||
import { kclErrToDiagnostic } from 'lang/errors' | ||
import { CSSRuleObject } from 'tailwindcss/types/config' | ||
|
@@ -50,7 +42,6 @@ | |
}: { | ||
theme: Themes.Light | Themes.Dark | ||
}) => { | ||
const pathParams = useParams() | ||
const { editorView, isLSPServerReady, setEditorView, setIsLSPServerReady } = | ||
useStore((s) => ({ | ||
editorView: s.editorView, | ||
|
@@ -113,18 +104,6 @@ | |
// const onChange = React.useCallback((value: string, viewUpdate: ViewUpdate) => { | ||
const onChange = (newCode: string) => { | ||
kclManager.setCodeAndExecute(newCode) | ||
if (isTauri() && pathParams.id) { | ||
// Save the file to disk | ||
// Note that PROJECT_ENTRYPOINT is hardcoded until we support multiple files | ||
writeTextFile(pathParams.id, newCode).catch((err) => { | ||
// TODO: add Sentry per GH issue #254 (https://github.com/KittyCAD/modeling-app/issues/254) | ||
console.error('error saving file', err) | ||
toast.error('Error saving file, please check file permissions') | ||
}) | ||
} | ||
if (editorView) { | ||
editorView?.dispatch({ effects: addLineHighlight.of([0, 0]) }) | ||
} | ||
Comment on lines
106
to
-127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} //, []); | ||
const onUpdate = (viewUpdate: ViewUpdate) => { | ||
if (!editorView) { | ||
|
@@ -220,7 +199,7 @@ | |
} | ||
|
||
return extensions | ||
}, [kclLSP, textWrapping, convertCallback]) | ||
Check warning on line 202 in src/components/TextEditor.tsx GitHub Actions / build-test-apps (macos-latest)
Check warning on line 202 in src/components/TextEditor.tsx GitHub Actions / build-test-apps (ubuntu-latest)
|
||
|
||
return ( | ||
<div | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,11 @@ import { bracket } from 'lib/exampleKcl' | |
import { createContext, useContext, useEffect, useState } from 'react' | ||
import { getNodeFromPath } from './queryAst' | ||
import { IndexLoaderData } from 'Router' | ||
import { useLoaderData } from 'react-router-dom' | ||
import { Params, useLoaderData } from 'react-router-dom' | ||
import { isTauri } from 'lib/isTauri' | ||
import { writeTextFile } from '@tauri-apps/api/fs' | ||
import { toast } from 'react-hot-toast' | ||
import { useParams } from 'react-router-dom' | ||
|
||
const PERSIST_CODE_TOKEN = 'persistCode' | ||
|
||
|
@@ -41,6 +45,7 @@ class KclManager { | |
private _kclErrors: KCLError[] = [] | ||
private _isExecuting = false | ||
private _wasmInitFailed = true | ||
private _params: Params<string> = {} | ||
|
||
engineCommandManager: EngineCommandManager | ||
private _defferer = deferExecution((code: string) => { | ||
|
@@ -71,6 +76,21 @@ class KclManager { | |
set code(code) { | ||
this._code = code | ||
this._codeCallBack(code) | ||
if (isTauri()) { | ||
setTimeout(() => { | ||
// Wait one event loop to give a chance for params to be set | ||
// Save the file to disk | ||
// Note that PROJECT_ENTRYPOINT is hardcoded until we support multiple files | ||
this._params.id && | ||
writeTextFile(this._params.id, code).catch((err) => { | ||
// TODO: add Sentry per GH issue #254 (https://github.com/KittyCAD/modeling-app/issues/254) | ||
console.error('error saving file', err) | ||
toast.error('Error saving file, please check file permissions') | ||
}) | ||
}) | ||
} else { | ||
localStorage.setItem(PERSIST_CODE_TOKEN, code) | ||
} | ||
} | ||
|
||
get programMemory() { | ||
|
@@ -117,8 +137,17 @@ class KclManager { | |
this._wasmInitFailedCallback(wasmInitFailed) | ||
} | ||
|
||
setParams(params: Params<string>) { | ||
this._params = params | ||
} | ||
|
||
constructor(engineCommandManager: EngineCommandManager) { | ||
this.engineCommandManager = engineCommandManager | ||
|
||
if (isTauri()) { | ||
this.code = '' | ||
return | ||
} | ||
const storedCode = localStorage.getItem(PERSIST_CODE_TOKEN) | ||
// TODO #819 remove zustand persistence logic in a few months | ||
// short term migration, shouldn't make a difference for tauri app users | ||
|
@@ -130,6 +159,7 @@ class KclManager { | |
zustandStore.state.code = '' | ||
localStorage.setItem('store', JSON.stringify(zustandStore)) | ||
} else if (storedCode === null) { | ||
console.log('stored brack thing') | ||
this.code = bracket | ||
} else { | ||
this.code = storedCode | ||
|
@@ -189,8 +219,7 @@ class KclManager { | |
this._programMemory = programMemory | ||
this._ast = { ...ast } | ||
if (updateCode) { | ||
this._code = recast(ast) | ||
this._codeCallBack(this._code) | ||
this.code = recast(ast) | ||
} | ||
this._executeCallback() | ||
} | ||
|
@@ -233,13 +262,17 @@ class KclManager { | |
this.ast = ast | ||
if (code) this.code = code | ||
} | ||
setCode(code: string) { | ||
setCode(code: string, shouldWriteFile = true) { | ||
if (shouldWriteFile) { | ||
// use the normal code setter | ||
this.code = code | ||
return | ||
} | ||
this._code = code | ||
this._codeCallBack(code) | ||
localStorage.setItem(PERSIST_CODE_TOKEN, code) | ||
} | ||
setCodeAndExecute(code: string) { | ||
this.setCode(code) | ||
setCodeAndExecute(code: string, shouldWriteFile = true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally, when the code is updated we want to save the file as well. Except when the file is first loaded because the user opened a project. |
||
this.setCode(code, shouldWriteFile) | ||
if (code.trim()) { | ||
this._defferer(code) | ||
return | ||
|
@@ -270,15 +303,12 @@ class KclManager { | |
execute: boolean, | ||
optionalParams?: { | ||
focusPath?: PathToNode | ||
callBack?: (ast: Program) => void | ||
} | ||
): Promise<Selections | null> { | ||
const newCode = recast(ast) | ||
const astWithUpdatedSource = parse(newCode) | ||
optionalParams?.callBack?.(astWithUpdatedSource) | ||
let returnVal: Selections | null = null | ||
|
||
this.code = newCode | ||
if (optionalParams?.focusPath) { | ||
const { node } = getNodeFromPath<any>( | ||
astWithUpdatedSource, | ||
|
@@ -299,12 +329,12 @@ class KclManager { | |
|
||
if (execute) { | ||
// Call execute on the set ast. | ||
await this.executeAst(astWithUpdatedSource) | ||
await this.executeAst(astWithUpdatedSource, true) | ||
} else { | ||
// When we don't re-execute, we still want to update the program | ||
// memory with the new ast. So we will hit the mock executor | ||
// instead. | ||
await this.executeAstMock(astWithUpdatedSource) | ||
await this.executeAstMock(astWithUpdatedSource, true) | ||
} | ||
return returnVal | ||
} | ||
|
@@ -369,6 +399,11 @@ export function KclContextProvider({ | |
setWasmInitFailed, | ||
}) | ||
}, []) | ||
|
||
const params = useParams() | ||
useEffect(() => { | ||
kclManager.setParams(params) | ||
}, [params]) | ||
return ( | ||
<KclContext.Provider | ||
value={{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this replaces https://github.com/KittyCAD/modeling-app/pull/897/files#diff-26ad4b834941d9b19ebf9db8082bd202aaf72ea0ddea85f5a8a0cb3c729cc6f2L86-L104