Autoindent Java case statement

This commit is contained in:
Rustam.Vishnyakov
2012-02-08 17:39:50 +04:00
parent bbc8c84fa9
commit 8f02db5241
@@ -30,6 +30,7 @@ import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.filters.ClassFilter;
import com.intellij.psi.filters.position.SuperParentFilter;
import com.intellij.psi.javadoc.PsiDocComment;
@@ -146,6 +147,11 @@ public class JavaTypedHandler extends TypedHandlerDelegate {
handleAfterJavaLT(editor, JavaTokenType.LT, JavaTokenType.GT, INVALID_INSIDE_REFERENCE);
return Result.STOP;
}
else if (c == ':') {
if (autoIndentCase(editor, project, file)) {
return Result.STOP;
}
}
return Result.CONTINUE;
}
@@ -283,4 +289,18 @@ public class JavaTypedHandler extends TypedHandlerDelegate {
return false;
}
private static boolean autoIndentCase(Editor editor, Project project, PsiFile file) {
int offset = editor.getCaretModel().getOffset();
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
PsiElement currElement = file.findElementAt(offset - 1);
if (currElement != null) {
PsiElement parent = currElement.getParent();
if (parent != null && parent instanceof PsiSwitchLabelStatement) {
CodeStyleManager.getInstance(project).adjustLineIndent(file, parent.getTextOffset());
return true;
}
}
return false;
}
}