From 650ae28b8abe6d09b32d73e9836c871e8c3e2e12 Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Sun, 24 Nov 2024 18:25:29 +0100 Subject: [PATCH] PY-77574: Do not allow hyphen in Django Project and Application names. `ProjectPathProvider.bindProjectName` is improved and support list of denied chars (as regex) and covered with test. Regex used both: for escaping and for substitution. GitOrigin-RevId: 6d4e4643a66358c485e4849ece347af8d1fa32aa --- .../pluginResources/messages/PyBundle.properties | 1 + .../python/newProjectWizard/KotlinDSUIUtils.kt | 15 +++++++++++++++ .../projectPath/ProjectPathProvider.kt | 11 +++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 python/src/com/jetbrains/python/newProjectWizard/KotlinDSUIUtils.kt diff --git a/python/pluginResources/messages/PyBundle.properties b/python/pluginResources/messages/PyBundle.properties index ddafe09530f8..b04900e86710 100644 --- a/python/pluginResources/messages/PyBundle.properties +++ b/python/pluginResources/messages/PyBundle.properties @@ -1583,4 +1583,5 @@ python.survey.user.job.dialog.blocks.checkbox.ml=Machine learning python.survey.user.job.dialog.blocks.checkbox.web=Web development python.survey.user.job.dialog.blocks.checkbox.scripts=Writing automation scripts / parsers / tests / system administration python.survey.user.job.dialog.blocks.checkbox.other=Other +validation.invalid.name=Invalid name diff --git a/python/src/com/jetbrains/python/newProjectWizard/KotlinDSUIUtils.kt b/python/src/com/jetbrains/python/newProjectWizard/KotlinDSUIUtils.kt new file mode 100644 index 000000000000..5fb1af76ab66 --- /dev/null +++ b/python/src/com/jetbrains/python/newProjectWizard/KotlinDSUIUtils.kt @@ -0,0 +1,15 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.jetbrains.python.newProjectWizard + +import com.intellij.openapi.ui.validation.DialogValidation +import com.intellij.openapi.ui.validation.validationErrorIf +import com.jetbrains.python.PyBundle + + +/** + * Kotlin DSL UI validator that makes sure no [deniedChars] are used + */ +fun deniedCharsValidation(deniedChars: Regex): DialogValidation.WithParameter<() -> String> = + validationErrorIf(PyBundle.message("validation.invalid.name")) { + deniedChars.find(it) != null + } \ No newline at end of file diff --git a/python/src/com/jetbrains/python/newProjectWizard/projectPath/ProjectPathProvider.kt b/python/src/com/jetbrains/python/newProjectWizard/projectPath/ProjectPathProvider.kt index d617188a8385..da6127d9177a 100644 --- a/python/src/com/jetbrains/python/newProjectWizard/projectPath/ProjectPathProvider.kt +++ b/python/src/com/jetbrains/python/newProjectWizard/projectPath/ProjectPathProvider.kt @@ -10,6 +10,7 @@ import com.intellij.ui.dsl.builder.bindText import com.intellij.ui.dsl.builder.text import com.intellij.ui.dsl.builder.trimmedTextValidation import com.intellij.util.concurrency.annotations.RequiresEdt +import com.jetbrains.python.newProjectWizard.deniedCharsValidation import com.jetbrains.python.newProjectWizard.projectPath.ProjectPathProvider.Companion.bindProjectName import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -34,16 +35,22 @@ interface ProjectPathProvider { val projectPathFlows: ProjectPathFlows companion object { + /** * Binds [com.jetbrains.python.newProjectWizard.PyV3ProjectTypeSpecificSettings] property to the certain cell and updates it each time project path is changes + * [deniedChars] are added to validation and replaced with `_` */ + @JvmOverloads @RequiresEdt - fun Cell.bindProjectName(projectPathProvider: ProjectPathProvider, property: KMutableProperty0<@NlsSafe String>) = apply { + fun Cell.bindProjectName(projectPathProvider: ProjectPathProvider, property: KMutableProperty0<@NlsSafe String>, deniedChars: Regex? = null) = apply { trimmedTextValidation(CHECK_NON_EMPTY, CHECK_NO_WHITESPACES) + deniedChars?.let { + trimmedTextValidation(deniedCharsValidation(it)) + } bindText(property) projectPathProvider.onProjectFileNameChanged { withContext(Dispatchers.EDT) { - text(it) + text(deniedChars?.replace(it, "_") ?: it) } } }