Search in sources :

Example 1 with AbstractStepDefinition

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

the class GrCucumberExtension method getGlues.

@NotNull
@Override
public Collection<String> getGlues(@NotNull GherkinFile file, Set<String> gluesFromOtherFiles) {
    if (gluesFromOtherFiles == null) {
        gluesFromOtherFiles = ContainerUtil.newHashSet();
    }
    final Set<String> glues = gluesFromOtherFiles;
    for (AbstractStepDefinition stepDefinition : getAllStepDefinitions(file.getProject())) {
        final PsiElement stepDefinitionElement = stepDefinition.getElement();
        final String glue = getGlue(stepDefinitionElement);
        if (glue != null) {
            glues.add(glue);
        }
    }
    return glues;
}
Also used : AbstractStepDefinition(org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with AbstractStepDefinition

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

the class CucumberStepRenameDialog method getSuggestedNames.

@Override
public String[] getSuggestedNames() {
    AbstractStepDefinition stepDefinition = getStepDefinition();
    if (stepDefinition != null) {
        String result = stepDefinition.getCucumberRegex();
        if (result != null) {
            result = StringUtil.trimStart(result, "^");
            result = StringUtil.trimEnd(result, "$");
            return new String[] { result };
        }
    }
    return super.getSuggestedNames();
}
Also used : AbstractStepDefinition(org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition)

Example 3 with AbstractStepDefinition

use of org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition 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 4 with AbstractStepDefinition

use of org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition 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 AbstractStepDefinition

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

the class CucumberCompletionContributor method addStepDefinitions.

private static void addStepDefinitions(CompletionResultSet result, PsiFile file) {
    result = result.withPrefixMatcher(new CucumberPrefixMatcher(result.getPrefixMatcher().getPrefix()));
    final List<AbstractStepDefinition> definitions = CucumberStepsIndex.getInstance(file.getProject()).getAllStepDefinitions(file);
    for (AbstractStepDefinition definition : definitions) {
        String text = definition.getCucumberRegex();
        if (text != null) {
            // trim regexp line start/end markers
            text = StringUtil.trimStart(text, "^");
            text = StringUtil.trimEnd(text, "$");
            text = StringUtil.replace(text, "\\\"", "\"");
            for (Map.Entry<String, String> group : GROUP_TYPE_MAP.entrySet()) {
                text = StringUtil.replace(text, group.getKey(), group.getValue());
            }
            for (Map.Entry<String, String> group : INTERPOLATION_PARAMETERS_MAP.entrySet()) {
                text = text.replaceAll(group.getKey(), group.getValue());
            }
            final List<TextRange> ranges = new ArrayList<>();
            Matcher m = QUESTION_MARK_PATTERN.matcher(text);
            if (m.find()) {
                text = m.replaceAll("$1");
            }
            m = POSSIBLE_GROUP_PATTERN.matcher(text);
            while (m.find()) {
                text = m.replaceAll("$1");
            }
            m = PARAMETERS_PATTERN.matcher(text);
            while (m.find()) {
                ranges.add(new TextRange(m.start(), m.end()));
            }
            final PsiElement element = definition.getElement();
            final LookupElementBuilder lookup = element != null ? LookupElementBuilder.create(element, text).bold() : LookupElementBuilder.create(text);
            result.addElement(lookup.withInsertHandler(new StepInsertHandler(ranges)));
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) TextRange(com.intellij.openapi.util.TextRange) AbstractStepDefinition(org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiElement(com.intellij.psi.PsiElement)

Aggregations

AbstractStepDefinition (org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition)12 PsiElement (com.intellij.psi.PsiElement)7 GherkinStep (org.jetbrains.plugins.cucumber.psi.GherkinStep)5 CucumberStepReference (org.jetbrains.plugins.cucumber.steps.reference.CucumberStepReference)5 PsiReference (com.intellij.psi.PsiReference)4 ArrayList (java.util.ArrayList)4 NotNull (org.jetbrains.annotations.NotNull)4 GherkinStepsHolder (org.jetbrains.plugins.cucumber.psi.GherkinStepsHolder)4 TextRange (com.intellij.openapi.util.TextRange)3 PsiFile (com.intellij.psi.PsiFile)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 JavaStepDefinition (org.jetbrains.plugins.cucumber.java.steps.JavaStepDefinition)2 GherkinFeature (org.jetbrains.plugins.cucumber.psi.GherkinFeature)2 GherkinFile (org.jetbrains.plugins.cucumber.psi.GherkinFile)2 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 ASTNode (com.intellij.lang.ASTNode)1 GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)1 Module (com.intellij.openapi.module.Module)1 PsiClass (com.intellij.psi.PsiClass)1 PsiMethod (com.intellij.psi.PsiMethod)1