Search in sources :

Example 1 with NavigatablePsiElement

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

the class PyHierarchyNodeDescriptor method update.

@Override
public boolean update() {
    boolean changes = super.update();
    final CompositeAppearance oldText = myHighlightedText;
    myHighlightedText = new CompositeAppearance();
    NavigatablePsiElement element = (NavigatablePsiElement) getPsiElement();
    if (element == null) {
        final String invalidPrefix = IdeBundle.message("node.hierarchy.invalid");
        if (!myHighlightedText.getText().startsWith(invalidPrefix)) {
            myHighlightedText.getBeginning().addText(invalidPrefix, HierarchyNodeDescriptor.getInvalidPrefixAttributes());
        }
        return true;
    }
    final ItemPresentation presentation = element.getPresentation();
    if (presentation != null) {
        if (element instanceof PyFunction) {
            final PyClass cls = ((PyFunction) element).getContainingClass();
            if (cls != null) {
                myHighlightedText.getEnding().addText(cls.getName() + ".");
            }
        }
        myHighlightedText.getEnding().addText(presentation.getPresentableText());
        myHighlightedText.getEnding().addText(" " + presentation.getLocationString(), HierarchyNodeDescriptor.getPackageNameAttributes());
    }
    myName = myHighlightedText.getText();
    if (!Comparing.equal(myHighlightedText, oldText)) {
        changes = true;
    }
    return changes;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) ItemPresentation(com.intellij.navigation.ItemPresentation) CompositeAppearance(com.intellij.openapi.roots.ui.util.CompositeAppearance) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement)

Example 2 with NavigatablePsiElement

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

the class PyLineMarkerProviderTest method testOverriding.

/**
   * Checks method has "up" arrow when overrides, and this arrow works
   */
public void testOverriding() throws Exception {
    myFixture.copyDirectoryToProject("lineMarkerTest", "");
    myFixture.configureByFile("spam.py");
    final ASTNode functionNode = myFixture.getElementAtCaret().getNode();
    // We need IDENTIFIER node
    final ASTNode[] functionChildren = functionNode.getChildren(TokenSet.create(PyTokenTypes.IDENTIFIER));
    assert functionChildren.length == 1 : "Wrong number of identifiers: " + functionChildren.length;
    final PsiElement element = functionChildren[0].getPsi();
    @SuppressWarnings("unchecked") final LineMarkerInfo<PsiElement> lineMarkerInfo = new PyLineMarkerProvider().getLineMarkerInfo(element);
    Assert.assertNotNull("No gutter displayed", lineMarkerInfo);
    final GutterIconNavigationHandler<PsiElement> handler = lineMarkerInfo.getNavigationHandler();
    Assert.assertNotNull("Gutter has no navigation handle", handler);
    handler.navigate(new MouseEvent(new JLabel(), 0, 0, 0, 0, 0, 0, false), element);
    final NavigatablePsiElement[] targets = PyLineMarkerNavigator.getNavigationTargets(element);
    Assert.assertNotNull("No navigation targets found", targets);
    Assert.assertThat("Wrong number of targets found", targets, Matchers.arrayWithSize(1));
    final NavigatablePsiElement parentMethod = targets[0];
    Assert.assertThat("Navigation target has wrong type", parentMethod, Matchers.instanceOf(PyPossibleClassMember.class));
    final PyClass parentClass = ((PyPossibleClassMember) parentMethod).getContainingClass();
    Assert.assertNotNull("Function overrides other function, but no parent displayed", parentClass);
    Assert.assertEquals("Wrong parent class name", "Eggs", parentClass.getName());
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) MouseEvent(java.awt.event.MouseEvent) ASTNode(com.intellij.lang.ASTNode) PyPossibleClassMember(com.jetbrains.python.psi.PyPossibleClassMember) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) PsiElement(com.intellij.psi.PsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement)

Example 3 with NavigatablePsiElement

use of com.intellij.psi.NavigatablePsiElement in project kotlin by JetBrains.

the class AbstractLineMarkersTest method assertNavigationElements.

private void assertNavigationElements(List<LineMarkerInfo> markers) {
    List<String> navigationDataComments = KotlinTestUtils.getLastCommentsInFile((KtFile) myFixture.getFile(), KotlinTestUtils.CommentType.BLOCK_COMMENT, false);
    if (navigationDataComments.isEmpty())
        return;
    for (String navigationComment : navigationDataComments) {
        final String description = getLineMarkerDescription(navigationComment);
        LineMarkerInfo navigateMarker = ContainerUtil.find(markers, new Condition<LineMarkerInfo>() {

            @Override
            public boolean value(LineMarkerInfo marker) {
                String tooltip = marker.getLineMarkerTooltip();
                return tooltip != null && tooltip.startsWith(description);
            }
        });
        assertNotNull(String.format("Can't find marker for navigation check with description \"%s\"", description), navigateMarker);
        GutterIconNavigationHandler handler = navigateMarker.getNavigationHandler();
        if (handler instanceof SuperDeclarationMarkerNavigationHandler) {
            PsiElement element = navigateMarker.getElement();
            //noinspection unchecked
            handler.navigate(null, element);
            List<NavigatablePsiElement> navigateElements = ((SuperDeclarationMarkerNavigationHandler) handler).getNavigationElements();
            Collections.sort(navigateElements, new Comparator<NavigatablePsiElement>() {

                @Override
                public int compare(@NotNull NavigatablePsiElement first, @NotNull NavigatablePsiElement second) {
                    String elementFirstStr = ReferenceUtils.renderAsGotoImplementation(first);
                    String elementSecondStr = ReferenceUtils.renderAsGotoImplementation(second);
                    return elementFirstStr.compareTo(elementSecondStr);
                }
            });
            String actualNavigationData = NavigationTestUtils.getNavigateElementsText(myFixture.getProject(), navigateElements);
            assertSameLines(getExpectedNavigationText(navigationComment), actualNavigationData);
        } else {
            Assert.fail("Only SuperDeclarationMarkerNavigationHandler are supported in navigate check");
        }
    }
}
Also used : GutterIconNavigationHandler(com.intellij.codeInsight.daemon.GutterIconNavigationHandler) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) SuperDeclarationMarkerNavigationHandler(org.jetbrains.kotlin.idea.highlighter.markers.SuperDeclarationMarkerNavigationHandler) PsiElement(com.intellij.psi.PsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement)

Example 4 with NavigatablePsiElement

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

the class PsiElementListNavigator method navigateOrCreatePopup.

@Nullable
private static JBPopup navigateOrCreatePopup(final NavigatablePsiElement[] targets, final String title, final String findUsagesTitle, final ListCellRenderer listRenderer, @Nullable final ListBackgroundUpdaterTask listUpdaterTask) {
    return navigateOrCreatePopup(targets, title, findUsagesTitle, listRenderer, listUpdaterTask, selectedElements -> {
        for (Object element : selectedElements) {
            PsiElement selected = (PsiElement) element;
            LOG.assertTrue(selected.isValid());
            ((NavigatablePsiElement) selected).navigate(true);
        }
    });
}
Also used : PsiElement(com.intellij.psi.PsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with NavigatablePsiElement

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

the class PsiElementListNavigator method navigateOrCreatePopup.

/**
   * listUpdaterTask should be started after alarm is initialized so one-item popup won't blink
   */
@Nullable
public static JBPopup navigateOrCreatePopup(@NotNull final NavigatablePsiElement[] targets, final String title, final String findUsagesTitle, final ListCellRenderer listRenderer, @Nullable final ListBackgroundUpdaterTask listUpdaterTask, @NotNull final Consumer<Object[]> consumer) {
    if (targets.length == 0)
        return null;
    if (targets.length == 1 && (listUpdaterTask == null || listUpdaterTask.isFinished())) {
        consumer.consume(targets);
        return null;
    }
    final CollectionListModel<NavigatablePsiElement> model = new CollectionListModel<>(targets);
    final JBList list = new JBList(model);
    HintUpdateSupply.installSimpleHintUpdateSupply(list);
    list.setTransferHandler(new TransferHandler() {

        @Nullable
        @Override
        protected Transferable createTransferable(JComponent c) {
            final Object[] selectedValues = list.getSelectedValues();
            final PsiElement[] copy = new PsiElement[selectedValues.length];
            for (int i = 0; i < selectedValues.length; i++) {
                copy[i] = (PsiElement) selectedValues[i];
            }
            return new PsiCopyPasteManager.MyTransferable(copy);
        }

        @Override
        public int getSourceActions(JComponent c) {
            return COPY;
        }
    });
    list.setCellRenderer(listRenderer);
    list.setFont(EditorUtil.getEditorFont());
    final PopupChooserBuilder builder = new PopupChooserBuilder(list);
    if (listRenderer instanceof PsiElementListCellRenderer) {
        ((PsiElementListCellRenderer) listRenderer).installSpeedSearch(builder);
    }
    PopupChooserBuilder popupChooserBuilder = builder.setTitle(title).setMovable(true).setResizable(true).setItemChoosenCallback(() -> {
        int[] ids = list.getSelectedIndices();
        if (ids == null || ids.length == 0)
            return;
        Object[] selectedElements = list.getSelectedValues();
        consumer.consume(selectedElements);
    }).setCancelCallback(() -> {
        HintUpdateSupply.hideHint(list);
        if (listUpdaterTask != null) {
            listUpdaterTask.cancelTask();
        }
        return true;
    });
    final Ref<UsageView> usageView = new Ref<>();
    if (findUsagesTitle != null) {
        popupChooserBuilder = popupChooserBuilder.setCouldPin(popup -> {
            final List<NavigatablePsiElement> items = model.getItems();
            usageView.set(FindUtil.showInUsageView(null, items.toArray(new PsiElement[items.size()]), findUsagesTitle, targets[0].getProject()));
            popup.cancel();
            return false;
        });
    }
    final JBPopup popup = popupChooserBuilder.createPopup();
    builder.getScrollPane().setBorder(null);
    builder.getScrollPane().setViewportBorder(null);
    if (listUpdaterTask != null) {
        listUpdaterTask.init((AbstractPopup) popup, list, usageView);
    }
    return popup;
}
Also used : EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) Transferable(java.awt.datatransfer.Transferable) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) PsiElement(com.intellij.psi.PsiElement) Logger(com.intellij.openapi.diagnostic.Logger) ProgressManager(com.intellij.openapi.progress.ProgressManager) JBList(com.intellij.ui.components.JBList) AbstractPopup(com.intellij.ui.popup.AbstractPopup) CollectionListModel(com.intellij.ui.CollectionListModel) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) Editor(com.intellij.openapi.editor.Editor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) MouseEvent(java.awt.event.MouseEvent) ListBackgroundUpdaterTask(com.intellij.codeInsight.navigation.ListBackgroundUpdaterTask) HintUpdateSupply(com.intellij.ui.popup.HintUpdateSupply) UsageView(com.intellij.usages.UsageView) Nullable(org.jetbrains.annotations.Nullable) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) List(java.util.List) FindUtil(com.intellij.find.FindUtil) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) RelativePoint(com.intellij.ui.awt.RelativePoint) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer) Consumer(com.intellij.util.Consumer) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) Transferable(java.awt.datatransfer.Transferable) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) UsageView(com.intellij.usages.UsageView) Ref(com.intellij.openapi.util.Ref) JBList(com.intellij.ui.components.JBList) JBList(com.intellij.ui.components.JBList) List(java.util.List) CollectionListModel(com.intellij.ui.CollectionListModel) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) JBPopup(com.intellij.openapi.ui.popup.JBPopup) Nullable(org.jetbrains.annotations.Nullable) PsiElement(com.intellij.psi.PsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

NavigatablePsiElement (com.intellij.psi.NavigatablePsiElement)11 PsiElement (com.intellij.psi.PsiElement)7 Nullable (org.jetbrains.annotations.Nullable)4 ItemPresentation (com.intellij.navigation.ItemPresentation)3 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)2 CompositeAppearance (com.intellij.openapi.roots.ui.util.CompositeAppearance)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 DartClass (com.jetbrains.lang.dart.psi.DartClass)2 PyClass (com.jetbrains.python.psi.PyClass)2 MouseEvent (java.awt.event.MouseEvent)2 GutterIconNavigationHandler (com.intellij.codeInsight.daemon.GutterIconNavigationHandler)1 LineMarkerInfo (com.intellij.codeInsight.daemon.LineMarkerInfo)1 ListBackgroundUpdaterTask (com.intellij.codeInsight.navigation.ListBackgroundUpdaterTask)1 RefElement (com.intellij.codeInspection.reference.RefElement)1 RefEntity (com.intellij.codeInspection.reference.RefEntity)1 FindUtil (com.intellij.find.FindUtil)1 PsiCopyPasteManager (com.intellij.ide.PsiCopyPasteManager)1 StructureViewTreeElement (com.intellij.ide.structureView.StructureViewTreeElement)1 PsiElementListCellRenderer (com.intellij.ide.util.PsiElementListCellRenderer)1