Search in sources :

Example 1 with GrSwitchStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement in project intellij-community by JetBrains.

the class GroovySmartEnterProcessor method reformat.

@Override
protected void reformat(PsiElement atCaret) throws IncorrectOperationException {
    PsiElement parent = atCaret.getParent();
    if (parent instanceof GrCodeBlock) {
        final GrCodeBlock block = (GrCodeBlock) parent;
        if (block.getStatements().length > 0 && block.getStatements()[0] == atCaret) {
            atCaret = block;
        }
    } else if (parent instanceof GrForStatement || parent instanceof GrSwitchStatement) {
        atCaret = parent;
    }
    super.reformat(atCaret);
}
Also used : GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement) GrSwitchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 2 with GrSwitchStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement in project intellij-community by JetBrains.

the class GrCreateMissingSwitchBranchesIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    if (!(element instanceof GrSwitchStatement))
        return;
    final List<PsiEnumConstant> constants = findUnusedConstants((GrSwitchStatement) element);
    if (constants.isEmpty())
        return;
    final PsiEnumConstant first = constants.get(0);
    final PsiClass aClass = first.getContainingClass();
    if (aClass == null)
        return;
    String qName = aClass.getQualifiedName();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    PsiElement anchor = findAnchor(element);
    for (PsiEnumConstant constant : constants) {
        final GrCaseSection section = factory.createSwitchSection("case " + qName + "." + constant.getName() + ":\n break");
        final PsiElement added = element.addBefore(section, anchor);
        element.addBefore(factory.createLineTerminator(1), anchor);
        JavaCodeStyleManager.getInstance(project).shortenClassReferences(added);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrSwitchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement)

Example 3 with GrSwitchStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement in project intellij-community by JetBrains.

the class GrSwitchBodyFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    GrSwitchStatement switchStatement = PsiTreeUtil.getParentOfType(psiElement, GrSwitchStatement.class);
    if (switchStatement == null || switchStatement.getLBrace() != null)
        return;
    if (!PsiTreeUtil.isAncestor(switchStatement.getCondition(), psiElement, false))
        return;
    final Document doc = editor.getDocument();
    PsiElement lBrace = switchStatement.getLBrace();
    if (lBrace != null)
        return;
    PsiElement eltToInsertAfter = switchStatement.getRParenth();
    String text = "{\n}";
    if (eltToInsertAfter == null) {
        eltToInsertAfter = switchStatement.getCondition();
        text = "){\n}";
    }
    doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text);
}
Also used : GrSwitchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 4 with GrSwitchStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement in project intellij-community by JetBrains.

the class GroovyDuplicateSwitchBranchInspection method collectCaseLabels.

private static GrCaseLabel[] collectCaseLabels(final GrSwitchStatement containingStatelent) {
    final Set<GrCaseLabel> labels = new HashSet<>();
    final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {

        @Override
        public void visitCaseLabel(@NotNull GrCaseLabel grCaseLabel) {
            super.visitCaseLabel(grCaseLabel);
            labels.add(grCaseLabel);
        }

        @Override
        public void visitSwitchStatement(@NotNull GrSwitchStatement grSwitchStatement) {
            if (containingStatelent.equals(grSwitchStatement)) {
                super.visitSwitchStatement(grSwitchStatement);
            }
        }
    };
    containingStatelent.accept(visitor);
    return labels.toArray(new GrCaseLabel[labels.size()]);
}
Also used : GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrSwitchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement) NotNull(org.jetbrains.annotations.NotNull) HashSet(java.util.HashSet)

Aggregations

GrSwitchStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement)4 Document (com.intellij.openapi.editor.Document)1 PsiElement (com.intellij.psi.PsiElement)1 HashSet (java.util.HashSet)1 NotNull (org.jetbrains.annotations.NotNull)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)1 GrForStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement)1 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)1 GrCaseLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel)1 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)1