java complete statement: don't put closing parenthesis before caret (IDEA-225079)

GitOrigin-RevId: ae2d530e850b4051f4c90ccab8dcdecf01e74eca
This commit is contained in:
peter
2019-10-21 13:14:46 +02:00
committed by intellij-monorepo-bot
parent 26a0dc94dd
commit f71bd07b0b
6 changed files with 43 additions and 17 deletions

View File

@@ -22,29 +22,32 @@ import com.intellij.psi.impl.source.jsp.jspJava.JspMethodCall;
import com.intellij.psi.infos.CandidateInfo;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.text.CharArrayUtil;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
public class MethodCallFixer implements Fixer {
@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
PsiExpressionList args = null;
PsiExpressionList argList = null;
if (psiElement instanceof PsiMethodCallExpression && !(psiElement instanceof JspMethodCall)) {
args = ((PsiMethodCallExpression) psiElement).getArgumentList();
argList = ((PsiMethodCallExpression) psiElement).getArgumentList();
} else if (psiElement instanceof PsiNewExpression) {
args = ((PsiNewExpression) psiElement).getArgumentList();
argList = ((PsiNewExpression) psiElement).getArgumentList();
}
if (args != null && !hasRParenth(args)) {
int caret = editor.getCaretModel().getOffset();
int caret = editor.getCaretModel().getOffset();
if (argList != null && !hasRParenth(argList)) {
PsiCallExpression innermostCall = PsiTreeUtil.findElementOfClassAtOffset(psiElement.getContainingFile(), caret - 1, PsiCallExpression.class, false);
if (innermostCall == null) return;
args = innermostCall.getArgumentList();
if (args == null) return;
argList = innermostCall.getArgumentList();
if (argList == null) return;
int endOffset = -1;
PsiElement child = args.getFirstChild();
PsiElement child = argList.getFirstChild();
while (child != null) {
if (child instanceof PsiErrorElement) {
final PsiErrorElement errorElement = (PsiErrorElement)child;
@@ -57,20 +60,21 @@ public class MethodCallFixer implements Fixer {
}
if (endOffset == -1) {
endOffset = args.getTextRange().getEndOffset();
endOffset = argList.getTextRange().getEndOffset();
}
final PsiExpression[] params = args.getExpressions();
if (params.length > 0 &&
startLine(editor, args) != startLine(editor, params[0]) &&
caret < params[0].getTextRange().getStartOffset()) {
endOffset = args.getTextRange().getStartOffset() + 1;
PsiExpression[] args = argList.getExpressions();
if (args.length > 0 &&
startLine(editor, argList) != startLine(editor, args[0]) &&
caret < args[0].getTextRange().getStartOffset()) {
endOffset = argList.getTextRange().getStartOffset() + 1;
}
if (!DumbService.isDumb(args.getProject())) {
if (!DumbService.isDumb(argList.getProject())) {
int caretArg = ContainerUtil.indexOf(Arrays.asList(args), arg -> arg.getTextRange().containsOffset(caret));
Integer argCount = getMinimalParameterCount(innermostCall);
if (argCount != null && argCount > 0 && argCount < params.length) {
endOffset = Math.min(endOffset, params[argCount - 1].getTextRange().getEndOffset());
if (argCount != null && argCount > 0 && argCount < args.length) {
endOffset = Math.min(endOffset, args[Math.max(argCount - 1, caretArg)].getTextRange().getEndOffset());
}
}

View File

@@ -0,0 +1,5 @@
class Test {
Object method() {
Integer.parseInt("1", 10<caret>
}
}

View File

@@ -0,0 +1,5 @@
class Test {
Object method() {
Integer.parseInt("1", 10);<caret>
}
}

View File

@@ -0,0 +1,5 @@
class Test {
Object method() {
String.format("%d, %d", 1, 2<caret>
}
}

View File

@@ -0,0 +1,5 @@
class Test {
Object method() {
String.format("%d, %d", 1, 2);<caret>
}
}

View File

@@ -157,6 +157,8 @@ public class CompleteStatementTest extends EditorActionTestCase {
public void testIDEADEV40479() { doTest(); }
public void testMultilineReturn() { doTest(); }
public void testMultilineCall() { doTest(); }
public void testVarargCall() { doTest(); }
public void testOverloadedCall() { doTest(); }
public void testIDEADEV13019() { doTestBracesNextLineStyle(); }
public void testIDEA25139() { doTestBracesNextLineStyle(); }
public void testClassBracesNextLine() { doTestBracesNextLineStyle(); }