Skip to content

Commit

Permalink
fix: Improve Twig block extension bundle detection
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Sep 30, 2021
1 parent f78ebd4 commit ae683d8
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 29 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ tasks {
systemProperty("jb.consents.confirmation.enabled", "false")
}

runIde {
jvmArgs("-Xmx4000m")
}

signPlugin {
certificateChain.set(System.getenv("CERTIFICATE_CHAIN"))
privateKey.set(System.getenv("PRIVATE_KEY"))
Expand Down
45 changes: 34 additions & 11 deletions src/main/kotlin/de/shyim/shopware6/index/ShopwareBundleIndex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import de.shyim.shopware6.index.dict.ShopwareBundle
import de.shyim.shopware6.index.externalizer.ObjectStreamDataExternalizer
import gnu.trove.THashMap
import org.apache.commons.io.FilenameUtils
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.io.path.Path

class ShopwareBundleIndex: FileBasedIndexExtension<String, ShopwareBundle>() {
private val EXTERNALIZER = ObjectStreamDataExternalizer<ShopwareBundle>()
Expand All @@ -23,7 +21,7 @@ class ShopwareBundleIndex: FileBasedIndexExtension<String, ShopwareBundle>() {
}

override fun getVersion(): Int {
return 1
return 2
}

override fun dependsOnFileContent(): Boolean {
Expand All @@ -32,12 +30,24 @@ class ShopwareBundleIndex: FileBasedIndexExtension<String, ShopwareBundle>() {

override fun getIndexer(): DataIndexer<String, ShopwareBundle, FileContent> {
return DataIndexer { inputData ->
if (!isValidForIndex(inputData)) {
return@DataIndexer mapOf()
}

val bundles = THashMap<String, ShopwareBundle>()

inputData.psiFile.acceptChildren(object: PsiRecursiveElementWalkingVisitor() {
inputData.psiFile.acceptChildren(object : PsiRecursiveElementWalkingVisitor() {
override fun visitElement(element: PsiElement) {
if (element is PhpClass && !element.isAbstract && isShopwareBundle(element)) {
bundles[element.name] = ShopwareBundle(element.name, inputData.file.path, getViewDirectory(inputData.file.path))
if (element.name.isEmpty()) {
return
}

val bundleDir = Paths.get(inputData.file.path).parent
val expectedStorefrontViewFolder =
FilenameUtils.separatorsToUnix("${bundleDir}/Resources/views/storefront/")
bundles[element.name] =
ShopwareBundle(element.name, inputData.file.path, expectedStorefrontViewFolder)
}

super.visitElement(element)
Expand Down Expand Up @@ -81,14 +91,27 @@ class ShopwareBundleIndex: FileBasedIndexExtension<String, ShopwareBundle>() {
return found
}

fun getViewDirectory(folderPath: String): String? {
val expectedViewDir =
FilenameUtils.separatorsToUnix("${Paths.get(folderPath).parent}/Resources/views/storefront/")
fun isValidForIndex(inputData: FileContent): Boolean {
val fileName = inputData.psiFile.name
val filePath = inputData.file.path.lowercase()

if (Files.exists(Path(expectedViewDir))) {
return expectedViewDir
if (fileName.startsWith(".") || fileName.endsWith("Test")) {
return false
}

return null
if (
filePath.contains("core/framework/test/plugin/_fixture/plugins/") ||
filePath.contains("core/framework/test/store/_fixtures") ||
filePath.contains("core/content/test/") ||
filePath.contains("core/system/test/snippet/files/_fixtures") ||
filePath.contains("core/system/test/systemconfig/_fixtures/") ||
filePath.contains("core/framework/test/plugin/requirement/_fixture/") ||
filePath.contains("core/framework/test/adapter/twig/fixtures") ||
filePath.contains("storefront/test/theme/fixtures/")
) {
return false
}

return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.apache.commons.lang.builder.HashCodeBuilder
import java.io.Serializable
import java.util.*

class ShopwareBundle(val name: String, val path: String, val viewPath: String?): Serializable {
class ShopwareBundle(val name: String, val path: String, val viewPath: String) : Serializable {
override fun hashCode(): Int {
return HashCodeBuilder()
.append(this.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ class ExtendTwigBlockIntention : PsiElementBaseIntentionAction() {
return
}

val bundleList = ShopwareBundleUtil.getAllBundlesWithViewDirectory(project).filter { bundle ->
val bundleList = ShopwareBundleUtil.getAllBundlesRelatedToViews(project).filter { bundle ->
return@filter bundle.name != currentBundle
}.filter {
val virtualFile = LocalFileSystem.getInstance().findFileByPath(it.path)!!
val psiFile = PsiManager.getInstance(project).findFile(virtualFile)!!

return@filter psiFile.manager.isInProject(psiFile)
}

val jbBundleList = JBList(bundleList)

jbBundleList.cellRenderer = object : JBList.StripedListCellRenderer() {
Expand Down Expand Up @@ -96,18 +102,25 @@ class ExtendTwigBlockIntention : PsiElementBaseIntentionAction() {
}

private fun extendBlockInBundle(bundle: ShopwareBundle, templatePath: String, blockName: String, project: Project) {
val localFolder = LocalFileSystem.getInstance().findFileByPath(bundle.viewPath!!)
var currentFolder = PsiManager.getInstance(project).findDirectory(localFolder!!) as PsiDirectory
val localFolder = LocalFileSystem.getInstance().findFileByPath(bundle.viewPath)

var currentFolder: PsiDirectory?
if (localFolder == null) {
currentFolder = createMissingViewFolder(bundle, project)
} else {
currentFolder = PsiManager.getInstance(project).findDirectory(localFolder) as PsiDirectory
}

var fileName: String? = null

val templateParts = templatePath.split("/")

templateParts.forEach { part ->
if (!part.endsWith(".twig")) {
currentFolder = if (currentFolder.findSubdirectory(part) != null) {
currentFolder.findSubdirectory(part)!!
currentFolder = if (currentFolder!!.findSubdirectory(part) != null) {
currentFolder!!.findSubdirectory(part)!!
} else {
currentFolder.createSubdirectory(part)
currentFolder!!.createSubdirectory(part)
}
} else {
fileName = part
Expand All @@ -124,8 +137,8 @@ class ExtendTwigBlockIntention : PsiElementBaseIntentionAction() {
{% sw_extends "@Storefront/storefront/${templatePath}" %}
""".trimIndent()

if (currentFolder.findFile(fileName!!) != null) {
val file = currentFolder.findFile(fileName!!)!!
if (currentFolder!!.findFile(fileName!!) != null) {
val file = currentFolder!!.findFile(fileName!!)!!

val editor = FileEditorManager.getInstance(project)
.openTextEditor(OpenFileDescriptor(project, file.containingFile.virtualFile), true) ?: return
Expand All @@ -138,14 +151,37 @@ class ExtendTwigBlockIntention : PsiElementBaseIntentionAction() {
} else {
val factory = PsiFileFactory.getInstance(project)
val file = factory.createFileFromText(fileName!!, TwigFileType.INSTANCE, blockHeader + "\n\n" + blockCode)
currentFolder.add(file)
currentFolder!!.add(file)

FileEditorManager.getInstance(project)
.openTextEditor(OpenFileDescriptor(project, currentFolder.findFile(fileName!!)!!.virtualFile), true)
.openTextEditor(OpenFileDescriptor(project, currentFolder!!.findFile(fileName!!)!!.virtualFile), true)
?: return
}
}

private fun createMissingViewFolder(bundle: ShopwareBundle, project: Project): PsiDirectory {
val bundleFile = LocalFileSystem.getInstance().findFileByPath(bundle.path)!!
val bundleFolder = PsiManager.getInstance(project).findFile(bundleFile)!!.containingDirectory

if (bundleFolder.findSubdirectory("Resources") == null) {
bundleFolder.createSubdirectory("Resources")
}

val resourcesFolder = bundleFolder.findSubdirectory("Resources")!!

if (resourcesFolder.findSubdirectory("views") == null) {
resourcesFolder.createSubdirectory("views")
}

val viewFolder = resourcesFolder.findSubdirectory("views")!!

if (viewFolder.findSubdirectory("storefront") != null) {
return viewFolder.findSubdirectory("storefront")!!
}

return viewFolder.createSubdirectory("storefront")
}

override fun checkFile(file: PsiFile?): Boolean {
return true
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/de/shyim/shopware6/util/ShopwareBundleUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import de.shyim.shopware6.index.ShopwareBundleIndex
import de.shyim.shopware6.index.dict.ShopwareBundle

object ShopwareBundleUtil {
private val NON_VIEW_SHOPWARE_BUNDLES =
arrayOf("Administration", "DevOps", "Checkout", "Profiling", "Elasticsearch", "Content", "System", "Framework")

fun getAllBundles(project: Project): MutableList<ShopwareBundle> {
val bundles: MutableList<ShopwareBundle> = ArrayList()

Expand All @@ -20,9 +23,9 @@ object ShopwareBundleUtil {
return bundles
}

fun getAllBundlesWithViewDirectory(project: Project): List<ShopwareBundle> {
return getAllBundles(project).filter { bundle ->
return@filter bundle.viewPath != null
fun getAllBundlesRelatedToViews(project: Project): List<ShopwareBundle> {
return getAllBundles(project).filter {
!NON_VIEW_SHOPWARE_BUNDLES.contains(it.name)
}
}
}
8 changes: 4 additions & 4 deletions src/main/kotlin/de/shyim/shopware6/util/TwigUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.intellij.openapi.project.Project
object TwigUtil {
fun getTemplatePathByFilePath(filePath: String, project: Project): String? {
var path: String? = null
ShopwareBundleUtil.getAllBundles(project).forEach { bundle ->
if (bundle.viewPath != null && filePath.startsWith(bundle.viewPath)) {
ShopwareBundleUtil.getAllBundlesRelatedToViews(project).forEach { bundle ->
if (filePath.startsWith(bundle.viewPath)) {
path = filePath.replace(bundle.viewPath, "")
}
}
Expand All @@ -16,8 +16,8 @@ object TwigUtil {

fun getBundleByFilePath(filePath: String, project: Project): String? {
var name: String? = null
ShopwareBundleUtil.getAllBundles(project).forEach { bundle ->
if (bundle.viewPath != null && filePath.startsWith(bundle.viewPath)) {
ShopwareBundleUtil.getAllBundlesRelatedToViews(project).forEach { bundle ->
if (filePath.startsWith(bundle.viewPath)) {
name = bundle.name
}
}
Expand Down

0 comments on commit ae683d8

Please sign in to comment.