Refactor Edge Treatment Module to Break Cyclic Dependency (#5243)
* break cycle * yarn fmt
This commit is contained in:
@ -27,7 +27,12 @@ import { getNodePathFromSourceRange } from 'lang/queryAstNodePathUtils'
|
|||||||
import { createLiteral } from 'lang/modifyAst'
|
import { createLiteral } from 'lang/modifyAst'
|
||||||
import { err } from 'lib/trap'
|
import { err } from 'lib/trap'
|
||||||
import { Selection, Selections } from 'lib/selections'
|
import { Selection, Selections } from 'lib/selections'
|
||||||
import { engineCommandManager, kclManager } from 'lib/singletons'
|
import {
|
||||||
|
codeManager,
|
||||||
|
editorManager,
|
||||||
|
engineCommandManager,
|
||||||
|
kclManager,
|
||||||
|
} from 'lib/singletons'
|
||||||
import { VITE_KC_DEV_TOKEN } from 'env'
|
import { VITE_KC_DEV_TOKEN } from 'env'
|
||||||
import { isOverlap } from 'lib/utils'
|
import { isOverlap } from 'lib/utils'
|
||||||
import { codeRefFromRange } from 'lang/std/artifactGraph'
|
import { codeRefFromRange } from 'lang/std/artifactGraph'
|
||||||
@ -55,6 +60,13 @@ afterAll(() => {
|
|||||||
engineCommandManager.tearDown()
|
engineCommandManager.tearDown()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const dependencies = {
|
||||||
|
kclManager,
|
||||||
|
engineCommandManager,
|
||||||
|
editorManager,
|
||||||
|
codeManager,
|
||||||
|
}
|
||||||
|
|
||||||
const runGetPathToExtrudeForSegmentSelectionTest = async (
|
const runGetPathToExtrudeForSegmentSelectionTest = async (
|
||||||
code: string,
|
code: string,
|
||||||
selectedSegmentSnippet: string,
|
selectedSegmentSnippet: string,
|
||||||
@ -131,7 +143,8 @@ const runGetPathToExtrudeForSegmentSelectionTest = async (
|
|||||||
const pathResult = getPathToExtrudeForSegmentSelection(
|
const pathResult = getPathToExtrudeForSegmentSelection(
|
||||||
ast,
|
ast,
|
||||||
selection,
|
selection,
|
||||||
artifactGraph
|
artifactGraph,
|
||||||
|
dependencies
|
||||||
)
|
)
|
||||||
if (err(pathResult)) return pathResult
|
if (err(pathResult)) return pathResult
|
||||||
const { pathToExtrudeNode } = pathResult
|
const { pathToExtrudeNode } = pathResult
|
||||||
@ -289,7 +302,12 @@ const runModifyAstCloneWithEdgeTreatmentAndTag = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// apply edge treatment to selection
|
// apply edge treatment to selection
|
||||||
const result = modifyAstWithEdgeTreatmentAndTag(ast, selection, parameters)
|
const result = modifyAstWithEdgeTreatmentAndTag(
|
||||||
|
ast,
|
||||||
|
selection,
|
||||||
|
parameters,
|
||||||
|
dependencies
|
||||||
|
)
|
||||||
if (err(result)) {
|
if (err(result)) {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
@ -34,13 +34,11 @@ import { err, trap } from 'lib/trap'
|
|||||||
import { Selection, Selections } from 'lib/selections'
|
import { Selection, Selections } from 'lib/selections'
|
||||||
import { KclCommandValue } from 'lib/commandTypes'
|
import { KclCommandValue } from 'lib/commandTypes'
|
||||||
import { Artifact, getSweepFromSuspectedPath } from 'lang/std/artifactGraph'
|
import { Artifact, getSweepFromSuspectedPath } from 'lang/std/artifactGraph'
|
||||||
import {
|
|
||||||
kclManager,
|
|
||||||
engineCommandManager,
|
|
||||||
editorManager,
|
|
||||||
codeManager,
|
|
||||||
} from 'lib/singletons'
|
|
||||||
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
||||||
|
import { KclManager } from 'lang/KclSingleton'
|
||||||
|
import { EngineCommandManager } from 'lang/std/engineConnection'
|
||||||
|
import EditorManager from 'editor/manager'
|
||||||
|
import CodeManager from 'lang/codeManager'
|
||||||
|
|
||||||
// Edge Treatment Types
|
// Edge Treatment Types
|
||||||
export enum EdgeTreatmentType {
|
export enum EdgeTreatmentType {
|
||||||
@ -62,21 +60,38 @@ export type EdgeTreatmentParameters = ChamferParameters | FilletParameters
|
|||||||
export async function applyEdgeTreatmentToSelection(
|
export async function applyEdgeTreatmentToSelection(
|
||||||
ast: Node<Program>,
|
ast: Node<Program>,
|
||||||
selection: Selections,
|
selection: Selections,
|
||||||
parameters: EdgeTreatmentParameters
|
parameters: EdgeTreatmentParameters,
|
||||||
|
dependencies: {
|
||||||
|
kclManager: KclManager
|
||||||
|
engineCommandManager: EngineCommandManager
|
||||||
|
editorManager: EditorManager
|
||||||
|
codeManager: CodeManager
|
||||||
|
}
|
||||||
): Promise<void | Error> {
|
): Promise<void | Error> {
|
||||||
// 1. clone and modify with edge treatment and tag
|
// 1. clone and modify with edge treatment and tag
|
||||||
const result = modifyAstWithEdgeTreatmentAndTag(ast, selection, parameters)
|
const result = modifyAstWithEdgeTreatmentAndTag(
|
||||||
|
ast,
|
||||||
|
selection,
|
||||||
|
parameters,
|
||||||
|
dependencies
|
||||||
|
)
|
||||||
if (err(result)) return result
|
if (err(result)) return result
|
||||||
const { modifiedAst, pathToEdgeTreatmentNode } = result
|
const { modifiedAst, pathToEdgeTreatmentNode } = result
|
||||||
|
|
||||||
// 2. update ast
|
// 2. update ast
|
||||||
await updateAstAndFocus(modifiedAst, pathToEdgeTreatmentNode)
|
await updateAstAndFocus(modifiedAst, pathToEdgeTreatmentNode, dependencies)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function modifyAstWithEdgeTreatmentAndTag(
|
export function modifyAstWithEdgeTreatmentAndTag(
|
||||||
ast: Node<Program>,
|
ast: Node<Program>,
|
||||||
selections: Selections,
|
selections: Selections,
|
||||||
parameters: EdgeTreatmentParameters
|
parameters: EdgeTreatmentParameters,
|
||||||
|
dependencies: {
|
||||||
|
kclManager: KclManager
|
||||||
|
engineCommandManager: EngineCommandManager
|
||||||
|
editorManager: EditorManager
|
||||||
|
codeManager: CodeManager
|
||||||
|
}
|
||||||
):
|
):
|
||||||
| { modifiedAst: Node<Program>; pathToEdgeTreatmentNode: Array<PathToNode> }
|
| { modifiedAst: Node<Program>; pathToEdgeTreatmentNode: Array<PathToNode> }
|
||||||
| Error {
|
| Error {
|
||||||
@ -86,7 +101,7 @@ export function modifyAstWithEdgeTreatmentAndTag(
|
|||||||
const astResult = insertParametersIntoAst(clonedAst, parameters)
|
const astResult = insertParametersIntoAst(clonedAst, parameters)
|
||||||
if (err(astResult)) return astResult
|
if (err(astResult)) return astResult
|
||||||
|
|
||||||
const artifactGraph = engineCommandManager.artifactGraph
|
const artifactGraph = dependencies.engineCommandManager.artifactGraph
|
||||||
|
|
||||||
// Step 1: modify ast with tags and group them by extrude nodes (bodies)
|
// Step 1: modify ast with tags and group them by extrude nodes (bodies)
|
||||||
const extrudeToTagsMap: Map<
|
const extrudeToTagsMap: Map<
|
||||||
@ -99,7 +114,8 @@ export function modifyAstWithEdgeTreatmentAndTag(
|
|||||||
const result = getPathToExtrudeForSegmentSelection(
|
const result = getPathToExtrudeForSegmentSelection(
|
||||||
clonedAstForGetExtrude,
|
clonedAstForGetExtrude,
|
||||||
selection,
|
selection,
|
||||||
artifactGraph
|
artifactGraph,
|
||||||
|
dependencies
|
||||||
)
|
)
|
||||||
if (err(result)) return result
|
if (err(result)) return result
|
||||||
const { pathToSegmentNode, pathToExtrudeNode } = result
|
const { pathToSegmentNode, pathToExtrudeNode } = result
|
||||||
@ -252,7 +268,13 @@ function insertParametersIntoAst(
|
|||||||
export function getPathToExtrudeForSegmentSelection(
|
export function getPathToExtrudeForSegmentSelection(
|
||||||
ast: Program,
|
ast: Program,
|
||||||
selection: Selection,
|
selection: Selection,
|
||||||
artifactGraph: ArtifactGraph
|
artifactGraph: ArtifactGraph,
|
||||||
|
dependencies: {
|
||||||
|
kclManager: KclManager
|
||||||
|
engineCommandManager: EngineCommandManager
|
||||||
|
editorManager: EditorManager
|
||||||
|
codeManager: CodeManager
|
||||||
|
}
|
||||||
): { pathToSegmentNode: PathToNode; pathToExtrudeNode: PathToNode } | Error {
|
): { pathToSegmentNode: PathToNode; pathToExtrudeNode: PathToNode } | Error {
|
||||||
const pathToSegmentNode = getNodePathFromSourceRange(
|
const pathToSegmentNode = getNodePathFromSourceRange(
|
||||||
ast,
|
ast,
|
||||||
@ -268,7 +290,7 @@ export function getPathToExtrudeForSegmentSelection(
|
|||||||
const sketchVar = varDecNode.node.declaration.id.name
|
const sketchVar = varDecNode.node.declaration.id.name
|
||||||
|
|
||||||
const sketch = sketchFromKclValue(
|
const sketch = sketchFromKclValue(
|
||||||
kclManager.programMemory.get(sketchVar),
|
dependencies.kclManager.programMemory.get(sketchVar),
|
||||||
sketchVar
|
sketchVar
|
||||||
)
|
)
|
||||||
if (trap(sketch)) return sketch
|
if (trap(sketch)) return sketch
|
||||||
@ -287,16 +309,28 @@ export function getPathToExtrudeForSegmentSelection(
|
|||||||
|
|
||||||
async function updateAstAndFocus(
|
async function updateAstAndFocus(
|
||||||
modifiedAst: Node<Program>,
|
modifiedAst: Node<Program>,
|
||||||
pathToEdgeTreatmentNode: Array<PathToNode>
|
pathToEdgeTreatmentNode: Array<PathToNode>,
|
||||||
|
dependencies: {
|
||||||
|
kclManager: KclManager
|
||||||
|
engineCommandManager: EngineCommandManager
|
||||||
|
editorManager: EditorManager
|
||||||
|
codeManager: CodeManager
|
||||||
|
}
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const updatedAst = await kclManager.updateAst(modifiedAst, true, {
|
const updatedAst = await dependencies.kclManager.updateAst(
|
||||||
focusPath: pathToEdgeTreatmentNode,
|
modifiedAst,
|
||||||
})
|
true,
|
||||||
|
{
|
||||||
|
focusPath: pathToEdgeTreatmentNode,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
await codeManager.updateEditorWithAstAndWriteToFile(updatedAst.newAst)
|
await dependencies.codeManager.updateEditorWithAstAndWriteToFile(
|
||||||
|
updatedAst.newAst
|
||||||
|
)
|
||||||
|
|
||||||
if (updatedAst?.selections) {
|
if (updatedAst?.selections) {
|
||||||
editorManager.selectRange(updatedAst?.selections)
|
dependencies.editorManager.selectRange(updatedAst?.selections)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,17 +19,28 @@ import {
|
|||||||
createVariableDeclaration,
|
createVariableDeclaration,
|
||||||
} from 'lang/modifyAst'
|
} from 'lang/modifyAst'
|
||||||
import { KCL_DEFAULT_CONSTANT_PREFIXES } from 'lib/constants'
|
import { KCL_DEFAULT_CONSTANT_PREFIXES } from 'lib/constants'
|
||||||
|
import { KclManager } from 'lang/KclSingleton'
|
||||||
|
import { EngineCommandManager } from 'lang/std/engineConnection'
|
||||||
|
import EditorManager from 'editor/manager'
|
||||||
|
import CodeManager from 'lang/codeManager'
|
||||||
|
|
||||||
export function addShell({
|
export function addShell({
|
||||||
node,
|
node,
|
||||||
selection,
|
selection,
|
||||||
artifactGraph,
|
artifactGraph,
|
||||||
thickness,
|
thickness,
|
||||||
|
dependencies,
|
||||||
}: {
|
}: {
|
||||||
node: Node<Program>
|
node: Node<Program>
|
||||||
selection: Selections
|
selection: Selections
|
||||||
artifactGraph: ArtifactGraph
|
artifactGraph: ArtifactGraph
|
||||||
thickness: Expr
|
thickness: Expr
|
||||||
|
dependencies: {
|
||||||
|
kclManager: KclManager
|
||||||
|
engineCommandManager: EngineCommandManager
|
||||||
|
editorManager: EditorManager
|
||||||
|
codeManager: CodeManager
|
||||||
|
}
|
||||||
}): Error | { modifiedAst: Node<Program>; pathToNode: PathToNode } {
|
}): Error | { modifiedAst: Node<Program>; pathToNode: PathToNode } {
|
||||||
const modifiedAst = structuredClone(node)
|
const modifiedAst = structuredClone(node)
|
||||||
|
|
||||||
@ -42,7 +53,8 @@ export function addShell({
|
|||||||
const extrudeLookupResult = getPathToExtrudeForSegmentSelection(
|
const extrudeLookupResult = getPathToExtrudeForSegmentSelection(
|
||||||
clonedAstForGetExtrude,
|
clonedAstForGetExtrude,
|
||||||
graphSelection,
|
graphSelection,
|
||||||
artifactGraph
|
artifactGraph,
|
||||||
|
dependencies
|
||||||
)
|
)
|
||||||
if (err(extrudeLookupResult)) {
|
if (err(extrudeLookupResult)) {
|
||||||
return new Error("Couldn't find extrude")
|
return new Error("Couldn't find extrude")
|
||||||
|
@ -1681,6 +1681,12 @@ export const modelingMachine = setup({
|
|||||||
// Extract inputs
|
// Extract inputs
|
||||||
const ast = kclManager.ast
|
const ast = kclManager.ast
|
||||||
const { selection, thickness } = input
|
const { selection, thickness } = input
|
||||||
|
const dependencies = {
|
||||||
|
kclManager,
|
||||||
|
engineCommandManager,
|
||||||
|
editorManager,
|
||||||
|
codeManager,
|
||||||
|
}
|
||||||
|
|
||||||
// Insert the thickness variable if it exists
|
// Insert the thickness variable if it exists
|
||||||
if (
|
if (
|
||||||
@ -1706,6 +1712,7 @@ export const modelingMachine = setup({
|
|||||||
'variableName' in thickness
|
'variableName' in thickness
|
||||||
? thickness.variableIdentifierAst
|
? thickness.variableIdentifierAst
|
||||||
: thickness.valueAst,
|
: thickness.valueAst,
|
||||||
|
dependencies,
|
||||||
})
|
})
|
||||||
if (err(shellResult)) {
|
if (err(shellResult)) {
|
||||||
return err(shellResult)
|
return err(shellResult)
|
||||||
@ -1745,12 +1752,19 @@ export const modelingMachine = setup({
|
|||||||
type: EdgeTreatmentType.Fillet,
|
type: EdgeTreatmentType.Fillet,
|
||||||
radius,
|
radius,
|
||||||
}
|
}
|
||||||
|
const dependencies = {
|
||||||
|
kclManager,
|
||||||
|
engineCommandManager,
|
||||||
|
editorManager,
|
||||||
|
codeManager,
|
||||||
|
}
|
||||||
|
|
||||||
// Apply fillet to selection
|
// Apply fillet to selection
|
||||||
const filletResult = await applyEdgeTreatmentToSelection(
|
const filletResult = await applyEdgeTreatmentToSelection(
|
||||||
ast,
|
ast,
|
||||||
selection,
|
selection,
|
||||||
parameters
|
parameters,
|
||||||
|
dependencies
|
||||||
)
|
)
|
||||||
if (err(filletResult)) return filletResult
|
if (err(filletResult)) return filletResult
|
||||||
}
|
}
|
||||||
@ -1772,12 +1786,19 @@ export const modelingMachine = setup({
|
|||||||
type: EdgeTreatmentType.Chamfer,
|
type: EdgeTreatmentType.Chamfer,
|
||||||
length,
|
length,
|
||||||
}
|
}
|
||||||
|
const dependencies = {
|
||||||
|
kclManager,
|
||||||
|
engineCommandManager,
|
||||||
|
editorManager,
|
||||||
|
codeManager,
|
||||||
|
}
|
||||||
|
|
||||||
// Apply chamfer to selection
|
// Apply chamfer to selection
|
||||||
const chamferResult = await applyEdgeTreatmentToSelection(
|
const chamferResult = await applyEdgeTreatmentToSelection(
|
||||||
ast,
|
ast,
|
||||||
selection,
|
selection,
|
||||||
parameters
|
parameters,
|
||||||
|
dependencies
|
||||||
)
|
)
|
||||||
if (err(chamferResult)) return chamferResult
|
if (err(chamferResult)) return chamferResult
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user