From 9114db853c75d57dfaca37f29f804fe24ccc31fd Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 16 Jan 2024 22:32:34 +0100 Subject: [PATCH] make parent directory searching recursive: move it to the right util function, rename this function --- .../api/models/fileSystemRelated/ChangeDirectory.kt | 5 +---- .../models/fileSystemRelated/ListDirectoryContents.kt | 2 +- .../api/models/utils/PsiElementsUtils.kt | 10 ++++++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt index 9053e52..5e7f037 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt @@ -15,10 +15,7 @@ class ChangeDirectory( val currentProjectDirectory = ideStateKeeper.currentProjectDirectory currentProjectDirectory.refresh() - val targetDir = when(targetDirectoryName) { - PARENT_DIRECTORY_NAME -> currentProjectDirectory.parentDirectory ?: error("No parent directory") - else -> currentProjectDirectory.findSubdirectoryRecursively(targetDirectoryName) - } + val targetDir = currentProjectDirectory.findDirectoryRecursively(targetDirectoryName) previousDirectory = currentProjectDirectory ideStateKeeper.currentProjectDirectory = targetDir diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt index b3cf07c..8746bb2 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt @@ -19,7 +19,7 @@ class ListDirectoryContents( override fun execute() { currentProjectDirectory.refresh() - val searchDirectory = currentProjectDirectory.findSubdirectoryRecursively(searchDirectoryName) + val searchDirectory = currentProjectDirectory.findDirectoryRecursively(searchDirectoryName) searchDirectoryItems = searchDirectory.fileSystemItems() } diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt index b583fb3..0f3e704 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt @@ -9,21 +9,22 @@ import java.util.concurrent.CompletableFuture const val PATH_DELIMITER = "/" -fun PsiDirectory.findSubdirectoryRecursively(targetDirectoryPath: String): PsiDirectory { +fun PsiDirectory.findDirectoryRecursively(targetDirectoryPath: String): PsiDirectory { val currentDirectory = when (val nextDirectoryInPath = targetDirectoryPath.substringBefore(PATH_DELIMITER)) { DEFAULT_DIRECTORY_NAME -> this + PARENT_DIRECTORY_NAME -> this.parentDirectory ?: error("Current directory doesn't have a parent directory") else -> this.findSubdirectory(nextDirectoryInPath) ?: error("No such subdirectory: $nextDirectoryInPath") } val remainingDirectoryPath = targetDirectoryPath.substringAfter(PATH_DELIMITER, "") - return if (remainingDirectoryPath.isNotEmpty()) currentDirectory.findSubdirectoryRecursively(remainingDirectoryPath) else currentDirectory + return if (remainingDirectoryPath.isNotEmpty()) currentDirectory.findDirectoryRecursively(remainingDirectoryPath) else currentDirectory } fun PsiDirectory.findFileRecursively(targetFilePath: String): PsiFile { val targetFileDirectory = when (val fileDirectoryPath = targetFilePath.substringBeforeLast(PATH_DELIMITER, "")) { "" -> this - else -> this.findSubdirectoryRecursively(fileDirectoryPath) + else -> this.findDirectoryRecursively(fileDirectoryPath) } val fileName = targetFilePath.substringAfterLast(PATH_DELIMITER) @@ -64,6 +65,7 @@ fun PsiDirectory.deleteFileByName(fileName: String) { ApplicationManager.getApplication().let { it.invokeAndWait { it.runWriteAction { + // TODO: можно возвращать отсюда старый удаленный файл и как бы из него текст вытаскивать.... this.findFileRecursively(fileName).delete() } } @@ -80,7 +82,7 @@ fun PsiDirectory.createSubdirectoryByName(directoryName: String) { fun PsiDirectory.deleteSubdirectoryByName(directoryName: String) { WriteCommandAction.runWriteCommandAction(project) { this.refresh() - val psiDirectory = this.findSubdirectoryRecursively(directoryName) + val psiDirectory = this.findDirectoryRecursively(directoryName) psiDirectory.delete() } }