mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
do not generate duplicated constructors, warn when nothing was added (IDEA-67659)
This commit is contained in:
@@ -161,9 +161,22 @@ public class GenerateConstructorHandler extends GenerateMembersHandlerBase {
|
||||
}
|
||||
constructors.add(new PsiGenerationInfo(generateConstructorPrototype(aClass, baseConstructor, myCopyJavadoc, fields)));
|
||||
}
|
||||
return constructors;
|
||||
return filterOutAlreadyInsertedConstructors(aClass, constructors);
|
||||
}
|
||||
return Collections.<GenerationInfo>singletonList(new PsiGenerationInfo(generateConstructorPrototype(aClass, null, false, fields)));
|
||||
final List<GenerationInfo> constructors =
|
||||
Collections.<GenerationInfo>singletonList(new PsiGenerationInfo(generateConstructorPrototype(aClass, null, false, fields)));
|
||||
return filterOutAlreadyInsertedConstructors(aClass, constructors);
|
||||
}
|
||||
|
||||
private static List<? extends GenerationInfo> filterOutAlreadyInsertedConstructors(PsiClass aClass, List<? extends GenerationInfo> constructors) {
|
||||
boolean alreadyExist = true;
|
||||
for (GenerationInfo constructor : constructors) {
|
||||
alreadyExist &= aClass.findMethodBySignature((PsiMethod)constructor.getPsiMember(), false) != null;
|
||||
}
|
||||
if (alreadyExist) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return constructors;
|
||||
}
|
||||
|
||||
public static PsiMethod generateConstructorPrototype(PsiClass aClass, PsiMethod baseConstructor, boolean copyJavaDoc, PsiField[] fields) throws IncorrectOperationException {
|
||||
@@ -172,6 +185,7 @@ public class GenerateConstructorHandler extends GenerateMembersHandlerBase {
|
||||
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
|
||||
|
||||
PsiMethod constructor = factory.createConstructor();
|
||||
constructor.setName(aClass.getName());
|
||||
@Modifier String modifier = getConstructorModifier(aClass);
|
||||
if (modifier != null) {
|
||||
PsiUtil.setModifierProperty(constructor, modifier, true);
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package com.intellij.codeInsight.generation;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightActionHandler;
|
||||
import com.intellij.codeInsight.hint.HintManager;
|
||||
import com.intellij.codeInsight.template.Template;
|
||||
import com.intellij.codeInsight.template.TemplateEditingAdapter;
|
||||
import com.intellij.codeInsight.template.TemplateManager;
|
||||
@@ -114,10 +115,16 @@ public abstract class GenerateMembersHandlerBase implements CodeInsightActionHan
|
||||
LOG.error(e);
|
||||
return;
|
||||
}
|
||||
if (newMembers.isEmpty()) return;
|
||||
|
||||
editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(line, col));
|
||||
|
||||
if (newMembers.isEmpty()) {
|
||||
if (!ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
HintManager.getInstance().showErrorHint(editor, "Nothing found to insert");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
final ArrayList<TemplateGenerationInfo> templates = new ArrayList<TemplateGenerationInfo>();
|
||||
for (GenerationInfo member : newMembers) {
|
||||
if (member instanceof TemplateGenerationInfo) {
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
public class Test {
|
||||
<caret>
|
||||
|
||||
public Test() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
public class Test {
|
||||
<caret>
|
||||
|
||||
public Test() {
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ public class GenerateConstructorTest extends LightCodeInsightTestCase {
|
||||
public void testBoundComments() throws Exception { doTest(); }
|
||||
public void testSameNamedFields() throws Exception { doTest(); }
|
||||
public void testEnumWithAbstractMethod() throws Exception { doTest(); }
|
||||
public void testNoMoreConstructorsCanBeGenerated() throws Exception { doTest(); }
|
||||
|
||||
public void testImmediatelyAfterRBrace() throws Exception { // IDEADEV-28811
|
||||
final CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(getProject()).getCurrentSettings();
|
||||
|
||||
Reference in New Issue
Block a user