Search in sources :

Example 76 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class InspectionRVContentProviderImpl method checkReportedProblems.

@Override
public boolean checkReportedProblems(@NotNull GlobalInspectionContextImpl context, @NotNull final InspectionToolWrapper toolWrapper) {
    InspectionToolPresentation presentation = context.getPresentation(toolWrapper);
    presentation.updateContent();
    final SearchScope searchScope = context.getCurrentScope().toSearchScope();
    if (searchScope instanceof LocalSearchScope) {
        final Map<String, Set<RefEntity>> contents = presentation.getContent();
        final Map<RefEntity, CommonProblemDescriptor[]> problemElements = presentation.getProblemElements();
        for (Set<RefEntity> entities : contents.values()) {
            for (Iterator<RefEntity> iterator = entities.iterator(); iterator.hasNext(); ) {
                RefEntity entity = iterator.next();
                if (entity instanceof RefElement) {
                    final PsiElement element = ((RefElement) entity).getElement();
                    if (element != null) {
                        final TextRange range = element.getTextRange();
                        if (range != null && ((LocalSearchScope) searchScope).containsRange(element.getContainingFile(), range)) {
                            continue;
                        }
                    }
                }
                problemElements.remove(entity);
                iterator.remove();
            }
        }
    }
    return presentation.hasReportedProblems();
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) HashSet(com.intellij.util.containers.HashSet) Set(java.util.Set) TextRange(com.intellij.openapi.util.TextRange) RefElement(com.intellij.codeInspection.reference.RefElement) RefEntity(com.intellij.codeInspection.reference.RefEntity) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiElement(com.intellij.psi.PsiElement)

Example 77 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class DfaPsiUtil method getVariableAssignmentsInFile.

@NotNull
public static Collection<PsiExpression> getVariableAssignmentsInFile(@NotNull PsiVariable psiVariable, final boolean literalsOnly, final PsiElement place) {
    Ref<Boolean> modificationRef = Ref.create(Boolean.FALSE);
    PsiElement codeBlock = place == null ? null : getTopmostBlockInSameClass(place);
    int placeOffset = codeBlock != null ? place.getTextRange().getStartOffset() : 0;
    PsiFile containingFile = psiVariable.getContainingFile();
    LocalSearchScope scope = new LocalSearchScope(new PsiElement[] { containingFile }, null, true);
    Collection<PsiReference> references = ReferencesSearch.search(psiVariable, scope).findAll();
    List<PsiExpression> list = ContainerUtil.mapNotNull(references, (NullableFunction<PsiReference, PsiExpression>) psiReference -> {
        if (modificationRef.get())
            return null;
        final PsiElement parent = psiReference.getElement().getParent();
        if (parent instanceof PsiAssignmentExpression) {
            final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) parent;
            final IElementType operation = assignmentExpression.getOperationTokenType();
            if (assignmentExpression.getLExpression() == psiReference) {
                if (JavaTokenType.EQ.equals(operation)) {
                    final PsiExpression rValue = assignmentExpression.getRExpression();
                    if (!literalsOnly || allOperandsAreLiterals(rValue)) {
                        if (codeBlock != null && PsiTreeUtil.isAncestor(codeBlock, parent, true) && placeOffset < parent.getTextRange().getStartOffset()) {
                            return null;
                        }
                        return rValue;
                    } else {
                        modificationRef.set(Boolean.TRUE);
                    }
                } else if (JavaTokenType.PLUSEQ.equals(operation)) {
                    modificationRef.set(Boolean.TRUE);
                }
            }
        }
        return null;
    });
    if (modificationRef.get())
        return Collections.emptyList();
    PsiExpression initializer = psiVariable.getInitializer();
    if (initializer != null && (!literalsOnly || allOperandsAreLiterals(initializer))) {
        list = ContainerUtil.concat(list, Collections.singletonList(initializer));
    }
    return list;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) JBIterable(com.intellij.util.containers.JBIterable) IElementType(com.intellij.psi.tree.IElementType) com.intellij.psi.util(com.intellij.psi.util) NullableFunction(com.intellij.util.NullableFunction) ContainerUtil(com.intellij.util.containers.ContainerUtil) NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager) Instruction(com.intellij.codeInspection.dataFlow.instructions.Instruction) ReturnInstruction(com.intellij.codeInspection.dataFlow.instructions.ReturnInstruction) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Nullable(org.jetbrains.annotations.Nullable) Stack(com.intellij.util.containers.Stack) AnnotationUtil(com.intellij.codeInsight.AnnotationUtil) com.intellij.psi(com.intellij.psi) PsiJavaPatterns(com.intellij.patterns.PsiJavaPatterns) NotNull(org.jetbrains.annotations.NotNull) JavaLanguage(com.intellij.lang.java.JavaLanguage) Ref(com.intellij.openapi.util.Ref) MultiMap(com.intellij.util.containers.MultiMap) MethodCallInstruction(com.intellij.codeInspection.dataFlow.instructions.MethodCallInstruction) IElementType(com.intellij.psi.tree.IElementType) NotNull(org.jetbrains.annotations.NotNull)

Example 78 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class MagicConstantInspection method processValuesFlownTo.

private static boolean processValuesFlownTo(@NotNull final PsiExpression argument, @NotNull PsiElement scope, @NotNull PsiManager manager, @NotNull final Processor<PsiExpression> processor) {
    SliceAnalysisParams params = new SliceAnalysisParams();
    params.dataFlowToThis = true;
    params.scope = new AnalysisScope(new LocalSearchScope(scope), manager.getProject());
    SliceRootNode rootNode = new SliceRootNode(manager.getProject(), new DuplicateMap(), LanguageSlicing.getProvider(argument).createRootUsage(argument, params));
    Collection<? extends AbstractTreeNode> children = rootNode.getChildren().iterator().next().getChildren();
    for (AbstractTreeNode child : children) {
        SliceUsage usage = (SliceUsage) child.getValue();
        PsiElement element = usage.getElement();
        if (element instanceof PsiExpression && !processor.process((PsiExpression) element))
            return false;
    }
    return !children.isEmpty();
}
Also used : AnalysisScope(com.intellij.analysis.AnalysisScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Example 79 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class AllClassesSearchExecutor method execute.

@Override
public boolean execute(@NotNull final AllClassesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiClass> consumer) {
    SearchScope scope = queryParameters.getScope();
    if (scope instanceof GlobalSearchScope) {
        return processAllClassesInGlobalScope((GlobalSearchScope) scope, queryParameters, consumer);
    }
    PsiElement[] scopeRoots = ((LocalSearchScope) scope).getScope();
    for (final PsiElement scopeRoot : scopeRoots) {
        if (!processScopeRootForAllClasses(scopeRoot, consumer))
            return false;
    }
    return true;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 80 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class ConvertSwitchToIfIntention method dumpBody.

private static void dumpBody(List<PsiElement> bodyStatements, Set<PsiLocalVariable> variables, @NonNls StringBuilder ifStatementString) {
    ifStatementString.append('{');
    for (PsiLocalVariable variable : variables) {
        if (ReferencesSearch.search(variable, new LocalSearchScope(bodyStatements.toArray(new PsiElement[bodyStatements.size()]))).findFirst() != null) {
            final PsiType varType = variable.getType();
            ifStatementString.append(varType.getCanonicalText());
            ifStatementString.append(' ');
            ifStatementString.append(variable.getName());
            ifStatementString.append(';');
        }
    }
    for (PsiElement bodyStatement : bodyStatements) {
        if (bodyStatement instanceof PsiBlockStatement) {
            final PsiBlockStatement blockStatement = (PsiBlockStatement) bodyStatement;
            final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
            final PsiStatement[] statements = codeBlock.getStatements();
            for (PsiStatement statement : statements) {
                appendElement(statement, ifStatementString);
            }
        } else {
            appendElement(bodyStatement, ifStatementString);
        }
    }
    ifStatementString.append("\n}");
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Aggregations

LocalSearchScope (com.intellij.psi.search.LocalSearchScope)113 SearchScope (com.intellij.psi.search.SearchScope)31 NotNull (org.jetbrains.annotations.NotNull)22 PsiElement (com.intellij.psi.PsiElement)19 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 Project (com.intellij.openapi.project.Project)18 Nullable (org.jetbrains.annotations.Nullable)13 ArrayList (java.util.ArrayList)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 UsageInfo (com.intellij.usageView.UsageInfo)11 TextRange (com.intellij.openapi.util.TextRange)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 com.intellij.psi (com.intellij.psi)8 PsiReference (com.intellij.psi.PsiReference)8 PsiFile (com.intellij.psi.PsiFile)7 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)7 AnalysisScope (com.intellij.analysis.AnalysisScope)6 Processor (com.intellij.util.Processor)6 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)5 Ref (com.intellij.openapi.util.Ref)4