Search in sources :

Example 1 with JavaFindUsagesHandler

use of com.intellij.find.findUsages.JavaFindUsagesHandler in project intellij-community by JetBrains.

the class FindUsagesTest method testNonCodeClassUsages.

public void testNonCodeClassUsages() throws Exception {
    final TempDirTestFixture tdf = IdeaTestFixtureFactory.getFixtureFactory().createTempDirTestFixture();
    tdf.setUp();
    try {
        new WriteCommandAction(getProject()) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                final ModifiableModuleModel moduleModel = ModuleManager.getInstance(getProject()).getModifiableModel();
                moduleModel.newModule("independent/independent.iml", StdModuleTypes.JAVA.getId());
                moduleModel.commit();
                tdf.createFile("plugin.xml", "<document>\n" + "  <action class=\"com.Foo\" />\n" + "  <action class=\"com.Foo.Bar\" />\n" + "  <action class=\"com.Foo$Bar\" />\n" + "</document>");
                PsiTestUtil.addContentRoot(ModuleManager.getInstance(getProject()).findModuleByName("independent"), tdf.getFile(""));
            }
        }.execute();
        GlobalSearchScope scope = GlobalSearchScope.allScope(getProject());
        PsiClass foo = myJavaFacade.findClass("com.Foo", scope);
        PsiClass bar = myJavaFacade.findClass("com.Foo.Bar", scope);
        final int[] count = { 0 };
        Processor<UsageInfo> processor = usageInfo -> {
            int navigationOffset = usageInfo.getNavigationOffset();
            assertTrue(navigationOffset > 0);
            String textAfter = usageInfo.getFile().getText().substring(navigationOffset);
            assertTrue(textAfter, textAfter.startsWith("Foo") || textAfter.startsWith("Bar") || textAfter.startsWith("com.Foo.Bar"));
            count[0]++;
            return true;
        };
        JavaFindUsagesHandler handler = new JavaFindUsagesHandler(bar, JavaFindUsagesHandlerFactory.getInstance(getProject()));
        count[0] = 0;
        handler.processUsagesInText(foo, processor, scope);
        assertEquals(3, count[0]);
        count[0] = 0;
        handler.processUsagesInText(bar, processor, scope);
        assertEquals(2, count[0]);
    } finally {
        tdf.tearDown();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) TempDirTestFixture(com.intellij.testFramework.fixtures.TempDirTestFixture) ModuleManager(com.intellij.openapi.module.ModuleManager) JavaFindUsagesHandlerFactory(com.intellij.find.findUsages.JavaFindUsagesHandlerFactory) UsageInfo(com.intellij.usageView.UsageInfo) IntArrayList(com.intellij.util.containers.IntArrayList) ArrayList(java.util.ArrayList) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) IdeaTestUtil(com.intellij.testFramework.IdeaTestUtil) PsiTestCase(com.intellij.testFramework.PsiTestCase) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) StdModuleTypes(com.intellij.openapi.module.StdModuleTypes) StdFileTypes(com.intellij.openapi.fileTypes.StdFileTypes) PsiTestUtil(com.intellij.testFramework.PsiTestUtil) JavaTestUtil(com.intellij.JavaTestUtil) OverridingMethodsSearch(com.intellij.psi.search.searches.OverridingMethodsSearch) Collection(java.util.Collection) TextRange(com.intellij.openapi.util.TextRange) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) JavaFindUsagesHandler(com.intellij.find.findUsages.JavaFindUsagesHandler) List(java.util.List) Result(com.intellij.openapi.application.Result) Processor(com.intellij.util.Processor) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) IdeaTestFixtureFactory(com.intellij.testFramework.fixtures.IdeaTestFixtureFactory) Collections(java.util.Collections) TempDirTestFixture(com.intellij.testFramework.fixtures.TempDirTestFixture) JavaFindUsagesHandler(com.intellij.find.findUsages.JavaFindUsagesHandler) Result(com.intellij.openapi.application.Result) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) UsageInfo(com.intellij.usageView.UsageInfo)

Example 2 with JavaFindUsagesHandler

use of com.intellij.find.findUsages.JavaFindUsagesHandler in project intellij-community by JetBrains.

the class GroovyFieldFindUsagesHandlerFactory method createFindUsagesHandler.

@Override
public FindUsagesHandler createFindUsagesHandler(@NotNull PsiElement element, boolean forHighlightUsages) {
    return new JavaFindUsagesHandler(element, this) {

        @NotNull
        @Override
        public PsiElement[] getSecondaryElements() {
            PsiElement element = getPsiElement();
            final PsiField field = (PsiField) element;
            PsiClass containingClass = field.getContainingClass();
            if (containingClass != null) {
                PsiMethod[] getters = GroovyPropertyUtils.getAllGettersByField(field);
                PsiMethod[] setters = GroovyPropertyUtils.getAllSettersByField(field);
                if (getters.length + setters.length > 0) {
                    final boolean doSearch;
                    if (arePhysical(getters) || arePhysical(setters)) {
                        if (ApplicationManager.getApplication().isUnitTestMode())
                            return PsiElement.EMPTY_ARRAY;
                        doSearch = Messages.showYesNoDialog(FindBundle.message("find.field.accessors.prompt", field.getName()), FindBundle.message("find.field.accessors.title"), Messages.getQuestionIcon()) == Messages.YES;
                    } else {
                        doSearch = true;
                    }
                    if (doSearch) {
                        final List<PsiElement> elements = new ArrayList<>();
                        for (PsiMethod getter : getters) {
                            ContainerUtil.addAll(elements, SuperMethodWarningUtil.checkSuperMethods(getter, ACTION_STRING));
                        }
                        for (PsiMethod setter : setters) {
                            ContainerUtil.addAll(elements, SuperMethodWarningUtil.checkSuperMethods(setter, ACTION_STRING));
                        }
                        for (Iterator<PsiElement> iterator = elements.iterator(); iterator.hasNext(); ) {
                            if (iterator.next() instanceof GrAccessorMethod)
                                iterator.remove();
                        }
                        return PsiUtilCore.toPsiElementArray(elements);
                    } else {
                        return PsiElement.EMPTY_ARRAY;
                    }
                }
            }
            return super.getSecondaryElements();
        }
    };
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) PsiMethod(com.intellij.psi.PsiMethod) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) ArrayList(java.util.ArrayList) JavaFindUsagesHandler(com.intellij.find.findUsages.JavaFindUsagesHandler) PsiElement(com.intellij.psi.PsiElement)

Aggregations

JavaFindUsagesHandler (com.intellij.find.findUsages.JavaFindUsagesHandler)2 ArrayList (java.util.ArrayList)2 JavaTestUtil (com.intellij.JavaTestUtil)1 JavaFindUsagesHandlerFactory (com.intellij.find.findUsages.JavaFindUsagesHandlerFactory)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 StdFileTypes (com.intellij.openapi.fileTypes.StdFileTypes)1 ModifiableModuleModel (com.intellij.openapi.module.ModifiableModuleModel)1 ModuleManager (com.intellij.openapi.module.ModuleManager)1 StdModuleTypes (com.intellij.openapi.module.StdModuleTypes)1 TextRange (com.intellij.openapi.util.TextRange)1 com.intellij.psi (com.intellij.psi)1 PsiClass (com.intellij.psi.PsiClass)1 PsiElement (com.intellij.psi.PsiElement)1 PsiField (com.intellij.psi.PsiField)1 PsiMethod (com.intellij.psi.PsiMethod)1 MethodReferencesSearch (com.intellij.psi.search.searches.MethodReferencesSearch)1 OverridingMethodsSearch (com.intellij.psi.search.searches.OverridingMethodsSearch)1 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)1 IdeaTestUtil (com.intellij.testFramework.IdeaTestUtil)1