|
|
|
@@ -18,6 +18,8 @@ package com.intellij.codeInsight.editorActions;
|
|
|
|
|
import com.intellij.codeInsight.AutoPopupController;
|
|
|
|
|
import com.intellij.codeInsight.CodeInsightSettings;
|
|
|
|
|
import com.intellij.codeInsight.completion.JavaClassReferenceCompletionContributor;
|
|
|
|
|
import com.intellij.codeInsight.editorActions.smartEnter.JavaSmartEnterProcessor;
|
|
|
|
|
import com.intellij.openapi.editor.Document;
|
|
|
|
|
import com.intellij.openapi.editor.Editor;
|
|
|
|
|
import com.intellij.openapi.editor.EditorModificationUtil;
|
|
|
|
|
import com.intellij.openapi.editor.ex.EditorEx;
|
|
|
|
@@ -125,11 +127,18 @@ public class JavaTypedHandler extends TypedHandlerDelegate {
|
|
|
|
|
if (iterator.atEnd() || iterator.getTokenType() == JavaTokenType.RBRACKET || iterator.getTokenType() == JavaTokenType.EQ) {
|
|
|
|
|
return Result.CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
|
|
|
|
|
Document doc = editor.getDocument();
|
|
|
|
|
PsiDocumentManager.getInstance(project).commitDocument(doc);
|
|
|
|
|
final PsiElement leaf = file.findElementAt(offset);
|
|
|
|
|
if (PsiTreeUtil.getParentOfType(leaf, PsiArrayInitializerExpression.class, false, PsiCodeBlock.class, PsiMember.class) != null) {
|
|
|
|
|
return Result.CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
PsiElement st = leaf != null ? leaf.getParent() : null;
|
|
|
|
|
if (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && isRparenth(leaf) &&
|
|
|
|
|
(st instanceof PsiWhileStatement || st instanceof PsiIfStatement) && noBody(st, doc)) {
|
|
|
|
|
new JavaSmartEnterProcessor().process(project, editor, file);
|
|
|
|
|
return Result.STOP;
|
|
|
|
|
}
|
|
|
|
|
if (PsiTreeUtil.getParentOfType(leaf, PsiCodeBlock.class, false, PsiMember.class) != null) {
|
|
|
|
|
EditorModificationUtil.insertStringAtCaret(editor, "{");
|
|
|
|
|
TypedHandler.indentOpenedBrace(project, editor);
|
|
|
|
@@ -140,6 +149,24 @@ public class JavaTypedHandler extends TypedHandlerDelegate {
|
|
|
|
|
return Result.CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean noBody(@NotNull PsiElement statement, @NotNull Document doc) {
|
|
|
|
|
PsiStatement block = statement instanceof PsiWhileStatement ? ((PsiWhileStatement)statement).getBody() : ((PsiIfStatement)statement).getThenBranch();
|
|
|
|
|
PsiExpression condition = PsiTreeUtil.getChildOfType(statement, PsiExpression.class);
|
|
|
|
|
return !(block instanceof PsiBlockStatement) && (block == null || startLine(doc, block) != startLine(doc, statement) || condition == null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean isRparenth(@Nullable PsiElement leaf) {
|
|
|
|
|
if (leaf == null) return false;
|
|
|
|
|
if (leaf.getNode().getElementType() == JavaTokenType.RPARENTH) return true;
|
|
|
|
|
PsiElement next = PsiTreeUtil.nextVisibleLeaf(leaf);
|
|
|
|
|
if (next == null) return false;
|
|
|
|
|
return next.getNode().getElementType() == JavaTokenType.RPARENTH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int startLine(@NotNull Document doc, @NotNull PsiElement psiElement) {
|
|
|
|
|
return doc.getLineNumber(psiElement.getTextRange().getStartOffset());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Result charTyped(final char c, final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) {
|
|
|
|
|
if (myJavaLTTyped) {
|
|
|
|
|