Skip to content

Commit

Permalink
Fixed (hopefully) search scope when searching for bean declarations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Bergholm committed Aug 29, 2016
1 parent 79559fc commit e71b9c1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

import com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.roots.ModuleFileIndex;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.AtomicNotNullLazyValue;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

import static com.avanza.astrix.intellij.AstrixContextUtility.isAstrixBeanRetriever;
import static com.intellij.codeInspection.ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
import static java.util.Collections.emptyList;

public class AstrixContextGetterInspector extends BaseJavaBatchLocalInspectionTool {

Expand All @@ -30,7 +38,12 @@ public AstrixContextGetterVisitor(ProblemsHolder problemsHolder) {
@Override
protected Collection<PsiMethod> compute() {
PsiFile file = problemsHolder.getFile();
return AstrixContextUtility.getBeanDeclarationCandidates(file.getResolveScope(), file.getProject());
GlobalSearchScope globalSearchScope = getSearchScope(file);
if(globalSearchScope == null) {
return emptyList();
} else {
return AstrixContextUtility.getBeanDeclarationCandidates(globalSearchScope, file.getProject());
}
}
};
}
Expand All @@ -50,4 +63,17 @@ private boolean hasBeanDeclaration(PsiExpressionList psiExpressionList) {
return candidates.getValue().stream().anyMatch(AstrixContextUtility.isBeanDeclaration(psiExpressionList));
}
}

@Nullable
private GlobalSearchScope getSearchScope(PsiFile file) {
VirtualFile virtualFile;
Module module;
if ((virtualFile = file.getVirtualFile()) == null || (module = ModuleUtil.findModuleForFile(virtualFile, file.getProject())) == null) {
return null;
}

ModuleFileIndex fileIndex = ModuleRootManager.getInstance(module).getFileIndex();
boolean includeTests = fileIndex.isInTestSourceContent(virtualFile);
return module.getModuleRuntimeScope(includeTests);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
import com.intellij.concurrency.JobLauncher;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressIndicatorProvider;
import com.intellij.openapi.roots.ModuleFileIndex;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -60,8 +65,8 @@ private Optional<LineMarkerInfo> createLineMarkerInfo(PsiElement element, Concur
if (element instanceof PsiReferenceExpression && (parent = element.getParent()) instanceof PsiMethodCallExpression) {
PsiMethodCallExpression psiMethodCallExpression = (PsiMethodCallExpression) parent;

if (isAstrixBeanRetriever(psiMethodCallExpression.resolveMethod())) {
GlobalSearchScope globalSearchScope = element.getResolveScope();
GlobalSearchScope globalSearchScope;
if (isAstrixBeanRetriever(psiMethodCallExpression.resolveMethod()) && (globalSearchScope = getSearchScope(element)) != null) {
Collection<PsiMethod> candidates = candidatesByModule.computeIfAbsent(globalSearchScope,
searchScope -> getBeanDeclarationCandidates(searchScope, element.getProject()));
Predicate<PsiMethod> isBeanDeclaration = isBeanDeclaration(psiMethodCallExpression.getArgumentList());
Expand All @@ -83,6 +88,19 @@ protected Collection<? extends PsiElement> compute() {
return Optional.empty();
}

@Nullable
private GlobalSearchScope getSearchScope(PsiElement element) {
Module module;
VirtualFile virtualFile;
if ((module = ModuleUtil.findModuleForPsiElement(element)) == null || (virtualFile = element.getContainingFile().getVirtualFile()) == null) {
return null;
}

ModuleFileIndex fileIndex = ModuleRootManager.getInstance(module).getFileIndex();
boolean includeTests = fileIndex.isInTestSourceContent(virtualFile);
return module.getModuleRuntimeScope(includeTests);
}

private String getTooltipText(PsiMethod method) {
StringBuilder sb = new StringBuilder("<html><body>");
if(isService(method)) {
Expand Down

0 comments on commit e71b9c1

Please sign in to comment.