Search in sources :

Example 56 with Navigatable

use of com.intellij.pom.Navigatable in project intellij-community by JetBrains.

the class InspectionResultsView method getData.

@Override
public Object getData(String dataId) {
    if (PlatformDataKeys.HELP_ID.is(dataId))
        return HELP_ID;
    if (DATA_KEY.is(dataId))
        return this;
    if (ExclusionHandler.EXCLUSION_HANDLER.is(dataId))
        return myExclusionHandler;
    if (myTree == null)
        return null;
    TreePath[] paths = myTree.getSelectionPaths();
    if (paths == null || paths.length == 0)
        return null;
    if (paths.length > 1) {
        if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) {
            return collectPsiElements();
        }
        return null;
    }
    TreePath path = paths[0];
    InspectionTreeNode selectedNode = (InspectionTreeNode) path.getLastPathComponent();
    if (!CommonDataKeys.NAVIGATABLE.is(dataId) && !CommonDataKeys.PSI_ELEMENT.is(dataId)) {
        return null;
    }
    if (selectedNode instanceof RefElementNode) {
        final RefElementNode refElementNode = (RefElementNode) selectedNode;
        RefEntity refElement = refElementNode.getElement();
        if (refElement == null)
            return null;
        final RefEntity item = refElement.getRefManager().getRefinedElement(refElement);
        if (!item.isValid())
            return null;
        PsiElement psiElement = item instanceof RefElement ? ((RefElement) item).getElement() : null;
        if (psiElement == null)
            return null;
        final CommonProblemDescriptor problem = refElementNode.getDescriptor();
        if (problem instanceof ProblemDescriptor) {
            PsiElement elementFromDescriptor = ((ProblemDescriptor) problem).getPsiElement();
            if (elementFromDescriptor == null && CommonDataKeys.NAVIGATABLE.is(dataId)) {
                final InspectionTreeNode node = (InspectionTreeNode) refElementNode.getChildAt(0);
                if (node.isValid()) {
                    return InspectionResultsViewUtil.getNavigatableForInvalidNode((ProblemDescriptionNode) node);
                }
            } else {
                psiElement = elementFromDescriptor;
            }
        }
        if (CommonDataKeys.NAVIGATABLE.is(dataId)) {
            return getSelectedNavigatable(problem, psiElement);
        } else if (CommonDataKeys.PSI_ELEMENT.is(dataId)) {
            return psiElement != null && psiElement.isValid() ? psiElement : null;
        }
    } else if (selectedNode instanceof ProblemDescriptionNode && CommonDataKeys.NAVIGATABLE.is(dataId)) {
        Navigatable navigatable = getSelectedNavigatable(((ProblemDescriptionNode) selectedNode).getDescriptor());
        return navigatable == null ? InspectionResultsViewUtil.getNavigatableForInvalidNode((ProblemDescriptionNode) selectedNode) : navigatable;
    }
    return null;
}
Also used : RefElement(com.intellij.codeInspection.reference.RefElement) TreePath(javax.swing.tree.TreePath) RefEntity(com.intellij.codeInspection.reference.RefEntity) Navigatable(com.intellij.pom.Navigatable)

Example 57 with Navigatable

use of com.intellij.pom.Navigatable in project intellij-community by JetBrains.

the class CallHierarchyNodeDescriptor method navigate.

public void navigate(boolean requestFocus) {
    if (!myNavigateToReference) {
        PsiElement element = getPsiElement();
        if (element instanceof Navigatable && ((Navigatable) element).canNavigate()) {
            ((Navigatable) element).navigate(requestFocus);
        }
        return;
    }
    final PsiReference firstReference = myReferences.get(0);
    final PsiElement element = firstReference.getElement();
    if (element == null)
        return;
    final PsiElement callElement = element.getParent();
    if (callElement instanceof Navigatable && ((Navigatable) callElement).canNavigate()) {
        ((Navigatable) callElement).navigate(requestFocus);
    } else {
        final PsiFile psiFile = callElement.getContainingFile();
        if (psiFile == null || psiFile.getVirtualFile() == null)
            return;
        FileEditorManager.getInstance(myProject).openFile(psiFile.getVirtualFile(), requestFocus);
    }
    Editor editor = PsiUtilBase.findEditor(callElement);
    if (editor != null) {
        HighlightManager highlightManager = HighlightManager.getInstance(myProject);
        EditorColorsManager colorManager = EditorColorsManager.getInstance();
        TextAttributes attributes = colorManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
        ArrayList<RangeHighlighter> highlighters = new ArrayList<>();
        for (PsiReference psiReference : myReferences) {
            final PsiElement eachElement = psiReference.getElement();
            if (eachElement != null) {
                final PsiElement eachMethodCall = eachElement.getParent();
                if (eachMethodCall != null) {
                    final TextRange textRange = eachMethodCall.getTextRange();
                    highlightManager.addRangeHighlight(editor, textRange.getStartOffset(), textRange.getEndOffset(), attributes, false, highlighters);
                }
            }
        }
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) ArrayList(java.util.ArrayList) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) TextRange(com.intellij.openapi.util.TextRange) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) Editor(com.intellij.openapi.editor.Editor) Navigatable(com.intellij.pom.Navigatable)

Example 58 with Navigatable

use of com.intellij.pom.Navigatable in project intellij-community by JetBrains.

the class CtrlMouseHandler method getInfoAt.

@Nullable
private static Info getInfoAt(@NotNull Project project, @NotNull final Editor editor, @NotNull PsiFile file, int offset, @NotNull BrowseMode browseMode) {
    PsiElement targetElement = null;
    if (browseMode == BrowseMode.TypeDeclaration) {
        try {
            targetElement = GotoTypeDeclarationAction.findSymbolType(editor, offset);
        } catch (IndexNotReadyException e) {
            showDumbModeNotification(project);
        }
    } else if (browseMode == BrowseMode.Declaration) {
        final PsiReference ref = TargetElementUtil.findReference(editor, offset);
        final List<PsiElement> resolvedElements = ref == null ? Collections.emptyList() : resolve(ref);
        final PsiElement resolvedElement = resolvedElements.size() == 1 ? resolvedElements.get(0) : null;
        final PsiElement[] targetElements = GotoDeclarationAction.findTargetElementsNoVS(project, editor, offset, false);
        final PsiElement elementAtPointer = file.findElementAt(TargetElementUtil.adjustOffset(file, editor.getDocument(), offset));
        if (targetElements != null) {
            if (targetElements.length == 0) {
                return null;
            } else if (targetElements.length == 1) {
                if (targetElements[0] != resolvedElement && elementAtPointer != null && targetElements[0].isPhysical()) {
                    return ref != null ? new InfoSingle(ref, targetElements[0]) : new InfoSingle(elementAtPointer, targetElements[0]);
                }
            } else {
                return elementAtPointer != null ? new InfoMultiple(elementAtPointer) : null;
            }
        }
        if (resolvedElements.size() == 1) {
            return new InfoSingle(ref, resolvedElements.get(0));
        }
        if (resolvedElements.size() > 1) {
            return elementAtPointer != null ? new InfoMultiple(elementAtPointer, ref) : null;
        }
    } else if (browseMode == BrowseMode.Implementation) {
        final PsiElement element = TargetElementUtil.getInstance().findTargetElement(editor, ImplementationSearcher.getFlags(), offset);
        PsiElement[] targetElements = new ImplementationSearcher() {

            @Override
            @NotNull
            protected PsiElement[] searchDefinitions(final PsiElement element, Editor editor) {
                final List<PsiElement> found = new ArrayList<>(2);
                DefinitionsScopedSearch.search(element, getSearchScope(element, editor)).forEach(psiElement -> {
                    found.add(psiElement);
                    return found.size() != 2;
                });
                return PsiUtilCore.toPsiElementArray(found);
            }
        }.searchImplementations(editor, element, offset);
        if (targetElements == null) {
            return null;
        }
        if (targetElements.length > 1) {
            PsiElement elementAtPointer = file.findElementAt(offset);
            if (elementAtPointer != null) {
                return new InfoMultiple(elementAtPointer);
            }
            return null;
        }
        if (targetElements.length == 1) {
            Navigatable descriptor = EditSourceUtil.getDescriptor(targetElements[0]);
            if (descriptor == null || !descriptor.canNavigate()) {
                return null;
            }
            targetElement = targetElements[0];
        }
    }
    if (targetElement != null && targetElement.isPhysical()) {
        PsiElement elementAtPointer = file.findElementAt(offset);
        if (elementAtPointer != null) {
            return new InfoSingle(elementAtPointer, targetElement);
        }
    }
    final PsiElement element = GotoDeclarationAction.findElementToShowUsagesOf(editor, offset);
    if (element instanceof PsiNameIdentifierOwner) {
        PsiElement identifier = ((PsiNameIdentifierOwner) element).getNameIdentifier();
        if (identifier != null && identifier.isValid()) {
            return new Info(identifier) {

                @Override
                public void showDocInfo(@NotNull DocumentationManager docManager) {
                }

                @NotNull
                @Override
                public DocInfo getInfo() {
                    String name = UsageViewUtil.getType(element) + " '" + UsageViewUtil.getShortName(element) + "'";
                    return new DocInfo("Show usages of " + name, null, element);
                }

                @Override
                public boolean isValid(@NotNull Document document) {
                    return element.isValid();
                }

                @Override
                public boolean isNavigatable() {
                    return true;
                }
            };
        }
    }
    return null;
}
Also used : DocumentationManager(com.intellij.codeInsight.documentation.DocumentationManager) UIUtil(com.intellij.util.ui.UIUtil) PresentationFactory(com.intellij.openapi.actionSystem.impl.PresentationFactory) ReadTask(com.intellij.openapi.progress.util.ReadTask) HyperlinkEvent(javax.swing.event.HyperlinkEvent) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) UsageViewShortNameLocation(com.intellij.usageView.UsageViewShortNameLocation) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) DefinitionsScopedSearch(com.intellij.psi.search.searches.DefinitionsScopedSearch) Keymap(com.intellij.openapi.keymap.Keymap) GotoDeclarationAction(com.intellij.codeInsight.navigation.actions.GotoDeclarationAction) IdeActions(com.intellij.openapi.actionSystem.IdeActions) EventObject(java.util.EventObject) FileEditorManagerEvent(com.intellij.openapi.fileEditor.FileEditorManagerEvent) UsageViewTypeLocation(com.intellij.usageView.UsageViewTypeLocation) DumbAwareRunnable(com.intellij.openapi.project.DumbAwareRunnable) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) Logger(com.intellij.openapi.diagnostic.Logger) TIntArrayList(gnu.trove.TIntArrayList) HintUtil(com.intellij.codeInsight.hint.HintUtil) DumbService(com.intellij.openapi.project.DumbService) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) ActionButton(com.intellij.openapi.actionSystem.impl.ActionButton) ScreenUtil(com.intellij.ui.ScreenUtil) DocumentationManagerProtocol(com.intellij.codeInsight.documentation.DocumentationManagerProtocol) TextRange(com.intellij.openapi.util.TextRange) com.intellij.openapi.editor.event(com.intellij.openapi.editor.event) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) KeymapUtil(com.intellij.openapi.keymap.KeymapUtil) HighlighterTargetArea(com.intellij.openapi.editor.markup.HighlighterTargetArea) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) JBLayeredPane(com.intellij.ui.components.JBLayeredPane) java.awt.event(java.awt.event) EditSourceUtil(com.intellij.ide.util.EditSourceUtil) EditorFactory(com.intellij.openapi.editor.EditorFactory) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) HintListener(com.intellij.ui.HintListener) LightweightHint(com.intellij.ui.LightweightHint) Consumer(com.intellij.util.Consumer) NavigationItem(com.intellij.navigation.NavigationItem) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) IdeTooltipManager(com.intellij.ide.IdeTooltipManager) KeymapManager(com.intellij.openapi.keymap.KeymapManager) ProgressIndicatorUtils(com.intellij.openapi.progress.util.ProgressIndicatorUtils) ArrayList(java.util.ArrayList) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) ItemPresentation(com.intellij.navigation.ItemPresentation) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) Comparing(com.intellij.openapi.util.Comparing) StartupManager(com.intellij.openapi.startup.StartupManager) HighlighterLayer(com.intellij.openapi.editor.markup.HighlighterLayer) Project(com.intellij.openapi.project.Project) InjectedLanguageUtil(com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil) JdkConstants(org.intellij.lang.annotations.JdkConstants) DocumentationProvider(com.intellij.lang.documentation.DocumentationProvider) HyperlinkListener(javax.swing.event.HyperlinkListener) FileEditorManagerListener(com.intellij.openapi.fileEditor.FileEditorManagerListener) StringUtil(com.intellij.openapi.util.text.StringUtil) Editor(com.intellij.openapi.editor.Editor) UsageViewUtil(com.intellij.usageView.UsageViewUtil) AbstractProjectComponent(com.intellij.openapi.components.AbstractProjectComponent) TestOnly(org.jetbrains.annotations.TestOnly) java.awt(java.awt) EditorColors(com.intellij.openapi.editor.colors.EditorColors) HintManagerImpl(com.intellij.codeInsight.hint.HintManagerImpl) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) GotoTypeDeclarationAction(com.intellij.codeInsight.navigation.actions.GotoTypeDeclarationAction) Navigatable(com.intellij.pom.Navigatable) HintManager(com.intellij.codeInsight.hint.HintManager) Collections(java.util.Collections) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull) Navigatable(com.intellij.pom.Navigatable) DocumentationManager(com.intellij.codeInsight.documentation.DocumentationManager) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) TIntArrayList(gnu.trove.TIntArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable)

Example 59 with Navigatable

use of com.intellij.pom.Navigatable in project intellij-community by JetBrains.

the class BaseRemoteFileEditor method contentLoaded.

protected final void contentLoaded() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    Navigatable navigatable = myPendingNavigatable;
    if (navigatable != null) {
        myPendingNavigatable = null;
        TextEditor editor = getTextEditor();
        assert editor != null;
        editor.navigateTo(navigatable);
    }
    if (myMockTextEditor != null) {
        if (!myMockTextEditor.isDisposed()) {
            EditorFactory.getInstance().releaseEditor(myMockTextEditor);
        }
        myMockTextEditor = null;
    }
}
Also used : TextEditor(com.intellij.openapi.fileEditor.TextEditor) Navigatable(com.intellij.pom.Navigatable)

Example 60 with Navigatable

use of com.intellij.pom.Navigatable in project intellij-community by JetBrains.

the class UsageViewImpl method initTree.

private void initTree() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    myTree.setShowsRootHandles(true);
    SmartExpander.installOn(myTree);
    TreeUtil.installActions(myTree);
    EditSourceOnDoubleClickHandler.install(myTree);
    myTree.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                TreePath leadSelectionPath = myTree.getLeadSelectionPath();
                if (leadSelectionPath == null)
                    return;
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) leadSelectionPath.getLastPathComponent();
                if (node instanceof UsageNode) {
                    final Usage usage = ((UsageNode) node).getUsage();
                    usage.navigate(false);
                    usage.highlightInEditor();
                } else if (node.isLeaf()) {
                    Navigatable navigatable = getNavigatableForNode(node);
                    if (navigatable != null && navigatable.canNavigate()) {
                        navigatable.navigate(false);
                    }
                }
            }
        }
    });
    TreeUtil.selectFirstNode(myTree);
    PopupHandler.installPopupHandler(myTree, IdeActions.GROUP_USAGE_VIEW_POPUP, ActionPlaces.USAGE_VIEW_POPUP);
    myTree.addTreeExpansionListener(new TreeExpansionListener() {

        @Override
        public void treeExpanded(TreeExpansionEvent event) {
            clearRendererCache();
            TreePath path = event.getPath();
            Object component = path.getLastPathComponent();
            if (component instanceof Node) {
                Node node = (Node) component;
                if (!expandingAll && node.needsUpdate()) {
                    checkNodeValidity(node, path);
                }
            }
        }

        @Override
        public void treeCollapsed(TreeExpansionEvent event) {
            clearRendererCache();
        }
    });
    TreeUIHelper.getInstance().installTreeSpeedSearch(myTree, o -> {
        Object value = o.getLastPathComponent();
        TreeCellRenderer renderer = myTree.getCellRenderer();
        if (renderer instanceof UsageViewTreeCellRenderer) {
            UsageViewTreeCellRenderer coloredRenderer = (UsageViewTreeCellRenderer) renderer;
            return coloredRenderer.getPlainTextForNode(value);
        }
        return value == null ? null : value.toString();
    }, true);
}
Also used : TreeExpansionListener(javax.swing.event.TreeExpansionListener) Navigatable(com.intellij.pom.Navigatable) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Aggregations

Navigatable (com.intellij.pom.Navigatable)70 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)19 Project (com.intellij.openapi.project.Project)14 Nullable (org.jetbrains.annotations.Nullable)14 PsiElement (com.intellij.psi.PsiElement)11 ArrayList (java.util.ArrayList)9 PsiFile (com.intellij.psi.PsiFile)7 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)7 TreePath (javax.swing.tree.TreePath)7 NotNull (org.jetbrains.annotations.NotNull)6 Editor (com.intellij.openapi.editor.Editor)4 List (java.util.List)4 TextRange (com.intellij.openapi.util.TextRange)3 XmlTag (com.intellij.psi.xml.XmlTag)3 BlazeConsoleView (com.google.idea.blaze.base.console.BlazeConsoleView)2 IssueOutput (com.google.idea.blaze.base.scope.output.IssueOutput)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Document (com.intellij.openapi.editor.Document)2