Search in sources :

Example 61 with LocalSearchScope

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

the class FlexMoveInnerClassProcessor method findUsages.

@NotNull
@Override
protected UsageInfo[] findUsages() {
    final Collection<UsageInfo> result = Collections.synchronizedCollection(new ArrayList<UsageInfo>());
    ReferencesSearch.search(myElement, new LocalSearchScope(myElement.getContainingFile())).forEach(reference -> {
        final PsiElement element = reference.getElement();
        if (!(element instanceof JSReferenceExpression)) {
            return true;
        }
        if (JSResolveUtil.isSelfReference(element)) {
            return true;
        }
        result.add(new UsageInfo(element));
        return true;
    });
    if (myElement instanceof JSClass) {
        final JSFunction constructor = ((JSClass) myElement).getConstructor();
        if (constructor != null) {
            result.add(new UsageInfo(constructor));
            JSRefactoringUtil.addConstructorUsages((JSClass) myElement, result);
        }
    }
    TextOccurrencesUtil.findNonCodeUsages(myElement, myElement.getName(), mySearchInComments, mySearchTextOccurences, StringUtil.getQualifiedName(myPackageName, myClassName), result);
    return UsageViewUtil.removeDuplicatedUsages(result.toArray(new UsageInfo[result.size()]));
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSFunction(com.intellij.lang.javascript.psi.JSFunction) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 62 with LocalSearchScope

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

the class AutoUnboxingInspection method isFixApplicable.

private static boolean isFixApplicable(PsiExpression location) {
    // conservative check to see if the result value of the postfix
    // expression is used later in the same expression statement.
    // Applying the quick fix in such a case would break the code
    // because the explicit unboxing code would split the expression in
    // multiple statements.
    final PsiElement parent = location.getParent();
    if (!(parent instanceof PsiPostfixExpression)) {
        return true;
    }
    final PsiReferenceExpression reference;
    if (location instanceof PsiReferenceExpression) {
        reference = (PsiReferenceExpression) location;
    } else if (location instanceof PsiArrayAccessExpression) {
        final PsiArrayAccessExpression arrayAccessExpression = (PsiArrayAccessExpression) location;
        final PsiExpression expression = arrayAccessExpression.getArrayExpression();
        if (!(expression instanceof PsiReferenceExpression)) {
            return true;
        }
        reference = (PsiReferenceExpression) expression;
    } else {
        return true;
    }
    final PsiElement element = reference.resolve();
    if (element == null) {
        return true;
    }
    final PsiStatement statement = PsiTreeUtil.getParentOfType(parent, PsiStatement.class);
    final LocalSearchScope scope = new LocalSearchScope(statement);
    final Query<PsiReference> query = ReferencesSearch.search(element, scope);
    final Collection<PsiReference> references = query.findAll();
    return references.size() <= 1;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 63 with LocalSearchScope

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

the class InspectionDescriptionInfo method doFindExtension.

@Nullable
private static Extension doFindExtension(Module module, PsiClass psiClass) {
    // Try search in narrow scopes first
    Project project = module.getProject();
    Set<DomFileElement<IdeaPlugin>> processed = new HashSet<>();
    for (GlobalSearchScope scope : DescriptionCheckerUtil.searchScopes(module)) {
        List<DomFileElement<IdeaPlugin>> origElements = DomService.getInstance().getFileElements(IdeaPlugin.class, project, scope);
        origElements.removeAll(processed);
        List<DomFileElement<IdeaPlugin>> elements = PluginDescriptorChooser.findAppropriateIntelliJModule(module.getName(), origElements);
        Query<PsiReference> query = ReferencesSearch.search(psiClass, new LocalSearchScope(elements.stream().map(DomFileElement::getFile).toArray(PsiElement[]::new)));
        Ref<Extension> result = Ref.create(null);
        query.forEach(ref -> {
            PsiElement element = ref.getElement();
            if (element instanceof XmlAttributeValue) {
                PsiElement parent = element.getParent();
                if (parent instanceof XmlAttribute && "implementationClass".equals(((XmlAttribute) parent).getName())) {
                    DomElement domElement = DomUtil.getDomElement(parent.getParent());
                    if (domElement instanceof Extension) {
                        Extension extension = (Extension) domElement;
                        ExtensionPoint extensionPoint = extension.getExtensionPoint();
                        if (extensionPoint != null && InheritanceUtil.isInheritor(extensionPoint.getBeanClass().getValue(), InspectionEP.class.getName())) {
                            result.set(extension);
                            return false;
                        }
                    }
                }
            }
            return true;
        });
        Extension extension = result.get();
        if (extension != null)
            return extension;
        processed.addAll(origElements);
    }
    return null;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ExtensionPoint(org.jetbrains.idea.devkit.dom.ExtensionPoint) DomFileElement(com.intellij.util.xml.DomFileElement) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Extension(org.jetbrains.idea.devkit.dom.Extension) Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 64 with LocalSearchScope

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

the class JoinDeclarationAndAssignmentAction method getPair.

private static Pair<PsiLocalVariable, PsiAssignmentExpression> getPair(PsiElement element) {
    PsiAssignmentExpression assignmentExpression = PsiTreeUtil.getParentOfType(element, PsiAssignmentExpression.class);
    PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(element, PsiDeclarationStatement.class);
    if (declarationStatement != null) {
        assignmentExpression = getAssignmentStatement(declarationStatement);
    } else if (assignmentExpression != null) {
        declarationStatement = getDeclarationStatement(assignmentExpression);
    }
    if (declarationStatement != null && assignmentExpression != null) {
        final PsiExpression lExpression = assignmentExpression.getLExpression();
        final PsiExpression rExpression = assignmentExpression.getRExpression();
        if (lExpression instanceof PsiReferenceExpression && rExpression != null) {
            final PsiElement resolve = ((PsiReferenceExpression) lExpression).resolve();
            if (resolve instanceof PsiLocalVariable && resolve.getParent() == declarationStatement) {
                final PsiLocalVariable variable = (PsiLocalVariable) resolve;
                if (ReferencesSearch.search(variable, new LocalSearchScope(rExpression), false).findFirst() != null) {
                    return null;
                }
                return Pair.createNonNull(variable, assignmentExpression);
            }
        }
    }
    return null;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 65 with LocalSearchScope

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

the class SurroundAutoCloseableAction method processVariable.

private static void processVariable(Project project, Editor editor, PsiLocalVariable variable) {
    PsiExpression initializer = ObjectUtils.assertNotNull(variable.getInitializer());
    PsiElement declaration = variable.getParent();
    PsiElement codeBlock = declaration.getParent();
    LocalSearchScope scope = new LocalSearchScope(codeBlock);
    PsiElement last = null;
    for (PsiReference reference : ReferencesSearch.search(variable, scope).findAll()) {
        PsiElement usage = PsiTreeUtil.findPrevParent(codeBlock, reference.getElement());
        if ((last == null || usage.getTextOffset() > last.getTextOffset())) {
            last = usage;
        }
    }
    String text = "try (" + variable.getTypeElement().getText() + " " + variable.getName() + " = " + initializer.getText() + ") {}";
    PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
    PsiTryStatement armStatement = (PsiTryStatement) declaration.replace(factory.createStatementFromText(text, codeBlock));
    List<PsiElement> toFormat = null;
    if (last != null) {
        toFormat = moveStatements(last, armStatement);
    }
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    PsiElement formattedElement = codeStyleManager.reformat(armStatement);
    if (toFormat != null) {
        for (PsiElement psiElement : toFormat) {
            codeStyleManager.reformat(psiElement);
        }
    }
    if (last == null) {
        PsiCodeBlock tryBlock = ((PsiTryStatement) formattedElement).getTryBlock();
        if (tryBlock != null) {
            PsiJavaToken brace = tryBlock.getLBrace();
            if (brace != null) {
                editor.getCaretModel().moveToOffset(brace.getTextOffset() + 1);
            }
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

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