Search in sources :

Example 1 with GherkinStep

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

the class CucumberStepRenameProcessor method renameElement.

@Override
public void renameElement(PsiElement element, String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    final CucumberStepReference reference = getCucumberStepReference(element);
    if (reference != null) {
        final AbstractStepDefinition stepDefinition = reference.resolveToDefinition();
        if (stepDefinition != null) {
            final PsiElement elementToRename = stepDefinition.getElement();
            final List<String> newStaticTexts = prepareRegexAndGetStaticTexts(newName);
            final String oldStepDefPatternText = stepDefinition.getCucumberRegex();
            if (oldStepDefPatternText != null) {
                final Pattern oldStepDefPattern = Pattern.compile(prepareRegexAndGetStaticTexts(oldStepDefPatternText).get(0));
                for (UsageInfo usage : usages) {
                    final PsiElement possibleStep = usage.getElement();
                    if (possibleStep instanceof GherkinStep) {
                        final String oldStepName = ((GherkinStep) possibleStep).getStepName();
                        final String newStepName = getNewStepName(oldStepName, oldStepDefPattern, newStaticTexts);
                        ((GherkinStep) possibleStep).setName(newStepName);
                    }
                }
                final String prefix = oldStepDefPatternText.startsWith("^") ? "^" : "";
                final String suffix = oldStepDefPatternText.endsWith("$") ? "$" : "";
                stepDefinition.setCucumberRegex(prefix + newName + suffix);
                if (listener != null && elementToRename != null) {
                    listener.elementRenamed(elementToRename);
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) CucumberStepReference(org.jetbrains.plugins.cucumber.steps.reference.CucumberStepReference) AbstractStepDefinition(org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition) GherkinStep(org.jetbrains.plugins.cucumber.psi.GherkinStep) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Example 2 with GherkinStep

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

the class AbstractStepDefinition method findSteps.

/**
   * Finds all steps points to this definition in some scope
   *
   * @param searchScope scope to find steps
   * @return steps
   */
@NotNull
public Collection<GherkinStep> findSteps(@NotNull final SearchScope searchScope) {
    final String regex = getCucumberRegex();
    final PsiElement element = getElement();
    if ((regex == null) || (element == null)) {
        return Collections.emptyList();
    }
    final CollectProcessor<PsiReference> consumer = new CollectProcessor<>();
    CucumberUtil.findGherkinReferencesToElement(element, regex, consumer, searchScope);
    // We use hash to get rid of duplicates
    final Collection<GherkinStep> results = new HashSet<>(consumer.getResults().size());
    for (final PsiReference reference : consumer.getResults()) {
        final PsiElement step = reference.getElement();
        if (step instanceof GherkinStep) {
            results.add((GherkinStep) step);
        }
    }
    return results;
}
Also used : GherkinStep(org.jetbrains.plugins.cucumber.psi.GherkinStep) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) CollectProcessor(com.intellij.util.CommonProcessors.CollectProcessor) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GherkinStep

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

the class Java8StepDefinitionCreator method buildStepDefinitionByStep.

private static PsiElement buildStepDefinitionByStep(@NotNull final GherkinStep step, Language language) {
    final Step cucumberStep = new Step(new ArrayList<>(), step.getKeyword().getText(), step.getStepName(), 0, null, null);
    final SnippetGenerator generator = new SnippetGenerator(new Java8Snippet());
    final String snippet = generator.getSnippet(cucumberStep, new FunctionNameGenerator(new CamelCaseConcatenator())).replace("PendingException", CucumberJavaUtil.getCucumberPendingExceptionFqn(step)).replaceAll("\\\\\\\\", "\\\\").replaceAll("\\\\d", "\\\\\\\\d");
    JVMElementFactory factory = JVMElementFactories.requireFactory(language, step.getProject());
    return factory.createExpressionFromText(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)

Example 4 with GherkinStep

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

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

the class AnnotationPackageProviderTest method createStepIn.

private static GherkinStep createStepIn(String language) {
    GherkinFile featureFile = EasyMock.createNiceMock(GherkinFile.class);
    expect(featureFile.getLocaleLanguage()).andReturn(language).anyTimes();
    GherkinStep step = EasyMock.createNiceMock(GherkinStep.class);
    expect(step.getContainingFile()).andReturn(featureFile).anyTimes();
    replay(featureFile, step);
    return step;
}
Also used : GherkinStep(org.jetbrains.plugins.cucumber.psi.GherkinStep) GherkinFile(org.jetbrains.plugins.cucumber.psi.GherkinFile)

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