mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 21:55:01 +07:00
force "prefer long names" if multiple fields/parameters of same type are generated (~ IDEA-99564)
This commit is contained in:
+21
-7
@@ -36,9 +36,7 @@ import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.SuggestedNameInfo;
|
||||
import com.intellij.psi.codeStyle.VariableKind;
|
||||
import com.intellij.psi.codeStyle.*;
|
||||
import com.intellij.psi.search.LocalSearchScope;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
@@ -48,6 +46,8 @@ import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
import com.intellij.util.text.UniqueNameGenerator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -246,14 +246,27 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction {
|
||||
|
||||
int i = 0;
|
||||
final HashMap<PsiField, String> usedFields = new HashMap<PsiField, String>();
|
||||
final MultiMap<PsiType, PsiVariable> types = new MultiMap<PsiType, PsiVariable>();
|
||||
for (PsiVariable param : params) {
|
||||
types.putValue(param.getType(), param);
|
||||
}
|
||||
|
||||
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
|
||||
final boolean preferLongerNames = settings.PREFER_LONGER_NAMES;
|
||||
for (PsiVariable param : params) {
|
||||
final PsiType paramType = param.getType();
|
||||
if (param instanceof PsiParameter) {
|
||||
newParamInfos[i++] = new ParameterInfoImpl(parameterList.getParameterIndex((PsiParameter)param), param.getName(), paramType, param.getName());
|
||||
} else {
|
||||
final String uniqueParameterName = getUniqueParameterName(parameters, param, usedFields);
|
||||
usedFields.put((PsiField)param, uniqueParameterName);
|
||||
newParamInfos[i++] = new ParameterInfoImpl(-1, uniqueParameterName, paramType, uniqueParameterName);
|
||||
try {
|
||||
settings.PREFER_LONGER_NAMES = preferLongerNames || types.get(paramType).size() > 1;
|
||||
final String uniqueParameterName = getUniqueParameterName(parameters, param, usedFields);
|
||||
usedFields.put((PsiField)param, uniqueParameterName);
|
||||
newParamInfos[i++] = new ParameterInfoImpl(-1, uniqueParameterName, paramType, uniqueParameterName);
|
||||
}
|
||||
finally {
|
||||
settings.PREFER_LONGER_NAMES = preferLongerNames;
|
||||
}
|
||||
}
|
||||
}
|
||||
final SmartPointerManager manager = SmartPointerManager.getInstance(project);
|
||||
@@ -298,7 +311,8 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction {
|
||||
if (isUnique(parameters, newName, usedNames)) {
|
||||
break;
|
||||
}
|
||||
newName = nameInfo.names[0] + n++;
|
||||
newName = n < nameInfo.names.length && !CodeStyleSettingsManager.getSettings(variable.getProject()).PREFER_LONGER_NAMES
|
||||
? nameInfo.names[n++] : nameInfo.names[0] + n++;
|
||||
}
|
||||
return newName;
|
||||
}
|
||||
|
||||
+27
-5
@@ -28,12 +28,12 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.SuggestedNameInfo;
|
||||
import com.intellij.psi.codeStyle.VariableKind;
|
||||
import com.intellij.psi.codeStyle.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -159,8 +159,20 @@ public class BindFieldsFromParametersAction extends BaseIntentionAction implemen
|
||||
LOG.assertTrue(method != null);
|
||||
|
||||
final HashSet<String> usedNames = new HashSet<String>();
|
||||
for (PsiParameter selected : selectParameters(project, method, copyUnboundedParamsAndClearOriginal(method), isInteractive)) {
|
||||
processParameter(project, selected, usedNames);
|
||||
final Iterable<PsiParameter> parameters = selectParameters(project, method, copyUnboundedParamsAndClearOriginal(method), isInteractive);
|
||||
final MultiMap<PsiType, PsiParameter> types = new MultiMap<PsiType, PsiParameter>();
|
||||
for (PsiParameter parameter : parameters) {
|
||||
types.putValue(parameter.getType(), parameter);
|
||||
}
|
||||
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
|
||||
final boolean preferLongerNames = settings.PREFER_LONGER_NAMES;
|
||||
for (PsiParameter selected : parameters) {
|
||||
try {
|
||||
settings.PREFER_LONGER_NAMES = preferLongerNames || types.get(selected.getType()).size() > 1;
|
||||
processParameter(project, selected, usedNames);
|
||||
} finally {
|
||||
settings.PREFER_LONGER_NAMES = preferLongerNames;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,6 +287,16 @@ public class BindFieldsFromParametersAction extends BaseIntentionAction implemen
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (usedNames.contains(name)) {
|
||||
for (String curName : names) {
|
||||
if (!usedNames.contains(curName)) {
|
||||
name = curName;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final String fieldName = usedNames.add(name) ? name
|
||||
: JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName(name, myParameter, true);
|
||||
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Bind constructor parameters to fields" "true"
|
||||
|
||||
class A{
|
||||
private final String myManager;
|
||||
private final Integer myNewManager;
|
||||
|
||||
public A(String oldManager, Integer newManager) {
|
||||
myManager = oldManager;
|
||||
myNewManager = newManager;
|
||||
}
|
||||
}
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Bind constructor parameters to fields" "true"
|
||||
|
||||
class A{
|
||||
private final String myOldClass;
|
||||
private final String myNewClass;
|
||||
|
||||
public A(String oldClass, String newClass) {
|
||||
myOldClass = oldClass;
|
||||
myNewClass = newClass;
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Bind constructor parameters to fields" "true"
|
||||
|
||||
class A{
|
||||
public A(String old<caret>Manager, Integer newManager) {
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Bind constructor parameters to fields" "true"
|
||||
|
||||
class A{
|
||||
public A(String old<caret>Class, String newClass) {
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Add constructor parameters" "true"
|
||||
class A {
|
||||
|
||||
private final String oldClass;
|
||||
private final String newClass;
|
||||
|
||||
A(String oldClass, String newClass) {
|
||||
this.oldClass = oldClass;
|
||||
this.newClass = newClass;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Add constructor parameters" "true"
|
||||
class A {
|
||||
|
||||
private final LibraryManager libraryManager;
|
||||
private final DependencyManager dependencyManager;
|
||||
|
||||
A(LibraryManager manager, DependencyManager dependencyManager) {
|
||||
this.libraryManager = manager;
|
||||
this.dependencyManager = dependencyManager;
|
||||
}
|
||||
|
||||
|
||||
private static class LibraryManager {}
|
||||
private static class DependencyManager {}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Add constructor parameters" "true"
|
||||
class A {
|
||||
|
||||
private final String old<caret>Class;
|
||||
private final String newClass;
|
||||
|
||||
A() {}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Add constructor parameters" "true"
|
||||
class A {
|
||||
|
||||
private final LibraryManager libr<caret>aryManager;
|
||||
private final DependencyManager dependencyManager;
|
||||
|
||||
A() {}
|
||||
|
||||
|
||||
private static class LibraryManager {}
|
||||
private static class DependencyManager {}
|
||||
}
|
||||
+7
@@ -23,17 +23,24 @@ import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
* @author Danila Ponomarenko
|
||||
*/
|
||||
public class BindFieldsFromParametersTest extends LightIntentionActionTestCase {
|
||||
private boolean myPreferLongNames;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
|
||||
settings.FIELD_NAME_PREFIX = "my";
|
||||
myPreferLongNames = settings.PREFER_LONGER_NAMES;
|
||||
if (getTestName(false).contains("SameParam")) {
|
||||
settings.PREFER_LONGER_NAMES = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
|
||||
settings.FIELD_NAME_PREFIX = "";
|
||||
settings.PREFER_LONGER_NAMES = myPreferLongNames;
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
|
||||
+15
@@ -1,6 +1,8 @@
|
||||
package com.intellij.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.siyeh.ig.style.MissortedModifiersInspection;
|
||||
import com.siyeh.ig.style.UnqualifiedFieldAccessInspection;
|
||||
|
||||
@@ -9,10 +11,23 @@ import com.siyeh.ig.style.UnqualifiedFieldAccessInspection;
|
||||
*/
|
||||
public class CreateConstructorParameterFromFieldTest extends LightQuickFixParameterizedTestCase {
|
||||
|
||||
private boolean myPreferLongNames;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
enableInspectionTools(new UnusedDeclarationInspection(), new MissortedModifiersInspection(), new UnqualifiedFieldAccessInspection());
|
||||
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
|
||||
myPreferLongNames = settings.PREFER_LONGER_NAMES;
|
||||
if (getTestName(false).contains("SameParameter")) {
|
||||
settings.PREFER_LONGER_NAMES = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
CodeStyleSettingsManager.getSettings(getProject()).PREFER_LONGER_NAMES = myPreferLongNames;
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public void test() throws Exception { doAllTests(); }
|
||||
|
||||
Reference in New Issue
Block a user