mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 13:31:28 +07:00
[java-completion] Avoid fixing missing comma for methods without parameters
Fixes IDEA-274311 Applying the completion adds a redundant comma on the next line GitOrigin-RevId: 3f4a55aff4a640e9e40e7a9c242611f0bb95475d
This commit is contained in:
committed by
intellij-monorepo-bot
parent
0114a0757a
commit
ec2238e7c8
@@ -3,26 +3,30 @@ package com.intellij.codeInsight.editorActions.smartEnter;
|
||||
|
||||
import com.intellij.core.JavaPsiBundle;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiErrorElement;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiExpressionList;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
|
||||
public class MissingCommaFixer implements Fixer {
|
||||
@Override
|
||||
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
if (psiElement instanceof PsiErrorElement) {
|
||||
if (((PsiErrorElement)psiElement).getErrorDescription().equals(JavaPsiBundle.message("expected.comma.or.rparen"))) {
|
||||
PsiElement parent = psiElement.getParent();
|
||||
if (parent instanceof PsiExpressionList) {
|
||||
PsiElement next = PsiTreeUtil.skipWhitespacesAndCommentsForward(psiElement);
|
||||
if (next instanceof PsiExpression) {
|
||||
editor.getDocument().insertString(psiElement.getTextOffset(), ",");
|
||||
}
|
||||
}
|
||||
if (!(psiElement instanceof PsiErrorElement) ||
|
||||
!((PsiErrorElement)psiElement).getErrorDescription().equals(JavaPsiBundle.message("expected.comma.or.rparen"))) {
|
||||
return;
|
||||
}
|
||||
PsiElement parent = psiElement.getParent();
|
||||
if (!(parent instanceof PsiExpressionList)) return;
|
||||
PsiElement next = PsiTreeUtil.skipWhitespacesAndCommentsForward(psiElement);
|
||||
if (!(next instanceof PsiExpression)) return;
|
||||
PsiElement call = parent.getParent();
|
||||
if (call instanceof PsiCall) {
|
||||
PsiMethod method = ((PsiCall)call).resolveMethod();
|
||||
if (method != null) {
|
||||
PsiParameterList list = method.getParameterList();
|
||||
int count = list.getParametersCount();
|
||||
if (count == 0 || count == 1 && !method.isVarArgs()) return;
|
||||
}
|
||||
}
|
||||
editor.getDocument().insertString(psiElement.getTextOffset(), ",");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import java.util.*;
|
||||
|
||||
public class CompletionShouldNotAddExtraComma {
|
||||
List<String> getResult(String str) {
|
||||
if (str == null) return Collections.empty<caret>
|
||||
Integer a = 1;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import java.util.*;
|
||||
|
||||
public class CompletionShouldNotAddExtraComma {
|
||||
List<String> getResult(String str) {
|
||||
if (str == null) return Collections.emptyList();<caret>
|
||||
Integer a = 1;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
@@ -2049,6 +2049,12 @@ class Bar {{
|
||||
checkResultByFile(getTestName(false) + "_after.java")
|
||||
}
|
||||
|
||||
void testCompletionShouldNotAddExtraComma() {
|
||||
configureByTestName()
|
||||
myFixture.performEditorAction(IdeActions.ACTION_CHOOSE_LOOKUP_ITEM_COMPLETE_STATEMENT)
|
||||
checkResultByFile(getTestName(false) + "_after.java")
|
||||
}
|
||||
|
||||
@NeedsIndex.Full
|
||||
void testCompletingClassWithSameNameAsPackage() {
|
||||
myFixture.addClass("package Apple; public class Apple {}")
|
||||
|
||||
Reference in New Issue
Block a user