Search in sources :

Example 6 with GherkinStep

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

the class GherkinStepRenameHandler method getGherkinStep.

@Nullable
public GherkinStep getGherkinStep(@Nullable final DataContext context) {
    PsiElement element = null;
    if (context == null)
        return null;
    final Editor editor = CommonDataKeys.EDITOR.getData(context);
    if (editor != null) {
        final PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(context);
        if (psiFile != null) {
            element = psiFile.findElementAt(editor.getCaretModel().getOffset());
        }
    }
    if (element == null) {
        element = CommonDataKeys.PSI_ELEMENT.getData(context);
    }
    return element instanceof GherkinStep ? (GherkinStep) element : PsiTreeUtil.getParentOfType(element, GherkinStep.class);
}
Also used : GherkinStep(org.jetbrains.plugins.cucumber.psi.GherkinStep) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with GherkinStep

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

the class GherkinStepRenameHandler method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
    final GherkinStep step = getGherkinStep(dataContext);
    if (step == null) {
        return;
    }
    if (!step.isRenameAllowed(null)) {
        CommonRefactoringUtil.showErrorHint(project, editor, GherkinStep.RENAME_DISABLED_MESSAGE, "", null);
        return;
    }
    final CucumberStepRenameDialog dialog = new CucumberStepRenameDialog(project, step, null, editor);
    Disposer.register(project, dialog.getDisposable());
    RenameDialog.showRenameDialog(dataContext, dialog);
}
Also used : GherkinStep(org.jetbrains.plugins.cucumber.psi.GherkinStep)

Example 8 with GherkinStep

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

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

Example 10 with GherkinStep

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

the class JavaStepDefinitionCreator method buildStepDefinitionByStep.

private static PsiMethod buildStepDefinitionByStep(@NotNull final GherkinStep step, Language language) {
    String annotationPackage = new AnnotationPackageProvider().getAnnotationPackageFor(step);
    String methodAnnotation = String.format("@%s.", annotationPackage);
    final Step cucumberStep = new Step(new ArrayList<>(), step.getKeyword().getText(), step.getStepName(), 0, null, null);
    final SnippetGenerator generator = new SnippetGenerator(new JavaSnippet());
    final String snippet = generator.getSnippet(cucumberStep, new FunctionNameGenerator(new CamelCaseConcatenator())).replace("PendingException", CucumberJavaUtil.getCucumberPendingExceptionFqn(step)).replaceFirst("@", methodAnnotation).replaceAll("\\\\\\\\", "\\\\").replaceAll("\\\\d", "\\\\\\\\d");
    JVMElementFactory factory = JVMElementFactories.requireFactory(language, step.getProject());
    return factory.createMethodFromText(snippet, step);
}
Also used : CamelCaseConcatenator(cucumber.runtime.snippets.CamelCaseConcatenator) SnippetGenerator(cucumber.runtime.snippets.SnippetGenerator) FunctionNameGenerator(cucumber.runtime.snippets.FunctionNameGenerator) Step(gherkin.formatter.model.Step) GherkinStep(org.jetbrains.plugins.cucumber.psi.GherkinStep)

Aggregations

GherkinStep (org.jetbrains.plugins.cucumber.psi.GherkinStep)14 PsiElement (com.intellij.psi.PsiElement)6 NotNull (org.jetbrains.annotations.NotNull)6 AbstractStepDefinition (org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition)5 PsiFile (com.intellij.psi.PsiFile)4 PsiReference (com.intellij.psi.PsiReference)4 GherkinFile (org.jetbrains.plugins.cucumber.psi.GherkinFile)4 GherkinStepsHolder (org.jetbrains.plugins.cucumber.psi.GherkinStepsHolder)4 CucumberStepReference (org.jetbrains.plugins.cucumber.steps.reference.CucumberStepReference)4 SnippetGenerator (cucumber.runtime.snippets.SnippetGenerator)3 Step (gherkin.formatter.model.Step)3 CamelCaseConcatenator (cucumber.runtime.snippets.CamelCaseConcatenator)2 FunctionNameGenerator (cucumber.runtime.snippets.FunctionNameGenerator)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 GherkinFeature (org.jetbrains.plugins.cucumber.psi.GherkinFeature)2 CucumberStepsIndex (org.jetbrains.plugins.cucumber.steps.CucumberStepsIndex)2 ASTNode (com.intellij.lang.ASTNode)1 GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)1 Editor (com.intellij.openapi.editor.Editor)1