[java-completion] Fix method body without () only when type is void (IDEA-23781)

GitOrigin-RevId: d669b3447dce6186a10f8b807b86bb14f9b98f82
This commit is contained in:
Tagir Valeev
2022-04-28 10:46:28 +02:00
committed by intellij-monorepo-bot
parent 057b1bc48d
commit d24dbdcf93
6 changed files with 12 additions and 10 deletions

View File

@@ -30,7 +30,7 @@ public class MissingMethodBodyFixer implements Fixer {
@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
if (psiElement instanceof PsiField) {
// replace something like `int x` with `int x() {...}`
// replace something like `void x` with `void x() {...}`
// while it's ambiguous whether user wants a field or a method, declaring a field is easier (just append a semicolon),
// so completing a method looks more useful
PsiField field = (PsiField)psiElement;
@@ -42,11 +42,7 @@ public class MissingMethodBodyFixer implements Fixer {
if (modifiers == null) return;
// Impossible modifiers for a method
if (modifiers.hasExplicitModifier(TRANSIENT) || modifiers.hasExplicitModifier(VOLATILE)) return;
// Modifier combination which is unlikely to see on methods but quite reasonable on fields
if ((modifiers.hasExplicitModifier(STATIC) || modifiers.hasExplicitModifier(PRIVATE)) &&
modifiers.hasExplicitModifier(FINAL)) {
return;
}
if (!PsiType.VOID.equals(field.getType())) return;
int endOffset = field.getTextRange().getEndOffset();
editor.getDocument().insertString(endOffset, "(){}");
editor.getCaretModel().moveToOffset(endOffset + 1);

View File

@@ -1,7 +1,6 @@
public class Test {
private String s(<caret>) {
}
private String s;<caret>
@Deprecated private String foo;
}

View File

@@ -1,4 +1,3 @@
public class Test {
String s(<caret>) {
}
String s;<caret>
}

View File

@@ -0,0 +1,3 @@
public class FieldWithEquals {
void foo<caret>
}

View File

@@ -0,0 +1,4 @@
public class FieldWithEquals {
void foo(<caret>) {
}
}

View File

@@ -85,6 +85,7 @@ public class CompleteStatementTest extends EditorActionTestCase {
public void testField() { doTest(); }
public void testMethod() { doTest(); }
public void testLikelyField() { doTest(); }
public void testVoidMethodIncomplete() { doTest(); }
public void testFieldWithEquals() { doTest(); }
public void testClass() { doTest(); }
public void testInnerEnumBeforeMethod() { doTest(); }