Search in sources :

Example 16 with Navigatable

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

the class PsiNavigateUtil method navigate.

public static void navigate(@Nullable final PsiElement psiElement) {
    if (psiElement != null && psiElement.isValid()) {
        final PsiElement navigationElement = psiElement.getNavigationElement();
        final int offset = navigationElement instanceof PsiFile ? -1 : navigationElement.getTextOffset();
        VirtualFile virtualFile = PsiUtilCore.getVirtualFile(psiElement);
        if (virtualFile != null && virtualFile.isValid()) {
            new OpenFileDescriptor(navigationElement.getProject(), virtualFile, offset).navigate(true);
        } else if (navigationElement instanceof Navigatable) {
            ((Navigatable) navigationElement).navigate(true);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PsiElement(com.intellij.psi.PsiElement) Navigatable(com.intellij.pom.Navigatable)

Example 17 with Navigatable

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

the class InspectionResultsView method getSelectedNavigatable.

@Nullable
private Navigatable getSelectedNavigatable(final CommonProblemDescriptor descriptor, final PsiElement psiElement) {
    if (descriptor instanceof ProblemDescriptorBase) {
        Navigatable navigatable = ((ProblemDescriptorBase) descriptor).getNavigatable();
        if (navigatable != null) {
            return navigatable;
        }
    }
    if (psiElement == null || !psiElement.isValid())
        return null;
    PsiFile containingFile = psiElement.getContainingFile();
    VirtualFile virtualFile = containingFile == null ? null : containingFile.getVirtualFile();
    if (virtualFile != null) {
        int startOffset = psiElement.getTextOffset();
        if (descriptor instanceof ProblemDescriptorBase) {
            final TextRange textRange = ((ProblemDescriptorBase) descriptor).getTextRangeForNavigation();
            if (textRange != null) {
                if (virtualFile instanceof VirtualFileWindow) {
                    virtualFile = ((VirtualFileWindow) virtualFile).getDelegate();
                }
                startOffset = textRange.getStartOffset();
            }
        }
        return new OpenFileDescriptor(myProject, virtualFile, startOffset);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) TextRange(com.intellij.openapi.util.TextRange) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Navigatable(com.intellij.pom.Navigatable) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with Navigatable

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

the class InspectionResultsViewUtil method getNavigatableForInvalidNode.

@Nullable
static Navigatable getNavigatableForInvalidNode(ProblemDescriptionNode node) {
    RefEntity element = node.getElement();
    while (element != null && !element.isValid()) {
        element = element.getOwner();
    }
    if (!(element instanceof RefElement))
        return null;
    PsiElement containingElement = ((RefElement) element).getElement();
    if (!(containingElement instanceof NavigatablePsiElement) || !containingElement.isValid())
        return null;
    final int lineNumber = node.getLineNumber();
    if (lineNumber != -1) {
        final PsiFile containingFile = containingElement.getContainingFile();
        if (containingFile != null) {
            final VirtualFile file = containingFile.getVirtualFile();
            final Document document = FileDocumentManager.getInstance().getDocument(file);
            if (document != null && document.getLineCount() > lineNumber) {
                return new OpenFileDescriptor(containingElement.getProject(), file, lineNumber, 0);
            }
        }
    }
    return (Navigatable) containingElement;
}
Also used : RefElement(com.intellij.codeInspection.reference.RefElement) VirtualFile(com.intellij.openapi.vfs.VirtualFile) RefEntity(com.intellij.codeInspection.reference.RefEntity) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Document(com.intellij.openapi.editor.Document) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) PsiElement(com.intellij.psi.PsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) Navigatable(com.intellij.pom.Navigatable) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with Navigatable

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

the class ShowUsagesAction method prepareTable.

@NotNull
private Runnable prepareTable(final MyTable table, final Editor editor, final RelativePoint popupPosition, final FindUsagesHandler handler, final int maxUsages, @NotNull final FindUsagesOptions options, final boolean previewMode) {
    SpeedSearchBase<JTable> speedSearch = new MySpeedSearch(table);
    speedSearch.setComparator(new SpeedSearchComparator(false));
    table.setRowHeight(PlatformIcons.CLASS_ICON.getIconHeight() + 2);
    table.setShowGrid(false);
    table.setShowVerticalLines(false);
    table.setShowHorizontalLines(false);
    table.setTableHeader(null);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
    table.setIntercellSpacing(new Dimension(0, 0));
    final AtomicReference<List<Object>> selectedUsages = new AtomicReference<>();
    final AtomicBoolean moreUsagesSelected = new AtomicBoolean();
    final AtomicBoolean outsideScopeUsagesSelected = new AtomicBoolean();
    table.getSelectionModel().addListSelectionListener(e -> {
        selectedUsages.set(null);
        outsideScopeUsagesSelected.set(false);
        moreUsagesSelected.set(false);
        List<Object> usages = null;
        for (int i : table.getSelectedRows()) {
            Object value = table.getValueAt(i, 0);
            if (value instanceof UsageNode) {
                Usage usage = ((UsageNode) value).getUsage();
                if (usage == USAGES_OUTSIDE_SCOPE_SEPARATOR) {
                    outsideScopeUsagesSelected.set(true);
                    usages = null;
                    break;
                } else if (usage == MORE_USAGES_SEPARATOR) {
                    moreUsagesSelected.set(true);
                    usages = null;
                    break;
                } else {
                    if (usages == null)
                        usages = new ArrayList<>();
                    usages.add(usage instanceof UsageInfo2UsageAdapter ? ((UsageInfo2UsageAdapter) usage).getUsageInfo().copy() : usage);
                }
            }
        }
        selectedUsages.set(usages);
    });
    final Runnable itemChosenCallback = () -> {
        if (moreUsagesSelected.get()) {
            appendMoreUsages(editor, popupPosition, handler, maxUsages, options);
            return;
        }
        if (outsideScopeUsagesSelected.get()) {
            options.searchScope = GlobalSearchScope.projectScope(handler.getProject());
            showElementUsages(editor, popupPosition, handler, maxUsages, options);
            return;
        }
        List<Object> usages = selectedUsages.get();
        if (usages != null) {
            for (Object usage : usages) {
                if (usage instanceof UsageInfo) {
                    UsageViewUtil.navigateTo((UsageInfo) usage, true);
                } else if (usage instanceof Navigatable) {
                    ((Navigatable) usage).navigate(true);
                }
            }
        }
    };
    if (previewMode) {
        table.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
                if (UIUtil.isActionClick(e, MouseEvent.MOUSE_RELEASED) && !UIUtil.isSelectionButtonDown(e) && !e.isConsumed()) {
                    itemChosenCallback.run();
                }
            }
        });
        table.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    itemChosenCallback.run();
                }
            }
        });
    }
    return itemChosenCallback;
}
Also used : MouseEvent(java.awt.event.MouseEvent) KeyAdapter(java.awt.event.KeyAdapter) MouseAdapter(java.awt.event.MouseAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) RelativePoint(com.intellij.ui.awt.RelativePoint) Navigatable(com.intellij.pom.Navigatable) KeyEvent(java.awt.event.KeyEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) List(java.util.List) UsageInfo(com.intellij.usageView.UsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with Navigatable

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

the class GotoClassAction method gotoActionPerformed.

@Override
public void gotoActionPerformed(@NotNull AnActionEvent e) {
    final Project project = e.getProject();
    if (project == null)
        return;
    FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.class");
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final GotoClassModel2 model = new GotoClassModel2(project);
    showNavigationPopup(e, model, new GotoActionCallback<Language>() {

        @Override
        protected ChooseByNameFilter<Language> createFilter(@NotNull ChooseByNamePopup popup) {
            return new ChooseByNameLanguageFilter(popup, model, GotoClassSymbolConfiguration.getInstance(project), project);
        }

        @Override
        public void elementChosen(ChooseByNamePopup popup, Object element) {
            ApplicationManager.getApplication().runReadAction(() -> {
                if (element instanceof PsiElement && ((PsiElement) element).isValid()) {
                    PsiElement psiElement = getElement(((PsiElement) element), popup);
                    psiElement = psiElement.getNavigationElement();
                    VirtualFile file = PsiUtilCore.getVirtualFile(psiElement);
                    if (file != null && popup.getLinePosition() != -1) {
                        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, popup.getLinePosition(), popup.getColumnPosition());
                        Navigatable n = descriptor.setUseCurrentWindow(popup.isOpenInCurrentWindowRequested());
                        if (n.canNavigate()) {
                            n.navigate(true);
                            return;
                        }
                    }
                    if (file != null && popup.getMemberPattern() != null) {
                        NavigationUtil.activateFileWithPsiElement(psiElement, !popup.isOpenInCurrentWindowRequested());
                        Navigatable member = findMember(popup.getMemberPattern(), psiElement, file);
                        if (member != null) {
                            member.navigate(true);
                        }
                    }
                    NavigationUtil.activateFileWithPsiElement(psiElement, !popup.isOpenInCurrentWindowRequested());
                } else {
                    EditSourceUtil.navigate(((NavigationItem) element), true, popup.isOpenInCurrentWindowRequested());
                }
            });
        }
    }, IdeBundle.message("go.to.class.toolwindow.title"), true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Navigatable(com.intellij.pom.Navigatable) Project(com.intellij.openapi.project.Project) Language(com.intellij.lang.Language) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PsiElement(com.intellij.psi.PsiElement)

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