Search in sources :

Example 6 with GherkinFile

use of org.jetbrains.plugins.cucumber.psi.GherkinFile in project intellij-plugins by JetBrains.

the class CucumberCreateAllStepsFix method createStepOrSteps.

@Override
protected void createStepOrSteps(GherkinStep sourceStep, @Nullable final Pair<PsiFile, BDDFrameworkType> fileAndFrameworkType) {
    final PsiFile probableGherkinFile = sourceStep.getContainingFile();
    if (!(probableGherkinFile instanceof GherkinFile)) {
        return;
    }
    final Set<String> createdStepDefPatterns = new HashSet<>();
    final GherkinFile gherkinFile = (GherkinFile) probableGherkinFile;
    for (GherkinFeature feature : gherkinFile.getFeatures()) {
        for (GherkinStepsHolder stepsHolder : feature.getScenarios()) {
            for (GherkinStep step : stepsHolder.getSteps()) {
                final PsiReference[] references = step.getReferences();
                for (PsiReference reference : references) {
                    if (!(reference instanceof CucumberStepReference))
                        continue;
                    final AbstractStepDefinition definition = ((CucumberStepReference) reference).resolveToDefinition();
                    if (definition == null) {
                        String pattern = Pattern.quote(step.getStepName());
                        pattern = StringUtil.trimEnd(StringUtil.trimStart(pattern, "\\Q"), "\\E");
                        pattern = CucumberUtil.prepareStepRegexp(pattern);
                        if (!createdStepDefPatterns.contains(pattern)) {
                            createFileOrStepDefinition(step, fileAndFrameworkType);
                            createdStepDefPatterns.add(pattern);
                        }
                    }
                }
            }
        }
    }
}
Also used : GherkinStep(org.jetbrains.plugins.cucumber.psi.GherkinStep) CucumberStepReference(org.jetbrains.plugins.cucumber.steps.reference.CucumberStepReference) AbstractStepDefinition(org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition) GherkinStepsHolder(org.jetbrains.plugins.cucumber.psi.GherkinStepsHolder) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile) GherkinFile(org.jetbrains.plugins.cucumber.psi.GherkinFile) HashSet(java.util.HashSet) GherkinFeature(org.jetbrains.plugins.cucumber.psi.GherkinFeature)

Example 7 with GherkinFile

use of org.jetbrains.plugins.cucumber.psi.GherkinFile 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

GherkinFile (org.jetbrains.plugins.cucumber.psi.GherkinFile)7 PsiFile (com.intellij.psi.PsiFile)4 GherkinStep (org.jetbrains.plugins.cucumber.psi.GherkinStep)4 PsiElement (com.intellij.psi.PsiElement)3 GherkinStepsHolder (org.jetbrains.plugins.cucumber.psi.GherkinStepsHolder)3 NotNull (org.jetbrains.annotations.NotNull)2 GherkinFeature (org.jetbrains.plugins.cucumber.psi.GherkinFeature)2 AbstractStepDefinition (org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition)2 CucumberStepsIndex (org.jetbrains.plugins.cucumber.steps.CucumberStepsIndex)2 GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)1 Document (com.intellij.openapi.editor.Document)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 NullableComputable (com.intellij.openapi.util.NullableComputable)1 Pair (com.intellij.openapi.util.Pair)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)1