Search in sources :

Example 31 with LocalSearchScope

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

the class DartControlFlow method analyze.

public static DartControlFlow analyze(PsiElement[] elements) {
    final PsiElement scope = PsiTreeUtil.getTopmostParentOfType(elements[0], DartExecutionScope.class);
    final PsiElement lastElement = elements[elements.length - 1];
    final int lastElementEndOffset = lastElement.getTextRange().getEndOffset();
    final int firstElementStartOffset = elements[0].getTextRange().getStartOffset();
    // find out params
    assert scope != null;
    final LocalSearchScope localSearchScope = new LocalSearchScope(scope);
    final List<DartComponentName> outDeclarations = ContainerUtil.filter(DartControlFlowUtil.getSimpleDeclarations(elements, null, false), componentName -> {
        for (PsiReference usage : ReferencesSearch.search(componentName, localSearchScope, false).findAll()) {
            if (usage.getElement().getTextRange().getStartOffset() > lastElementEndOffset) {
                return true;
            }
        }
        return false;
    });
    // find params
    final DartReferenceVisitor dartReferenceVisitor = new DartReferenceVisitor();
    for (PsiElement element : elements) {
        element.accept(dartReferenceVisitor);
    }
    final List<DartComponentName> inComponentNames = ContainerUtil.filter(dartReferenceVisitor.getComponentNames(), componentName -> {
        final int offset = componentName.getTextRange().getStartOffset();
        final boolean declarationInElements = firstElementStartOffset <= offset && offset < lastElementEndOffset;
        return !declarationInElements;
    });
    return new DartControlFlow(inComponentNames, outDeclarations);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

Example 32 with LocalSearchScope

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

the class DartServerFindUsagesTest method testBoolUsagesWithScope.

public void testBoolUsagesWithScope() throws Exception {
    final PsiFile psiFile1 = myFixture.configureByText("file.dart", "/// [bool]\n" + "<caret>bool foo() {\n" + "  var bool = #bool;\n" + "}");
    final PsiFile psiFile2 = myFixture.addFileToProject("file1.dart", "bool x;");
    // warm up
    myFixture.doHighlighting();
    DartTestUtils.letAnalyzerSmellCoreFile(myFixture, "iterable.dart");
    myFixture.openFileInEditor(psiFile1.getVirtualFile());
    final String[] allProjectUsages = { "PsiCommentImpl in " + psiFile1.getName() + "@5:9 (non-code usage)", "DartReferenceExpressionImpl in " + psiFile1.getName() + "@11:15", "DartReferenceExpressionImpl in file1.dart@0:4" };
    checkUsages(new LocalSearchScope(psiFile1), "PsiCommentImpl in " + psiFile1.getName() + "@5:9 (non-code usage)", "DartReferenceExpressionImpl in " + psiFile1.getName() + "@11:15");
    checkUsages(new LocalSearchScope(psiFile2), "DartReferenceExpressionImpl in file1.dart@0:4");
    checkUsages(new LocalSearchScope(new PsiFile[] { psiFile1, psiFile2 }), allProjectUsages);
    checkUsages(GlobalSearchScope.fileScope(getProject(), psiFile1.getVirtualFile()), "PsiCommentImpl in " + psiFile1.getName() + "@5:9 (non-code usage)", "DartReferenceExpressionImpl in " + psiFile1.getName() + "@11:15");
    checkUsages(GlobalSearchScope.fileScope(getProject(), psiFile2.getVirtualFile()), "DartReferenceExpressionImpl in file1.dart@0:4");
    checkUsages(GlobalSearchScope.filesScope(getProject(), Arrays.asList(psiFile1.getVirtualFile(), psiFile2.getVirtualFile())), allProjectUsages);
    checkUsages(GlobalSearchScope.projectScope(getProject()), allProjectUsages);
    final Collection<UsageInfo> usages = findUsages(GlobalSearchScope.allScope(getProject()));
    assertTrue(String.valueOf(usages.size()), usages.size() > 15);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiFile(com.intellij.psi.PsiFile) UsageInfo(com.intellij.usageView.UsageInfo)

Example 33 with LocalSearchScope

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

the class AnnotatedMembersSearcher method execute.

@Override
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer) {
    final PsiClass annClass = p.getAnnotationClass();
    assert annClass.isAnnotationType() : "Annotation type should be passed to annotated members search";
    final String annotationFQN = ApplicationManager.getApplication().runReadAction(new Computable<String>() {

        @Override
        public String compute() {
            return annClass.getQualifiedName();
        }
    });
    assert annotationFQN != null;
    final SearchScope scope = p.getScope();
    final List<PsiModifierListOwner> candidates;
    if (scope instanceof GlobalSearchScope) {
        candidates = getAnnotatedMemberCandidates(annClass, ((GlobalSearchScope) scope));
    } else {
        candidates = new ArrayList<>();
        for (final PsiElement element : ((LocalSearchScope) scope).getScope()) {
            ApplicationManager.getApplication().runReadAction(() -> {
                if (element instanceof GroovyPsiElement) {
                    ((GroovyPsiElement) element).accept(new GroovyRecursiveElementVisitor() {

                        @Override
                        public void visitMethod(@NotNull GrMethod method) {
                            candidates.add(method);
                        }

                        @Override
                        public void visitField(@NotNull GrField field) {
                            candidates.add(field);
                        }
                    });
                }
            });
        }
    }
    for (final PsiModifierListOwner candidate : candidates) {
        boolean accepted = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

            @Override
            public Boolean compute() {
                if (AnnotatedElementsSearcher.isInstanceof(candidate, p.getTypes())) {
                    PsiModifierList list = candidate.getModifierList();
                    if (list != null) {
                        for (PsiAnnotation annotation : list.getAnnotations()) {
                            if ((p.isApproximate() || annotationFQN.equals(annotation.getQualifiedName())) && !consumer.process(candidate)) {
                                return false;
                            }
                        }
                    }
                }
                return true;
            }
        });
        if (!accepted)
            return false;
    }
    return true;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 34 with LocalSearchScope

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

the class IdentifierHighlighterPass method getUsages.

@NotNull
private static Couple<Collection<TextRange>> getUsages(@NotNull PsiElement target, PsiElement psiElement, boolean withDeclarations, boolean detectAccess) {
    List<TextRange> readRanges = new ArrayList<>();
    List<TextRange> writeRanges = new ArrayList<>();
    final ReadWriteAccessDetector detector = detectAccess ? ReadWriteAccessDetector.findDetector(target) : null;
    final FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(target.getProject())).getFindUsagesManager();
    final FindUsagesHandler findUsagesHandler = findUsagesManager.getFindUsagesHandler(target, true);
    final LocalSearchScope scope = new LocalSearchScope(psiElement);
    Collection<PsiReference> refs = findUsagesHandler != null ? findUsagesHandler.findReferencesToHighlight(target, scope) : ReferencesSearch.search(target, scope).findAll();
    for (PsiReference psiReference : refs) {
        if (psiReference == null) {
            LOG.error("Null reference returned, findUsagesHandler=" + findUsagesHandler + "; target=" + target + " of " + target.getClass());
            continue;
        }
        List<TextRange> destination;
        if (detector == null || detector.getReferenceAccess(target, psiReference) == ReadWriteAccessDetector.Access.Read) {
            destination = readRanges;
        } else {
            destination = writeRanges;
        }
        HighlightUsagesHandler.collectRangesToHighlight(psiReference, destination);
    }
    if (withDeclarations) {
        final TextRange declRange = HighlightUsagesHandler.getNameIdentifierRange(psiElement.getContainingFile(), target);
        if (declRange != null) {
            if (detector != null && detector.isDeclarationWriteAccess(target)) {
                writeRanges.add(declRange);
            } else {
                readRanges.add(declRange);
            }
        }
    }
    return Couple.<Collection<TextRange>>of(readRanges, writeRanges);
}
Also used : FindManagerImpl(com.intellij.find.impl.FindManagerImpl) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) FindUsagesHandler(com.intellij.find.findUsages.FindUsagesHandler) ReadWriteAccessDetector(com.intellij.codeInsight.highlighting.ReadWriteAccessDetector) TextRange(com.intellij.openapi.util.TextRange) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager) NotNull(org.jetbrains.annotations.NotNull)

Example 35 with LocalSearchScope

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

the class MemberInplaceRenamer method appendAdditionalElement.

@Override
protected boolean appendAdditionalElement(Collection<PsiReference> refs, Collection<Pair<PsiElement, TextRange>> stringUsages) {
    boolean showChooser = super.appendAdditionalElement(refs, stringUsages);
    PsiNamedElement variable = getVariable();
    if (variable != null) {
        final PsiElement substituted = getSubstituted();
        if (substituted != null) {
            appendAdditionalElement(stringUsages, variable, substituted);
            RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(substituted);
            final HashMap<PsiElement, String> allRenames = new HashMap<>();
            PsiFile currentFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument());
            processor.prepareRenaming(substituted, "", allRenames, new LocalSearchScope(currentFile));
            for (PsiElement element : allRenames.keySet()) {
                appendAdditionalElement(stringUsages, variable, element);
            }
        }
    }
    return showChooser;
}
Also used : RenamePsiElementProcessor(com.intellij.refactoring.rename.RenamePsiElementProcessor) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) HashMap(java.util.HashMap)

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