Search in sources :

Example 1 with CucumberStepsIndex

use of org.jetbrains.plugins.cucumber.steps.CucumberStepsIndex in project intellij-plugins by JetBrains.

the class CucumberCreateStepFixBase method createStepDefinition.

private static void createStepDefinition(GherkinStep step, @NotNull final Pair<PsiFile, BDDFrameworkType> fileAndFrameworkType) {
    CucumberStepsIndex stepsIndex = CucumberStepsIndex.getInstance(step.getProject());
    StepDefinitionCreator stepDefCreator = stepsIndex.getExtensionMap().get(fileAndFrameworkType.getSecond()).getStepDefinitionCreator();
    PsiFile file = fileAndFrameworkType.first;
    WriteCommandAction.runWriteCommandAction(step.getProject(), null, null, () -> stepDefCreator.createStepDefinition(step, file), file);
}
Also used : PsiFile(com.intellij.psi.PsiFile) CucumberStepsIndex(org.jetbrains.plugins.cucumber.steps.CucumberStepsIndex)

Example 2 with CucumberStepsIndex

use of org.jetbrains.plugins.cucumber.steps.CucumberStepsIndex in project intellij-plugins by JetBrains.

the class CucumberCreateStepFixBase method getStepDefinitionContainers.

public static Set<Pair<PsiFile, BDDFrameworkType>> getStepDefinitionContainers(@NotNull final GherkinFile featureFile) {
    final Set<Pair<PsiFile, BDDFrameworkType>> result = CucumberStepsIndex.getInstance(featureFile.getProject()).getStepDefinitionContainers(featureFile);
    CucumberStepsIndex stepsIndex = CucumberStepsIndex.getInstance(featureFile.getProject());
    for (Pair<PsiFile, BDDFrameworkType> item : result) {
        if (stepsIndex.getExtensionMap().get(item.getSecond()) == null) {
            result.remove(item);
        }
    }
    return result;
}
Also used : PsiFile(com.intellij.psi.PsiFile) Pair(com.intellij.openapi.util.Pair) CucumberStepsIndex(org.jetbrains.plugins.cucumber.steps.CucumberStepsIndex)

Example 3 with CucumberStepsIndex

use of org.jetbrains.plugins.cucumber.steps.CucumberStepsIndex 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 4 with CucumberStepsIndex

use of org.jetbrains.plugins.cucumber.steps.CucumberStepsIndex in project intellij-plugins by JetBrains.

the class CucumberCreateStepFixBase method applyFix.

public void applyFix(@NotNull final Project project, @NotNull ProblemDescriptor descriptor) {
    final GherkinStep step = (GherkinStep) descriptor.getPsiElement();
    final GherkinFile featureFile = (GherkinFile) step.getContainingFile();
    // TODO + step defs pairs from other content roots
    final List<Pair<PsiFile, BDDFrameworkType>> pairs = ContainerUtil.newArrayList(getStepDefinitionContainers(featureFile));
    if (!pairs.isEmpty()) {
        pairs.add(0, null);
        final JBPopupFactory popupFactory = JBPopupFactory.getInstance();
        final ListPopup popupStep = popupFactory.createListPopup(new BaseListPopupStep<Pair<PsiFile, BDDFrameworkType>>(CucumberBundle.message("choose.step.definition.file"), ContainerUtil.newArrayList(pairs)) {

            @Override
            public boolean isSpeedSearchEnabled() {
                return true;
            }

            @NotNull
            @Override
            public String getTextFor(Pair<PsiFile, BDDFrameworkType> value) {
                if (value == null) {
                    return CucumberBundle.message("create.new.file");
                }
                final VirtualFile file = value.getFirst().getVirtualFile();
                assert file != null;
                CucumberStepsIndex stepsIndex = CucumberStepsIndex.getInstance(value.getFirst().getProject());
                StepDefinitionCreator stepDefinitionCreator = stepsIndex.getExtensionMap().get(value.getSecond()).getStepDefinitionCreator();
                return stepDefinitionCreator.getStepDefinitionFilePath(value.getFirst());
            }

            @Override
            public Icon getIconFor(Pair<PsiFile, BDDFrameworkType> value) {
                return value == null ? AllIcons.Actions.CreateFromUsage : value.getFirst().getIcon(0);
            }

            @Override
            public PopupStep onChosen(final Pair<PsiFile, BDDFrameworkType> selectedValue, boolean finalChoice) {
                return doFinalStep(() -> createStepOrSteps(step, selectedValue));
            }
        });
        if (!ApplicationManager.getApplication().isUnitTestMode()) {
            popupStep.showCenteredInCurrentWindow(step.getProject());
        } else {
            createStepOrSteps(step, pairs.get(1));
        }
    } else {
        createFileOrStepDefinition(step, null);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GherkinStep(org.jetbrains.plugins.cucumber.psi.GherkinStep) ListPopup(com.intellij.openapi.ui.popup.ListPopup) NotNull(org.jetbrains.annotations.NotNull) CucumberStepsIndex(org.jetbrains.plugins.cucumber.steps.CucumberStepsIndex) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) PopupStep(com.intellij.openapi.ui.popup.PopupStep) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) PsiFile(com.intellij.psi.PsiFile) GherkinFile(org.jetbrains.plugins.cucumber.psi.GherkinFile) Pair(com.intellij.openapi.util.Pair)

Aggregations

PsiFile (com.intellij.psi.PsiFile)4 CucumberStepsIndex (org.jetbrains.plugins.cucumber.steps.CucumberStepsIndex)4 Pair (com.intellij.openapi.util.Pair)2 NotNull (org.jetbrains.annotations.NotNull)2 GherkinFile (org.jetbrains.plugins.cucumber.psi.GherkinFile)2 GherkinStep (org.jetbrains.plugins.cucumber.psi.GherkinStep)2 GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)1 JBPopupFactory (com.intellij.openapi.ui.popup.JBPopupFactory)1 ListPopup (com.intellij.openapi.ui.popup.ListPopup)1 PopupStep (com.intellij.openapi.ui.popup.PopupStep)1 BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 ArrayList (java.util.ArrayList)1 GherkinFeature (org.jetbrains.plugins.cucumber.psi.GherkinFeature)1 GherkinStepsHolder (org.jetbrains.plugins.cucumber.psi.GherkinStepsHolder)1 AbstractStepDefinition (org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition)1