Search in sources :

Example 16 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-elixir by KronicDeth.

the class Issue429Test method testUseScope.

/*
     * Tests
     */
public void testUseScope() {
    myFixture.configureByFiles("get_use_scope.ex");
    PsiElement callable = myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent().getPrevSibling().getLastChild().getLastChild().getLastChild();
    assertInstanceOf(callable, Call.class);
    SearchScope useScope = callable.getUseScope();
    assertInstanceOf(useScope, LocalSearchScope.class);
    LocalSearchScope localSearchScope = (LocalSearchScope) useScope;
    PsiElement[] scope = localSearchScope.getScope();
    assertEquals(1, scope.length);
    PsiElement singleScope = scope[0];
    assertTrue("Use Scope is not the surrounding if", singleScope.getText().startsWith("if auth == "));
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiElement(com.intellij.psi.PsiElement)

Example 17 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-elixir by KronicDeth.

the class Callable method variableUseScope.

private static LocalSearchScope variableUseScope(@NotNull Match match) {
    LocalSearchScope useScope;
    PsiElement parent = match.getParent();
    if (parent instanceof ElixirStabBody) {
        @SuppressWarnings("unchecked") PsiElement ancestor = PsiTreeUtil.getContextOfType(parent, ElixirAnonymousFunction.class, ElixirBlockItem.class, ElixirDoBlock.class, ElixirParentheticalStab.class, ElixirStabOperation.class);
        if (ancestor instanceof ElixirParentheticalStab) {
            useScope = variableUseScope(parent);
        } else {
            /* all non-ElixirParentheticalStab are block-like and so could have multiple statements after the match
                   where the match variable is used */
            useScope = followingSiblingsSearchScope(match);
        }
    } else if (parent instanceof PsiFile) {
        useScope = followingSiblingsSearchScope(match);
    } else {
        useScope = variableUseScope(parent);
    }
    return useScope;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 18 with LocalSearchScope

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

the class ReassignVariableUtil method reassign.

@VisibleForTesting
public static boolean reassign(final Editor editor) {
    final SmartPsiElementPointer<PsiDeclarationStatement> pointer = editor.getUserData(DECLARATION_KEY);
    final PsiDeclarationStatement declaration = pointer != null ? pointer.getElement() : null;
    final PsiType type = getVariableType(declaration);
    if (type != null) {
        VariablesProcessor proc = findVariablesOfType(declaration, type);
        if (proc.size() > 0) {
            List<PsiVariable> vars = new ArrayList<>();
            for (int i = 0; i < proc.size(); i++) {
                final PsiVariable variable = proc.getResult(i);
                PsiElement outerCodeBlock = PsiUtil.getVariableCodeBlock(variable, null);
                if (outerCodeBlock == null)
                    continue;
                if (ReferencesSearch.search(variable, new LocalSearchScope(outerCodeBlock)).forEach(reference -> {
                    final PsiElement element = reference.getElement();
                    if (element != null) {
                        return HighlightControlFlowUtil.getInnerClassVariableReferencedFrom(variable, element) == null;
                    }
                    return true;
                })) {
                    vars.add(variable);
                }
            }
            if (vars.isEmpty()) {
                return true;
            }
            if (vars.size() == 1) {
                replaceWithAssignment(declaration, proc.getResult(0), editor);
                return true;
            }
            final DefaultListModel<PsiVariable> model = new DefaultListModel<>();
            for (PsiVariable var : vars) {
                model.addElement(var);
            }
            final JBList list = new JBList(model);
            list.setCellRenderer(new ListCellRendererWrapper<PsiVariable>() {

                @Override
                public void customize(JList list, PsiVariable value, int index, boolean selected, boolean hasFocus) {
                    if (value != null) {
                        setText(value.getName());
                        setIcon(value.getIcon(0));
                    }
                }
            });
            final VisualPosition visualPosition = editor.getCaretModel().getVisualPosition();
            final Point point = editor.visualPositionToXY(new VisualPosition(visualPosition.line + 1, visualPosition.column));
            JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle("Choose variable to reassign").setRequestFocus(true).setItemChoosenCallback(() -> replaceWithAssignment(declaration, (PsiVariable) list.getSelectedValue(), editor)).createPopup().show(new RelativePoint(editor.getContentComponent(), point));
        }
        return true;
    }
    return false;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) VariablesProcessor(com.intellij.psi.scope.processor.VariablesProcessor) ArrayList(java.util.ArrayList) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) JBList(com.intellij.ui.components.JBList) VisualPosition(com.intellij.openapi.editor.VisualPosition) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 19 with LocalSearchScope

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

the class JavaPullUpHelper method buildConstructorsToSubConstructorsMap.

private HashMap<PsiMethod, HashSet<PsiMethod>> buildConstructorsToSubConstructorsMap(final PsiMethod[] constructors) {
    final HashMap<PsiMethod, HashSet<PsiMethod>> constructorsToSubConstructors = new HashMap<>();
    for (PsiMethod constructor : constructors) {
        final HashSet<PsiMethod> referencingSubConstructors = new HashSet<>();
        constructorsToSubConstructors.put(constructor, referencingSubConstructors);
        if (constructor != null) {
            // find references
            for (PsiReference reference : ReferencesSearch.search(constructor, new LocalSearchScope(mySourceClass), false)) {
                final PsiElement element = reference.getElement();
                if (element != null && "super".equals(element.getText())) {
                    PsiMethod parentMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
                    if (parentMethod != null && parentMethod.isConstructor()) {
                        referencingSubConstructors.add(parentMethod);
                    }
                }
            }
        }
        // check default constructor
        if (constructor == null || constructor.getParameterList().getParametersCount() == 0) {
            RefactoringUtil.visitImplicitSuperConstructorUsages(mySourceClass, new RefactoringUtil.ImplicitConstructorUsageVisitor() {

                public void visitConstructor(PsiMethod constructor, PsiMethod baseConstructor) {
                    referencingSubConstructors.add(constructor);
                }

                public void visitClassWithoutConstructors(PsiClass aClass) {
                }
            }, myTargetSuperClass);
        }
    }
    return constructorsToSubConstructors;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) HashMap(com.intellij.util.containers.HashMap) RefactoringUtil(com.intellij.refactoring.util.RefactoringUtil)

Example 20 with LocalSearchScope

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

the class PsiClassImplUtil method getClassUseScope.

@NotNull
public static SearchScope getClassUseScope(@NotNull PsiClass aClass) {
    if (aClass instanceof PsiAnonymousClass) {
        return new LocalSearchScope(aClass);
    }
    final GlobalSearchScope maximalUseScope = ResolveScopeManager.getElementUseScope(aClass);
    PsiFile file = aClass.getContainingFile();
    if (PsiImplUtil.isInServerPage(file))
        return maximalUseScope;
    final PsiClass containingClass = aClass.getContainingClass();
    if (aClass.hasModifierProperty(PsiModifier.PUBLIC) || aClass.hasModifierProperty(PsiModifier.PROTECTED)) {
        return containingClass == null ? maximalUseScope : containingClass.getUseScope();
    } else if (aClass.hasModifierProperty(PsiModifier.PRIVATE) || aClass instanceof PsiTypeParameter) {
        PsiClass topClass = PsiUtil.getTopLevelClass(aClass);
        return new LocalSearchScope(topClass == null ? aClass.getContainingFile() : topClass);
    } else {
        PsiPackage aPackage = null;
        if (file instanceof PsiJavaFile) {
            aPackage = JavaPsiFacade.getInstance(aClass.getProject()).findPackage(((PsiJavaFile) file).getPackageName());
        }
        if (aPackage == null) {
            PsiDirectory dir = file.getContainingDirectory();
            if (dir != null) {
                aPackage = JavaDirectoryService.getInstance().getPackage(dir);
            }
        }
        if (aPackage != null) {
            SearchScope scope = PackageScope.packageScope(aPackage, false);
            scope = scope.intersectWith(maximalUseScope);
            return scope;
        }
        return new LocalSearchScope(file);
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NotNull(org.jetbrains.annotations.NotNull)

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