2024-07-02 17:16:27 +10:00
|
|
|
import {
|
|
|
|
|
Program,
|
2025-02-12 10:22:56 +13:00
|
|
|
executeWithEngine,
|
|
|
|
|
executeMock,
|
2024-07-02 17:16:27 +10:00
|
|
|
kclLint,
|
2024-10-09 19:38:40 -04:00
|
|
|
emptyExecState,
|
|
|
|
|
ExecState,
|
2025-02-12 10:22:56 +13:00
|
|
|
VariableMap,
|
2024-07-02 17:16:27 +10:00
|
|
|
} from 'lang/wasm'
|
|
|
|
|
import { EngineCommandManager } from 'lang/std/engineConnection'
|
|
|
|
|
import { KCLError } from 'lang/errors'
|
|
|
|
|
import { Diagnostic } from '@codemirror/lint'
|
2024-10-30 16:52:17 -04:00
|
|
|
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
2024-07-02 17:16:27 +10:00
|
|
|
|
|
|
|
|
export type ToolTip =
|
|
|
|
|
| 'lineTo'
|
|
|
|
|
| 'line'
|
|
|
|
|
| 'angledLine'
|
|
|
|
|
| 'angledLineOfXLength'
|
|
|
|
|
| 'angledLineOfYLength'
|
|
|
|
|
| 'angledLineToX'
|
|
|
|
|
| 'angledLineToY'
|
|
|
|
|
| 'xLine'
|
|
|
|
|
| 'yLine'
|
|
|
|
|
| 'xLineTo'
|
|
|
|
|
| 'yLineTo'
|
|
|
|
|
| 'angledLineThatIntersects'
|
|
|
|
|
| 'tangentialArcTo'
|
2024-09-23 22:42:51 +10:00
|
|
|
| 'circle'
|
2025-02-15 00:57:04 +11:00
|
|
|
| 'circleThreePoint'
|
2024-07-02 17:16:27 +10:00
|
|
|
|
2024-09-23 22:42:51 +10:00
|
|
|
export const toolTips: Array<ToolTip> = [
|
2024-07-02 17:16:27 +10:00
|
|
|
'line',
|
|
|
|
|
'lineTo',
|
|
|
|
|
'angledLine',
|
|
|
|
|
'angledLineOfXLength',
|
|
|
|
|
'angledLineOfYLength',
|
|
|
|
|
'angledLineToX',
|
|
|
|
|
'angledLineToY',
|
|
|
|
|
'xLine',
|
|
|
|
|
'yLine',
|
|
|
|
|
'xLineTo',
|
|
|
|
|
'yLineTo',
|
|
|
|
|
'angledLineThatIntersects',
|
|
|
|
|
'tangentialArcTo',
|
2025-02-15 00:57:04 +11:00
|
|
|
'circleThreePoint',
|
2024-09-13 21:14:14 +10:00
|
|
|
]
|
2024-07-02 17:16:27 +10:00
|
|
|
|
|
|
|
|
export async function executeAst({
|
|
|
|
|
ast,
|
2025-01-28 15:43:39 -08:00
|
|
|
path,
|
2024-07-02 17:16:27 +10:00
|
|
|
engineCommandManager,
|
2025-02-12 10:22:56 +13:00
|
|
|
isMock,
|
|
|
|
|
usePrevMemory,
|
|
|
|
|
variables,
|
2024-07-02 17:16:27 +10:00
|
|
|
}: {
|
2024-10-30 16:52:17 -04:00
|
|
|
ast: Node<Program>
|
2025-01-28 15:43:39 -08:00
|
|
|
path?: string
|
2024-07-02 17:16:27 +10:00
|
|
|
engineCommandManager: EngineCommandManager
|
2025-02-12 10:22:56 +13:00
|
|
|
isMock: boolean
|
|
|
|
|
usePrevMemory?: boolean
|
|
|
|
|
variables?: VariableMap
|
2024-08-30 05:14:24 -05:00
|
|
|
isInterrupted?: boolean
|
2024-07-02 17:16:27 +10:00
|
|
|
}): Promise<{
|
|
|
|
|
logs: string[]
|
|
|
|
|
errors: KCLError[]
|
2024-10-09 19:38:40 -04:00
|
|
|
execState: ExecState
|
2024-08-30 05:14:24 -05:00
|
|
|
isInterrupted: boolean
|
2024-07-02 17:16:27 +10:00
|
|
|
}> {
|
|
|
|
|
try {
|
2025-02-12 10:22:56 +13:00
|
|
|
const execState = await (isMock
|
|
|
|
|
? executeMock(ast, usePrevMemory, path, variables)
|
|
|
|
|
: executeWithEngine(ast, engineCommandManager, path))
|
2024-12-05 19:51:06 -08:00
|
|
|
|
2024-12-12 18:06:26 -08:00
|
|
|
await engineCommandManager.waitForAllCommands()
|
2024-07-02 17:16:27 +10:00
|
|
|
return {
|
|
|
|
|
logs: [],
|
|
|
|
|
errors: [],
|
2024-10-09 19:38:40 -04:00
|
|
|
execState,
|
2024-08-30 05:14:24 -05:00
|
|
|
isInterrupted: false,
|
2024-07-02 17:16:27 +10:00
|
|
|
}
|
|
|
|
|
} catch (e: any) {
|
2024-08-30 05:14:24 -05:00
|
|
|
let isInterrupted = false
|
2024-07-02 17:16:27 +10:00
|
|
|
if (e instanceof KCLError) {
|
2024-08-30 05:14:24 -05:00
|
|
|
// Detect if it is a force interrupt error which is not a KCL processing error.
|
|
|
|
|
if (
|
|
|
|
|
e.msg ===
|
|
|
|
|
'Failed to wait for promise from engine: JsValue("Force interrupt, executionIsStale, new AST requested")'
|
|
|
|
|
) {
|
|
|
|
|
isInterrupted = true
|
|
|
|
|
}
|
2024-07-02 17:16:27 +10:00
|
|
|
return {
|
|
|
|
|
errors: [e],
|
|
|
|
|
logs: [],
|
2024-10-09 19:38:40 -04:00
|
|
|
execState: emptyExecState(),
|
2024-08-30 05:14:24 -05:00
|
|
|
isInterrupted,
|
2024-07-02 17:16:27 +10:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.log(e)
|
|
|
|
|
return {
|
|
|
|
|
logs: [e],
|
|
|
|
|
errors: [],
|
2024-10-09 19:38:40 -04:00
|
|
|
execState: emptyExecState(),
|
2024-08-30 05:14:24 -05:00
|
|
|
isInterrupted,
|
2024-07-02 17:16:27 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function lintAst({
|
|
|
|
|
ast,
|
|
|
|
|
}: {
|
|
|
|
|
ast: Program
|
|
|
|
|
}): Promise<Array<Diagnostic>> {
|
|
|
|
|
try {
|
|
|
|
|
const discovered_findings = await kclLint(ast)
|
|
|
|
|
return discovered_findings.map((lint) => {
|
|
|
|
|
return {
|
|
|
|
|
message: lint.finding.title,
|
|
|
|
|
severity: 'info',
|
|
|
|
|
from: lint.pos[0],
|
|
|
|
|
to: lint.pos[1],
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
console.log(e)
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
}
|