Skip to content

Commit

Permalink
Merge pull request #69 from dmeybohm/fix-view-path
Browse files Browse the repository at this point in the history
Underscore the view names for Cake 3 / 4
  • Loading branch information
dmeybohm authored Apr 28, 2022
2 parents 6fad7d1 + 9b0d2ba commit 39516f5
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
## [0.8.1] - 2022-04-26
### Changed
- Update for PhpStorm 2022.1
- 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
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/daveme/chocolateCakePHP/ConfigForm.form
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
</constraints>
<properties>
<font style="1"/>
<text value="Cake 3/4"/>
<text value="CakePHP 3 and CakePHP 4"/>
</properties>
</component>
<component id="9dbcf" class="javax.swing.JCheckBox" binding="enableCake3SupportCheckBox" default-binding="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Enable Cake 3 / Cake 4 Support"/>
<text value="Enable CakePHP 3 / CakePHP 4 Support"/>
</properties>
</component>
<component id="a650c" class="javax.swing.JSeparator">
Expand Down Expand Up @@ -159,7 +159,7 @@
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="This is the extension used for view files. By default it is &quot;ctp.&quot;"/>
<text value="This is the extension used for view files. By default this is &quot;ctp&quot;. NOTE: this is only for CakePHP 3."/>
</properties>
</component>
<vspacer id="4cda0">
Expand Down Expand Up @@ -206,7 +206,7 @@
</constraints>
<properties>
<font style="1"/>
<text value="Cake 2"/>
<text value="CakePHP 2"/>
</properties>
</component>
<component id="aa16a" class="javax.swing.JSeparator">
Expand Down
22 changes: 12 additions & 10 deletions src/main/kotlin/com/daveme/chocolateCakePHP/Cake.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/com/daveme/chocolateCakePHP/files.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/com/daveme/chocolateCakePHP/strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


<!-- please see http://confluence.jetbrains.net/display/IDEADEV/Build+Number+Ranges for description -->
<idea-version since-build="193.3519.25"/>
<idea-version since-build="213.6461.21"/>
<extensions defaultExtensionNs="com.intellij">
<projectService serviceImplementation="com.daveme.chocolateCakePHP.Settings" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class ViewTest : BaseTestCase() {
"cake4/vendor/cakephp.php"
)

myFixture.configureByFilePathAndText("cake4/templates/Movie/artist.ctp", """
myFixture.configureByFilePathAndText("cake4/templates/Movie/artist.php", """
<?php
${'$'}this-><caret>
""".trimIndent())
Expand Down

0 comments on commit 39516f5

Please sign in to comment.