-
Notifications
You must be signed in to change notification settings - Fork 105
861: Developed context actions group, added generate routes.xml context action #958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bohdan-harniuk
merged 5 commits into
magento:4.3.0-develop
from
bohdan-harniuk:861-context-dependent-actions
Feb 7, 2022
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ea2a3d1
861: Developed context actions group, added generate routes.xml conte…
bohdan-harniuk 64b36dc
861: Reverted breaking changes
bohdan-harniuk 35540ca
Merge branch '4.3.0-develop' of github.com:magento/magento2-phpstorm-…
bohdan-harniuk 0bfcf92
Merge branch '4.3.0-develop' of github.com:magento/magento2-phpstorm-…
bohdan-harniuk c9465c0
861: Code refactoring after code review
bohdan-harniuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
<?xml version="1.0"?> | ||
#parse("XML File Header.xml") | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> | ||
<router id="${ROUTER_ID}"> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> | ||
<router id="${ROUTER_ID}">#if (${HAS_ROUTE_DATA}) | ||
<route id="${ROUTE_ID}" frontName="${FRONT_NAME}"> | ||
<module name="${MODULE_NAME}"/> | ||
</route> | ||
#end | ||
</router> | ||
</config> |
This file contains hidden or 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
161 changes: 161 additions & 0 deletions
161
src/com/magento/idea/magento2plugin/actions/context/AbstractContextAction.java
This file contains hidden or 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,161 @@ | ||
/* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
package com.magento.idea.magento2plugin.actions.context; | ||
|
||
import com.intellij.ide.fileTemplates.FileTemplate; | ||
import com.intellij.ide.fileTemplates.FileTemplateManager; | ||
import com.intellij.ide.fileTemplates.actions.AttributesDefaults; | ||
import com.intellij.ide.fileTemplates.actions.CreateFromTemplateActionBase; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.actionSystem.DataContext; | ||
import com.intellij.openapi.actionSystem.DataKey; | ||
import com.intellij.openapi.actionSystem.LangDataKeys; | ||
import com.intellij.openapi.actionSystem.impl.SimpleDataContext; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiDirectory; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.magento.idea.magento2plugin.MagentoIcons; | ||
import com.magento.idea.magento2plugin.magento.files.ModuleFileInterface; | ||
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public abstract class AbstractContextAction extends CreateFromTemplateActionBase { | ||
|
||
private static final DataKey<AttributesDefaults> ATTRIBUTE_DEFAULTS = DataKey.create( | ||
"attribute.defaults" | ||
); | ||
private final ModuleFileInterface moduleFile; | ||
private DataContext customDataContext; | ||
|
||
/** | ||
* Abstract context action constructor. | ||
* | ||
* @param title String | ||
* @param description String | ||
* @param moduleFile ModuleFileInterface | ||
*/ | ||
public AbstractContextAction( | ||
final @NotNull String title, | ||
final @NotNull String description, | ||
final @NotNull ModuleFileInterface moduleFile | ||
) { | ||
super(title, description, MagentoIcons.MODULE); | ||
this.moduleFile = moduleFile; | ||
} | ||
|
||
@Override | ||
public void update(final @NotNull AnActionEvent event) { | ||
event.getPresentation().setEnabled(false); | ||
event.getPresentation().setVisible(false); | ||
|
||
final Project project = event.getProject(); | ||
|
||
if (project == null) { | ||
return; | ||
} | ||
final DataContext context = event.getDataContext(); | ||
final PsiElement targetElement = LangDataKeys.PSI_ELEMENT.getData(context); | ||
|
||
if (targetElement == null) { | ||
return; | ||
} | ||
PsiDirectory targetDirectory = null; | ||
PsiFile targetFile = null; | ||
|
||
if (targetElement instanceof PsiDirectory) { | ||
targetDirectory = (PsiDirectory) targetElement; | ||
} else if (targetElement instanceof PsiFile) { | ||
targetFile = (PsiFile) targetElement; | ||
targetDirectory = targetFile.getContainingDirectory(); | ||
} | ||
|
||
if (targetDirectory == null) { | ||
return; | ||
} | ||
final GetMagentoModuleUtil.MagentoModuleData moduleData = GetMagentoModuleUtil | ||
.getByContext(targetDirectory, project); | ||
|
||
if (moduleData == null | ||
|| moduleData.getName() == null | ||
|| !isVisible(moduleData, targetDirectory, targetFile)) { | ||
return; | ||
} | ||
customDataContext = SimpleDataContext | ||
.builder() | ||
.add( | ||
ATTRIBUTE_DEFAULTS, | ||
getProperties( | ||
new AttributesDefaults(), | ||
moduleData, | ||
targetDirectory, | ||
targetFile | ||
) | ||
) | ||
.build(); | ||
|
||
event.getPresentation().setEnabled(true); | ||
event.getPresentation().setVisible(true); | ||
} | ||
|
||
/** | ||
* Implement check if an action should be shown in the context defined by the module, | ||
* target directory or target file. | ||
* | ||
* @param moduleData GetMagentoModuleUtil.MagentoModuleData | ||
* @param targetDirectory PsiDirectory | ||
* @param targetFile PsiFile | ||
* | ||
* @return boolean | ||
*/ | ||
protected abstract boolean isVisible( | ||
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, | ||
final PsiDirectory targetDirectory, | ||
final PsiFile targetFile | ||
); | ||
|
||
/** | ||
* Implement to populate used in the target template variables. | ||
* | ||
* @param defaults AttributesDefaults | ||
* @param moduleData GetMagentoModuleUtil.MagentoModuleData | ||
* @param targetDirectory PsiDirectory | ||
* @param targetFile PsiFile | ||
* | ||
* @return AttributesDefaults | ||
*/ | ||
protected abstract AttributesDefaults getProperties( | ||
final @NotNull AttributesDefaults defaults, | ||
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, | ||
final PsiDirectory targetDirectory, | ||
final PsiFile targetFile | ||
); | ||
|
||
@Override | ||
protected @Nullable AttributesDefaults getAttributesDefaults( | ||
final @NotNull DataContext dataContext | ||
) { | ||
return customDataContext.getData(ATTRIBUTE_DEFAULTS); | ||
} | ||
|
||
@Override | ||
protected FileTemplate getTemplate( | ||
final @NotNull Project project, | ||
final @NotNull PsiDirectory directory | ||
) { | ||
final FileTemplateManager manager = FileTemplateManager.getInstance(project); | ||
final FileTemplate template = manager.findInternalTemplate(moduleFile.getTemplate()); | ||
template.setFileName(moduleFile.getFileName()); | ||
|
||
return template; | ||
} | ||
|
||
@Override | ||
public boolean isDumbAware() { | ||
return false; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/com/magento/idea/magento2plugin/actions/context/xml/NewRoutesXmlAction.java
This file contains hidden or 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,68 @@ | ||
/* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
package com.magento.idea.magento2plugin.actions.context.xml; | ||
|
||
import com.intellij.ide.fileTemplates.actions.AttributesDefaults; | ||
import com.intellij.psi.PsiDirectory; | ||
import com.intellij.psi.PsiFile; | ||
import com.magento.idea.magento2plugin.actions.context.AbstractContextAction; | ||
import com.magento.idea.magento2plugin.magento.files.RoutesXml; | ||
import com.magento.idea.magento2plugin.magento.packages.Areas; | ||
import com.magento.idea.magento2plugin.magento.packages.ComponentType; | ||
import com.magento.idea.magento2plugin.magento.packages.Package; | ||
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class NewRoutesXmlAction extends AbstractContextAction { | ||
|
||
public static final String ACTION_NAME = "Magento 2 Routes File"; | ||
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 routes.xml file"; | ||
|
||
/** | ||
* New routes.xml file generation action constructor. | ||
*/ | ||
public NewRoutesXmlAction() { | ||
super(ACTION_NAME, ACTION_DESCRIPTION, new RoutesXml()); | ||
} | ||
|
||
@Override | ||
protected boolean isVisible( | ||
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, | ||
final @NotNull PsiDirectory targetDirectory, | ||
final PsiFile targetFile | ||
) { | ||
final List<String> allowedDirectories = Arrays.asList( | ||
Package.moduleBaseAreaDir, | ||
Areas.adminhtml.toString(), | ||
Areas.frontend.toString() | ||
); | ||
|
||
return allowedDirectories.contains(targetDirectory.getName()) | ||
&& moduleData.getType().equals(ComponentType.module); | ||
} | ||
|
||
@Override | ||
protected AttributesDefaults getProperties( | ||
final @NotNull AttributesDefaults defaults, | ||
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, | ||
final PsiDirectory targetDirectory, | ||
final PsiFile targetFile | ||
) { | ||
defaults.addPredefined("HAS_ROUTE_DATA", String.valueOf(true)); | ||
defaults.addPredefined("MODULE_NAME", moduleData.getName()); | ||
|
||
if (Areas.adminhtml.toString().equals(targetDirectory.getName())) { | ||
defaults.addPredefined("ROUTER_ID", RoutesXml.ROUTER_ID_ADMIN); | ||
} else if (Package.moduleBaseAreaDir.equals(targetDirectory.getName()) | ||
|| Areas.frontend.toString().equals(targetDirectory.getName())) { | ||
defaults.addPredefined("ROUTER_ID", RoutesXml.ROUTER_ID_STANDARD); | ||
} | ||
|
||
return defaults; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add new line at end of file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done