Skip to content

Commit

Permalink
make parent directory searching recursive: move it to the right util …
Browse files Browse the repository at this point in the history
…function, rename this function
  • Loading branch information
diffitask committed Jan 16, 2024
1 parent 459b234 commit 9114db8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ListDirectoryContents(

override fun execute() {
currentProjectDirectory.refresh()
val searchDirectory = currentProjectDirectory.findSubdirectoryRecursively(searchDirectoryName)
val searchDirectory = currentProjectDirectory.findDirectoryRecursively(searchDirectoryName)
searchDirectoryItems = searchDirectory.fileSystemItems()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -64,6 +65,7 @@ fun PsiDirectory.deleteFileByName(fileName: String) {
ApplicationManager.getApplication().let {
it.invokeAndWait {
it.runWriteAction {
// TODO: можно возвращать отсюда старый удаленный файл и как бы из него текст вытаскивать....
this.findFileRecursively(fileName).delete()
}
}
Expand All @@ -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()
}
}
Expand Down

0 comments on commit 9114db8

Please sign in to comment.