Search in sources :

Example 6 with FindUsagesManager

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

the class ChangeMethodSignatureFromUsageFix method performChange.

public static List<ParameterInfoImpl> performChange(final Project project, final Editor editor, final PsiFile file, final PsiMethod method, final int minUsagesNumber, final ParameterInfoImpl[] newParametersInfo, final boolean changeAllUsages, final boolean allowDelegation) {
    if (!FileModificationService.getInstance().prepareFileForWrite(method.getContainingFile()))
        return null;
    final FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(project)).getFindUsagesManager();
    final FindUsagesHandler handler = findUsagesManager.getFindUsagesHandler(method, false);
    //on failure or cancel (e.g. cancel of super methods dialog)
    if (handler == null)
        return null;
    final JavaMethodFindUsagesOptions options = new JavaMethodFindUsagesOptions(project);
    options.isImplementingMethods = true;
    options.isOverridingMethods = true;
    options.isUsages = true;
    options.isSearchForTextOccurrences = false;
    final int[] usagesFound = new int[1];
    Runnable runnable = () -> {
        Processor<UsageInfo> processor = t -> ++usagesFound[0] < minUsagesNumber;
        handler.processElementUsages(method, processor, options);
    };
    String progressTitle = QuickFixBundle.message("searching.for.usages.progress.title");
    if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(runnable, progressTitle, true, project))
        return null;
    if (ApplicationManager.getApplication().isUnitTestMode() || usagesFound[0] < minUsagesNumber) {
        ChangeSignatureProcessor processor = new ChangeSignatureProcessor(project, method, false, null, method.getName(), method.getReturnType(), newParametersInfo) {

            @Override
            @NotNull
            protected UsageInfo[] findUsages() {
                return changeAllUsages ? super.findUsages() : UsageInfo.EMPTY_ARRAY;
            }

            @Override
            protected void performRefactoring(@NotNull UsageInfo[] usages) {
                CommandProcessor.getInstance().setCurrentCommandName(getCommandName());
                super.performRefactoring(usages);
            }
        };
        processor.run();
        ApplicationManager.getApplication().runWriteAction(() -> UndoUtil.markPsiFileForUndo(file));
        return Arrays.asList(newParametersInfo);
    } else {
        final List<ParameterInfoImpl> parameterInfos = newParametersInfo != null ? new ArrayList<>(Arrays.asList(newParametersInfo)) : new ArrayList<>();
        final PsiReferenceExpression refExpr = JavaTargetElementEvaluator.findReferenceExpression(editor);
        JavaChangeSignatureDialog dialog = JavaChangeSignatureDialog.createAndPreselectNew(project, method, parameterInfos, allowDelegation, refExpr);
        dialog.setParameterInfos(parameterInfos);
        dialog.show();
        return dialog.isOK() ? dialog.getParameters() : null;
    }
}
Also used : FindUsagesHandler(com.intellij.find.findUsages.FindUsagesHandler) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor) CommandProcessor(com.intellij.openapi.command.CommandProcessor) Processor(com.intellij.util.Processor) JavaMethodFindUsagesOptions(com.intellij.find.findUsages.JavaMethodFindUsagesOptions) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) NotNull(org.jetbrains.annotations.NotNull) FindManagerImpl(com.intellij.find.impl.FindManagerImpl) JavaChangeSignatureDialog(com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager) UsageInfo(com.intellij.usageView.UsageInfo) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Example 7 with FindUsagesManager

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

the class TextOccurrencesUtil method isSearchTextOccurencesEnabled.

public static boolean isSearchTextOccurencesEnabled(@NotNull PsiElement element) {
    final FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(element.getProject())).getFindUsagesManager();
    final FindUsagesHandler handler = findUsagesManager.getFindUsagesHandler(element, true);
    return FindUsagesUtil.isSearchForTextOccurrencesAvailable(element, false, handler);
}
Also used : FindManagerImpl(com.intellij.find.impl.FindManagerImpl) FindUsagesHandler(com.intellij.find.findUsages.FindUsagesHandler) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager)

Example 8 with FindUsagesManager

use of com.intellij.find.findUsages.FindUsagesManager in project intellij-plugins by JetBrains.

the class DartServerFindUsagesTest method testFileUsage.

public void testFileUsage() {
    final PsiFile barFile = myFixture.configureByText("bar.dart", "");
    // it is important that foo.dart is not open in the editor
    myFixture.addFileToProject("foo.dart", "import '" + barFile.getName() + "';");
    // warm up
    myFixture.doHighlighting();
    final FindUsagesManager manager = ((FindManagerImpl) FindManager.getInstance(getProject())).getFindUsagesManager();
    final FindUsagesHandler handler = manager.getFindUsagesHandler(getFile(), false);
    assertNotNull(handler);
    assertFalse(handler instanceof DartServerFindUsagesHandler);
    final Collection<PsiReference> usages = handler.findReferencesToHighlight(getFile(), GlobalSearchScope.allScope(getProject()));
    assertSize(1, usages);
    final PsiReference reference = usages.iterator().next();
    assertInstanceOf(reference, DartFileReference.class);
    assertEquals("foo.dart", reference.getElement().getContainingFile().getName());
    assertEquals("import '" + barFile.getName() + "';", reference.getElement().getParent().getText());
}
Also used : FindManagerImpl(com.intellij.find.impl.FindManagerImpl) FindUsagesHandler(com.intellij.find.findUsages.FindUsagesHandler) DartServerFindUsagesHandler(com.jetbrains.lang.dart.ide.findUsages.DartServerFindUsagesHandler) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager) DartServerFindUsagesHandler(com.jetbrains.lang.dart.ide.findUsages.DartServerFindUsagesHandler)

Example 9 with FindUsagesManager

use of com.intellij.find.findUsages.FindUsagesManager in project intellij-plugins by JetBrains.

the class DartServerFindUsagesTest method findUsages.

@NotNull
private Collection<UsageInfo> findUsages(@NotNull final SearchScope scope) {
    final PsiElement elementToSearch = getFile().findElementAt(getEditor().getCaretModel().getOffset());
    assertNotNull(elementToSearch);
    final FindUsagesManager manager = ((FindManagerImpl) FindManager.getInstance(getProject())).getFindUsagesManager();
    final FindUsagesHandler handler = manager.getFindUsagesHandler(elementToSearch, false);
    assertInstanceOf(handler, DartServerFindUsagesHandler.class);
    final CommonProcessors.CollectProcessor<UsageInfo> processor = new CommonProcessors.CollectProcessor<>();
    handler.processElementUsages(elementToSearch, processor, new FindUsagesOptions(scope));
    return processor.getResults();
}
Also used : FindManagerImpl(com.intellij.find.impl.FindManagerImpl) FindUsagesHandler(com.intellij.find.findUsages.FindUsagesHandler) DartServerFindUsagesHandler(com.jetbrains.lang.dart.ide.findUsages.DartServerFindUsagesHandler) FindUsagesOptions(com.intellij.find.findUsages.FindUsagesOptions) CommonProcessors(com.intellij.util.CommonProcessors) PsiElement(com.intellij.psi.PsiElement) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager) UsageInfo(com.intellij.usageView.UsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

FindUsagesManager (com.intellij.find.findUsages.FindUsagesManager)9 FindUsagesHandler (com.intellij.find.findUsages.FindUsagesHandler)7 FindManagerImpl (com.intellij.find.impl.FindManagerImpl)7 NotNull (org.jetbrains.annotations.NotNull)4 PsiElement (com.intellij.psi.PsiElement)3 UsageInfo (com.intellij.usageView.UsageInfo)3 FindUsagesOptions (com.intellij.find.findUsages.FindUsagesOptions)2 Project (com.intellij.openapi.project.Project)2 PsiFile (com.intellij.psi.PsiFile)2 ConfigurableUsageTarget (com.intellij.usages.ConfigurableUsageTarget)2 CommonProcessors (com.intellij.util.CommonProcessors)2 DartServerFindUsagesHandler (com.jetbrains.lang.dart.ide.findUsages.DartServerFindUsagesHandler)2 ArrayList (java.util.ArrayList)2 ReadWriteAccessDetector (com.intellij.codeInsight.highlighting.ReadWriteAccessDetector)1 JavaMethodFindUsagesOptions (com.intellij.find.findUsages.JavaMethodFindUsagesOptions)1 PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)1 ItemPresentation (com.intellij.navigation.ItemPresentation)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 Document (com.intellij.openapi.editor.Document)1 BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)1