Search in sources :

Example 71 with SearchScope

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

the class PsiAnnotationMethodReferencesSearcher method execute.

@Override
public boolean execute(@NotNull final ReferencesSearch.SearchParameters p, @NotNull final Processor<PsiReference> consumer) {
    final PsiElement refElement = p.getElementToSearch();
    boolean isAnnotation = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

        @Override
        public Boolean compute() {
            return PsiUtil.isAnnotationMethod(refElement);
        }
    });
    if (isAnnotation) {
        final PsiMethod method = (PsiMethod) refElement;
        PsiClass containingClass = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass>() {

            @Override
            public PsiClass compute() {
                boolean isValueMethod = PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME.equals(method.getName()) && method.getParameterList().getParametersCount() == 0;
                return isValueMethod ? method.getContainingClass() : null;
            }
        });
        if (containingClass != null) {
            SearchScope scope = ApplicationManager.getApplication().runReadAction(new Computable<SearchScope>() {

                @Override
                public SearchScope compute() {
                    return p.getEffectiveSearchScope();
                }
            });
            final Query<PsiReference> query = ReferencesSearch.search(containingClass, scope, p.isIgnoreAccessScope());
            return query.forEach(createImplicitDefaultAnnotationMethodConsumer(consumer));
        }
    }
    return true;
}
Also used : SearchScope(com.intellij.psi.search.SearchScope)

Example 72 with SearchScope

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

the class ExtractMethodObjectProcessor method findUsages.

@NotNull
protected UsageInfo[] findUsages() {
    final ArrayList<UsageInfo> result = new ArrayList<>();
    final PsiClass containingClass = getMethod().getContainingClass();
    final SearchScope scope = PsiUtilCore.getVirtualFile(containingClass) == null ? new LocalSearchScope(containingClass) : GlobalSearchScope.projectScope(myProject);
    PsiReference[] refs = ReferencesSearch.search(getMethod(), scope, false).toArray(PsiReference.EMPTY_ARRAY);
    for (PsiReference ref : refs) {
        final PsiElement element = ref.getElement();
        if (element != null && element.isValid()) {
            result.add(new UsageInfo(element));
        }
    }
    if (isCreateInnerClass()) {
        final Set<PsiMethod> usedMethods = new LinkedHashSet<>();
        getMethod().accept(new JavaRecursiveElementWalkingVisitor() {

            @Override
            public void visitMethodCallExpression(PsiMethodCallExpression expression) {
                super.visitMethodCallExpression(expression);
                final PsiMethod method = expression.resolveMethod();
                if (method != null) {
                    usedMethods.add(method);
                }
            }
        });
        for (PsiMethod usedMethod : usedMethods) {
            if (usedMethod.hasModifierProperty(PsiModifier.PRIVATE) && (!usedMethod.hasModifierProperty(PsiModifier.STATIC) || myExtractProcessor.isStatic())) {
                PsiMethod toMove = usedMethod;
                for (PsiReference reference : ReferencesSearch.search(usedMethod)) {
                    if (!PsiTreeUtil.isAncestor(getMethod(), reference.getElement(), false)) {
                        toMove = null;
                        break;
                    }
                }
                if (toMove != null) {
                    myUsages.add(new MethodToMoveUsageInfo(toMove));
                }
            }
        }
    }
    UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]);
    return UsageViewUtil.removeDuplicatedUsages(usageInfos);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) UsageInfo(com.intellij.usageView.UsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 73 with SearchScope

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

the class CachesBasedRefSearcher method processQuery.

@Override
public void processQuery(@NotNull ReferencesSearch.SearchParameters p, @NotNull Processor<PsiReference> consumer) {
    final PsiElement refElement = p.getElementToSearch();
    boolean caseSensitive = refElement.getLanguage().isCaseSensitive();
    String text = null;
    if (refElement instanceof PsiFileSystemItem && !(refElement instanceof SyntheticFileSystemItem)) {
        final VirtualFile vFile = ((PsiFileSystemItem) refElement).getVirtualFile();
        if (vFile != null) {
            text = vFile.getNameWithoutExtension();
        }
        // We must not look for file references with the file language's case-sensitivity, 
        // since case-sensitivity of the references themselves depends either on file system 
        // or on the rules of the language of reference
        caseSensitive = false;
    } else if (refElement instanceof PsiNamedElement) {
        text = ((PsiNamedElement) refElement).getName();
        if (refElement instanceof PsiMetaOwner) {
            final PsiMetaData metaData = ((PsiMetaOwner) refElement).getMetaData();
            if (metaData != null)
                text = metaData.getName();
        }
    }
    if (text == null && refElement instanceof PsiMetaOwner) {
        final PsiMetaData metaData = ((PsiMetaOwner) refElement).getMetaData();
        if (metaData != null)
            text = metaData.getName();
    }
    if (StringUtil.isNotEmpty(text)) {
        final SearchScope searchScope = p.getEffectiveSearchScope();
        p.getOptimizer().searchWord(text, searchScope, caseSensitive, refElement);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SyntheticFileSystemItem(com.intellij.psi.impl.SyntheticFileSystemItem) PsiMetaData(com.intellij.psi.meta.PsiMetaData) PsiNamedElement(com.intellij.psi.PsiNamedElement) SearchScope(com.intellij.psi.search.SearchScope) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner) PsiElement(com.intellij.psi.PsiElement)

Example 74 with SearchScope

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

the class VariableAccessVisitor method buildReferenceMap.

private static HashMap<PsiMethod, Collection<PsiReference>> buildReferenceMap(Set<PsiMethod> privateMethods) {
    final HashMap<PsiMethod, Collection<PsiReference>> referenceMap = new HashMap<>();
    for (PsiMethod method : privateMethods) {
        ProgressManager.checkCanceled();
        final SearchScope scope = method.getUseScope();
        final Collection<PsiReference> references = ReferencesSearch.search(method, scope).findAll();
        referenceMap.put(method, references);
    }
    return referenceMap;
}
Also used : HashMap(com.intellij.util.containers.HashMap) SearchScope(com.intellij.psi.search.SearchScope) Collection(java.util.Collection)

Example 75 with SearchScope

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

the class IncrementDecrementUsedAsExpressionInspection method extractPrefixPostfixExpressionToSeparateStatement.

public static void extractPrefixPostfixExpressionToSeparateStatement(PsiElement element) {
    final PsiExpression operand;
    if (element instanceof PsiPostfixExpression) {
        final PsiPostfixExpression postfixExpression = (PsiPostfixExpression) element;
        operand = postfixExpression.getOperand();
    } else if (element instanceof PsiPrefixExpression) {
        final PsiPrefixExpression prefixExpression = (PsiPrefixExpression) element;
        operand = prefixExpression.getOperand();
    } else {
        assert false;
        return;
    }
    if (operand == null) {
        return;
    }
    final PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
    if (statement == null) {
        return;
    }
    final PsiElement parent = statement.getParent();
    if (parent == null) {
        return;
    }
    final Project project = element.getProject();
    final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
    final String newStatementText = element.getText() + ';';
    final String operandText = operand.getText();
    if (parent instanceof PsiIfStatement || parent instanceof PsiLoopStatement) {
        // need to add braces because
        // in/decrement is inside braceless control statement body
        final StringBuilder text = new StringBuilder();
        text.append('{');
        final String elementText = PsiReplacementUtil.getElementText(statement, element, operandText);
        if (element instanceof PsiPostfixExpression) {
            text.append(elementText);
            text.append(newStatementText);
        } else {
            text.append(newStatementText);
            text.append(elementText);
        }
        text.append('}');
        final PsiCodeBlock codeBlock = factory.createCodeBlockFromText(text.toString(), parent);
        statement.replace(codeBlock);
        return;
    }
    final PsiStatement newStatement = factory.createStatementFromText(newStatementText, element);
    if (statement instanceof PsiReturnStatement) {
        if (element instanceof PsiPostfixExpression) {
            // special handling of postfix expression in return statement
            final PsiReturnStatement returnStatement = (PsiReturnStatement) statement;
            final PsiExpression returnValue = returnStatement.getReturnValue();
            if (returnValue == null) {
                return;
            }
            final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
            final String variableName = javaCodeStyleManager.suggestUniqueVariableName("result", returnValue, true);
            final PsiType type = returnValue.getType();
            if (type == null) {
                return;
            }
            final String newReturnValueText = PsiReplacementUtil.getElementText(returnValue, element, operandText);
            final String declarationStatementText = type.getCanonicalText() + ' ' + variableName + '=' + newReturnValueText + ';';
            final PsiStatement declarationStatement = factory.createStatementFromText(declarationStatementText, returnStatement);
            parent.addBefore(declarationStatement, statement);
            parent.addBefore(newStatement, statement);
            final PsiStatement newReturnStatement = factory.createStatementFromText("return " + variableName + ';', returnStatement);
            returnStatement.replace(newReturnStatement);
            return;
        } else {
            parent.addBefore(newStatement, statement);
        }
    } else if (statement instanceof PsiThrowStatement) {
        if (element instanceof PsiPostfixExpression) {
            // special handling of postfix expression in throw statement
            final PsiThrowStatement returnStatement = (PsiThrowStatement) statement;
            final PsiExpression exception = returnStatement.getException();
            if (exception == null) {
                return;
            }
            final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
            final String variableName = javaCodeStyleManager.suggestUniqueVariableName("e", exception, true);
            final PsiType type = exception.getType();
            if (type == null) {
                return;
            }
            final String newReturnValueText = PsiReplacementUtil.getElementText(exception, element, operandText);
            final String declarationStatementText = type.getCanonicalText() + ' ' + variableName + '=' + newReturnValueText + ';';
            final PsiStatement declarationStatement = factory.createStatementFromText(declarationStatementText, returnStatement);
            parent.addBefore(declarationStatement, statement);
            parent.addBefore(newStatement, statement);
            final PsiStatement newReturnStatement = factory.createStatementFromText("throw " + variableName + ';', returnStatement);
            returnStatement.replace(newReturnStatement);
            return;
        } else {
            parent.addBefore(newStatement, statement);
        }
    } else if (!(statement instanceof PsiForStatement)) {
        if (element instanceof PsiPostfixExpression) {
            parent.addAfter(newStatement, statement);
        } else {
            parent.addBefore(newStatement, statement);
        }
    } else if (operand instanceof PsiReferenceExpression) {
        final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) operand;
        final PsiElement target = referenceExpression.resolve();
        if (target != null) {
            final SearchScope useScope = target.getUseScope();
            if (!new LocalSearchScope(statement).equals(useScope)) {
                if (element instanceof PsiPostfixExpression) {
                    parent.addAfter(newStatement, statement);
                } else {
                    parent.addBefore(newStatement, statement);
                }
            }
        }
    }
    if (statement instanceof PsiLoopStatement) {
        // in/decrement inside loop statement condition
        final PsiLoopStatement loopStatement = (PsiLoopStatement) statement;
        final PsiStatement body = loopStatement.getBody();
        if (body instanceof PsiBlockStatement) {
            final PsiBlockStatement blockStatement = (PsiBlockStatement) body;
            final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
            if (element instanceof PsiPostfixExpression) {
                final PsiElement firstElement = codeBlock.getFirstBodyElement();
                codeBlock.addBefore(newStatement, firstElement);
            } else {
                codeBlock.add(newStatement);
            }
        } else {
            final StringBuilder blockText = new StringBuilder();
            blockText.append('{');
            if (element instanceof PsiPostfixExpression) {
                blockText.append(newStatementText);
                if (body != null) {
                    blockText.append(body.getText());
                }
            } else {
                if (body != null) {
                    blockText.append(body.getText());
                }
                blockText.append(newStatementText);
            }
            blockText.append('}');
            final PsiStatement blockStatement = factory.createStatementFromText(blockText.toString(), statement);
            if (body == null) {
                loopStatement.add(blockStatement);
            } else {
                body.replace(blockStatement);
            }
        }
    }
    PsiReplacementUtil.replaceExpression((PsiExpression) element, operandText);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Aggregations

SearchScope (com.intellij.psi.search.SearchScope)90 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)39 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)36 Project (com.intellij.openapi.project.Project)21 NotNull (org.jetbrains.annotations.NotNull)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 PsiElement (com.intellij.psi.PsiElement)16 PsiClass (com.intellij.psi.PsiClass)8 Processor (com.intellij.util.Processor)8 com.intellij.psi (com.intellij.psi)6 PsiMethod (com.intellij.psi.PsiMethod)6 HashMap (com.intellij.util.containers.HashMap)6 ClassInheritorsSearch (com.intellij.psi.search.searches.ClassInheritorsSearch)5 Nullable (org.jetbrains.annotations.Nullable)5 AnalysisScope (com.intellij.analysis.AnalysisScope)4 ProgressManager (com.intellij.openapi.progress.ProgressManager)4 TextRange (com.intellij.openapi.util.TextRange)4 PsiFile (com.intellij.psi.PsiFile)4 PsiSearchHelper (com.intellij.psi.search.PsiSearchHelper)4 QueryExecutor (com.intellij.util.QueryExecutor)4