Search in sources :

Example 1 with CucumberStepReference

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

the class CucumberResolveTest method checkReference.

protected void checkReference(@NotNull final String step, @Nullable final String stepDefinitionName) {
    final CucumberStepReference ref = (CucumberStepReference) findReferenceBySignature(step);
    assert ref != null;
    final ResolveResult[] result = ref.multiResolve(true);
    boolean ok = stepDefinitionName == null;
    for (ResolveResult rr : result) {
        final PsiElement resolvedElement = rr.getElement();
        if (resolvedElement != null) {
            if (stepDefinitionName == null) {
                ok = false;
            } else {
                final String resolvedStepDefName = getStepDefinitionName(resolvedElement);
                if (resolvedStepDefName != null && resolvedStepDefName.equals(stepDefinitionName)) {
                    ok = true;
                    break;
                }
            }
        }
    }
    assertTrue(ok);
}
Also used : CucumberStepReference(org.jetbrains.plugins.cucumber.steps.reference.CucumberStepReference) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement)

Example 2 with CucumberStepReference

use of org.jetbrains.plugins.cucumber.steps.reference.CucumberStepReference 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 3 with CucumberStepReference

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

the class GherkinAnnotatorVisitor method visitStep.

@Override
public void visitStep(GherkinStep step) {
    final PsiReference[] references = step.getReferences();
    if (references.length != 1 || !(references[0] instanceof CucumberStepReference))
        return;
    CucumberStepReference reference = (CucumberStepReference) references[0];
    final AbstractStepDefinition definition = reference.resolveToDefinition();
    if (definition != null) {
        final List<TextRange> parameterRanges = GherkinPsiUtil.buildParameterRanges(step, definition, reference.getRangeInElement().getStartOffset());
        if (parameterRanges == null)
            return;
        for (TextRange range : parameterRanges) {
            if (range.getLength() > 0) {
                highlight(step, range, GherkinHighlighter.REGEXP_PARAMETER);
            }
        }
        highlightOutlineParams(step, reference);
    }
}
Also used : CucumberStepReference(org.jetbrains.plugins.cucumber.steps.reference.CucumberStepReference) AbstractStepDefinition(org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange)

Example 4 with CucumberStepReference

use of org.jetbrains.plugins.cucumber.steps.reference.CucumberStepReference 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 5 with CucumberStepReference

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

the class GherkinStepParameterSelectioner method select.

@NotNull
@Override
public List<TextRange> select(@NotNull final PsiElement e, @NotNull final CharSequence editorText, final int cursorOffset, @NotNull final Editor editor) {
    final List<TextRange> result = new ArrayList<>();
    if (editor.getSettings().isCamelWords()) {
        result.addAll(super.select(e, editorText, cursorOffset, editor));
    }
    final PsiElement parent = e.getParent();
    if (parent instanceof GherkinStep) {
        final GherkinStep step = (GherkinStep) parent;
        for (final PsiReference reference : step.getReferences()) {
            if (reference instanceof CucumberStepReference && !DumbService.isDumb(step.getProject())) {
                final AbstractStepDefinition definition = ((CucumberStepReference) reference).resolveToDefinition();
                if (definition != null) {
                    final List<TextRange> ranges = GherkinPsiUtil.buildParameterRanges(step, definition, step.getTextOffset() + reference.getRangeInElement().getStartOffset());
                    if (ranges != null) {
                        result.addAll(ranges);
                        result.addAll(buildAdditionalRanges(ranges, editorText));
                    }
                }
            }
        }
        buildAdditionalRanges(result, editorText);
    } else if (parent instanceof GherkinStepsHolder) {
        final ASTNode stepHolderNode = parent.getNode();
        if (stepHolderNode != null) {
            final ASTNode keyword = stepHolderNode.findChildByType(GherkinTokenTypes.KEYWORDS);
            if (keyword != null) {
                result.add(TextRange.create(keyword.getTextRange().getStartOffset(), parent.getTextRange().getEndOffset()));
            }
        }
        result.add(parent.getTextRange());
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) GherkinStep(org.jetbrains.plugins.cucumber.psi.GherkinStep) CucumberStepReference(org.jetbrains.plugins.cucumber.steps.reference.CucumberStepReference) AbstractStepDefinition(org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition) ASTNode(com.intellij.lang.ASTNode) PsiReference(com.intellij.psi.PsiReference) GherkinStepsHolder(org.jetbrains.plugins.cucumber.psi.GherkinStepsHolder) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CucumberStepReference (org.jetbrains.plugins.cucumber.steps.reference.CucumberStepReference)6 AbstractStepDefinition (org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition)5 PsiElement (com.intellij.psi.PsiElement)4 PsiReference (com.intellij.psi.PsiReference)4 GherkinStep (org.jetbrains.plugins.cucumber.psi.GherkinStep)4 GherkinStepsHolder (org.jetbrains.plugins.cucumber.psi.GherkinStepsHolder)3 TextRange (com.intellij.openapi.util.TextRange)2 NotNull (org.jetbrains.annotations.NotNull)2 ASTNode (com.intellij.lang.ASTNode)1 PsiFile (com.intellij.psi.PsiFile)1 ResolveResult (com.intellij.psi.ResolveResult)1 UsageInfo (com.intellij.usageView.UsageInfo)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Pattern (java.util.regex.Pattern)1 GherkinElementVisitor (org.jetbrains.plugins.cucumber.psi.GherkinElementVisitor)1 GherkinFeature (org.jetbrains.plugins.cucumber.psi.GherkinFeature)1 GherkinFile (org.jetbrains.plugins.cucumber.psi.GherkinFile)1