Search in sources :

Example 1 with GherkinExamplesBlockImpl

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

the class CucumberCreateExamplesSectionFix method applyFix.

public void applyFix(@NotNull final Project project, @NotNull ProblemDescriptor descriptor) {
    final GherkinScenarioOutlineImpl outline = (GherkinScenarioOutlineImpl) descriptor.getPsiElement();
    final PsiFile featureFile = outline.getContainingFile();
    final String language = GherkinKeywordTable.getFeatureLanguage(featureFile);
    final GherkinKeywordTable keywordsTable = GherkinKeywordTable.getKeywordsTable(featureFile, project);
    final StringBuilder buff = new StringBuilder();
    buff.append(keywordsTable.getScenarioOutlineKeyword()).append(": boo\n");
    buff.append(keywordsTable.getExampleSectionKeyword()).append(":\n|");
    final List<String> params = new ArrayList<>();
    final PsiElement[] elements = outline.getChildren();
    for (PsiElement element : elements) {
        if (!(element instanceof GherkinStep)) {
            continue;
        }
        final GherkinStep step = (GherkinStep) element;
        final List<String> substitutions = step.getParamsSubstitutions();
        for (String substitution : substitutions) {
            if (!params.contains(substitution)) {
                params.add(substitution);
            }
        }
    }
    if (params.isEmpty()) {
        buff.append(" |");
    } else {
        for (String substitution : params) {
            buff.append(' ').append(substitution).append(" |");
        }
    }
    final String text = buff.toString();
    GherkinScenarioOutline fakeScenario = (GherkinScenarioOutline) GherkinElementFactory.createScenarioFromText(project, language, text);
    final GherkinExamplesBlock fakeExampleSection = fakeScenario.getExamplesBlocks().get(0);
    assert fakeExampleSection != null;
    GherkinExamplesBlockImpl addedSection = (GherkinExamplesBlockImpl) outline.add(fakeExampleSection);
    addedSection = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(addedSection);
    final GherkinTable table = addedSection.getTable();
    assert table != null;
    final GherkinTableRow headerRow = table.getHeaderRow();
    assert headerRow != null;
    final List<GherkinTableCell> cells = headerRow.getPsiCells();
    final int firstCellOffset = cells.size() > 0 && cells.get(0).getTextLength() > 0 ? cells.get(0).getTextOffset() : headerRow.getTextOffset() + 1;
    final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    assert editor != null;
    // commit current document
    final Document document = editor.getDocument();
    PsiDocumentManager.getInstance(project).commitDocument(document);
    editor.getCaretModel().moveToOffset(firstCellOffset);
}
Also used : ArrayList(java.util.ArrayList) Document(com.intellij.openapi.editor.Document) GherkinExamplesBlockImpl(org.jetbrains.plugins.cucumber.psi.impl.GherkinExamplesBlockImpl) GherkinScenarioOutlineImpl(org.jetbrains.plugins.cucumber.psi.impl.GherkinScenarioOutlineImpl) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 2 with GherkinExamplesBlockImpl

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

the class CucumberExamplesColonInspection method buildVisitor.

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

        @Override
        public void visitExamplesBlock(GherkinExamplesBlockImpl block) {
            final PsiElement examples = block.getFirstChild();
            assert examples != null;
            final PsiElement next = examples.getNextSibling();
            final String text = next != null ? next.getText() : null;
            if (text == null || !text.contains(":")) {
                holder.registerProblem(examples, new TextRange(0, examples.getTextRange().getEndOffset() - examples.getTextOffset()), CucumberBundle.message("inspection.missed.colon.example.name"), new CucumberAddExamplesColonFix());
            }
        }
    };
}
Also used : GherkinExamplesBlockImpl(org.jetbrains.plugins.cucumber.psi.impl.GherkinExamplesBlockImpl) GherkinElementVisitor(org.jetbrains.plugins.cucumber.psi.GherkinElementVisitor) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GherkinExamplesBlockImpl

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

the class CucumberMissedExamplesInspection method buildVisitor.

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

        @Override
        public void visitScenarioOutline(GherkinScenarioOutline outline) {
            super.visitScenarioOutline(outline);
            final GherkinExamplesBlockImpl block = PsiTreeUtil.getChildOfType(outline, GherkinExamplesBlockImpl.class);
            if (block == null) {
                ASTNode[] keyword = outline.getNode().getChildren(GherkinTokenTypes.KEYWORDS);
                if (keyword.length > 0) {
                    holder.registerProblem(outline, keyword[0].getTextRange().shiftRight(-outline.getTextOffset()), CucumberBundle.message("inspection.missed.example.msg.name"), new CucumberCreateExamplesSectionFix());
                }
            }
        }
    };
}
Also used : GherkinExamplesBlockImpl(org.jetbrains.plugins.cucumber.psi.impl.GherkinExamplesBlockImpl) ASTNode(com.intellij.lang.ASTNode) GherkinElementVisitor(org.jetbrains.plugins.cucumber.psi.GherkinElementVisitor) GherkinScenarioOutline(org.jetbrains.plugins.cucumber.psi.GherkinScenarioOutline) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GherkinExamplesBlockImpl (org.jetbrains.plugins.cucumber.psi.impl.GherkinExamplesBlockImpl)3 PsiElement (com.intellij.psi.PsiElement)2 NotNull (org.jetbrains.annotations.NotNull)2 GherkinElementVisitor (org.jetbrains.plugins.cucumber.psi.GherkinElementVisitor)2 ASTNode (com.intellij.lang.ASTNode)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiFile (com.intellij.psi.PsiFile)1 ArrayList (java.util.ArrayList)1 GherkinScenarioOutline (org.jetbrains.plugins.cucumber.psi.GherkinScenarioOutline)1 GherkinScenarioOutlineImpl (org.jetbrains.plugins.cucumber.psi.impl.GherkinScenarioOutlineImpl)1