use of com.intellij.codeInspection.ex.GlobalInspectionContextBase in project intellij-community by JetBrains.
the class DuplicatePropertyInspection method checkFile.
private void checkFile(final PsiFile file, final InspectionManager manager, GlobalInspectionContextBase context, final RefManager refManager, final ProblemDescriptionsProcessor processor) {
if (!(file instanceof PropertiesFile))
return;
if (!context.isToCheckFile(file, this) || SuppressionUtil.inspectionResultSuppressed(file, this))
return;
final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(file.getProject());
final PropertiesFile propertiesFile = (PropertiesFile) file;
final List<IProperty> properties = propertiesFile.getProperties();
Module module = ModuleUtilCore.findModuleForPsiElement(file);
if (module == null)
return;
final GlobalSearchScope scope = CURRENT_FILE ? GlobalSearchScope.fileScope(file) : MODULE_WITH_DEPENDENCIES ? GlobalSearchScope.moduleWithDependenciesScope(module) : GlobalSearchScope.projectScope(file.getProject());
final Map<String, Set<PsiFile>> processedValueToFiles = Collections.synchronizedMap(new HashMap<String, Set<PsiFile>>());
final Map<String, Set<PsiFile>> processedKeyToFiles = Collections.synchronizedMap(new HashMap<String, Set<PsiFile>>());
final ProgressIndicator original = ProgressManager.getInstance().getProgressIndicator();
final ProgressIndicator progress = ProgressWrapper.wrap(original);
ProgressManager.getInstance().runProcess(() -> {
if (!JobLauncher.getInstance().invokeConcurrentlyUnderProgress(properties, progress, false, property -> {
if (original != null) {
if (original.isCanceled())
return false;
original.setText2(PropertiesBundle.message("searching.for.property.key.progress.text", property.getUnescapedKey()));
}
processTextUsages(processedValueToFiles, property.getValue(), processedKeyToFiles, searchHelper, scope);
processTextUsages(processedKeyToFiles, property.getUnescapedKey(), processedValueToFiles, searchHelper, scope);
return true;
}))
throw new ProcessCanceledException();
List<ProblemDescriptor> problemDescriptors = new ArrayList<>();
Map<String, Set<String>> keyToDifferentValues = new HashMap<>();
if (CHECK_DUPLICATE_KEYS || CHECK_DUPLICATE_KEYS_WITH_DIFFERENT_VALUES) {
prepareDuplicateKeysByFile(processedKeyToFiles, manager, keyToDifferentValues, problemDescriptors, file, original);
}
if (CHECK_DUPLICATE_VALUES)
prepareDuplicateValuesByFile(processedValueToFiles, manager, problemDescriptors, file, original);
if (CHECK_DUPLICATE_KEYS_WITH_DIFFERENT_VALUES) {
processDuplicateKeysWithDifferentValues(keyToDifferentValues, processedKeyToFiles, problemDescriptors, manager, file, original);
}
if (!problemDescriptors.isEmpty()) {
processor.addProblemElement(refManager.getReference(file), problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]));
}
}, progress);
}
use of com.intellij.codeInspection.ex.GlobalInspectionContextBase in project intellij-community by JetBrains.
the class CleanupIntention method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
if (!FileModificationService.getInstance().preparePsiElementForWrite(file))
return;
final InspectionManager managerEx = InspectionManager.getInstance(project);
final GlobalInspectionContextBase globalContext = (GlobalInspectionContextBase) managerEx.createNewGlobalContext(false);
final AnalysisScope scope = getScope(project, file);
if (scope != null) {
globalContext.codeCleanup(scope, InspectionProjectProfileManager.getInstance(project).getCurrentProfile(), getText(), null, false);
}
}
use of com.intellij.codeInspection.ex.GlobalInspectionContextBase in project intellij-community by JetBrains.
the class CodeCleanupAction method runInspections.
@Override
protected void runInspections(Project project, AnalysisScope scope) {
final InspectionProfile profile = myExternalProfile != null ? myExternalProfile : InspectionProjectProfileManager.getInstance(project).getCurrentProfile();
final InspectionManager managerEx = InspectionManager.getInstance(project);
final GlobalInspectionContextBase globalContext = (GlobalInspectionContextBase) managerEx.createNewGlobalContext(false);
globalContext.codeCleanup(scope, profile, getTemplatePresentation().getText(), null, false);
}
use of com.intellij.codeInspection.ex.GlobalInspectionContextBase in project intellij-community by JetBrains.
the class UnusedDeclarationInspectionBase method runInspection.
@Override
public void runInspection(@NotNull final AnalysisScope scope, @NotNull InspectionManager manager, @NotNull final GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) {
globalContext.getRefManager().iterate(new RefJavaVisitor() {
@Override
public void visitElement(@NotNull final RefEntity refEntity) {
if (refEntity instanceof RefElementImpl) {
final RefElementImpl refElement = (RefElementImpl) refEntity;
if (!refElement.isSuspicious())
return;
PsiFile file = refElement.getContainingFile();
if (file == null)
return;
final boolean isSuppressed = refElement.isSuppressed(getShortName(), ALTERNATIVE_ID);
if (isSuppressed || !((GlobalInspectionContextBase) globalContext).isToCheckFile(file, UnusedDeclarationInspectionBase.this)) {
if (isSuppressed || !scope.contains(file)) {
getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
}
}
}
}
});
if (isAddNonJavaUsedEnabled()) {
checkForReachableRefs(globalContext);
final StrictUnreferencedFilter strictUnreferencedFilter = new StrictUnreferencedFilter(this, globalContext);
ProgressManager.getInstance().runProcess(new Runnable() {
@Override
public void run() {
final RefManager refManager = globalContext.getRefManager();
final PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(refManager.getProject());
refManager.iterate(new RefJavaVisitor() {
@Override
public void visitElement(@NotNull final RefEntity refEntity) {
if (refEntity instanceof RefClass && strictUnreferencedFilter.accepts((RefClass) refEntity)) {
findExternalClassReferences((RefClass) refEntity);
} else if (refEntity instanceof RefMethod) {
RefMethod refMethod = (RefMethod) refEntity;
if (refMethod.isConstructor() && strictUnreferencedFilter.accepts(refMethod)) {
findExternalClassReferences(refMethod.getOwnerClass());
}
}
}
private void findExternalClassReferences(final RefClass refElement) {
final PsiClass psiClass = refElement.getElement();
String qualifiedName = psiClass != null ? psiClass.getQualifiedName() : null;
if (qualifiedName != null) {
final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(globalContext.getProject());
final PsiNonJavaFileReferenceProcessor processor = (file, startOffset, endOffset) -> {
getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
return false;
};
final DelegatingGlobalSearchScope globalSearchScope = new DelegatingGlobalSearchScope(projectScope) {
@Override
public boolean contains(@NotNull VirtualFile file) {
return file.getFileType() != JavaFileType.INSTANCE && super.contains(file);
}
};
if (helper.processUsagesInNonJavaFiles(qualifiedName, processor, globalSearchScope)) {
final PsiReference reference = ReferencesSearch.search(psiClass, globalSearchScope).findFirst();
if (reference != null) {
getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
for (PsiMethod method : psiClass.getMethods()) {
final RefElement refMethod = refManager.getReference(method);
if (refMethod != null) {
getEntryPointsManager(globalContext).addEntryPoint(refMethod, false);
}
}
}
}
}
}
});
}
}, null);
}
myProcessedSuspicious = new HashSet<>();
myPhase = 1;
}
Aggregations