Skip to content

Commit bfb3a63

Browse files
authored
Merge pull request #935 from bohdan-harniuk/620-exclude-test-files-from-search
620: Introduced custom search scope to exclude test files from search
2 parents 48f5dce + b72fb51 commit bfb3a63

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,9 @@
561561
<errorHandler implementation="com.magento.idea.magento2plugin.project.diagnostic.DefaultErrorReportSubmitter"/>
562562

563563
<configurationType implementation="com.magento.idea.magento2uct.execution.configurations.UctRunConfigurationType"/>
564+
565+
<testSourcesFilter implementation="com.magento.idea.magento2plugin.lang.roots.MagentoTestSourceFilter"/>
566+
<searchScopesProvider implementation="com.magento.idea.magento2plugin.lang.psi.search.MagentoSearchScopesProvider"/>
564567
</extensions>
565568

566569
<extensions defaultExtensionNs="com.jetbrains.php">
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.lang.psi.search;
7+
8+
import com.intellij.openapi.module.Module;
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.openapi.vfs.VirtualFile;
11+
import com.intellij.psi.search.GlobalSearchScope;
12+
import com.magento.idea.magento2plugin.lang.roots.MagentoTestSourceFilter;
13+
import org.jetbrains.annotations.NotNull;
14+
import org.jetbrains.annotations.Nullable;
15+
16+
public final class AllFilesExceptTestsScope extends GlobalSearchScope {
17+
18+
public static final String SCOPE_NAME = "All Files Except Tests";
19+
private static AllFilesExceptTestsScope instance;
20+
private final Project project;
21+
22+
/**
23+
* Get search scope instance.
24+
*
25+
* @param project Project
26+
*
27+
* @return AllFilesExceptTestsScope
28+
*/
29+
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
30+
public static synchronized AllFilesExceptTestsScope getInstance(
31+
final @Nullable Project project
32+
) {
33+
if (instance == null) {
34+
instance = new AllFilesExceptTestsScope(project);
35+
}
36+
37+
return instance;
38+
}
39+
40+
/**
41+
* Magento search scope constructor.
42+
*
43+
* @param project Project
44+
*/
45+
private AllFilesExceptTestsScope(final @Nullable Project project) {
46+
super(project);
47+
this.project = project;
48+
}
49+
50+
@Override
51+
public @NotNull String getDisplayName() {
52+
return SCOPE_NAME;
53+
}
54+
55+
@Override
56+
public boolean contains(final @NotNull VirtualFile file) {
57+
assert project != null;
58+
return GlobalSearchScope.allScope(project).contains(file)
59+
&& !(new MagentoTestSourceFilter().isTestSource(file, project));
60+
}
61+
62+
@Override
63+
public boolean isSearchInModuleContent(final @NotNull Module module) {
64+
return true;
65+
}
66+
67+
@Override
68+
public boolean isSearchInLibraries() {
69+
return true;
70+
}
71+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.lang.psi.search;
7+
8+
import com.intellij.openapi.actionSystem.DataContext;
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.openapi.util.NlsContexts;
11+
import com.intellij.psi.search.SearchScope;
12+
import com.intellij.psi.search.SearchScopeProvider;
13+
import java.util.Collections;
14+
import java.util.List;
15+
import org.jetbrains.annotations.NotNull;
16+
import org.jetbrains.annotations.Nullable;
17+
18+
public class MagentoSearchScopesProvider implements SearchScopeProvider {
19+
20+
@Override
21+
public @Nullable @NlsContexts.Separator String getDisplayName() {
22+
return "Magento";
23+
}
24+
25+
@Override
26+
public @NotNull List<SearchScope> getSearchScopes(
27+
final @NotNull Project project,
28+
final @NotNull DataContext dataContext
29+
) {
30+
return Collections.singletonList(AllFilesExceptTestsScope.getInstance(project));
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.lang.roots;
7+
8+
import com.intellij.openapi.project.Project;
9+
import com.intellij.openapi.roots.TestSourcesFilter;
10+
import com.intellij.openapi.vfs.VirtualFile;
11+
import com.jetbrains.php.lang.PhpFileType;
12+
import org.jetbrains.annotations.NotNull;
13+
14+
public class MagentoTestSourceFilter extends TestSourcesFilter {
15+
16+
private static final String TEST_CLASS_SUFFIX = "Test";
17+
18+
@Override
19+
public boolean isTestSource(final @NotNull VirtualFile file, final @NotNull Project project) {
20+
final String fileName = file.getNameWithoutExtension();
21+
final int suffixIndex = fileName.length() - TEST_CLASS_SUFFIX.length();
22+
23+
if (suffixIndex < 0) {
24+
return false;
25+
}
26+
27+
return file.getFileType().equals(PhpFileType.INSTANCE)
28+
&& "Test".equals(fileName.substring(suffixIndex));
29+
}
30+
}

0 commit comments

Comments
 (0)