mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 13:31:28 +07:00
java complete statement: don't put closing parenthesis before caret (IDEA-225079)
GitOrigin-RevId: ae2d530e850b4051f4c90ccab8dcdecf01e74eca
This commit is contained in:
committed by
intellij-monorepo-bot
parent
26a0dc94dd
commit
f71bd07b0b
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
Object method() {
|
||||
Integer.parseInt("1", 10<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
Object method() {
|
||||
Integer.parseInt("1", 10);<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
Object method() {
|
||||
String.format("%d, %d", 1, 2<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
Object method() {
|
||||
String.format("%d, %d", 1, 2);<caret>
|
||||
}
|
||||
}
|
||||
@@ -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(); }
|
||||
|
||||
Reference in New Issue
Block a user