generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add CreateEventListenerIntention
- Loading branch information
Showing
7 changed files
with
192 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
src/main/kotlin/de/shyim/shopware6/intentions/CreateEventListenerIntention.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package de.shyim.shopware6.intentions | ||
|
||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.command.CommandProcessor | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.fileEditor.FileEditorManager | ||
import com.intellij.openapi.fileEditor.OpenFileDescriptor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.ui.Messages | ||
import com.intellij.openapi.ui.popup.PopupChooserBuilder | ||
import com.intellij.openapi.vfs.LocalFileSystem | ||
import com.intellij.psi.* | ||
import com.intellij.ui.components.JBList | ||
import com.jetbrains.php.lang.PhpFileType | ||
import com.jetbrains.php.lang.psi.elements.PhpClass | ||
import com.jetbrains.php.roots.PhpNamespaceByFilesProvider | ||
import de.shyim.shopware6.index.dict.ShopwareBundle | ||
import de.shyim.shopware6.templates.ShopwareTemplates | ||
import de.shyim.shopware6.util.ShopwareBundleUtil | ||
import java.awt.Component | ||
import javax.swing.JLabel | ||
import javax.swing.JList | ||
|
||
class CreateEventListenerIntention : PsiElementBaseIntentionAction() { | ||
override fun getFamilyName() = "Subscribe to this event using a event listener" | ||
override fun getText() = "Subscribe to this event using a event listener" | ||
|
||
override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean { | ||
if (element.parent !is PhpClass || editor == null) { | ||
return false | ||
} | ||
|
||
val phpClass = element.parent as PhpClass | ||
var currentClass = phpClass | ||
|
||
while (true) { | ||
if (currentClass.superFQN === null || currentClass.supers.isEmpty()) { | ||
return false | ||
} | ||
|
||
if (currentClass.superFQN == "\\Symfony\\Contracts\\EventDispatcher\\Event") { | ||
return true | ||
} else { | ||
currentClass = currentClass.supers[0] | ||
} | ||
} | ||
} | ||
|
||
override fun invoke(project: Project, editor: Editor?, element: PsiElement) { | ||
val phpClass = element.parent as PhpClass | ||
|
||
val bundleList = ShopwareBundleUtil.getAllBundles(project).filter { | ||
val virtualFile = LocalFileSystem.getInstance().findFileByPath(it.path)!! | ||
val psiFile = PsiManager.getInstance(project).findFile(virtualFile)!! | ||
|
||
return@filter psiFile.manager.isInProject(psiFile) | ||
}.sortedBy { shopwareBundle -> shopwareBundle.name } | ||
|
||
val jbBundleList = JBList(bundleList) | ||
|
||
jbBundleList.cellRenderer = object : JBList.StripedListCellRenderer() { | ||
override fun getListCellRendererComponent( | ||
list: JList<*>?, | ||
value: Any?, | ||
index: Int, | ||
isSelected: Boolean, | ||
cellHasFocus: Boolean | ||
): Component { | ||
val renderer = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus) | ||
|
||
if (renderer is JLabel && value is ShopwareBundle) { | ||
renderer.text = value.name | ||
} | ||
|
||
return renderer | ||
} | ||
} | ||
|
||
PopupChooserBuilder(jbBundleList) | ||
.setTitle("Shopware: Select bundle") | ||
.setItemChoosenCallback { | ||
CommandProcessor.getInstance().executeCommand(project, { | ||
ApplicationManager.getApplication().runWriteAction { | ||
this.createEventListener( | ||
jbBundleList.selectedValue!!, | ||
phpClass.fqn, | ||
project | ||
) | ||
} | ||
}, "Create event listener", null) | ||
} | ||
.createPopup() | ||
.showInBestPositionFor(editor!!) | ||
} | ||
|
||
private fun createEventListener(bundle: ShopwareBundle, eventClassName: String, project: Project) { | ||
val classParts = eventClassName.split("\\") | ||
val eventShort = classParts.get(classParts.size - 1) | ||
val className = "${eventShort}Listener" | ||
val classFileName = "${eventShort}Listener.php" | ||
|
||
val bundleFile = LocalFileSystem.getInstance().findFileByPath(bundle.path)!! | ||
val bundleFolder = PsiManager.getInstance(project).findFile(bundleFile)!!.containingDirectory | ||
var expectedFolder = bundleFolder.findSubdirectory("EventListener") | ||
|
||
if (expectedFolder == null) { | ||
expectedFolder = bundleFolder.createSubdirectory("EventListener") | ||
} | ||
|
||
if (expectedFolder.findFile(classFileName) != null) { | ||
Messages.showInfoMessage("File exists", "Error") | ||
return | ||
} | ||
|
||
val content = ShopwareTemplates.applyShopwarePHPEventListener( | ||
project, mapOf( | ||
"NAMESPACE" to getNamespaceOfFolder(expectedFolder).replace("\\\\", "\\"), | ||
"EVENT" to eventClassName.substring(1), | ||
"EVENT_SHORT" to eventShort, | ||
"CLASSNAME" to className, | ||
) | ||
) | ||
|
||
val factory = PsiFileFactory.getInstance(project) | ||
val file = factory.createFileFromText(classFileName, PhpFileType.INSTANCE, content) | ||
expectedFolder.add(file) | ||
|
||
FileEditorManager.getInstance(project) | ||
.openTextEditor(OpenFileDescriptor(project, expectedFolder.findFile(classFileName)!!.virtualFile), true) | ||
?: return | ||
} | ||
|
||
private fun getNamespaceOfFolder(folder: PsiDirectory): String { | ||
val namespaces = PhpNamespaceByFilesProvider.INSTANCE.suggestNamespaces(folder) | ||
|
||
if (namespaces != null && namespaces.size > 0) { | ||
return namespaces.get(0) | ||
} | ||
|
||
val superNamespaces = PhpNamespaceByFilesProvider.INSTANCE.suggestNamespaces(folder.parent!!) | ||
|
||
if (superNamespaces != null && superNamespaces.size > 0) { | ||
return superNamespaces.get(0) + "\\\\" + folder.name | ||
} | ||
|
||
return "" | ||
} | ||
|
||
override fun checkFile(file: PsiFile?): Boolean { | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/resources/fileTemplates/j2ee/php/Shopware PHP Event Listener.php.ft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace ${NAMESPACE}; | ||
|
||
use ${EVENT}; | ||
|
||
class ${CLASSNAME} | ||
{ | ||
public function __invoke(${EVENT_SHORT} $event): void | ||
{ | ||
|
||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/resources/intentionDescriptions/CreateEventListenerIntention/description.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
Create a new Symfony event listener in selected Shopware bundle with the chosen event class | ||
</body> | ||
</html> |