diff --git a/e2e/playwright/point-click-assemblies.spec.ts b/e2e/playwright/point-click-assemblies.spec.ts index 0746cc110..b0e16958d 100644 --- a/e2e/playwright/point-click-assemblies.spec.ts +++ b/e2e/playwright/point-click-assemblies.spec.ts @@ -70,22 +70,28 @@ test.describe('Point-and-click assemblies tests', () => { await test.step('Setup parts and expect empty assembly scene', async () => { const projectName = 'assembly' await context.folderSetupFn(async (dir) => { - const bracketDir = path.join(dir, projectName) - await fsp.mkdir(bracketDir, { recursive: true }) + const projDir = path.join(dir, projectName) + const nestedProjDir = path.join(dir, projectName, 'nested', 'twice') + await fsp.mkdir(projDir, { recursive: true }) + await fsp.mkdir(nestedProjDir, { recursive: true }) await Promise.all([ fsp.copyFile( executorInputPath('cylinder.kcl'), - path.join(bracketDir, 'cylinder.kcl') + path.join(projDir, 'cylinder.kcl') + ), + fsp.copyFile( + executorInputPath('cylinder.kcl'), + path.join(nestedProjDir, 'main.kcl') ), fsp.copyFile( executorInputPath('e2e-can-sketch-on-chamfer.kcl'), - path.join(bracketDir, 'bracket.kcl') + path.join(projDir, 'bracket.kcl') ), fsp.copyFile( testsInputPath('cube.step'), - path.join(bracketDir, 'cube.step') + path.join(projDir, 'cube.step') ), - fsp.writeFile(path.join(bracketDir, 'main.kcl'), ''), + fsp.writeFile(path.join(projDir, 'main.kcl'), ''), ]) }) await page.setBodyDimensions({ width: 1000, height: 500 }) @@ -167,6 +173,25 @@ test.describe('Point-and-click assemblies tests', () => { await expect( page.getByText('This file is already imported') ).toBeVisible() + await cmdBar.closeCmdBar() + }) + + await test.step('Insert a nested kcl part', async () => { + await insertPartIntoAssembly( + 'nested/twice/main.kcl', + 'main', + toolbar, + cmdBar, + page + ) + await toolbar.openPane('code') + await page.waitForTimeout(10000) + await editor.expectEditor.toContain( + ` + import "nested/twice/main.kcl" as main + `, + { shouldNormalise: true } + ) }) } ) diff --git a/src/components/FileMachineProvider.tsx b/src/components/FileMachineProvider.tsx index d9c2be2f4..63a3e6252 100644 --- a/src/components/FileMachineProvider.tsx +++ b/src/components/FileMachineProvider.tsx @@ -441,39 +441,46 @@ export const FileMachineProvider = ({ ) useMenuListener(cb) - const kclCommandMemo = useMemo( - () => - kclCommands({ - authToken: token ?? '', - projectData, - settings: { - defaultUnit: - settings.modeling.defaultUnit.current ?? - DEFAULT_DEFAULT_LENGTH_UNIT, - }, - specialPropsForInsertCommand: { - providedOptions: (isDesktop() && project?.children - ? project.children - : [] - ).flatMap((v) => { - // TODO: add support for full tree traversal when KCL support subdir imports - const relativeFilePath = v.path.replace( - project?.path + window.electron.sep, - '' - ) - const isDirectory = v.children - const isCurrentFile = v.path === file?.path - return isDirectory || isCurrentFile - ? [] - : { - name: relativeFilePath, - value: relativeFilePath, - } - }), - }, - }), - [codeManager, kclManager, send, project, file] - ) + const kclCommandMemo = useMemo(() => { + const providedOptions = [] + if (isDesktop() && project?.children && file?.path) { + const projectPath = project.path + const filePath = file.path + let children = project.children + while (children.length > 0) { + const v = children.pop() + if (!v) { + continue + } + + if (v.children) { + children.push(...v.children) + continue + } + + const relativeFilePath = v.path.replace( + projectPath + window.electron.sep, + '' + ) + const isCurrentFile = v.path === filePath + if (!isCurrentFile) { + providedOptions.push({ + name: relativeFilePath.replaceAll(window.electron.sep, '/'), + value: relativeFilePath.replaceAll(window.electron.sep, '/'), + }) + } + } + } + return kclCommands({ + authToken: token ?? '', + projectData, + settings: { + defaultUnit: + settings.modeling.defaultUnit.current ?? DEFAULT_DEFAULT_LENGTH_UNIT, + }, + specialPropsForInsertCommand: { providedOptions }, + }) + }, [codeManager, kclManager, send, project, file]) useEffect(() => { commandBarActor.send({