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);
}
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);
}
}
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);
}
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()]);
}
Aggregations