Search in sources :

Example 1 with GherkinFeature

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

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

Aggregations

PsiFile (com.intellij.psi.PsiFile)2 GherkinFeature (org.jetbrains.plugins.cucumber.psi.GherkinFeature)2 GherkinFile (org.jetbrains.plugins.cucumber.psi.GherkinFile)2 GherkinStep (org.jetbrains.plugins.cucumber.psi.GherkinStep)2 GherkinStepsHolder (org.jetbrains.plugins.cucumber.psi.GherkinStepsHolder)2 AbstractStepDefinition (org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition)2 GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)1 PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 NotNull (org.jetbrains.annotations.NotNull)1 CucumberStepsIndex (org.jetbrains.plugins.cucumber.steps.CucumberStepsIndex)1 CucumberStepReference (org.jetbrains.plugins.cucumber.steps.reference.CucumberStepReference)1