Search in sources :

Example 11 with GherkinStep

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

Example 12 with GherkinStep

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

the class CucumberStepInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new GherkinElementVisitor() {

        @Override
        public void visitStep(GherkinStep step) {
            super.visitStep(step);
            final PsiElement parent = step.getParent();
            if (parent instanceof GherkinStepsHolder) {
                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) {
                    CucumberCreateStepFix createStepFix = null;
                    CucumberCreateAllStepsFix createAllStepsFix = null;
                    if (CucumberStepsIndex.getInstance(step.getProject()).getExtensionCount() > 0) {
                        createStepFix = new CucumberCreateStepFix();
                        createAllStepsFix = new CucumberCreateAllStepsFix();
                    }
                    holder.registerProblem(reference.getElement(), reference.getRangeInElement(), CucumberBundle.message("cucumber.inspection.undefined.step.msg.name") + " #loc #ref", createStepFix, createAllStepsFix);
                }
            }
        }
    };
}
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) GherkinElementVisitor(org.jetbrains.plugins.cucumber.psi.GherkinElementVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 13 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 14 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)

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