From 8a8c3913fea16ee827b461c8336feb3e30a31f92 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 11 Feb 2022 19:12:48 +0200 Subject: [PATCH 1/5] 977: Added system.xml file in context generation --- resources/META-INF/plugin.xml | 1 + .../internal/Magento System XML.xml.ft | 7 +++ .../internal/Magento System XML.xml.html | 27 +++++++++ .../context/AbstractContextAction.java | 13 +++++ .../actions/context/xml/NewAclXmlAction.java | 7 +++ .../context/xml/NewConfigXmlAction.java | 7 +++ .../actions/context/xml/NewDiXmlAction.java | 7 +++ .../context/xml/NewRoutesXmlAction.java | 7 +++ .../context/xml/NewSystemXmlAction.java | 57 +++++++++++++++++++ .../context/xml/NewWebapiXmlAction.java | 7 +++ .../xml/XmlCompletionContributor.java | 14 ++--- ...ystemXml.java => ModuleSystemXmlFile.java} | 24 +++++++- .../util/magento/GetMagentoModuleUtil.java | 37 +++++++++++- ...ackendModelXmlCompletionRegistrarTest.java | 8 +-- ...SourceModelXmlCompletionRegistrarTest.java | 6 +- 15 files changed, 212 insertions(+), 17 deletions(-) create mode 100644 resources/fileTemplates/internal/Magento System XML.xml.ft create mode 100644 resources/fileTemplates/internal/Magento System XML.xml.html create mode 100644 src/com/magento/idea/magento2plugin/actions/context/xml/NewSystemXmlAction.java rename src/com/magento/idea/magento2plugin/magento/files/{ModuleSystemXml.java => ModuleSystemXmlFile.java} (50%) diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index a3f9dffd1..bacddef60 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -64,6 +64,7 @@ + diff --git a/resources/fileTemplates/internal/Magento System XML.xml.ft b/resources/fileTemplates/internal/Magento System XML.xml.ft new file mode 100644 index 000000000..b38d0ee44 --- /dev/null +++ b/resources/fileTemplates/internal/Magento System XML.xml.ft @@ -0,0 +1,7 @@ + +#parse("XML File Header.xml") + + + + diff --git a/resources/fileTemplates/internal/Magento System XML.xml.html b/resources/fileTemplates/internal/Magento System XML.xml.html new file mode 100644 index 000000000..9b8c016bb --- /dev/null +++ b/resources/fileTemplates/internal/Magento System XML.xml.html @@ -0,0 +1,27 @@ + + + + + + + + + + +
+ The system.xml file allows you to manage the Magento system configuration. Use this topic as a general reference for the system.xml file. + The system.xml file is located under etc/adminhtml/system.xml in a given Magento 2 extension. +
+
+ Read more about the system.xml in the + + DevDocs. +
+
+ + diff --git a/src/com/magento/idea/magento2plugin/actions/context/AbstractContextAction.java b/src/com/magento/idea/magento2plugin/actions/context/AbstractContextAction.java index 91c835645..d54bbaa75 100644 --- a/src/com/magento/idea/magento2plugin/actions/context/AbstractContextAction.java +++ b/src/com/magento/idea/magento2plugin/actions/context/AbstractContextAction.java @@ -20,6 +20,7 @@ import com.intellij.psi.PsiFile; import com.magento.idea.magento2plugin.MagentoIcons; import com.magento.idea.magento2plugin.magento.files.ModuleFileInterface; +import com.magento.idea.magento2plugin.magento.packages.Package; import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -154,6 +155,18 @@ protected FileTemplate getTemplate( return template; } + protected @Nullable PsiDirectory getGlobalScopeDir(final @NotNull PsiDirectory directory) { + PsiDirectory globalScopeDir; + + if (Package.moduleBaseAreaDir.equals(directory.getName())) { + globalScopeDir = directory; + } else { + globalScopeDir = directory.getParentDirectory(); + } + + return globalScopeDir; + } + @Override public boolean isDumbAware() { return false; diff --git a/src/com/magento/idea/magento2plugin/actions/context/xml/NewAclXmlAction.java b/src/com/magento/idea/magento2plugin/actions/context/xml/NewAclXmlAction.java index 0b996ce97..9c432d0b3 100644 --- a/src/com/magento/idea/magento2plugin/actions/context/xml/NewAclXmlAction.java +++ b/src/com/magento/idea/magento2plugin/actions/context/xml/NewAclXmlAction.java @@ -33,7 +33,14 @@ protected boolean isVisible( final @NotNull PsiDirectory targetDirectory, final PsiFile targetFile ) { + final PsiDirectory configDir = moduleData.getConfigDir(); + + if (configDir == null) { + return false; + } + return targetDirectory.getName().equals(Package.moduleBaseAreaDir) + && targetDirectory.equals(configDir) && moduleData.getType().equals(ComponentType.module); } diff --git a/src/com/magento/idea/magento2plugin/actions/context/xml/NewConfigXmlAction.java b/src/com/magento/idea/magento2plugin/actions/context/xml/NewConfigXmlAction.java index 76cdd1060..f6c5db590 100644 --- a/src/com/magento/idea/magento2plugin/actions/context/xml/NewConfigXmlAction.java +++ b/src/com/magento/idea/magento2plugin/actions/context/xml/NewConfigXmlAction.java @@ -33,7 +33,14 @@ protected boolean isVisible( final @NotNull PsiDirectory targetDirectory, final PsiFile targetFile ) { + final PsiDirectory configDir = moduleData.getConfigDir(); + + if (configDir == null) { + return false; + } + return targetDirectory.getName().equals(Package.moduleBaseAreaDir) + && targetDirectory.equals(configDir) && moduleData.getType().equals(ComponentType.module); } diff --git a/src/com/magento/idea/magento2plugin/actions/context/xml/NewDiXmlAction.java b/src/com/magento/idea/magento2plugin/actions/context/xml/NewDiXmlAction.java index 67c00db14..80c9a6284 100644 --- a/src/com/magento/idea/magento2plugin/actions/context/xml/NewDiXmlAction.java +++ b/src/com/magento/idea/magento2plugin/actions/context/xml/NewDiXmlAction.java @@ -45,8 +45,15 @@ protected boolean isVisible( Areas.graphql.toString(), Areas.crontab.toString() ); + final PsiDirectory configDir = moduleData.getConfigDir(); + final PsiDirectory globalScopeDir = getGlobalScopeDir(targetDirectory); + + if (configDir == null || globalScopeDir == null) { + return false; + } return allowedDirectories.contains(targetDirectory.getName()) + && globalScopeDir.equals(configDir) && moduleData.getType().equals(ComponentType.module); } diff --git a/src/com/magento/idea/magento2plugin/actions/context/xml/NewRoutesXmlAction.java b/src/com/magento/idea/magento2plugin/actions/context/xml/NewRoutesXmlAction.java index cd007dffe..b259dcbd9 100644 --- a/src/com/magento/idea/magento2plugin/actions/context/xml/NewRoutesXmlAction.java +++ b/src/com/magento/idea/magento2plugin/actions/context/xml/NewRoutesXmlAction.java @@ -36,6 +36,12 @@ protected boolean isVisible( final @NotNull PsiDirectory targetDirectory, final PsiFile targetFile ) { + final PsiDirectory configDir = moduleData.getConfigDir(); + final PsiDirectory globalScopeDir = getGlobalScopeDir(targetDirectory); + + if (configDir == null || globalScopeDir == null) { + return false; + } final List allowedDirectories = Arrays.asList( Package.moduleBaseAreaDir, Areas.adminhtml.toString(), @@ -43,6 +49,7 @@ protected boolean isVisible( ); return allowedDirectories.contains(targetDirectory.getName()) + && globalScopeDir.equals(configDir) && moduleData.getType().equals(ComponentType.module); } diff --git a/src/com/magento/idea/magento2plugin/actions/context/xml/NewSystemXmlAction.java b/src/com/magento/idea/magento2plugin/actions/context/xml/NewSystemXmlAction.java new file mode 100644 index 000000000..f1a581834 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/context/xml/NewSystemXmlAction.java @@ -0,0 +1,57 @@ +/* + * 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.ModuleSystemXmlFile; +import com.magento.idea.magento2plugin.magento.packages.Areas; +import com.magento.idea.magento2plugin.magento.packages.ComponentType; +import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; +import org.jetbrains.annotations.NotNull; + +public class NewSystemXmlAction extends AbstractContextAction { + + public static final String ACTION_NAME = "Magento 2 System File"; + public static final String ACTION_DESCRIPTION = "Create a new Magento 2 system.xml file"; + + /** + * New system.xml file generation action constructor. + */ + public NewSystemXmlAction() { + super(ACTION_NAME, ACTION_DESCRIPTION, new ModuleSystemXmlFile()); + } + + @Override + protected boolean isVisible( + final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, + final @NotNull PsiDirectory targetDirectory, + final PsiFile targetFile + ) { + final PsiDirectory parentDir = targetDirectory.getParentDirectory(); + final PsiDirectory configDir = moduleData.getConfigDir(); + + if (parentDir == null || configDir == null) { + return false; + } + + return targetDirectory.getName().equals(Areas.adminhtml.toString()) + && parentDir.equals(configDir) + && moduleData.getType().equals(ComponentType.module); + } + + @Override + protected AttributesDefaults getProperties( + final @NotNull AttributesDefaults defaults, + final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, + final PsiDirectory targetDirectory, + final PsiFile targetFile + ) { + return defaults; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/context/xml/NewWebapiXmlAction.java b/src/com/magento/idea/magento2plugin/actions/context/xml/NewWebapiXmlAction.java index 241d24d83..ada13372f 100644 --- a/src/com/magento/idea/magento2plugin/actions/context/xml/NewWebapiXmlAction.java +++ b/src/com/magento/idea/magento2plugin/actions/context/xml/NewWebapiXmlAction.java @@ -33,7 +33,14 @@ protected boolean isVisible( final @NotNull PsiDirectory targetDirectory, final PsiFile targetFile ) { + final PsiDirectory configDir = moduleData.getConfigDir(); + + if (configDir == null) { + return false; + } + return targetDirectory.getName().equals(Package.moduleBaseAreaDir) + && targetDirectory.equals(configDir) && moduleData.getType().equals(ComponentType.module); } diff --git a/src/com/magento/idea/magento2plugin/completion/xml/XmlCompletionContributor.java b/src/com/magento/idea/magento2plugin/completion/xml/XmlCompletionContributor.java index 6590d10cf..e22847d74 100644 --- a/src/com/magento/idea/magento2plugin/completion/xml/XmlCompletionContributor.java +++ b/src/com/magento/idea/magento2plugin/completion/xml/XmlCompletionContributor.java @@ -154,24 +154,24 @@ public XmlCompletionContributor() { // php class completion in system.xml files. extend(CompletionType.BASIC, PlatformPatterns.psiElement(XmlTokenType.XML_DATA_CHARACTERS) - .inside(XmlPatterns.xmlTag().withName(ModuleSystemXml.XML_TAG_SOURCE_MODEL) - .withParent(XmlPatterns.xmlTag().withName(ModuleSystemXml.FIELD_ELEMENT_NAME)) - ).inFile(XmlPatterns.xmlFile().withName(StandardPatterns.string().matches(ModuleSystemXml.FILE_NAME))), + .inside(XmlPatterns.xmlTag().withName(ModuleSystemXmlFile.XML_TAG_SOURCE_MODEL) + .withParent(XmlPatterns.xmlTag().withName(ModuleSystemXmlFile.FIELD_ELEMENT_NAME)) + ).inFile(XmlPatterns.xmlFile().withName(StandardPatterns.string().matches(ModuleSystemXmlFile.FILE_NAME))), new PhpClassCompletionProvider() ); // completion extend(CompletionType.BASIC, PlatformPatterns.psiElement(XmlTokenType.XML_DATA_CHARACTERS) - .inside(XmlPatterns.xmlTag().withName(ModuleSystemXml.XML_TAG_FRONTEND_MODEL)), + .inside(XmlPatterns.xmlTag().withName(ModuleSystemXmlFile.XML_TAG_FRONTEND_MODEL)), new PhpClassCompletionProvider() ); // completion in system.xml extend(CompletionType.BASIC, PlatformPatterns.psiElement(XmlTokenType.XML_DATA_CHARACTERS) - .inside(XmlPatterns.xmlTag().withName(ModuleSystemXml.XML_TAG_BACKEND_MODEL) - .withParent(XmlPatterns.xmlTag().withName(ModuleSystemXml.FIELD_ELEMENT_NAME)) - ).inFile(XmlPatterns.xmlFile().withName(StandardPatterns.string().matches(ModuleSystemXml.FILE_NAME))), + .inside(XmlPatterns.xmlTag().withName(ModuleSystemXmlFile.XML_TAG_BACKEND_MODEL) + .withParent(XmlPatterns.xmlTag().withName(ModuleSystemXmlFile.FIELD_ELEMENT_NAME)) + ).inFile(XmlPatterns.xmlFile().withName(StandardPatterns.string().matches(ModuleSystemXmlFile.FILE_NAME))), new PhpClassCompletionProvider() ); diff --git a/src/com/magento/idea/magento2plugin/magento/files/ModuleSystemXml.java b/src/com/magento/idea/magento2plugin/magento/files/ModuleSystemXmlFile.java similarity index 50% rename from src/com/magento/idea/magento2plugin/magento/files/ModuleSystemXml.java rename to src/com/magento/idea/magento2plugin/magento/files/ModuleSystemXmlFile.java index 2545ee75a..11017dd18 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/ModuleSystemXml.java +++ b/src/com/magento/idea/magento2plugin/magento/files/ModuleSystemXmlFile.java @@ -2,12 +2,34 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + package com.magento.idea.magento2plugin.magento.files; -public class ModuleSystemXml { +import com.intellij.lang.Language; +import com.intellij.lang.xml.XMLLanguage; + +public final class ModuleSystemXmlFile implements ModuleFileInterface { + public static final String FILE_NAME = "system.xml"; + public static final String TEMPLATE = "Magento System XML"; + public static final String FIELD_ELEMENT_NAME = "field"; public static final String XML_TAG_SOURCE_MODEL = "source_model"; public static final String XML_TAG_FRONTEND_MODEL = "frontend_model"; public static final String XML_TAG_BACKEND_MODEL = "backend_model"; + + @Override + public String getFileName() { + return FILE_NAME; + } + + @Override + public String getTemplate() { + return TEMPLATE; + } + + @Override + public Language getLanguage() { + return XMLLanguage.INSTANCE; + } } diff --git a/src/com/magento/idea/magento2plugin/util/magento/GetMagentoModuleUtil.java b/src/com/magento/idea/magento2plugin/util/magento/GetMagentoModuleUtil.java index 067191935..e4e712648 100644 --- a/src/com/magento/idea/magento2plugin/util/magento/GetMagentoModuleUtil.java +++ b/src/com/magento/idea/magento2plugin/util/magento/GetMagentoModuleUtil.java @@ -17,7 +17,10 @@ import com.magento.idea.magento2plugin.magento.files.RegistrationPhp; import com.magento.idea.magento2plugin.magento.packages.ComponentType; import java.util.Collection; + +import com.magento.idea.magento2plugin.magento.packages.Package; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public final class GetMagentoModuleUtil { @@ -46,6 +49,9 @@ public static MagentoModuleData getByContext( if (registrationFile == null) { return null; } + final PsiDirectory configDir = registrationFile + .getContainingDirectory() + .findSubdirectory(Package.moduleBaseAreaDir); final Collection methodReferences = PsiTreeUtil.findChildrenOfType( registrationFile, MethodReference.class @@ -71,7 +77,7 @@ public static MagentoModuleData getByContext( return null; } - return new MagentoModuleData(name, resolvedType); + return new MagentoModuleData(name, resolvedType, configDir); } return null; @@ -124,13 +130,36 @@ public static class MagentoModuleData { private final String name; private final ComponentType type; - + private final PsiDirectory configDir; + + /** + * Default constructor. + * + * @param name String + * @param type ComponentType + */ public MagentoModuleData( final @NotNull String name, final @NotNull ComponentType type + ) { + this(name, type, null); + } + + /** + * Constructor with a config directory specified. + * + * @param name String + * @param type ComponentType + * @param configDir PsiDirectory + */ + public MagentoModuleData( + final @NotNull String name, + final @NotNull ComponentType type, + final @Nullable PsiDirectory configDir ) { this.name = name; this.type = type; + this.configDir = configDir; } public String getName() { @@ -140,5 +169,9 @@ public String getName() { public ComponentType getType() { return type; } + + public @Nullable PsiDirectory getConfigDir() { + return configDir; + } } } diff --git a/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java b/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java index c6a49fba0..4b929839e 100644 --- a/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java +++ b/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java @@ -5,7 +5,7 @@ package com.magento.idea.magento2plugin.completion.xml; import com.magento.idea.magento2plugin.magento.files.ModuleConfigXml; -import com.magento.idea.magento2plugin.magento.files.ModuleSystemXml; +import com.magento.idea.magento2plugin.magento.files.ModuleSystemXmlFile; public class BackendModelXmlCompletionRegistrarTest extends CompletionXmlFixtureTestCase { @@ -17,7 +17,7 @@ public class BackendModelXmlCompletionRegistrarTest extends CompletionXmlFixture }; public void testSystemXmlElementProvideCompletion() { - String filePath = this.getFixturePath(ModuleSystemXml.FILE_NAME); + String filePath = this.getFixturePath(ModuleSystemXmlFile.FILE_NAME); myFixture.configureByFile(filePath); assertCompletionContains(filePath, systemXmlBackendModelLookupStringCheck); @@ -25,7 +25,7 @@ public void testSystemXmlElementProvideCompletion() { public void testSystemXmlElementCompletionWontShow() { String filePath = this.getFixturePath( - ModuleSystemXml.FILE_NAME + ModuleSystemXmlFile.FILE_NAME ); myFixture.copyFileToProject(filePath); @@ -34,7 +34,7 @@ public void testSystemXmlElementCompletionWontShow() { public void testSystemXmlBackendModelAttributeMatchWithFile() { String filePath = this.getFixturePath( - ModuleSystemXml.FILE_NAME + ModuleSystemXmlFile.FILE_NAME ); assertFileContainsCompletions(filePath, systemXmlBackendModelLookupStringCheck); diff --git a/tests/com/magento/idea/magento2plugin/completion/xml/SourceModelXmlCompletionRegistrarTest.java b/tests/com/magento/idea/magento2plugin/completion/xml/SourceModelXmlCompletionRegistrarTest.java index c158498a2..278f3bbc0 100644 --- a/tests/com/magento/idea/magento2plugin/completion/xml/SourceModelXmlCompletionRegistrarTest.java +++ b/tests/com/magento/idea/magento2plugin/completion/xml/SourceModelXmlCompletionRegistrarTest.java @@ -4,7 +4,7 @@ */ package com.magento.idea.magento2plugin.completion.xml; -import com.magento.idea.magento2plugin.magento.files.ModuleSystemXml; +import com.magento.idea.magento2plugin.magento.files.ModuleSystemXmlFile; import com.magento.idea.magento2plugin.magento.files.ModuleWidgetXml; import java.io.IOException; @@ -15,14 +15,14 @@ public class SourceModelXmlCompletionRegistrarTest extends CompletionXmlFixtureT }; public void testSourceModelXmlElementMustProvideCompletion() { - String filePath = this.getFixturePath(ModuleSystemXml.FILE_NAME); + String filePath = this.getFixturePath(ModuleSystemXmlFile.FILE_NAME); myFixture.copyFileToProject(filePath); assertCompletionContains(filePath, lookupStringsCheck); } public void testSourceModelXmlElementMatchWithFilePositiveCase() { - String filePath = this.getFixturePath(ModuleSystemXml.FILE_NAME); + String filePath = this.getFixturePath(ModuleSystemXmlFile.FILE_NAME); myFixture.copyFileToProject(filePath); assertFileContainsCompletions(filePath, lookupStringsCheck); From 2b92a49ce2436e061619facfe72923ffa9abfdc5 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 11 Feb 2022 19:19:42 +0200 Subject: [PATCH 2/5] 977: Code refactoring --- .../util/magento/GetMagentoModuleUtil.java | 3 +- ...ackendModelXmlCompletionRegistrarTest.java | 33 ++++++++++--------- ...SourceModelXmlCompletionRegistrarTest.java | 27 +++++++-------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/util/magento/GetMagentoModuleUtil.java b/src/com/magento/idea/magento2plugin/util/magento/GetMagentoModuleUtil.java index e4e712648..207b7eaf4 100644 --- a/src/com/magento/idea/magento2plugin/util/magento/GetMagentoModuleUtil.java +++ b/src/com/magento/idea/magento2plugin/util/magento/GetMagentoModuleUtil.java @@ -16,9 +16,8 @@ import com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl; import com.magento.idea.magento2plugin.magento.files.RegistrationPhp; import com.magento.idea.magento2plugin.magento.packages.ComponentType; -import java.util.Collection; - import com.magento.idea.magento2plugin.magento.packages.Package; +import java.util.Collection; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java b/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java index 4b929839e..41e9e949d 100644 --- a/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java +++ b/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java @@ -2,6 +2,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + package com.magento.idea.magento2plugin.completion.xml; import com.magento.idea.magento2plugin.magento.files.ModuleConfigXml; @@ -9,22 +10,22 @@ public class BackendModelXmlCompletionRegistrarTest extends CompletionXmlFixtureTestCase { - private static final String[] systemXmlBackendModelLookupStringCheck = new String[]{ + private static final String[] SYSTEM_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK = { "Magento\\Backend\\Model\\Source\\Roles" }; - private static final String[] configXmlBackendModelLookupStringCheck = new String[]{ + private static final String[] CONFIG_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK = { "Magento\\Backend\\Model\\Source\\YesNo" }; public void testSystemXmlElementProvideCompletion() { - String filePath = this.getFixturePath(ModuleSystemXmlFile.FILE_NAME); + final String filePath = this.getFixturePath(ModuleSystemXmlFile.FILE_NAME); myFixture.configureByFile(filePath); - assertCompletionContains(filePath, systemXmlBackendModelLookupStringCheck); + assertCompletionContains(filePath, SYSTEM_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK); } public void testSystemXmlElementCompletionWontShow() { - String filePath = this.getFixturePath( + final String filePath = this.getFixturePath( ModuleSystemXmlFile.FILE_NAME ); myFixture.copyFileToProject(filePath); @@ -33,35 +34,35 @@ public void testSystemXmlElementCompletionWontShow() { } public void testSystemXmlBackendModelAttributeMatchWithFile() { - String filePath = this.getFixturePath( + final String filePath = this.getFixturePath( ModuleSystemXmlFile.FILE_NAME ); - assertFileContainsCompletions(filePath, systemXmlBackendModelLookupStringCheck); + assertFileContainsCompletions(filePath, SYSTEM_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK); } public void testSystemXmlBackendModelAttributeDontMatchWithFile() { - String filePath = this.getFixturePath( + final String filePath = this.getFixturePath( "other-file-than-system.xml" ); assertFileNotContainsCompletions( filePath, - systemXmlBackendModelLookupStringCheck + SYSTEM_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK ); } public void testConfigXmlElementProvideCompletion() { - String filePath = this.getFixturePath( + final String filePath = this.getFixturePath( ModuleConfigXml.FILE_NAME ); myFixture.copyFileToProject(filePath); - assertCompletionContains(filePath, configXmlBackendModelLookupStringCheck); + assertCompletionContains(filePath, CONFIG_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK); } public void testConfigXmlElementCompletionWontShow() { - String filePath = this.getFixturePath( + final String filePath = this.getFixturePath( ModuleConfigXml.FILE_NAME ); myFixture.copyFileToProject(filePath); @@ -70,21 +71,21 @@ public void testConfigXmlElementCompletionWontShow() { } public void testConfigXmlBackendModelAttributeMatchWithFile() { - String filePath = this.getFixturePath( + final String filePath = this.getFixturePath( ModuleConfigXml.FILE_NAME ); - assertFileContainsCompletions(filePath, configXmlBackendModelLookupStringCheck); + assertFileContainsCompletions(filePath, CONFIG_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK); } public void testConfigXmlBackendModelAttributeDontMatchWithFile() { - String filePath = this.getFixturePath( + final String filePath = this.getFixturePath( "other-file-than-config.xml" ); assertFileNotContainsCompletions( filePath, - configXmlBackendModelLookupStringCheck + CONFIG_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK ); } } diff --git a/tests/com/magento/idea/magento2plugin/completion/xml/SourceModelXmlCompletionRegistrarTest.java b/tests/com/magento/idea/magento2plugin/completion/xml/SourceModelXmlCompletionRegistrarTest.java index 278f3bbc0..2d032f9bf 100644 --- a/tests/com/magento/idea/magento2plugin/completion/xml/SourceModelXmlCompletionRegistrarTest.java +++ b/tests/com/magento/idea/magento2plugin/completion/xml/SourceModelXmlCompletionRegistrarTest.java @@ -2,6 +2,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + package com.magento.idea.magento2plugin.completion.xml; import com.magento.idea.magento2plugin.magento.files.ModuleSystemXmlFile; @@ -10,55 +11,55 @@ public class SourceModelXmlCompletionRegistrarTest extends CompletionXmlFixtureTestCase { - private static final String[] lookupStringsCheck = new String[]{ + private static final String[] LOOKUP_STRINGS_CHECK = { "Magento\\Backend\\Model\\Source\\YesNo" }; public void testSourceModelXmlElementMustProvideCompletion() { - String filePath = this.getFixturePath(ModuleSystemXmlFile.FILE_NAME); + final String filePath = this.getFixturePath(ModuleSystemXmlFile.FILE_NAME); myFixture.copyFileToProject(filePath); - assertCompletionContains(filePath, lookupStringsCheck); + assertCompletionContains(filePath, LOOKUP_STRINGS_CHECK); } public void testSourceModelXmlElementMatchWithFilePositiveCase() { - String filePath = this.getFixturePath(ModuleSystemXmlFile.FILE_NAME); + final String filePath = this.getFixturePath(ModuleSystemXmlFile.FILE_NAME); myFixture.copyFileToProject(filePath); - assertFileContainsCompletions(filePath, lookupStringsCheck); + assertFileContainsCompletions(filePath, LOOKUP_STRINGS_CHECK); } public void testSourceModelXmlElementMatchWithFileNegativeCase() { - String filePath = this.getFixturePath("not-system.xml"); + final String filePath = this.getFixturePath("not-system.xml"); myFixture.copyFileToProject(filePath); assertFileNotContainsCompletions( filePath, - lookupStringsCheck + LOOKUP_STRINGS_CHECK ); } public void testSourceModelXmlAttributeMustProvideCompletion() { - String filePath = this.getFixturePath(ModuleWidgetXml.FILE_NAME); + final String filePath = this.getFixturePath(ModuleWidgetXml.FILE_NAME); myFixture.copyFileToProject(filePath); - assertCompletionContains(filePath, lookupStringsCheck); + assertCompletionContains(filePath, LOOKUP_STRINGS_CHECK); } public void testSourceModelXmlAttributeMatchWithFilePositiveCase() { - String filePath = this.getFixturePath(ModuleWidgetXml.FILE_NAME); + final String filePath = this.getFixturePath(ModuleWidgetXml.FILE_NAME); myFixture.copyFileToProject(filePath); - assertFileContainsCompletions(filePath, lookupStringsCheck); + assertFileContainsCompletions(filePath, LOOKUP_STRINGS_CHECK); } public void testSourceModelXmlAttributeMatchWithFileNegativeCase() throws IOException { - String filePath = this.getFixturePath("not-widget.xml"); + final String filePath = this.getFixturePath("not-widget.xml"); myFixture.copyFileToProject(filePath); assertFileNotContainsCompletions( filePath, - lookupStringsCheck + LOOKUP_STRINGS_CHECK ); } } From 51d110ddd590a5ade554851a9afaf5da471ba885 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 11 Feb 2022 19:35:49 +0200 Subject: [PATCH 3/5] 977: Code refactoring --- .../xml/BackendModelXmlCompletionRegistrarTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java b/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java index 41e9e949d..6fb78a563 100644 --- a/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java +++ b/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java @@ -11,10 +11,10 @@ public class BackendModelXmlCompletionRegistrarTest extends CompletionXmlFixtureTestCase { private static final String[] SYSTEM_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK = { - "Magento\\Backend\\Model\\Source\\Roles" - }; + "Magento\\Backend\\Model\\Source\\Roles" + }; private static final String[] CONFIG_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK = { - "Magento\\Backend\\Model\\Source\\YesNo" + "Magento\\Backend\\Model\\Source\\YesNo" }; public void testSystemXmlElementProvideCompletion() { From c7c8693962dacda37b433e60b1bd5c9ef6685cc8 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 11 Feb 2022 19:46:53 +0200 Subject: [PATCH 4/5] 977: Code refactoring --- ...ackendModelXmlCompletionRegistrarTest.java | 38 +++++++++++++++---- ...SourceModelXmlCompletionRegistrarTest.java | 25 ++++++++++-- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java b/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java index 6fb78a563..0a5a14b63 100644 --- a/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java +++ b/tests/com/magento/idea/magento2plugin/completion/xml/BackendModelXmlCompletionRegistrarTest.java @@ -17,6 +17,9 @@ public class BackendModelXmlCompletionRegistrarTest extends CompletionXmlFixture "Magento\\Backend\\Model\\Source\\YesNo" }; + /** + * Test system.xml file element completion. + */ public void testSystemXmlElementProvideCompletion() { final String filePath = this.getFixturePath(ModuleSystemXmlFile.FILE_NAME); myFixture.configureByFile(filePath); @@ -24,26 +27,35 @@ public void testSystemXmlElementProvideCompletion() { assertCompletionContains(filePath, SYSTEM_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK); } + /** + * Test system.xml file element completion won't show. + */ public void testSystemXmlElementCompletionWontShow() { final String filePath = this.getFixturePath( - ModuleSystemXmlFile.FILE_NAME + ModuleSystemXmlFile.FILE_NAME ); myFixture.copyFileToProject(filePath); assertCompletionNotShowing(filePath); } + /** + * Test system.xml backend model attribute match with the file. + */ public void testSystemXmlBackendModelAttributeMatchWithFile() { final String filePath = this.getFixturePath( - ModuleSystemXmlFile.FILE_NAME + ModuleSystemXmlFile.FILE_NAME ); assertFileContainsCompletions(filePath, SYSTEM_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK); } + /** + * Test system.xml backend model attribute doesn't match with the file. + */ public void testSystemXmlBackendModelAttributeDontMatchWithFile() { final String filePath = this.getFixturePath( - "other-file-than-system.xml" + "other-file-than-system.xml" ); assertFileNotContainsCompletions( @@ -52,35 +64,47 @@ public void testSystemXmlBackendModelAttributeDontMatchWithFile() { ); } + /** + * Test config.xml file element completion. + */ public void testConfigXmlElementProvideCompletion() { final String filePath = this.getFixturePath( - ModuleConfigXml.FILE_NAME + ModuleConfigXml.FILE_NAME ); myFixture.copyFileToProject(filePath); assertCompletionContains(filePath, CONFIG_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK); } + /** + * Test config.xml file element completion won't show. + */ public void testConfigXmlElementCompletionWontShow() { final String filePath = this.getFixturePath( - ModuleConfigXml.FILE_NAME + ModuleConfigXml.FILE_NAME ); myFixture.copyFileToProject(filePath); assertCompletionNotShowing(filePath); } + /** + * Test config.xml backend model attribute match with the file. + */ public void testConfigXmlBackendModelAttributeMatchWithFile() { final String filePath = this.getFixturePath( - ModuleConfigXml.FILE_NAME + ModuleConfigXml.FILE_NAME ); assertFileContainsCompletions(filePath, CONFIG_XML_BACKEND_MODEL_LOOKUP_STRING_CHECK); } + /** + * Test config.xml backend model attribute doesn't match with the file. + */ public void testConfigXmlBackendModelAttributeDontMatchWithFile() { final String filePath = this.getFixturePath( - "other-file-than-config.xml" + "other-file-than-config.xml" ); assertFileNotContainsCompletions( diff --git a/tests/com/magento/idea/magento2plugin/completion/xml/SourceModelXmlCompletionRegistrarTest.java b/tests/com/magento/idea/magento2plugin/completion/xml/SourceModelXmlCompletionRegistrarTest.java index 2d032f9bf..939380523 100644 --- a/tests/com/magento/idea/magento2plugin/completion/xml/SourceModelXmlCompletionRegistrarTest.java +++ b/tests/com/magento/idea/magento2plugin/completion/xml/SourceModelXmlCompletionRegistrarTest.java @@ -7,7 +7,6 @@ import com.magento.idea.magento2plugin.magento.files.ModuleSystemXmlFile; import com.magento.idea.magento2plugin.magento.files.ModuleWidgetXml; -import java.io.IOException; public class SourceModelXmlCompletionRegistrarTest extends CompletionXmlFixtureTestCase { @@ -15,6 +14,9 @@ public class SourceModelXmlCompletionRegistrarTest extends CompletionXmlFixtureT "Magento\\Backend\\Model\\Source\\YesNo" }; + /** + * Test source model xml element completion. + */ public void testSourceModelXmlElementMustProvideCompletion() { final String filePath = this.getFixturePath(ModuleSystemXmlFile.FILE_NAME); myFixture.copyFileToProject(filePath); @@ -22,6 +24,9 @@ public void testSourceModelXmlElementMustProvideCompletion() { assertCompletionContains(filePath, LOOKUP_STRINGS_CHECK); } + /** + * Test source model xml element completion match with the file false positive. + */ public void testSourceModelXmlElementMatchWithFilePositiveCase() { final String filePath = this.getFixturePath(ModuleSystemXmlFile.FILE_NAME); myFixture.copyFileToProject(filePath); @@ -29,16 +34,22 @@ public void testSourceModelXmlElementMatchWithFilePositiveCase() { assertFileContainsCompletions(filePath, LOOKUP_STRINGS_CHECK); } + /** + * Test source model xml element completion match with the file negative case. + */ public void testSourceModelXmlElementMatchWithFileNegativeCase() { final String filePath = this.getFixturePath("not-system.xml"); myFixture.copyFileToProject(filePath); assertFileNotContainsCompletions( - filePath, + filePath, LOOKUP_STRINGS_CHECK ); } + /** + * Test source model attribute must provide completion. + */ public void testSourceModelXmlAttributeMustProvideCompletion() { final String filePath = this.getFixturePath(ModuleWidgetXml.FILE_NAME); myFixture.copyFileToProject(filePath); @@ -46,6 +57,9 @@ public void testSourceModelXmlAttributeMustProvideCompletion() { assertCompletionContains(filePath, LOOKUP_STRINGS_CHECK); } + /** + * Test source model attribute match with the file positive case. + */ public void testSourceModelXmlAttributeMatchWithFilePositiveCase() { final String filePath = this.getFixturePath(ModuleWidgetXml.FILE_NAME); myFixture.copyFileToProject(filePath); @@ -53,12 +67,15 @@ public void testSourceModelXmlAttributeMatchWithFilePositiveCase() { assertFileContainsCompletions(filePath, LOOKUP_STRINGS_CHECK); } - public void testSourceModelXmlAttributeMatchWithFileNegativeCase() throws IOException { + /** + * Test source model attribute match with the file negative case. + */ + public void testSourceModelXmlAttributeMatchWithFileNegativeCase() { final String filePath = this.getFixturePath("not-widget.xml"); myFixture.copyFileToProject(filePath); assertFileNotContainsCompletions( - filePath, + filePath, LOOKUP_STRINGS_CHECK ); } From 8f685a98947bbeddba337e9fe8f7da47764b508e Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 11 Feb 2022 20:11:29 +0200 Subject: [PATCH 5/5] 977: Code refactoring --- .../actions/context/xml/NewDiXmlAction.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/context/xml/NewDiXmlAction.java b/src/com/magento/idea/magento2plugin/actions/context/xml/NewDiXmlAction.java index 80c9a6284..9118ef874 100644 --- a/src/com/magento/idea/magento2plugin/actions/context/xml/NewDiXmlAction.java +++ b/src/com/magento/idea/magento2plugin/actions/context/xml/NewDiXmlAction.java @@ -36,6 +36,12 @@ protected boolean isVisible( final @NotNull PsiDirectory targetDirectory, final PsiFile targetFile ) { + final PsiDirectory configDir = moduleData.getConfigDir(); + final PsiDirectory globalScopeDir = getGlobalScopeDir(targetDirectory); + + if (configDir == null || globalScopeDir == null) { + return false; + } final List allowedDirectories = Arrays.asList( Package.moduleBaseAreaDir, Areas.adminhtml.toString(), @@ -45,12 +51,6 @@ protected boolean isVisible( Areas.graphql.toString(), Areas.crontab.toString() ); - final PsiDirectory configDir = moduleData.getConfigDir(); - final PsiDirectory globalScopeDir = getGlobalScopeDir(targetDirectory); - - if (configDir == null || globalScopeDir == null) { - return false; - } return allowedDirectories.contains(targetDirectory.getName()) && globalScopeDir.equals(configDir)