2024-07-02 17:16:27 +10:00
|
|
|
import { executeAst, lintAst } from 'lang/langHelpers'
|
2024-12-20 13:39:06 +11:00
|
|
|
import { handleSelectionBatch, Selections } from 'lib/selections'
|
2024-12-06 13:57:31 +13:00
|
|
|
import {
|
|
|
|
KCLError,
|
|
|
|
complilationErrorsToDiagnostics,
|
|
|
|
kclErrorsToDiagnostics,
|
|
|
|
} from './errors'
|
2024-04-03 19:38:16 +11:00
|
|
|
import { uuidv4 } from 'lib/utils'
|
2024-03-22 16:55:30 +11:00
|
|
|
import { EngineCommandManager } from './std/engineConnection'
|
2024-06-24 11:45:40 -04:00
|
|
|
import { err } from 'lib/trap'
|
2024-08-30 05:14:24 -05:00
|
|
|
import { EXECUTE_AST_INTERRUPT_ERROR_MESSAGE } from 'lib/constants'
|
2024-03-22 16:55:30 +11:00
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
import {
|
2024-02-11 12:59:00 +11:00
|
|
|
CallExpression,
|
2024-12-06 14:56:53 -08:00
|
|
|
clearSceneAndBustCache,
|
2024-10-09 19:38:40 -04:00
|
|
|
emptyExecState,
|
|
|
|
ExecState,
|
2023-10-11 13:36:54 +11:00
|
|
|
initPromise,
|
|
|
|
parse,
|
|
|
|
PathToNode,
|
|
|
|
Program,
|
|
|
|
ProgramMemory,
|
|
|
|
recast,
|
2024-07-04 01:19:24 -04:00
|
|
|
SourceRange,
|
2023-10-11 13:36:54 +11:00
|
|
|
} from 'lang/wasm'
|
|
|
|
import { getNodeFromPath } from './queryAst'
|
2024-05-06 19:28:30 +10:00
|
|
|
import { codeManager, editorManager, sceneInfra } from 'lib/singletons'
|
2024-07-29 08:41:02 -07:00
|
|
|
import { Diagnostic } from '@codemirror/lint'
|
2024-11-07 17:23:03 -05:00
|
|
|
import { markOnce } from 'lib/performance'
|
2024-10-30 16:52:17 -04:00
|
|
|
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
2024-12-20 13:39:06 +11:00
|
|
|
import {
|
|
|
|
EntityType_type,
|
|
|
|
ModelingCmdReq_type,
|
|
|
|
} from '@kittycad/lib/dist/types/src/models'
|
2024-12-20 16:19:59 -05:00
|
|
|
import { Operation } from 'wasm-lib/kcl/bindings/Operation'
|
2023-10-11 13:36:54 +11:00
|
|
|
|
2024-08-14 08:49:00 -07:00
|
|
|
interface ExecuteArgs {
|
2024-10-30 16:52:17 -04:00
|
|
|
ast?: Node<Program>
|
2024-08-14 08:49:00 -07:00
|
|
|
zoomToFit?: boolean
|
|
|
|
executionId?: number
|
|
|
|
zoomOnRangeAndType?: {
|
|
|
|
range: SourceRange
|
|
|
|
type: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 16:55:30 +11:00
|
|
|
export class KclManager {
|
2024-10-30 16:52:17 -04:00
|
|
|
private _ast: Node<Program> = {
|
2023-10-11 13:36:54 +11:00
|
|
|
body: [],
|
2024-11-26 16:39:57 +13:00
|
|
|
shebang: null,
|
2023-10-11 13:36:54 +11:00
|
|
|
start: 0,
|
|
|
|
end: 0,
|
2024-11-07 11:23:41 -05:00
|
|
|
moduleId: 0,
|
2023-10-11 13:36:54 +11:00
|
|
|
nonCodeMeta: {
|
|
|
|
nonCodeNodes: {},
|
2024-10-30 16:52:17 -04:00
|
|
|
startNodes: [],
|
2023-10-11 13:36:54 +11:00
|
|
|
},
|
|
|
|
}
|
2024-10-09 19:38:40 -04:00
|
|
|
private _execState: ExecState = emptyExecState()
|
2024-07-22 19:43:40 -04:00
|
|
|
private _programMemory: ProgramMemory = ProgramMemory.empty()
|
2024-10-02 13:15:40 +10:00
|
|
|
lastSuccessfulProgramMemory: ProgramMemory = ProgramMemory.empty()
|
2024-12-20 16:19:59 -05:00
|
|
|
lastSuccessfulOperations: Operation[] = []
|
2023-10-11 13:36:54 +11:00
|
|
|
private _logs: string[] = []
|
2024-12-20 16:19:59 -05:00
|
|
|
private _errors: KCLError[] = []
|
2024-12-06 13:57:31 +13:00
|
|
|
private _diagnostics: Diagnostic[] = []
|
2023-10-11 13:36:54 +11:00
|
|
|
private _isExecuting = false
|
2024-08-14 08:49:00 -07:00
|
|
|
private _executeIsStale: ExecuteArgs | null = null
|
2023-10-17 12:07:48 +11:00
|
|
|
private _wasmInitFailed = true
|
2024-12-06 13:57:31 +13:00
|
|
|
private _hasErrors = false
|
2024-12-06 14:56:53 -08:00
|
|
|
private _switchedFiles = false
|
2023-10-11 13:36:54 +11:00
|
|
|
|
|
|
|
engineCommandManager: EngineCommandManager
|
|
|
|
|
2023-10-17 12:07:48 +11:00
|
|
|
private _isExecutingCallback: (arg: boolean) => void = () => {}
|
2024-10-30 16:52:17 -04:00
|
|
|
private _astCallBack: (arg: Node<Program>) => void = () => {}
|
2023-10-11 13:36:54 +11:00
|
|
|
private _programMemoryCallBack: (arg: ProgramMemory) => void = () => {}
|
|
|
|
private _logsCallBack: (arg: string[]) => void = () => {}
|
2024-12-20 16:19:59 -05:00
|
|
|
private _kclErrorsCallBack: (errors: KCLError[]) => void = () => {}
|
|
|
|
private _diagnosticsCallback: (errors: Diagnostic[]) => void = () => {}
|
2023-10-17 12:07:48 +11:00
|
|
|
private _wasmInitFailedCallback: (arg: boolean) => void = () => {}
|
2023-10-18 08:03:02 +11:00
|
|
|
private _executeCallback: () => void = () => {}
|
2023-10-11 13:36:54 +11:00
|
|
|
|
|
|
|
get ast() {
|
|
|
|
return this._ast
|
|
|
|
}
|
|
|
|
set ast(ast) {
|
|
|
|
this._ast = ast
|
|
|
|
this._astCallBack(ast)
|
|
|
|
}
|
|
|
|
|
2024-12-06 14:56:53 -08:00
|
|
|
set switchedFiles(switchedFiles: boolean) {
|
|
|
|
this._switchedFiles = switchedFiles
|
|
|
|
}
|
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
get programMemory() {
|
|
|
|
return this._programMemory
|
|
|
|
}
|
2024-10-09 19:38:40 -04:00
|
|
|
// This is private because callers should be setting the entire execState.
|
|
|
|
private set programMemory(programMemory) {
|
2023-10-11 13:36:54 +11:00
|
|
|
this._programMemory = programMemory
|
|
|
|
this._programMemoryCallBack(programMemory)
|
|
|
|
}
|
|
|
|
|
2024-12-05 19:51:06 -08:00
|
|
|
private set execState(execState) {
|
2024-10-09 19:38:40 -04:00
|
|
|
this._execState = execState
|
|
|
|
this.programMemory = execState.memory
|
|
|
|
}
|
|
|
|
|
|
|
|
get execState() {
|
|
|
|
return this._execState
|
|
|
|
}
|
|
|
|
|
2024-12-20 16:19:59 -05:00
|
|
|
get errors() {
|
|
|
|
return this._errors
|
|
|
|
}
|
|
|
|
set errors(errors) {
|
|
|
|
this._errors = errors
|
|
|
|
this._kclErrorsCallBack(errors)
|
|
|
|
}
|
2023-10-11 13:36:54 +11:00
|
|
|
get logs() {
|
|
|
|
return this._logs
|
|
|
|
}
|
|
|
|
set logs(logs) {
|
|
|
|
this._logs = logs
|
|
|
|
this._logsCallBack(logs)
|
|
|
|
}
|
|
|
|
|
2024-12-06 13:57:31 +13:00
|
|
|
get diagnostics() {
|
|
|
|
return this._diagnostics
|
2024-07-29 08:41:02 -07:00
|
|
|
}
|
|
|
|
|
2024-12-06 13:57:31 +13:00
|
|
|
set diagnostics(ds) {
|
|
|
|
if (ds === this._diagnostics) return
|
|
|
|
this._diagnostics = ds
|
|
|
|
this.setDiagnosticsForCurrentErrors()
|
2024-07-29 08:41:02 -07:00
|
|
|
}
|
|
|
|
|
2024-12-06 13:57:31 +13:00
|
|
|
addDiagnostics(ds: Diagnostic[]) {
|
|
|
|
if (ds.length === 0) return
|
|
|
|
this.diagnostics = this.diagnostics.concat(ds)
|
2024-08-04 15:16:34 -07:00
|
|
|
}
|
|
|
|
|
2024-12-06 13:57:31 +13:00
|
|
|
hasErrors(): boolean {
|
|
|
|
return this._hasErrors
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
|
|
|
|
2024-12-06 13:57:31 +13:00
|
|
|
setDiagnosticsForCurrentErrors() {
|
|
|
|
editorManager?.setDiagnostics(this.diagnostics)
|
2024-12-20 16:19:59 -05:00
|
|
|
this._diagnosticsCallback(this.diagnostics)
|
2024-07-29 08:41:02 -07:00
|
|
|
}
|
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
get isExecuting() {
|
|
|
|
return this._isExecuting
|
|
|
|
}
|
2024-08-30 05:14:24 -05:00
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
set isExecuting(isExecuting) {
|
|
|
|
this._isExecuting = isExecuting
|
2024-08-14 08:49:00 -07:00
|
|
|
// If we have finished executing, but the execute is stale, we should
|
|
|
|
// execute again.
|
|
|
|
if (!isExecuting && this.executeIsStale) {
|
|
|
|
const args = this.executeIsStale
|
|
|
|
this.executeIsStale = null
|
2024-09-09 18:17:45 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2024-08-14 08:49:00 -07:00
|
|
|
this.executeAst(args)
|
|
|
|
}
|
2023-10-11 13:36:54 +11:00
|
|
|
this._isExecutingCallback(isExecuting)
|
|
|
|
}
|
|
|
|
|
2024-08-14 08:49:00 -07:00
|
|
|
get executeIsStale() {
|
|
|
|
return this._executeIsStale
|
|
|
|
}
|
|
|
|
|
|
|
|
set executeIsStale(executeIsStale) {
|
|
|
|
this._executeIsStale = executeIsStale
|
|
|
|
}
|
|
|
|
|
2023-10-17 12:07:48 +11:00
|
|
|
get wasmInitFailed() {
|
|
|
|
return this._wasmInitFailed
|
|
|
|
}
|
|
|
|
set wasmInitFailed(wasmInitFailed) {
|
|
|
|
this._wasmInitFailed = wasmInitFailed
|
|
|
|
this._wasmInitFailedCallback(wasmInitFailed)
|
|
|
|
}
|
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
constructor(engineCommandManager: EngineCommandManager) {
|
|
|
|
this.engineCommandManager = engineCommandManager
|
2023-11-06 11:49:13 +11:00
|
|
|
|
2024-09-09 18:17:45 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2024-12-06 14:56:53 -08:00
|
|
|
this.ensureWasmInit().then(async () => {
|
|
|
|
await this.safeParse(codeManager.code).then((ast) => {
|
|
|
|
if (ast) {
|
|
|
|
this.ast = ast
|
|
|
|
}
|
|
|
|
})
|
2024-02-11 12:59:00 +11:00
|
|
|
})
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
2024-04-17 20:18:07 -07:00
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
registerCallBacks({
|
|
|
|
setProgramMemory,
|
|
|
|
setAst,
|
|
|
|
setLogs,
|
2024-12-20 16:19:59 -05:00
|
|
|
setErrors,
|
|
|
|
setDiagnostics,
|
2023-10-11 13:36:54 +11:00
|
|
|
setIsExecuting,
|
2023-10-17 12:07:48 +11:00
|
|
|
setWasmInitFailed,
|
2023-10-11 13:36:54 +11:00
|
|
|
}: {
|
|
|
|
setProgramMemory: (arg: ProgramMemory) => void
|
2024-10-30 16:52:17 -04:00
|
|
|
setAst: (arg: Node<Program>) => void
|
2023-10-11 13:36:54 +11:00
|
|
|
setLogs: (arg: string[]) => void
|
2024-12-20 16:19:59 -05:00
|
|
|
setErrors: (errors: KCLError[]) => void
|
|
|
|
setDiagnostics: (errors: Diagnostic[]) => void
|
2023-10-11 13:36:54 +11:00
|
|
|
setIsExecuting: (arg: boolean) => void
|
2023-10-17 12:07:48 +11:00
|
|
|
setWasmInitFailed: (arg: boolean) => void
|
2023-10-11 13:36:54 +11:00
|
|
|
}) {
|
|
|
|
this._programMemoryCallBack = setProgramMemory
|
|
|
|
this._astCallBack = setAst
|
|
|
|
this._logsCallBack = setLogs
|
2024-12-20 16:19:59 -05:00
|
|
|
this._kclErrorsCallBack = setErrors
|
|
|
|
this._diagnosticsCallback = setDiagnostics
|
2023-10-11 13:36:54 +11:00
|
|
|
this._isExecutingCallback = setIsExecuting
|
2023-10-17 12:07:48 +11:00
|
|
|
this._wasmInitFailedCallback = setWasmInitFailed
|
|
|
|
}
|
2023-10-18 08:03:02 +11:00
|
|
|
registerExecuteCallback(callback: () => void) {
|
|
|
|
this._executeCallback = callback
|
|
|
|
}
|
2023-10-17 12:07:48 +11:00
|
|
|
|
2024-06-19 16:15:22 -04:00
|
|
|
clearAst() {
|
|
|
|
this._ast = {
|
|
|
|
body: [],
|
2024-11-26 16:39:57 +13:00
|
|
|
shebang: null,
|
2024-06-19 16:15:22 -04:00
|
|
|
start: 0,
|
|
|
|
end: 0,
|
2024-11-07 11:23:41 -05:00
|
|
|
moduleId: 0,
|
2024-06-19 16:15:22 -04:00
|
|
|
nonCodeMeta: {
|
|
|
|
nonCodeNodes: {},
|
2024-10-30 16:52:17 -04:00
|
|
|
startNodes: [],
|
2024-06-19 16:15:22 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-06 14:56:53 -08:00
|
|
|
// (jess) I'm not in love with this, but it ensures we clear the scene and
|
|
|
|
// bust the cache on
|
|
|
|
// errors from parsing when opening new files.
|
|
|
|
// Why not just clear the cache on all parse errors, you ask? well its actually
|
|
|
|
// really nice to keep the cache on parse errors within the same file, and
|
|
|
|
// only bust on engine errors esp if they take a long time to execute and
|
|
|
|
// you hit the wrong key!
|
|
|
|
private async checkIfSwitchedFilesShouldClear() {
|
|
|
|
// If we were switching files and we hit an error on parse we need to bust
|
|
|
|
// the cache and clear the scene.
|
|
|
|
if (this._hasErrors && this._switchedFiles) {
|
|
|
|
await clearSceneAndBustCache(this.engineCommandManager)
|
|
|
|
} else if (this._switchedFiles) {
|
|
|
|
// Reset the switched files boolean.
|
|
|
|
this._switchedFiles = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async safeParse(code: string): Promise<Node<Program> | null> {
|
2024-12-06 13:57:31 +13:00
|
|
|
const result = parse(code)
|
|
|
|
this.diagnostics = []
|
|
|
|
this._hasErrors = false
|
|
|
|
|
|
|
|
if (err(result)) {
|
|
|
|
const kclerror: KCLError = result as KCLError
|
|
|
|
this.diagnostics = kclErrorsToDiagnostics([kclerror])
|
|
|
|
this._hasErrors = true
|
2024-12-06 14:56:53 -08:00
|
|
|
|
|
|
|
await this.checkIfSwitchedFilesShouldClear()
|
2024-12-06 13:57:31 +13:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addDiagnostics(complilationErrorsToDiagnostics(result.errors))
|
|
|
|
this.addDiagnostics(complilationErrorsToDiagnostics(result.warnings))
|
|
|
|
if (result.errors.length > 0) {
|
|
|
|
this._hasErrors = true
|
|
|
|
|
2024-12-06 14:56:53 -08:00
|
|
|
await this.checkIfSwitchedFilesShouldClear()
|
2024-12-06 13:57:31 +13:00
|
|
|
return null
|
|
|
|
}
|
2024-06-24 11:45:40 -04:00
|
|
|
|
2024-12-06 13:57:31 +13:00
|
|
|
return result.program
|
2023-11-06 17:47:45 +11:00
|
|
|
}
|
|
|
|
|
2023-10-17 12:07:48 +11:00
|
|
|
async ensureWasmInit() {
|
|
|
|
try {
|
|
|
|
await initPromise
|
|
|
|
if (this.wasmInitFailed) {
|
|
|
|
this.wasmInitFailed = false
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.wasmInitFailed = true
|
|
|
|
}
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
|
|
|
|
2024-02-19 09:23:18 +11:00
|
|
|
private _cancelTokens: Map<number, boolean> = new Map()
|
|
|
|
|
2024-04-17 20:18:07 -07:00
|
|
|
// This NEVER updates the code, if you want to update the code DO NOT add to
|
|
|
|
// this function, too many other things that don't want it exist.
|
|
|
|
// just call to codeManager from wherever you want in other files.
|
2024-08-14 08:49:00 -07:00
|
|
|
async executeAst(args: ExecuteArgs = {}): Promise<void> {
|
|
|
|
if (this.isExecuting) {
|
|
|
|
this.executeIsStale = args
|
2024-08-30 05:14:24 -05:00
|
|
|
|
|
|
|
// The previous execteAst will be rejected and cleaned up. The execution will be marked as stale.
|
|
|
|
// A new executeAst will start.
|
|
|
|
this.engineCommandManager.rejectAllModelingCommands(
|
|
|
|
EXECUTE_AST_INTERRUPT_ERROR_MESSAGE
|
|
|
|
)
|
2024-08-14 08:49:00 -07:00
|
|
|
// Exit early if we are already executing.
|
|
|
|
return
|
2024-07-04 01:19:24 -04:00
|
|
|
}
|
2024-08-14 08:49:00 -07:00
|
|
|
|
|
|
|
const ast = args.ast || this.ast
|
2024-11-07 17:23:03 -05:00
|
|
|
markOnce('code/startExecuteAst')
|
2024-08-14 08:49:00 -07:00
|
|
|
|
|
|
|
const currentExecutionId = args.executionId || Date.now()
|
2024-02-19 09:23:18 +11:00
|
|
|
this._cancelTokens.set(currentExecutionId, false)
|
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
this.isExecuting = true
|
2024-02-26 21:02:33 +11:00
|
|
|
await this.ensureWasmInit()
|
2024-10-09 19:38:40 -04:00
|
|
|
const { logs, errors, execState, isInterrupted } = await executeAst({
|
2023-10-11 13:36:54 +11:00
|
|
|
ast,
|
|
|
|
engineCommandManager: this.engineCommandManager,
|
|
|
|
})
|
2024-06-26 00:04:52 -04:00
|
|
|
|
2024-08-30 05:14:24 -05:00
|
|
|
// Program was not interrupted, setup the scene
|
|
|
|
// Do not send send scene commands if the program was interrupted, go to clean up
|
|
|
|
if (!isInterrupted) {
|
2024-12-06 13:57:31 +13:00
|
|
|
this.addDiagnostics(await lintAst({ ast: ast }))
|
2024-12-20 13:39:06 +11:00
|
|
|
setSelectionFilterToDefault(this.engineCommandManager)
|
2024-08-30 05:14:24 -05:00
|
|
|
|
|
|
|
if (args.zoomToFit) {
|
|
|
|
let zoomObjectId: string | undefined = ''
|
|
|
|
if (args.zoomOnRangeAndType) {
|
|
|
|
zoomObjectId = this.engineCommandManager?.mapRangeToObjectId(
|
|
|
|
args.zoomOnRangeAndType.range,
|
|
|
|
args.zoomOnRangeAndType.type
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.engineCommandManager.sendSceneCommand({
|
|
|
|
type: 'modeling_cmd_req',
|
|
|
|
cmd_id: uuidv4(),
|
|
|
|
cmd: {
|
|
|
|
type: 'zoom_to_fit',
|
|
|
|
object_ids: zoomObjectId ? [zoomObjectId] : [], // leave empty to zoom to all objects
|
|
|
|
padding: 0.1, // padding around the objects
|
2024-10-04 13:47:44 -07:00
|
|
|
animated: false, // don't animate the zoom for now
|
2024-08-30 05:14:24 -05:00
|
|
|
},
|
|
|
|
})
|
2024-07-04 01:19:24 -04:00
|
|
|
}
|
2024-05-24 12:32:15 -07:00
|
|
|
}
|
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
this.isExecuting = false
|
2024-08-14 08:49:00 -07:00
|
|
|
|
2024-02-19 09:23:18 +11:00
|
|
|
// Check the cancellation token for this execution before applying side effects
|
|
|
|
if (this._cancelTokens.get(currentExecutionId)) {
|
|
|
|
this._cancelTokens.delete(currentExecutionId)
|
|
|
|
return
|
|
|
|
}
|
2024-10-09 10:33:20 -04:00
|
|
|
|
|
|
|
// Exit sketch mode if the AST is empty
|
|
|
|
if (this._isAstEmpty(ast)) {
|
|
|
|
await this.disableSketchMode()
|
|
|
|
}
|
|
|
|
|
2023-11-08 12:27:43 +11:00
|
|
|
this.logs = logs
|
2024-12-20 16:19:59 -05:00
|
|
|
this.errors = errors
|
2024-08-30 05:14:24 -05:00
|
|
|
// Do not add the errors since the program was interrupted and the error is not a real KCL error
|
2024-12-06 13:57:31 +13:00
|
|
|
this.addDiagnostics(isInterrupted ? [] : kclErrorsToDiagnostics(errors))
|
2024-10-09 19:38:40 -04:00
|
|
|
this.execState = execState
|
2024-10-02 13:15:40 +10:00
|
|
|
if (!errors.length) {
|
2024-10-09 19:38:40 -04:00
|
|
|
this.lastSuccessfulProgramMemory = execState.memory
|
2024-12-20 16:19:59 -05:00
|
|
|
this.lastSuccessfulOperations = execState.operations
|
2024-10-02 13:15:40 +10:00
|
|
|
}
|
2023-11-08 12:27:43 +11:00
|
|
|
this.ast = { ...ast }
|
2024-12-12 18:06:26 -08:00
|
|
|
// updateArtifactGraph relies on updated executeState/programMemory
|
|
|
|
await this.engineCommandManager.updateArtifactGraph(this.ast)
|
2023-10-18 08:03:02 +11:00
|
|
|
this._executeCallback()
|
2024-12-16 10:34:11 -05:00
|
|
|
if (!isInterrupted) {
|
2024-12-12 18:06:26 -08:00
|
|
|
sceneInfra.modelingSend({ type: 'code edit during sketch' })
|
2024-12-16 10:34:11 -05:00
|
|
|
}
|
|
|
|
|
2024-03-22 16:55:30 +11:00
|
|
|
this.engineCommandManager.addCommandLog({
|
2023-11-24 08:59:24 +11:00
|
|
|
type: 'execution-done',
|
|
|
|
data: null,
|
|
|
|
})
|
2024-08-30 05:14:24 -05:00
|
|
|
|
2024-02-19 09:23:18 +11:00
|
|
|
this._cancelTokens.delete(currentExecutionId)
|
2024-11-07 17:23:03 -05:00
|
|
|
markOnce('code/endExecuteAst')
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
2024-04-17 20:18:07 -07:00
|
|
|
// NOTE: this always updates the code state and editor.
|
|
|
|
// DO NOT CALL THIS from codemirror ever.
|
2024-02-11 12:59:00 +11:00
|
|
|
async executeAstMock(
|
|
|
|
ast: Program = this._ast,
|
|
|
|
{
|
|
|
|
updates,
|
|
|
|
}: {
|
2024-04-17 20:18:07 -07:00
|
|
|
updates: 'none' | 'artifactRanges'
|
2024-02-11 12:59:00 +11:00
|
|
|
} = { updates: 'none' }
|
|
|
|
) {
|
2023-10-17 12:07:48 +11:00
|
|
|
await this.ensureWasmInit()
|
2024-06-11 19:23:35 -04:00
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
const newCode = recast(ast)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(newCode)) {
|
|
|
|
console.error(newCode)
|
|
|
|
return
|
|
|
|
}
|
2024-12-06 14:56:53 -08:00
|
|
|
const newAst = await this.safeParse(newCode)
|
2024-06-26 00:04:52 -04:00
|
|
|
if (!newAst) {
|
|
|
|
this.clearAst()
|
|
|
|
return
|
|
|
|
}
|
2023-10-11 13:36:54 +11:00
|
|
|
this._ast = { ...newAst }
|
|
|
|
|
2024-10-09 19:38:40 -04:00
|
|
|
const { logs, errors, execState } = await executeAst({
|
2023-10-11 13:36:54 +11:00
|
|
|
ast: newAst,
|
|
|
|
engineCommandManager: this.engineCommandManager,
|
2024-12-05 19:51:06 -08:00
|
|
|
// We make sure to send an empty program memory to denote we mean mock mode.
|
|
|
|
programMemoryOverride: ProgramMemory.empty(),
|
2023-10-11 13:36:54 +11:00
|
|
|
})
|
2024-06-26 00:04:52 -04:00
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
this._logs = logs
|
2024-12-06 13:57:31 +13:00
|
|
|
this.addDiagnostics(kclErrorsToDiagnostics(errors))
|
2024-10-09 19:38:40 -04:00
|
|
|
this._execState = execState
|
|
|
|
this._programMemory = execState.memory
|
2024-10-02 13:15:40 +10:00
|
|
|
if (!errors.length) {
|
2024-10-09 19:38:40 -04:00
|
|
|
this.lastSuccessfulProgramMemory = execState.memory
|
2024-12-20 16:19:59 -05:00
|
|
|
this.lastSuccessfulOperations = execState.operations
|
2024-10-02 13:15:40 +10:00
|
|
|
}
|
2024-04-17 20:18:07 -07:00
|
|
|
if (updates !== 'artifactRanges') return
|
2024-08-03 18:08:51 +10:00
|
|
|
|
|
|
|
// TODO the below seems like a work around, I wish there's a comment explaining exactly what
|
|
|
|
// problem this solves, but either way we should strive to remove it.
|
|
|
|
Array.from(this.engineCommandManager.artifactGraph).forEach(
|
2024-02-11 12:59:00 +11:00
|
|
|
([commandId, artifact]) => {
|
2024-12-16 10:34:11 -05:00
|
|
|
if (!('codeRef' in artifact)) return
|
2024-10-30 16:52:17 -04:00
|
|
|
const _node1 = getNodeFromPath<Node<CallExpression>>(
|
2024-03-22 16:55:30 +11:00
|
|
|
this.ast,
|
2024-08-03 18:08:51 +10:00
|
|
|
artifact.codeRef.pathToNode,
|
2024-02-11 12:59:00 +11:00
|
|
|
'CallExpression'
|
2024-06-24 11:45:40 -04:00
|
|
|
)
|
|
|
|
if (err(_node1)) return
|
|
|
|
const { node } = _node1
|
2024-02-11 12:59:00 +11:00
|
|
|
if (node.type !== 'CallExpression') return
|
2024-08-03 18:08:51 +10:00
|
|
|
const [oldStart, oldEnd] = artifact.codeRef.range
|
2024-02-11 12:59:00 +11:00
|
|
|
if (oldStart === 0 && oldEnd === 0) return
|
|
|
|
if (oldStart === node.start && oldEnd === node.end) return
|
2024-08-03 18:08:51 +10:00
|
|
|
this.engineCommandManager.artifactGraph.set(commandId, {
|
|
|
|
...artifact,
|
|
|
|
codeRef: {
|
|
|
|
...artifact.codeRef,
|
2024-12-06 13:57:31 +13:00
|
|
|
range: [node.start, node.end, true],
|
2024-08-03 18:08:51 +10:00
|
|
|
},
|
|
|
|
})
|
2024-02-11 12:59:00 +11:00
|
|
|
}
|
|
|
|
)
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
2024-02-19 09:23:18 +11:00
|
|
|
cancelAllExecutions() {
|
|
|
|
this._cancelTokens.forEach((_, key) => {
|
|
|
|
this._cancelTokens.set(key, true)
|
|
|
|
})
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
2024-07-23 20:37:04 -04:00
|
|
|
async executeCode(zoomToFit?: boolean): Promise<void> {
|
2024-12-06 14:56:53 -08:00
|
|
|
const ast = await this.safeParse(codeManager.code)
|
2024-06-19 16:15:22 -04:00
|
|
|
if (!ast) {
|
|
|
|
this.clearAst()
|
|
|
|
return
|
|
|
|
}
|
2024-04-18 12:40:05 -07:00
|
|
|
this.ast = { ...ast }
|
2024-08-14 08:49:00 -07:00
|
|
|
return this.executeAst({ zoomToFit })
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
2024-12-06 14:56:53 -08:00
|
|
|
async format() {
|
2024-04-17 20:18:07 -07:00
|
|
|
const originalCode = codeManager.code
|
2024-12-06 14:56:53 -08:00
|
|
|
const ast = await this.safeParse(originalCode)
|
2024-06-19 16:15:22 -04:00
|
|
|
if (!ast) {
|
|
|
|
this.clearAst()
|
|
|
|
return
|
|
|
|
}
|
2024-04-17 20:18:07 -07:00
|
|
|
const code = recast(ast)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(code)) {
|
|
|
|
console.error(code)
|
|
|
|
return
|
|
|
|
}
|
2024-04-17 20:18:07 -07:00
|
|
|
if (originalCode === code) return
|
|
|
|
|
|
|
|
// Update the code state and the editor.
|
|
|
|
codeManager.updateCodeStateEditor(code)
|
2024-11-08 17:16:46 -05:00
|
|
|
|
2024-11-16 16:49:44 -05:00
|
|
|
// Write back to the file system.
|
|
|
|
void codeManager.writeToFile().then(() => this.executeCode())
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
2023-11-01 17:34:54 -05:00
|
|
|
// There's overlapping responsibility between updateAst and executeAst.
|
2023-10-11 13:36:54 +11:00
|
|
|
// updateAst was added as it was used a lot before xState migration so makes the port easier.
|
|
|
|
// but should probably have think about which of the function to keep
|
2024-04-17 20:18:07 -07:00
|
|
|
// This always updates the code state and editor and writes to the file system.
|
2023-10-11 13:36:54 +11:00
|
|
|
async updateAst(
|
2024-10-30 16:52:17 -04:00
|
|
|
ast: Node<Program>,
|
2023-10-11 13:36:54 +11:00
|
|
|
execute: boolean,
|
|
|
|
optionalParams?: {
|
2024-09-23 08:07:31 +02:00
|
|
|
focusPath?: Array<PathToNode>
|
2024-07-04 01:19:24 -04:00
|
|
|
zoomToFit?: boolean
|
|
|
|
zoomOnRangeAndType?: {
|
|
|
|
range: SourceRange
|
|
|
|
type: string
|
|
|
|
}
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
2024-06-24 11:45:40 -04:00
|
|
|
): Promise<{
|
2024-10-30 16:52:17 -04:00
|
|
|
newAst: Node<Program>
|
2024-06-24 11:45:40 -04:00
|
|
|
selections?: Selections
|
|
|
|
}> {
|
2023-10-11 13:36:54 +11:00
|
|
|
const newCode = recast(ast)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(newCode)) return Promise.reject(newCode)
|
|
|
|
|
2024-12-06 14:56:53 -08:00
|
|
|
const astWithUpdatedSource = await this.safeParse(newCode)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (!astWithUpdatedSource) return Promise.reject(new Error('bad ast'))
|
|
|
|
let returnVal: Selections | undefined = undefined
|
2023-10-11 13:36:54 +11:00
|
|
|
|
|
|
|
if (optionalParams?.focusPath) {
|
|
|
|
returnVal = {
|
2024-11-21 15:04:30 +11:00
|
|
|
graphSelections: [],
|
2024-09-23 08:07:31 +02:00
|
|
|
otherSelections: [],
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const path of optionalParams.focusPath) {
|
|
|
|
const getNodeFromPathResult = getNodeFromPath<any>(
|
|
|
|
astWithUpdatedSource,
|
|
|
|
path
|
|
|
|
)
|
|
|
|
if (err(getNodeFromPathResult))
|
|
|
|
return Promise.reject(getNodeFromPathResult)
|
|
|
|
const { node } = getNodeFromPathResult
|
|
|
|
|
|
|
|
const { start, end } = node
|
|
|
|
|
|
|
|
if (!start || !end)
|
|
|
|
return {
|
|
|
|
selections: undefined,
|
|
|
|
newAst: astWithUpdatedSource,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start && end) {
|
2024-11-21 15:04:30 +11:00
|
|
|
returnVal.graphSelections.push({
|
|
|
|
codeRef: {
|
2024-12-06 13:57:31 +13:00
|
|
|
range: [start, end, true],
|
2024-11-21 15:04:30 +11:00
|
|
|
pathToNode: path,
|
|
|
|
},
|
2024-09-23 08:07:31 +02:00
|
|
|
})
|
|
|
|
}
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (execute) {
|
2024-08-14 08:49:00 -07:00
|
|
|
await this.executeAst({
|
|
|
|
ast: astWithUpdatedSource,
|
|
|
|
zoomToFit: optionalParams?.zoomToFit,
|
|
|
|
zoomOnRangeAndType: optionalParams?.zoomOnRangeAndType,
|
|
|
|
})
|
2023-10-11 13:36:54 +11:00
|
|
|
} 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
|
2024-04-17 20:18:07 -07:00
|
|
|
// instead..
|
|
|
|
// Execute ast mock will update the code state and editor.
|
|
|
|
await this.executeAstMock(astWithUpdatedSource)
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
2024-06-24 11:45:40 -04:00
|
|
|
|
|
|
|
return { selections: returnVal, newAst: astWithUpdatedSource }
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
2024-02-17 07:04:24 +11:00
|
|
|
|
|
|
|
get defaultPlanes() {
|
|
|
|
return this?.engineCommandManager?.defaultPlanes
|
|
|
|
}
|
|
|
|
|
2024-06-18 16:08:41 +10:00
|
|
|
showPlanes(all = false) {
|
|
|
|
if (!this.defaultPlanes) return Promise.all([])
|
|
|
|
const thePromises = [
|
|
|
|
this.engineCommandManager.setPlaneHidden(this.defaultPlanes.xy, false),
|
|
|
|
this.engineCommandManager.setPlaneHidden(this.defaultPlanes.yz, false),
|
|
|
|
this.engineCommandManager.setPlaneHidden(this.defaultPlanes.xz, false),
|
|
|
|
]
|
|
|
|
if (all) {
|
|
|
|
thePromises.push(
|
|
|
|
this.engineCommandManager.setPlaneHidden(
|
|
|
|
this.defaultPlanes.negXy,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
)
|
|
|
|
thePromises.push(
|
|
|
|
this.engineCommandManager.setPlaneHidden(
|
|
|
|
this.defaultPlanes.negYz,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
)
|
|
|
|
thePromises.push(
|
|
|
|
this.engineCommandManager.setPlaneHidden(
|
|
|
|
this.defaultPlanes.negXz,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return Promise.all(thePromises)
|
2024-02-17 07:04:24 +11:00
|
|
|
}
|
|
|
|
|
2024-06-18 16:08:41 +10:00
|
|
|
hidePlanes(all = false) {
|
|
|
|
if (!this.defaultPlanes) return Promise.all([])
|
|
|
|
const thePromises = [
|
|
|
|
this.engineCommandManager.setPlaneHidden(this.defaultPlanes.xy, true),
|
|
|
|
this.engineCommandManager.setPlaneHidden(this.defaultPlanes.yz, true),
|
|
|
|
this.engineCommandManager.setPlaneHidden(this.defaultPlanes.xz, true),
|
|
|
|
]
|
|
|
|
if (all) {
|
|
|
|
thePromises.push(
|
|
|
|
this.engineCommandManager.setPlaneHidden(this.defaultPlanes.negXy, true)
|
|
|
|
)
|
|
|
|
thePromises.push(
|
|
|
|
this.engineCommandManager.setPlaneHidden(this.defaultPlanes.negYz, true)
|
|
|
|
)
|
|
|
|
thePromises.push(
|
|
|
|
this.engineCommandManager.setPlaneHidden(this.defaultPlanes.negXz, true)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return Promise.all(thePromises)
|
2024-02-17 07:04:24 +11:00
|
|
|
}
|
2024-11-26 11:36:14 -05:00
|
|
|
/** TODO: this function is hiding unawaited asynchronous work */
|
2024-12-20 13:39:06 +11:00
|
|
|
defaultSelectionFilter(selectionsToRestore?: Selections) {
|
|
|
|
setSelectionFilterToDefault(this.engineCommandManager, selectionsToRestore)
|
2024-11-26 11:36:14 -05:00
|
|
|
}
|
|
|
|
/** TODO: this function is hiding unawaited asynchronous work */
|
|
|
|
setSelectionFilter(filter: EntityType_type[]) {
|
|
|
|
setSelectionFilter(filter, this.engineCommandManager)
|
2024-05-21 05:55:34 +10:00
|
|
|
}
|
2024-10-09 10:33:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We can send a single command of 'enable_sketch_mode' or send this in a batched request.
|
|
|
|
* When there is no code in the KCL editor we should be sending 'sketch_mode_disable' since any previous half finished
|
|
|
|
* code could leave the state of the application in sketch mode on the engine side.
|
|
|
|
*/
|
|
|
|
async disableSketchMode() {
|
|
|
|
await this.engineCommandManager.sendSceneCommand({
|
|
|
|
type: 'modeling_cmd_req',
|
|
|
|
cmd_id: uuidv4(),
|
|
|
|
cmd: { type: 'sketch_mode_disable' },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determines if there is no KCL code which means it is executing a blank KCL file
|
2024-10-30 16:52:17 -04:00
|
|
|
_isAstEmpty(ast: Node<Program>) {
|
2024-10-09 10:33:20 -04:00
|
|
|
return ast.start === 0 && ast.end === 0 && ast.body.length === 0
|
|
|
|
}
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
|
|
|
|
2024-11-26 11:36:14 -05:00
|
|
|
const defaultSelectionFilter: EntityType_type[] = [
|
|
|
|
'face',
|
|
|
|
'edge',
|
|
|
|
'solid2d',
|
|
|
|
'curve',
|
|
|
|
'object',
|
|
|
|
]
|
|
|
|
|
|
|
|
/** TODO: This function is not synchronous but is currently treated as such */
|
|
|
|
function setSelectionFilterToDefault(
|
2024-12-20 13:39:06 +11:00
|
|
|
engineCommandManager: EngineCommandManager,
|
|
|
|
selectionsToRestore?: Selections
|
2024-03-22 16:55:30 +11:00
|
|
|
) {
|
2024-09-09 18:17:45 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2024-12-20 13:39:06 +11:00
|
|
|
setSelectionFilter(
|
|
|
|
defaultSelectionFilter,
|
|
|
|
engineCommandManager,
|
|
|
|
selectionsToRestore
|
|
|
|
)
|
2024-11-26 11:36:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** TODO: This function is not synchronous but is currently treated as such */
|
|
|
|
function setSelectionFilter(
|
|
|
|
filter: EntityType_type[],
|
2024-12-20 13:39:06 +11:00
|
|
|
engineCommandManager: EngineCommandManager,
|
|
|
|
selectionsToRestore?: Selections
|
2024-11-26 11:36:14 -05:00
|
|
|
) {
|
2024-12-20 13:39:06 +11:00
|
|
|
const { engineEvents } = selectionsToRestore
|
|
|
|
? handleSelectionBatch({
|
|
|
|
selections: selectionsToRestore,
|
|
|
|
})
|
|
|
|
: { engineEvents: undefined }
|
|
|
|
if (!selectionsToRestore || !engineEvents) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
|
|
engineCommandManager.sendSceneCommand({
|
|
|
|
type: 'modeling_cmd_req',
|
|
|
|
cmd_id: uuidv4(),
|
|
|
|
cmd: {
|
|
|
|
type: 'set_selection_filter',
|
|
|
|
filter,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const modelingCmd: ModelingCmdReq_type[] = []
|
|
|
|
engineEvents.forEach((event) => {
|
|
|
|
if (event.type === 'modeling_cmd_req') {
|
|
|
|
modelingCmd.push({
|
|
|
|
cmd_id: uuidv4(),
|
|
|
|
cmd: event.cmd,
|
|
|
|
})
|
|
|
|
}
|
2024-11-26 11:36:14 -05:00
|
|
|
})
|
2024-12-20 13:39:06 +11:00
|
|
|
// batch is needed other wise the selection flickers.
|
|
|
|
engineCommandManager
|
|
|
|
.sendSceneCommand({
|
|
|
|
type: 'modeling_cmd_batch_req',
|
|
|
|
batch_id: uuidv4(),
|
|
|
|
requests: [
|
|
|
|
{
|
|
|
|
cmd_id: uuidv4(),
|
|
|
|
cmd: {
|
|
|
|
type: 'set_selection_filter',
|
|
|
|
filter,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
...modelingCmd,
|
|
|
|
],
|
|
|
|
responses: false,
|
|
|
|
})
|
|
|
|
.catch(reportError)
|
2024-03-22 10:23:04 +11:00
|
|
|
}
|