Search in sources :

Example 6 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project intellij-plugins by JetBrains.

the class CucumberGoToRelatedProvider method getItems.

@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement psiElement) {
    final PsiFile file = psiElement.getContainingFile();
    if (file instanceof GherkinFile) {
        final List<GherkinStep> steps = new ArrayList<>();
        final GherkinFile gherkinFile = (GherkinFile) file;
        final GherkinFeature[] features = gherkinFile.getFeatures();
        for (GherkinFeature feature : features) {
            final GherkinStepsHolder[] stepHolders = feature.getScenarios();
            for (GherkinStepsHolder stepHolder : stepHolders) {
                Collections.addAll(steps, stepHolder.getSteps());
            }
        }
        final CucumberStepsIndex index = CucumberStepsIndex.getInstance(file.getProject());
        final List<PsiFile> resultFiles = new ArrayList<>();
        final List<GotoRelatedItem> result = new ArrayList<>();
        for (GherkinStep step : steps) {
            AbstractStepDefinition stepDef = index.findStepDefinition(gherkinFile, step);
            final PsiElement stepDefMethod = stepDef != null ? stepDef.getElement() : null;
            if (stepDefMethod != null) {
                final PsiFile stepDefFile = stepDefMethod.getContainingFile();
                if (!resultFiles.contains(stepDefFile)) {
                    resultFiles.add(stepDefFile);
                    result.add(new GotoRelatedItem(stepDefFile, "Step definition file"));
                }
            }
        }
        return result;
    }
    return Collections.emptyList();
}
Also used : ArrayList(java.util.ArrayList) GherkinStep(org.jetbrains.plugins.cucumber.psi.GherkinStep) CucumberStepsIndex(org.jetbrains.plugins.cucumber.steps.CucumberStepsIndex) AbstractStepDefinition(org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition) GherkinStepsHolder(org.jetbrains.plugins.cucumber.psi.GherkinStepsHolder) PsiFile(com.intellij.psi.PsiFile) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) GherkinFile(org.jetbrains.plugins.cucumber.psi.GherkinFile) PsiElement(com.intellij.psi.PsiElement) GherkinFeature(org.jetbrains.plugins.cucumber.psi.GherkinFeature) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project intellij-plugins by JetBrains.

the class CucumberGotoRelatedFileTest method testGotoRelated.

public void testGotoRelated() {
    CucumberStepsIndex.getInstance(getProject()).reset();
    myFixture.copyDirectoryToProject("gotoRelated", "");
    myFixture.configureByFile("gotoRelated/test.feature");
    List<GotoRelatedItem> items = GotoRelatedSymbolAction.getItems(myFixture.getFile(), myFixture.getEditor(), null);
    assertEquals(1, items.size());
    PsiElement gotoElement = items.get(0).getElement();
    assertTrue(gotoElement instanceof PsiJavaFile);
    assertEquals("ShoppingStepdefs.java", ((PsiJavaFile) gotoElement).getName());
}
Also used : PsiJavaFile(com.intellij.psi.PsiJavaFile) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) PsiElement(com.intellij.psi.PsiElement)

Example 8 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project intellij-community by JetBrains.

the class GotoRelatedSymbolAction method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    final DataContext dataContext = e.getDataContext();
    final PsiElement element = getContextElement(dataContext);
    if (element == null)
        return;
    List<GotoRelatedItem> items = NavigationUtil.collectRelatedItems(element, dataContext);
    if (items.isEmpty()) {
        final JComponent label = HintUtil.createErrorLabel("No related symbols");
        label.setBorder(IdeBorderFactory.createEmptyBorder(2, 7, 2, 7));
        JBPopupFactory.getInstance().createBalloonBuilder(label).setFadeoutTime(3000).setFillColor(HintUtil.getErrorColor()).createBalloon().show(JBPopupFactory.getInstance().guessBestPopupLocation(dataContext), Balloon.Position.above);
        return;
    }
    if (items.size() == 1 && items.get(0).getElement() != null) {
        items.get(0).navigate();
        return;
    }
    NavigationUtil.getRelatedItemsPopup(items, "Choose Target").showInBestPositionFor(dataContext);
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) PsiElement(com.intellij.psi.PsiElement)

Example 9 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project intellij-community by JetBrains.

the class RelatedItemLineMarkerGotoAdapter method getItems.

@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement context) {
    List<PsiElement> parents = new ArrayList<>();
    PsiElement current = context;
    Set<Language> languages = new HashSet<>();
    while (current != null) {
        parents.add(current);
        languages.add(current.getLanguage());
        if (current instanceof PsiFile)
            break;
        current = current.getParent();
    }
    List<LineMarkerProvider> providers = new ArrayList<>();
    for (Language language : languages) {
        providers.addAll(LineMarkersPass.getMarkerProviders(language, context.getProject()));
    }
    List<GotoRelatedItem> items = new ArrayList<>();
    for (LineMarkerProvider provider : providers) {
        if (provider instanceof RelatedItemLineMarkerProvider) {
            List<RelatedItemLineMarkerInfo> markers = new ArrayList<>();
            RelatedItemLineMarkerProvider relatedItemLineMarkerProvider = (RelatedItemLineMarkerProvider) provider;
            for (PsiElement parent : parents) {
                ContainerUtil.addIfNotNull(markers, relatedItemLineMarkerProvider.getLineMarkerInfo(parent));
            }
            relatedItemLineMarkerProvider.collectNavigationMarkers(parents, markers, true);
            addItemsForMarkers(markers, items);
        }
    }
    return items;
}
Also used : RelatedItemLineMarkerProvider(com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider) LineMarkerProvider(com.intellij.codeInsight.daemon.LineMarkerProvider) RelatedItemLineMarkerInfo(com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo) Language(com.intellij.lang.Language) RelatedItemLineMarkerProvider(com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider) PsiFile(com.intellij.psi.PsiFile) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project intellij-community by JetBrains.

the class NavigationUtil method getRelatedItemsPopup.

/**
   * Returns navigation popup that shows list of related items from {@code items} list
   * @param items
   * @param title
   * @param showContainingModules Whether the popup should show additional information that aligned at the right side of the dialog.<br>
   *                              It's usually a module name or library name of corresponding navigation item.<br>
   *                              {@code false} by default
   * @return
   */
@NotNull
public static JBPopup getRelatedItemsPopup(final List<? extends GotoRelatedItem> items, String title, boolean showContainingModules) {
    Object[] elements = new Object[items.size()];
    //todo[nik] move presentation logic to GotoRelatedItem class
    final Map<PsiElement, GotoRelatedItem> itemsMap = new HashMap<>();
    for (int i = 0; i < items.size(); i++) {
        GotoRelatedItem item = items.get(i);
        elements[i] = item.getElement() != null ? item.getElement() : item;
        itemsMap.put(item.getElement(), item);
    }
    return getPsiElementPopup(elements, itemsMap, title, showContainingModules, element -> {
        if (element instanceof PsiElement) {
            itemsMap.get(element).navigate();
        } else {
            ((GotoRelatedItem) element).navigate();
        }
        return true;
    });
}
Also used : GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)21 PsiElement (com.intellij.psi.PsiElement)13 NotNull (org.jetbrains.annotations.NotNull)11 PsiFile (com.intellij.psi.PsiFile)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 ArrayList (java.util.ArrayList)3 ResourceBundle (com.intellij.lang.properties.ResourceBundle)2 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)2 GotoRelatedProvider (com.intellij.navigation.GotoRelatedProvider)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 FileEditor (com.intellij.openapi.fileEditor.FileEditor)2 Project (com.intellij.openapi.project.Project)2 Computable (com.intellij.openapi.util.Computable)2 PsiClass (com.intellij.psi.PsiClass)2 LineMarkerProvider (com.intellij.codeInsight.daemon.LineMarkerProvider)1 RelatedItemLineMarkerInfo (com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo)1 RelatedItemLineMarkerProvider (com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider)1 DomGotoRelatedItem (com.intellij.codeInsight.navigation.DomGotoRelatedItem)1 Location (com.intellij.execution.Location)1 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)1