From 806d8725a72970f5fa41b8ba35b847284da3bdaf Mon Sep 17 00:00:00 2001 From: David Meybohm Date: Wed, 27 Apr 2022 08:33:05 -0400 Subject: [PATCH 1/5] Underscore the view names for Cake 3 / 4 - Underscore view names for Cake 3 / Cake 4 - Change default view filename to .php so when user its "Default" button in interface, that is used. (Should probably select diff default there, but going to re-do the UI code soon anyway). --- CHANGELOG.md | 2 ++ .../kotlin/com/daveme/chocolateCakePHP/Settings.kt | 2 +- src/main/kotlin/com/daveme/chocolateCakePHP/files.kt | 5 +++-- .../kotlin/com/daveme/chocolateCakePHP/strings.kt | 11 +++++++++++ src/main/resources/META-INF/plugin.xml | 2 +- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd6fa750..1de89d02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ ## [0.8.1] - 2022-04-26 ### Changed - Update for PhpStorm 2022.1 +- Map from camelcased action names to underscored names for view files +- Set default template filename to '.php' when user selects view button ## [0.8.0] - 2022-03-11 ### Added diff --git a/src/main/kotlin/com/daveme/chocolateCakePHP/Settings.kt b/src/main/kotlin/com/daveme/chocolateCakePHP/Settings.kt index a7bfcc54..e989ba82 100644 --- a/src/main/kotlin/com/daveme/chocolateCakePHP/Settings.kt +++ b/src/main/kotlin/com/daveme/chocolateCakePHP/Settings.kt @@ -4,7 +4,7 @@ import com.intellij.openapi.components.* import com.intellij.openapi.project.Project data class SettingsState( - var cakeTemplateExtension: String = "ctp", + var cakeTemplateExtension: String = "php", var appDirectory: String = "src", var appNamespace: String = "\\App", var pluginPath: String = "plugins", diff --git a/src/main/kotlin/com/daveme/chocolateCakePHP/files.kt b/src/main/kotlin/com/daveme/chocolateCakePHP/files.kt index 34c41bbf..e5ae7235 100644 --- a/src/main/kotlin/com/daveme/chocolateCakePHP/files.kt +++ b/src/main/kotlin/com/daveme/chocolateCakePHP/files.kt @@ -45,10 +45,11 @@ fun templatePathToVirtualFile( ): VirtualFile? { var relativeFile: VirtualFile? = null if (settings.cake3Enabled) { - val cakeThreeTemplatePath = CakeThree.templatePath(settings, controllerName, controllerAction) + val underscored = controllerAction.camelCaseToUnderscore() + val cakeThreeTemplatePath = CakeThree.templatePath(settings, controllerName, underscored) relativeFile = findRelativeFile(appOrPluginDir, cakeThreeTemplatePath) if (relativeFile == null) { - val cakeFourTemplatePath = CakeFour.templatePath(settings, controllerName, controllerAction) + val cakeFourTemplatePath = CakeFour.templatePath(settings, controllerName, underscored) relativeFile = findRelativeFile(appOrPluginDir, cakeFourTemplatePath) } } diff --git a/src/main/kotlin/com/daveme/chocolateCakePHP/strings.kt b/src/main/kotlin/com/daveme/chocolateCakePHP/strings.kt index 9b1db3f6..0fc2fb5d 100644 --- a/src/main/kotlin/com/daveme/chocolateCakePHP/strings.kt +++ b/src/main/kotlin/com/daveme/chocolateCakePHP/strings.kt @@ -18,3 +18,14 @@ fun String.controllerBaseName(): String? = null else substring(0, length - "Controller".length) + +val matchUpperCase = Regex("([A-Z])") +fun String.camelCaseToUnderscore(): String { + val removeFirst = this.isNotEmpty() && this[0].isUpperCase() + val result = matchUpperCase.replace(this, "_$1") + return if (removeFirst) + result.substring(1).lowercase() + else + result.lowercase() +} + diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 3e11d8fe..c33d4746 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -24,7 +24,7 @@ - + From f7fe0fb6f0f6987e603556daf66f67130f4bf24f Mon Sep 17 00:00:00 2001 From: David Meybohm Date: Wed, 27 Apr 2022 08:45:06 -0400 Subject: [PATCH 2/5] Update UI description --- src/main/java/com/daveme/chocolateCakePHP/ConfigForm.form | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/daveme/chocolateCakePHP/ConfigForm.form b/src/main/java/com/daveme/chocolateCakePHP/ConfigForm.form index b9b3e066..dd0c0884 100644 --- a/src/main/java/com/daveme/chocolateCakePHP/ConfigForm.form +++ b/src/main/java/com/daveme/chocolateCakePHP/ConfigForm.form @@ -159,7 +159,7 @@ - + From c45aee8d486c4fb7041202504bf7b35377b7dedc Mon Sep 17 00:00:00 2001 From: David Meybohm Date: Wed, 27 Apr 2022 08:55:12 -0400 Subject: [PATCH 3/5] Fix tests --- .../templates/Movie/{artist.ctp => artist.php} | 0 .../com/daveme/chocolateCakePHP/test/ViewTest.kt | 14 +++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) rename src/test/fixtures/cake4/templates/Movie/{artist.ctp => artist.php} (100%) diff --git a/src/test/fixtures/cake4/templates/Movie/artist.ctp b/src/test/fixtures/cake4/templates/Movie/artist.php similarity index 100% rename from src/test/fixtures/cake4/templates/Movie/artist.ctp rename to src/test/fixtures/cake4/templates/Movie/artist.php diff --git a/src/test/kotlin/com/daveme/chocolateCakePHP/test/ViewTest.kt b/src/test/kotlin/com/daveme/chocolateCakePHP/test/ViewTest.kt index 9f014292..e0c1dbfa 100644 --- a/src/test/kotlin/com/daveme/chocolateCakePHP/test/ViewTest.kt +++ b/src/test/kotlin/com/daveme/chocolateCakePHP/test/ViewTest.kt @@ -16,6 +16,12 @@ class ViewTest : BaseTestCase() { @Test fun `test completing view helper inside a view`() { + // change template extension: + val originalSettings = Settings.getInstance(myFixture.project) + val newState = originalSettings.state!!.copy() + newState.cakeTemplateExtension = "ctp" + originalSettings.loadState(newState) + myFixture.configureByFiles( "cake3/src/Controller/AppController.php", "cake3/src/Controller/Component/MovieMetadataComponent.php", @@ -36,6 +42,12 @@ class ViewTest : BaseTestCase() { @Test fun `test completing view helper methods inside a view`() { + // change template extension: + val originalSettings = Settings.getInstance(myFixture.project) + val newState = originalSettings.state!!.copy() + newState.cakeTemplateExtension = "ctp" + originalSettings.loadState(newState) + myFixture.configureByFiles( "cake3/src/Controller/AppController.php", "cake3/src/Controller/Component/MovieMetadataComponent.php", @@ -127,7 +139,7 @@ class ViewTest : BaseTestCase() { "cake4/vendor/cakephp.php" ) - myFixture.configureByFilePathAndText("cake4/templates/Movie/artist.ctp", """ + myFixture.configureByFilePathAndText("cake4/templates/Movie/artist.php", """ """.trimIndent()) From ebcb0a57d54db80d75f50952c26fb4186c90dbc0 Mon Sep 17 00:00:00 2001 From: David Meybohm Date: Wed, 27 Apr 2022 19:16:22 -0400 Subject: [PATCH 4/5] Hard-code CakePHP 4 extension as php I think it could be problematic changing the default of the CakePHP 3 config to PHP - it might cause the plugin view navigating / completion to not work on CakePHP 3. So, instead of doing that, let's just split out the extension config and make it unconfigurable for Cake 4. This also makes the old CakePHP 3 view tests pass, which makes me feel better about releasing with this. --- .../daveme/chocolateCakePHP/ConfigForm.form | 8 +++---- .../com/daveme/chocolateCakePHP/Cake.kt | 22 ++++++++++--------- .../com/daveme/chocolateCakePHP/Settings.kt | 2 +- .../daveme/chocolateCakePHP/test/ViewTest.kt | 12 ---------- 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/daveme/chocolateCakePHP/ConfigForm.form b/src/main/java/com/daveme/chocolateCakePHP/ConfigForm.form index dd0c0884..9b0a640d 100644 --- a/src/main/java/com/daveme/chocolateCakePHP/ConfigForm.form +++ b/src/main/java/com/daveme/chocolateCakePHP/ConfigForm.form @@ -30,7 +30,7 @@ - + @@ -38,7 +38,7 @@ - + @@ -159,7 +159,7 @@ - + @@ -206,7 +206,7 @@ - + diff --git a/src/main/kotlin/com/daveme/chocolateCakePHP/Cake.kt b/src/main/kotlin/com/daveme/chocolateCakePHP/Cake.kt index 08ac34f9..2a4f858e 100644 --- a/src/main/kotlin/com/daveme/chocolateCakePHP/Cake.kt +++ b/src/main/kotlin/com/daveme/chocolateCakePHP/Cake.kt @@ -36,6 +36,11 @@ fun isCakeViewFile(settings: Settings, file: PsiFile): Boolean { return false } + val hasCakeFour = if (settings.cake3Enabled) + file.name.endsWith("php") + else + false + val hasCakeThree = if (settings.cake3Enabled) file.name.endsWith(settings.cakeTemplateExtension) else @@ -47,15 +52,12 @@ fun isCakeViewFile(settings: Settings, file: PsiFile): Boolean { false val topDir = topSourceDirectoryFromFile(settings, file) - if (hasCakeThree) { - if (CakeFour.isCakeViewFile(settings, topDir, file)) { - return true - } - if (CakeThree.isCakeViewFile(settings, topDir, file)) { - return true - } + if (hasCakeFour && CakeFour.isCakeViewFile(settings, topDir, file)) { + return true + } + if (hasCakeThree && CakeThree.isCakeViewFile(settings, topDir, file)) { + return true } - if (hasCakeTwo && CakeTwo.isCakeViewFile(settings, topDir, file)) { return true } @@ -101,10 +103,10 @@ private fun appDirectoryFromFile(settings: Settings, file: PsiFile): PsiDirector object CakeFour : Cake(viewDirectory = "templates", elementTop = "element") { override fun templatePath(settings: Settings, controllerName: String, controllerAction: String) = - "../$viewDirectory/$controllerName/$controllerAction.${settings.cakeTemplateExtension}" + "../$viewDirectory/$controllerName/$controllerAction.php" override fun elementPath(settings: Settings, elementPath: String): String = - "../$viewDirectory/$elementTop/$elementPath.${settings.cakeTemplateExtension}" + "../$viewDirectory/$elementTop/$elementPath.php" override fun isCakeViewFile(settings: Settings, topDir: PsiDirectory?, file: PsiFile): Boolean { return topDir?.name == viewDirectory diff --git a/src/main/kotlin/com/daveme/chocolateCakePHP/Settings.kt b/src/main/kotlin/com/daveme/chocolateCakePHP/Settings.kt index e989ba82..a7bfcc54 100644 --- a/src/main/kotlin/com/daveme/chocolateCakePHP/Settings.kt +++ b/src/main/kotlin/com/daveme/chocolateCakePHP/Settings.kt @@ -4,7 +4,7 @@ import com.intellij.openapi.components.* import com.intellij.openapi.project.Project data class SettingsState( - var cakeTemplateExtension: String = "php", + var cakeTemplateExtension: String = "ctp", var appDirectory: String = "src", var appNamespace: String = "\\App", var pluginPath: String = "plugins", diff --git a/src/test/kotlin/com/daveme/chocolateCakePHP/test/ViewTest.kt b/src/test/kotlin/com/daveme/chocolateCakePHP/test/ViewTest.kt index e0c1dbfa..6bed8de0 100644 --- a/src/test/kotlin/com/daveme/chocolateCakePHP/test/ViewTest.kt +++ b/src/test/kotlin/com/daveme/chocolateCakePHP/test/ViewTest.kt @@ -16,12 +16,6 @@ class ViewTest : BaseTestCase() { @Test fun `test completing view helper inside a view`() { - // change template extension: - val originalSettings = Settings.getInstance(myFixture.project) - val newState = originalSettings.state!!.copy() - newState.cakeTemplateExtension = "ctp" - originalSettings.loadState(newState) - myFixture.configureByFiles( "cake3/src/Controller/AppController.php", "cake3/src/Controller/Component/MovieMetadataComponent.php", @@ -42,12 +36,6 @@ class ViewTest : BaseTestCase() { @Test fun `test completing view helper methods inside a view`() { - // change template extension: - val originalSettings = Settings.getInstance(myFixture.project) - val newState = originalSettings.state!!.copy() - newState.cakeTemplateExtension = "ctp" - originalSettings.loadState(newState) - myFixture.configureByFiles( "cake3/src/Controller/AppController.php", "cake3/src/Controller/Component/MovieMetadataComponent.php", From 9b0d2ba2af6aa8a11567823eaadacd2fe066a955 Mon Sep 17 00:00:00 2001 From: David Meybohm Date: Wed, 27 Apr 2022 19:18:36 -0400 Subject: [PATCH 5/5] Update changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1de89d02..761729d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,8 @@ ## [0.8.1] - 2022-04-26 ### Changed - Update for PhpStorm 2022.1 -- Map from camelcased action names to underscored names for view files -- Set default template filename to '.php' when user selects view button +- Map from camel-cased action names to underscored names for view files +- Fix handling of extension between CakePHP 3 and CakePHP 4. ## [0.8.0] - 2022-03-11 ### Added