Skip to content

620: Introduced custom search scope to exclude test files from search #935

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,9 @@
<errorHandler implementation="com.magento.idea.magento2plugin.project.diagnostic.DefaultErrorReportSubmitter"/>

<configurationType implementation="com.magento.idea.magento2uct.execution.configurations.UctRunConfigurationType"/>

<testSourcesFilter implementation="com.magento.idea.magento2plugin.lang.roots.MagentoTestSourceFilter"/>
<searchScopesProvider implementation="com.magento.idea.magento2plugin.lang.psi.search.MagentoSearchScopesProvider"/>
</extensions>

<extensions defaultExtensionNs="com.jetbrains.php">
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<SearchScope> getSearchScopes(
final @NotNull Project project,
final @NotNull DataContext dataContext
) {
return Collections.singletonList(AllFilesExceptTestsScope.getInstance(project));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}