diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 99ee8d8ba..3e06b23fe 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -561,6 +561,9 @@ + + + diff --git a/src/com/magento/idea/magento2plugin/lang/psi/search/AllFilesExceptTestsScope.java b/src/com/magento/idea/magento2plugin/lang/psi/search/AllFilesExceptTestsScope.java new file mode 100644 index 000000000..99e523820 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/lang/psi/search/AllFilesExceptTestsScope.java @@ -0,0 +1,71 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.lang.psi.search; + +import com.intellij.openapi.module.Module; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.search.GlobalSearchScope; +import com.magento.idea.magento2plugin.lang.roots.MagentoTestSourceFilter; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class AllFilesExceptTestsScope extends GlobalSearchScope { + + public static final String SCOPE_NAME = "All Files Except Tests"; + private static AllFilesExceptTestsScope instance; + private final Project project; + + /** + * Get search scope instance. + * + * @param project Project + * + * @return AllFilesExceptTestsScope + */ + @SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel") + public static synchronized AllFilesExceptTestsScope getInstance( + final @Nullable Project project + ) { + if (instance == null) { + instance = new AllFilesExceptTestsScope(project); + } + + return instance; + } + + /** + * Magento search scope constructor. + * + * @param project Project + */ + private AllFilesExceptTestsScope(final @Nullable Project project) { + super(project); + this.project = project; + } + + @Override + public @NotNull String getDisplayName() { + return SCOPE_NAME; + } + + @Override + public boolean contains(final @NotNull VirtualFile file) { + assert project != null; + return GlobalSearchScope.allScope(project).contains(file) + && !(new MagentoTestSourceFilter().isTestSource(file, project)); + } + + @Override + public boolean isSearchInModuleContent(final @NotNull Module module) { + return true; + } + + @Override + public boolean isSearchInLibraries() { + return true; + } +} diff --git a/src/com/magento/idea/magento2plugin/lang/psi/search/MagentoSearchScopesProvider.java b/src/com/magento/idea/magento2plugin/lang/psi/search/MagentoSearchScopesProvider.java new file mode 100644 index 000000000..e1a89c659 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/lang/psi/search/MagentoSearchScopesProvider.java @@ -0,0 +1,32 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.lang.psi.search; + +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.NlsContexts; +import com.intellij.psi.search.SearchScope; +import com.intellij.psi.search.SearchScopeProvider; +import java.util.Collections; +import java.util.List; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class MagentoSearchScopesProvider implements SearchScopeProvider { + + @Override + public @Nullable @NlsContexts.Separator String getDisplayName() { + return "Magento"; + } + + @Override + public @NotNull List getSearchScopes( + final @NotNull Project project, + final @NotNull DataContext dataContext + ) { + return Collections.singletonList(AllFilesExceptTestsScope.getInstance(project)); + } +} diff --git a/src/com/magento/idea/magento2plugin/lang/roots/MagentoTestSourceFilter.java b/src/com/magento/idea/magento2plugin/lang/roots/MagentoTestSourceFilter.java new file mode 100644 index 000000000..da99c0040 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/lang/roots/MagentoTestSourceFilter.java @@ -0,0 +1,30 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.lang.roots; + +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.TestSourcesFilter; +import com.intellij.openapi.vfs.VirtualFile; +import com.jetbrains.php.lang.PhpFileType; +import org.jetbrains.annotations.NotNull; + +public class MagentoTestSourceFilter extends TestSourcesFilter { + + private static final String TEST_CLASS_SUFFIX = "Test"; + + @Override + public boolean isTestSource(final @NotNull VirtualFile file, final @NotNull Project project) { + final String fileName = file.getNameWithoutExtension(); + final int suffixIndex = fileName.length() - TEST_CLASS_SUFFIX.length(); + + if (suffixIndex < 0) { + return false; + } + + return file.getFileType().equals(PhpFileType.INSTANCE) + && "Test".equals(fileName.substring(suffixIndex)); + } +}