[eel] IJPL-165252: Improve detection of non-empty directories

(cherry picked from commit aa653a766305ae3c01c6cddbe3f68ec0bc95f32c)

IJ-CR-148300

GitOrigin-RevId: 41bdafa8e9810d1bcbc2307f8b07d5fab8ae0666
This commit is contained in:
Konstantin Nisht
2024-11-04 09:33:19 +01:00
committed by intellij-monorepo-bot
parent c0b7d8f94c
commit da120409a7
3 changed files with 28 additions and 42 deletions

View File

@@ -5180,8 +5180,6 @@ f:com.intellij.openapi.ui.validation.ValidationUtilKt
- sf:validationErrorFor(kotlin.jvm.functions.Function2):com.intellij.openapi.ui.validation.DialogValidation$WithTwoParameters
- sf:validationErrorIf(java.lang.String,kotlin.jvm.functions.Function1):com.intellij.openapi.ui.validation.DialogValidation$WithParameter
- sf:validationErrorIf(java.lang.String,kotlin.jvm.functions.Function2):com.intellij.openapi.ui.validation.DialogValidation$WithTwoParameters
- sf:validationFileErrorFor(kotlin.jvm.functions.Function1):com.intellij.openapi.ui.validation.DialogValidation$WithParameter
- sf:validationFileErrorFor(kotlin.jvm.functions.Function2):com.intellij.openapi.ui.validation.DialogValidation$WithTwoParameters
- sf:validationPathErrorFor(kotlin.jvm.functions.Function1):com.intellij.openapi.ui.validation.DialogValidation$WithParameter
- sf:validationPathErrorFor(kotlin.jvm.functions.Function2):com.intellij.openapi.ui.validation.DialogValidation$WithTwoParameters
c:com.intellij.openapi.util.DefaultModificationTracker

View File

@@ -3,7 +3,8 @@ package com.intellij.openapi.ui.validation
import com.intellij.openapi.util.NlsContexts
import com.intellij.openapi.ui.ValidationInfo
import java.io.File
import java.io.IOException
import java.nio.file.InvalidPathException
import java.nio.file.Path
/**
@@ -32,7 +33,17 @@ fun <T1, T2> validationErrorFor(getMessage: (T1, T2) -> @NlsContexts.DialogMessa
* Created validation with [Path] parameter that produces error if [getMessage] returns non-null value.
*/
fun validationPathErrorFor(getMessage: (Path) -> @NlsContexts.DialogMessage String?): DialogValidation.WithParameter<() -> String> =
validationErrorFor<String> { getMessage(Path.of(it)) }
validationErrorFor<String> {
try {
getMessage(Path.of(it))
}
catch (exception: InvalidPathException) {
exception.message
}
catch (exception: IOException) {
exception.message
}
}
/**
* Created validation with custom and [Path] parameters that produces error if [getMessage] returns non-null value.
@@ -40,18 +51,6 @@ fun validationPathErrorFor(getMessage: (Path) -> @NlsContexts.DialogMessage Stri
fun <T> validationPathErrorFor(getMessage: (T, Path) -> @NlsContexts.DialogMessage String?): DialogValidation.WithTwoParameters<T, () -> String> =
validationErrorWithTwoParametersFor(getMessage) { validationPathErrorFor(it) }
/**
* Created validation with [File] parameter that produces error if [getMessage] returns non-null value.
*/
fun validationFileErrorFor(getMessage: (File) -> @NlsContexts.DialogMessage String?): DialogValidation.WithParameter<() -> String> =
validationPathErrorFor { getMessage(it.toFile()) }
/**
* Created validation with custom and [File] parameters that produces error if [getMessage] returns non-null value.
*/
fun <T> validationFileErrorFor(getMessage: (T, File) -> @NlsContexts.DialogMessage String?): DialogValidation.WithTwoParameters<T, () -> String> =
validationErrorWithTwoParametersFor(getMessage) { validationFileErrorFor(it) }
/**
* Created validation with parameter that produces error if [isNotValid] is true.
*/

View File

@@ -6,11 +6,11 @@ import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.rootManager
import com.intellij.ui.UIBundle
import java.io.IOException
import java.nio.file.Files
import java.nio.file.InvalidPathException
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.exists
import kotlin.io.path.isDirectory
import kotlin.io.path.isWritable
import kotlin.io.path.listDirectoryEntries
import kotlin.io.path.name
val CHECK_NON_EMPTY: DialogValidation.WithParameter<() -> String> = validationErrorIf<String>(UIBundle.message("kotlin.dsl.validation.missing.value")) { it.isEmpty() }
@@ -29,31 +29,20 @@ val CHECK_NAME_FORMAT: DialogValidation.WithParameter<() -> String> = validation
!firstSymbolNamePattern.matches(it)
}
val CHECK_NON_EMPTY_DIRECTORY: DialogValidation.WithParameter<() -> String> = validationFileErrorFor { file ->
val path = file.toPath()
val children by lazy { Files.list(path).toList() }
if (Files.exists(path) && children != null && children.isNotEmpty()) {
UIBundle.message("label.project.wizard.new.project.directory.not.empty.warning", file.name)
val CHECK_NON_EMPTY_DIRECTORY: DialogValidation.WithParameter<() -> String> = validationPathErrorFor { path ->
if (path.exists() && path.listDirectoryEntries().isNotEmpty()) {
UIBundle.message("label.project.wizard.new.project.directory.not.empty.warning", path.name)
}
else null
}.asWarning().withOKEnabled()
val CHECK_DIRECTORY: DialogValidation.WithParameter<() -> String> = validationErrorFor { text ->
runCatching { Path.of(text).toFile() }
.mapCatching { file ->
when {
!file.exists() -> null
!file.canWrite() -> UIBundle.message("label.project.wizard.new.project.directory.not.writable.error", file.name)
!file.isDirectory -> UIBundle.message("label.project.wizard.new.project.file.not.directory.error", file.name)
else -> null
}
}.getOrElse { exception ->
when (exception) {
is InvalidPathException -> exception.message
is IOException -> exception.message
else -> throw exception
}
}
val CHECK_DIRECTORY: DialogValidation.WithParameter<() -> String> = validationPathErrorFor { path ->
when {
!path.exists() -> null
!path.isWritable() -> UIBundle.message("label.project.wizard.new.project.directory.not.writable.error", path.name)
!path.isDirectory() -> UIBundle.message("label.project.wizard.new.project.file.not.directory.error", path.name)
else -> null
}
}
private val firstSymbolGroupIdPattern = "[a-zA-Z_].*".toRegex()