mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
inplace introduce: suggest unique names
This commit is contained in:
+2
-12
@@ -15,35 +15,24 @@
|
||||
*/
|
||||
package com.intellij.refactoring.introduceField;
|
||||
|
||||
import com.intellij.codeInsight.AnnotationUtil;
|
||||
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
|
||||
import com.intellij.codeInsight.template.impl.TemplateState;
|
||||
import com.intellij.ide.util.PropertiesComponent;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.RangeMarker;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.JavaRefactoringSettings;
|
||||
import com.intellij.refactoring.introduceParameter.AbstractJavaInplaceIntroducer;
|
||||
import com.intellij.refactoring.introduceParameter.VisibilityListener;
|
||||
import com.intellij.refactoring.move.moveMembers.MoveMembersImpl;
|
||||
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
|
||||
import com.intellij.refactoring.util.occurences.OccurenceManager;
|
||||
import com.intellij.ui.StateRestoringCheckBox;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ItemEvent;
|
||||
import java.awt.event.ItemListener;
|
||||
|
||||
@@ -165,7 +154,8 @@ public class InplaceIntroduceConstantPopup extends AbstractInplaceIntroduceField
|
||||
|
||||
@Override
|
||||
protected String[] suggestNames(PsiType defaultType, String propName) {
|
||||
return IntroduceConstantDialog.createNameSuggestionGenerator(propName, myExpr, JavaCodeStyleManager.getInstance(myProject), null)
|
||||
return IntroduceConstantDialog.createNameSuggestionGenerator(propName, myExpr, JavaCodeStyleManager.getInstance(myProject), null,
|
||||
myParentClass)
|
||||
.getSuggestedNameInfo(defaultType).names;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -107,7 +107,7 @@ public class InplaceIntroduceFieldPopup extends AbstractInplaceIntroduceFieldPop
|
||||
|
||||
@Override
|
||||
protected String[] suggestNames(PsiType defaultType, String propName) {
|
||||
return IntroduceFieldDialog.createGenerator(myStatic, (PsiLocalVariable)getLocalVariable(), myExpr, getLocalVariable() != null, null)
|
||||
return IntroduceFieldDialog.createGenerator(myStatic, (PsiLocalVariable)getLocalVariable(), myExpr, getLocalVariable() != null, null, myParentClass)
|
||||
.getSuggestedNameInfo(defaultType).names;
|
||||
}
|
||||
|
||||
|
||||
+13
-3
@@ -34,6 +34,7 @@ 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.impl.source.PsiClassImpl;
|
||||
import com.intellij.psi.impl.source.resolve.JavaResolveUtil;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.refactoring.HelpID;
|
||||
@@ -225,7 +226,7 @@ class IntroduceConstantDialog extends DialogWrapper {
|
||||
}
|
||||
final NameSuggestionsManager nameSuggestionsManager =
|
||||
new NameSuggestionsManager(myTypeSelector, myNameField, createNameSuggestionGenerator(propertyName, myInitializerExpression,
|
||||
myCodeStyleManager, myEnteredName));
|
||||
myCodeStyleManager, myEnteredName, myParentClass));
|
||||
|
||||
nameSuggestionsManager.setLabelsFor(myTypeLabel, myNameSuggestionLabel);
|
||||
//////////
|
||||
@@ -292,11 +293,20 @@ class IntroduceConstantDialog extends DialogWrapper {
|
||||
protected static NameSuggestionsGenerator createNameSuggestionGenerator(final String propertyName,
|
||||
final PsiExpression psiExpression,
|
||||
final JavaCodeStyleManager codeStyleManager,
|
||||
final String enteredName) {
|
||||
final String enteredName, final PsiClass parentClass) {
|
||||
return new NameSuggestionsGenerator() {
|
||||
public SuggestedNameInfo getSuggestedNameInfo(PsiType type) {
|
||||
final SuggestedNameInfo nameInfo =
|
||||
SuggestedNameInfo nameInfo =
|
||||
codeStyleManager.suggestVariableName(VariableKind.STATIC_FINAL_FIELD, propertyName, psiExpression, type);
|
||||
if (psiExpression != null) {
|
||||
String[] names = nameInfo.names;
|
||||
for (int i = 0, namesLength = names.length; i < namesLength; i++) {
|
||||
String name = names[i];
|
||||
if (parentClass.findFieldByName(name, false) != null) {
|
||||
names[i] = codeStyleManager.suggestUniqueVariableName(name, psiExpression, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
final String[] strings = JavaCompletionUtil
|
||||
.completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, nameInfo);
|
||||
return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[]{enteredName}, strings): strings, nameInfo);
|
||||
|
||||
+12
-2
@@ -177,7 +177,8 @@ class IntroduceFieldDialog extends DialogWrapper {
|
||||
namePrompt.setLabelFor(myNameField.getFocusableComponent());
|
||||
|
||||
myNameSuggestionsManager = new NameSuggestionsManager(myTypeSelector, myNameField,
|
||||
createGenerator(myWillBeDeclaredStatic, myLocalVariable, myInitializerExpression, myIsInvokedOnDeclaration, myEnteredName));
|
||||
createGenerator(myWillBeDeclaredStatic, myLocalVariable, myInitializerExpression, myIsInvokedOnDeclaration, myEnteredName,
|
||||
myParentClass));
|
||||
myNameSuggestionsManager.setLabelsFor(type, namePrompt);
|
||||
|
||||
return panel;
|
||||
@@ -200,7 +201,7 @@ class IntroduceFieldDialog extends DialogWrapper {
|
||||
static NameSuggestionsGenerator createGenerator(final boolean willBeDeclaredStatic,
|
||||
final PsiLocalVariable localVariable,
|
||||
final PsiExpression initializerExpression,
|
||||
final boolean isInvokedOnDeclaration, final String enteredName) {
|
||||
final boolean isInvokedOnDeclaration, final String enteredName, final PsiClass parentClass) {
|
||||
return new NameSuggestionsGenerator() {
|
||||
private final JavaCodeStyleManager myCodeStyleManager = JavaCodeStyleManager.getInstance(localVariable != null ? localVariable.getProject()
|
||||
: initializerExpression.getProject());
|
||||
@@ -212,6 +213,15 @@ class IntroduceFieldDialog extends DialogWrapper {
|
||||
propertyName = myCodeStyleManager.variableNameToPropertyName(localVariable.getName(), VariableKind.LOCAL_VARIABLE);
|
||||
}
|
||||
final SuggestedNameInfo nameInfo = myCodeStyleManager.suggestVariableName(variableKind, propertyName, initializerExpression, type);
|
||||
if (initializerExpression != null) {
|
||||
String[] names = nameInfo.names;
|
||||
for (int i = 0, namesLength = names.length; i < namesLength; i++) {
|
||||
String name = names[i];
|
||||
if (parentClass.findFieldByName(name, false) != null) {
|
||||
names[i] = myCodeStyleManager.suggestUniqueVariableName(name, initializerExpression, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
final String[] strings = JavaCompletionUtil.completeVariableNameForRefactoring(myCodeStyleManager, type, VariableKind.LOCAL_VARIABLE, nameInfo);
|
||||
return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[]{enteredName}, strings) : strings, nameInfo);
|
||||
}
|
||||
|
||||
+4
-1
@@ -272,7 +272,10 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase implements R
|
||||
return new NameSuggestionsGenerator() {
|
||||
public SuggestedNameInfo getSuggestedNameInfo(PsiType type) {
|
||||
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
|
||||
final SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, propName, expr, type);
|
||||
SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, propName, expr, type);
|
||||
if (expr != null) {
|
||||
info = codeStyleManager.suggestUniqueVariableName(info, expr, true);
|
||||
}
|
||||
final String[] strings = JavaCompletionUtil
|
||||
.completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, info);
|
||||
return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[]{enteredName}, strings): strings, info);
|
||||
|
||||
+2
-2
@@ -15,10 +15,10 @@
|
||||
*/
|
||||
class Test {
|
||||
private static final String FOO = "foo";
|
||||
public static final String BAR = "foo";
|
||||
public static final String FOO1 = "foo";
|
||||
|
||||
public void method(Map<Object, Object> args) {
|
||||
System.out.println(FOO);
|
||||
System.out.println(BAR);
|
||||
System.out.println(FOO1);
|
||||
}
|
||||
}
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2000-2011 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
class Test {
|
||||
public void method(String FOO) {
|
||||
System.out.println(FOO);
|
||||
System.out.println("fo<caret>o");
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2000-2011 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
class Test {
|
||||
|
||||
public static final String FOO = "foo";
|
||||
|
||||
public void method(String FOO) {
|
||||
System.out.println(FOO);
|
||||
System.out.println(Test.FOO);
|
||||
}
|
||||
}
|
||||
@@ -60,7 +60,15 @@ public class InplaceIntroduceConstantTest extends AbstractInplaceIntroduceTest {
|
||||
doTest(new Pass<AbstractInplaceIntroducer>() {
|
||||
@Override
|
||||
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
|
||||
type("BAR");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void testNoConflictingConstantName() throws Exception {
|
||||
|
||||
doTest(new Pass<AbstractInplaceIntroducer>() {
|
||||
@Override
|
||||
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user