Search in sources :

Example 1 with PsiElement2UsageTargetAdapter

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

the class MavenDomTestCase method search.

protected List<PsiElement> search(VirtualFile file) throws IOException {
    final MapDataContext context = createDataContext(file);
    UsageTarget[] targets = UsageTargetUtil.findUsageTargets(new DataProvider() {

        @Override
        public Object getData(@NonNls String dataId) {
            return context.getData(dataId);
        }
    });
    PsiElement target = ((PsiElement2UsageTargetAdapter) targets[0]).getElement();
    List<PsiReference> result = new ArrayList<>(ReferencesSearch.search(target).findAll());
    return ContainerUtil.map(result, psiReference -> psiReference.getElement());
}
Also used : DataProvider(com.intellij.openapi.actionSystem.DataProvider) MapDataContext(com.intellij.testFramework.MapDataContext) ArrayList(java.util.ArrayList) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) UsageTarget(com.intellij.usages.UsageTarget)

Example 2 with PsiElement2UsageTargetAdapter

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

the class JavaUsageTypeProviderTest method assertUsageType.

private void assertUsageType(UsageType expected, PsiClass target) {
    UsageTarget[] targets = { new PsiElement2UsageTargetAdapter(target) };
    PsiElement element = myFixture.getReferenceAtCaretPositionWithAssertion().getElement();
    UsageType usageType = new JavaUsageTypeProvider().getUsageType(element, targets);
    assertEquals(expected, usageType);
}
Also used : JavaUsageTypeProvider(com.intellij.usages.impl.rules.JavaUsageTypeProvider) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) UsageType(com.intellij.usages.impl.rules.UsageType) PsiElement(com.intellij.psi.PsiElement) UsageTarget(com.intellij.usages.UsageTarget)

Example 3 with PsiElement2UsageTargetAdapter

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

the class UsageViewTest method testUsageViewCanRerunAfterTargetWasInvalidatedAndRestored.

public void testUsageViewCanRerunAfterTargetWasInvalidatedAndRestored() throws Exception {
    PsiFile psiFile = myFixture.addFileToProject("X.java", "public class X{" + "    void foo() {\n" + "        bar();\n" + "        bar();\n" + "    }" + "    void bar() {}\n" + "}");
    Usage usage = createUsage(psiFile, psiFile.getText().indexOf("bar();"));
    PsiElement[] members = psiFile.getChildren()[psiFile.getChildren().length - 1].getChildren();
    PsiNamedElement bar = (PsiNamedElement) members[members.length - 3];
    assertEquals("bar", bar.getName());
    UsageTarget target = new PsiElement2UsageTargetAdapter(bar);
    FindUsagesManager usagesManager = ((FindManagerImpl) FindManager.getInstance(getProject())).getFindUsagesManager();
    FindUsagesHandler handler = usagesManager.getNewFindUsagesHandler(bar, false);
    UsageViewImpl usageView = (UsageViewImpl) usagesManager.doFindUsages(new PsiElement[] { bar }, PsiElement.EMPTY_ARRAY, handler, handler.getFindUsagesOptions(), false);
    Disposer.register(myFixture.getTestRootDisposable(), usageView);
    assertTrue(usageView.canPerformReRun());
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    Document document = documentManager.getDocument(psiFile);
    String barDef = "void bar() {}\n";
    String commentedBarDef = "//" + barDef;
    WriteCommandAction.runWriteCommandAction(getProject(), () -> {
        String text = document.getText();
        document.replaceString(text.indexOf(barDef), text.indexOf(barDef) + barDef.length(), commentedBarDef);
    });
    documentManager.commitAllDocuments();
    // target invalidated
    assertFalse(usageView.canPerformReRun());
    WriteCommandAction.runWriteCommandAction(getProject(), () -> {
        String text = document.getText();
        document.replaceString(text.indexOf(commentedBarDef), text.indexOf(commentedBarDef) + commentedBarDef.length(), barDef);
    });
    documentManager.commitAllDocuments();
    assertTrue(usageView.canPerformReRun());
    usageView.doReRun();
    Set<Usage> usages = usageView.getUsages();
    assertEquals(2, usages.size());
}
Also used : FindUsagesHandler(com.intellij.find.findUsages.FindUsagesHandler) PsiNamedElement(com.intellij.psi.PsiNamedElement) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) Document(com.intellij.openapi.editor.Document) FindManagerImpl(com.intellij.find.impl.FindManagerImpl) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 4 with PsiElement2UsageTargetAdapter

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

the class DartRenameDialog method fillTargetsAndUsageToEditIdMap.

private void fillTargetsAndUsageToEditIdMap(@NotNull final List<UsageTarget> usageTargets, @NotNull final Map<Usage, String> usageToEditIdMap) {
    final SourceChange change = myRefactoring.getChange();
    assert change != null;
    final DartAnalysisServerService service = DartAnalysisServerService.getInstance(myProject);
    final PsiManager psiManager = PsiManager.getInstance(myProject);
    for (SourceFileEdit fileEdit : change.getEdits()) {
        final VirtualFile file = AssistUtils.findVirtualFile(fileEdit);
        final PsiFile psiFile = file == null ? null : psiManager.findFile(file);
        if (psiFile == null)
            continue;
        for (SourceEdit sourceEdit : fileEdit.getEdits()) {
            final int offset = service.getConvertedOffset(file, sourceEdit.getOffset());
            final int length = service.getConvertedOffset(file, sourceEdit.getOffset() + sourceEdit.getLength()) - offset;
            final TextRange range = TextRange.create(offset, offset + length);
            final boolean potentialUsage = myRefactoring.getPotentialEdits().contains(sourceEdit.getId());
            final PsiElement usageElement = DartServerFindUsagesHandler.getUsagePsiElement(psiFile, range);
            if (usageElement != null) {
                if (DartComponentType.typeOf(usageElement) != null) {
                    usageTargets.add(new PsiElement2UsageTargetAdapter(usageElement));
                } else {
                    final UsageInfo usageInfo = DartServerFindUsagesHandler.getUsageInfo(usageElement, range, potentialUsage);
                    if (usageInfo != null) {
                        usageToEditIdMap.put(new UsageInfo2UsageAdapter(usageInfo), sourceEdit.getId());
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFileEdit(org.dartlang.analysis.server.protocol.SourceFileEdit) PsiManager(com.intellij.psi.PsiManager) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) TextRange(com.intellij.openapi.util.TextRange) SourceEdit(org.dartlang.analysis.server.protocol.SourceEdit) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) PsiFile(com.intellij.psi.PsiFile) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Example 5 with PsiElement2UsageTargetAdapter

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

the class JavaSafeDeleteProcessor method showUsages.

@Override
public UsageView showUsages(UsageInfo[] usages, UsageViewPresentation presentation, UsageViewManager manager, PsiElement[] elements) {
    final List<PsiElement> overridingMethods = new ArrayList<>();
    final List<UsageInfo> others = new ArrayList<>();
    for (UsageInfo usage : usages) {
        if (usage instanceof SafeDeleteOverridingMethodUsageInfo) {
            overridingMethods.add(((SafeDeleteOverridingMethodUsageInfo) usage).getOverridingMethod());
        } else {
            others.add(usage);
        }
    }
    UsageTarget[] targets = new UsageTarget[elements.length + overridingMethods.size()];
    for (int i = 0; i < targets.length; i++) {
        if (i < elements.length) {
            targets[i] = new PsiElement2UsageTargetAdapter(elements[i]);
        } else {
            targets[i] = new PsiElement2UsageTargetAdapter(overridingMethods.get(i - elements.length));
        }
    }
    return manager.showUsages(targets, UsageInfoToUsageConverter.convert(elements, others.toArray(new UsageInfo[others.size()])), presentation);
}
Also used : PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)9 PsiElement (com.intellij.psi.PsiElement)6 PsiFile (com.intellij.psi.PsiFile)4 UsageInfo (com.intellij.usageView.UsageInfo)4 UsageTarget (com.intellij.usages.UsageTarget)3 Logger (com.intellij.openapi.diagnostic.Logger)2 Project (com.intellij.openapi.project.Project)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2 FindUsagesHandler (com.intellij.find.findUsages.FindUsagesHandler)1 FindUsagesManager (com.intellij.find.findUsages.FindUsagesManager)1 FindManagerImpl (com.intellij.find.impl.FindManagerImpl)1 LocalHistory (com.intellij.history.LocalHistory)1 LocalHistoryAction (com.intellij.history.LocalHistoryAction)1 CopyProvider (com.intellij.ide.CopyProvider)1 DataManager (com.intellij.ide.DataManager)1 DeleteProvider (com.intellij.ide.DeleteProvider)1 ContextHelpAction (com.intellij.ide.actions.ContextHelpAction)1 Language (com.intellij.lang.Language)1 IProperty (com.intellij.lang.properties.IProperty)1