mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 21:55:01 +07:00
introduce variable in local class results to introduced field (IDEA-175213)
This commit is contained in:
+18
-9
@@ -919,8 +919,15 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
|
||||
initializer = simplifyVariableInitializer(initializer, selectedType.getType());
|
||||
|
||||
PsiType type = stripNullabilityAnnotationsFromTargetType(selectedType, project);
|
||||
PsiDeclarationStatement declaration = JavaPsiFacade.getInstance(project).getElementFactory()
|
||||
.createVariableDeclarationStatement(settings.getEnteredName(), type, initializer, container);
|
||||
PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
|
||||
PsiElement declaration;
|
||||
if (container instanceof PsiClass) {
|
||||
declaration = elementFactory.createField(settings.getEnteredName(), type);
|
||||
((PsiField)declaration).setInitializer(initializer);
|
||||
}
|
||||
else {
|
||||
declaration = elementFactory.createVariableDeclarationStatement(settings.getEnteredName(), type, initializer, container);
|
||||
}
|
||||
if (!isInsideLoop) {
|
||||
declaration = addDeclaration(declaration, initializer);
|
||||
LOG.assertTrue(expr1.isValid());
|
||||
@@ -940,7 +947,7 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
|
||||
}
|
||||
}
|
||||
|
||||
PsiExpression ref = JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText(settings.getEnteredName(), null);
|
||||
PsiExpression ref = elementFactory.createExpressionFromText(settings.getEnteredName(), null);
|
||||
if (settings.isReplaceAllOccurrences()) {
|
||||
ArrayList<PsiElement> array = new ArrayList<>();
|
||||
for (PsiExpression occurrence : occurrences) {
|
||||
@@ -970,9 +977,11 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
|
||||
}
|
||||
}
|
||||
|
||||
declaration = (PsiDeclarationStatement) RefactoringUtil.putStatementInLoopBody(declaration, container, anchorStatement, replaceSelf && replaceLoop);
|
||||
declaration = (PsiDeclarationStatement)JavaCodeStyleManager.getInstance(project).shortenClassReferences(declaration);
|
||||
PsiVariable var = (PsiVariable) declaration.getDeclaredElements()[0];
|
||||
if (declaration instanceof PsiDeclarationStatement) {
|
||||
declaration = RefactoringUtil.putStatementInLoopBody((PsiStatement)declaration, container, anchorStatement, replaceSelf && replaceLoop);
|
||||
}
|
||||
declaration = JavaCodeStyleManager.getInstance(project).shortenClassReferences(declaration);
|
||||
PsiVariable var = (PsiVariable) (declaration instanceof PsiDeclarationStatement ? ((PsiDeclarationStatement) declaration).getDeclaredElements()[0] : declaration);
|
||||
PsiUtil.setModifierProperty(var, PsiModifier.FINAL, settings.isDeclareFinal());
|
||||
fieldConflictsResolver.fix();
|
||||
return SmartPointerManager.getInstance(project).createSmartPsiElementPointer(var);
|
||||
@@ -982,7 +991,7 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
|
||||
return null;
|
||||
}
|
||||
|
||||
private PsiDeclarationStatement addDeclaration(PsiDeclarationStatement declaration, PsiExpression initializer) {
|
||||
private PsiElement addDeclaration(PsiElement declaration, PsiExpression initializer) {
|
||||
if (anchor instanceof PsiDeclarationStatement) {
|
||||
final PsiElement[] declaredElements = ((PsiDeclarationStatement)anchor).getDeclaredElements();
|
||||
if (declaredElements.length > 1) {
|
||||
@@ -1001,11 +1010,11 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
|
||||
final PsiVariable psiVariable = (PsiVariable)declaredElements[usedFirstVar[0]];
|
||||
psiVariable.normalizeDeclaration();
|
||||
final PsiDeclarationStatement parDeclarationStatement = PsiTreeUtil.getParentOfType(psiVariable, PsiDeclarationStatement.class);
|
||||
return (PsiDeclarationStatement)container.addAfter(declaration, parDeclarationStatement);
|
||||
return container.addAfter(declaration, parDeclarationStatement);
|
||||
}
|
||||
}
|
||||
}
|
||||
return (PsiDeclarationStatement) container.addBefore(declaration, anchor);
|
||||
return container.addBefore(declaration, anchor);
|
||||
}
|
||||
});
|
||||
return pointer != null ? pointer.getElement() : null;
|
||||
|
||||
+18
-9
@@ -63,7 +63,7 @@ import java.util.List;
|
||||
|
||||
public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer {
|
||||
|
||||
private SmartPsiElementPointer<PsiDeclarationStatement> myPointer;
|
||||
private SmartPsiElementPointer<? extends PsiElement> myPointer;
|
||||
|
||||
private JCheckBox myCanBeFinalCb;
|
||||
private final IntroduceVariableSettings mySettings;
|
||||
@@ -120,12 +120,12 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer
|
||||
|
||||
@Nullable
|
||||
protected PsiVariable getVariable() {
|
||||
final PsiDeclarationStatement declarationStatement = myPointer != null ? myPointer.getElement() : null;
|
||||
if (declarationStatement != null) {
|
||||
PsiElement[] declaredElements = declarationStatement.getDeclaredElements();
|
||||
final PsiElement declarationStatement = myPointer != null ? myPointer.getElement() : null;
|
||||
if (declarationStatement instanceof PsiDeclarationStatement) {
|
||||
PsiElement[] declaredElements = ((PsiDeclarationStatement)declarationStatement).getDeclaredElements();
|
||||
return declaredElements.length == 0 ? null : (PsiVariable)declaredElements[0];
|
||||
}
|
||||
return null;
|
||||
return declarationStatement instanceof PsiVariable ? (PsiVariable)declarationStatement : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -427,10 +427,19 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer
|
||||
protected PsiVariable introduceVariable() {
|
||||
PsiVariable variable = IntroduceVariableBase
|
||||
.introduce(myProject, myExpr, myEditor, myChosenAnchor.getElement(), getOccurrences(), mySettings);
|
||||
final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(variable, PsiDeclarationStatement.class);
|
||||
myPointer = declarationStatement != null ? SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(declarationStatement) : null;
|
||||
myEditor.putUserData(ReassignVariableUtil.DECLARATION_KEY, myPointer);
|
||||
setAdvertisementText(getAdvertisementText(declarationStatement, variable.getType(), myHasTypeSuggestion));
|
||||
SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject);
|
||||
if (variable instanceof PsiField) {
|
||||
myPointer = smartPointerManager.createSmartPsiElementPointer(variable);
|
||||
}
|
||||
else {
|
||||
final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(variable, PsiDeclarationStatement.class);
|
||||
if (declarationStatement != null) {
|
||||
SmartPsiElementPointer<PsiDeclarationStatement> pointer = smartPointerManager.createSmartPsiElementPointer(declarationStatement);
|
||||
myPointer = pointer;
|
||||
myEditor.putUserData(ReassignVariableUtil.DECLARATION_KEY, pointer);
|
||||
setAdvertisementText(getAdvertisementText(declarationStatement, variable.getType(), myHasTypeSuggestion));
|
||||
}
|
||||
}
|
||||
|
||||
PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myEditor.getDocument());
|
||||
return variable;
|
||||
|
||||
+2
-3
@@ -37,7 +37,6 @@ import com.intellij.refactoring.rename.inplace.InplaceRefactoring;
|
||||
import com.intellij.ui.ListCellRendererWrapper;
|
||||
import com.intellij.ui.awt.RelativePoint;
|
||||
import com.intellij.ui.components.JBList;
|
||||
import com.intellij.util.Processor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -92,7 +91,7 @@ public class ReassignVariableUtil {
|
||||
for (PsiVariable var : vars) {
|
||||
model.addElement(var);
|
||||
}
|
||||
final JBList list = new JBList(model);
|
||||
final JBList<PsiVariable> list = new JBList<>(model);
|
||||
list.setCellRenderer(new ListCellRendererWrapper<PsiVariable>() {
|
||||
@Override
|
||||
public void customize(JList list, PsiVariable value, int index, boolean selected, boolean hasFocus) {
|
||||
@@ -109,7 +108,7 @@ public class ReassignVariableUtil {
|
||||
JBPopupFactory.getInstance().createListPopupBuilder(list)
|
||||
.setTitle("Choose variable to reassign")
|
||||
.setRequestFocus(true)
|
||||
.setItemChoosenCallback(() -> replaceWithAssignment(declaration, (PsiVariable)list.getSelectedValue(), editor)).createPopup().show(new RelativePoint(editor.getContentComponent(), point));
|
||||
.setItemChoosenCallback(() -> replaceWithAssignment(declaration, list.getSelectedValue(), editor)).createPopup().show(new RelativePoint(editor.getContentComponent(), point));
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
class Main {
|
||||
int getSomething() {return 0;}
|
||||
|
||||
void testSimple() {
|
||||
class X {
|
||||
void test() {
|
||||
int x = getSomething();
|
||||
}
|
||||
|
||||
void test2() {
|
||||
int y = get<caret>Something();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
class Main {
|
||||
int getSomething() {return 0;}
|
||||
|
||||
void testSimple() {
|
||||
class X {
|
||||
private final int smth = getSomething();
|
||||
|
||||
void test() {
|
||||
int x = smth;
|
||||
}
|
||||
|
||||
void test2() {
|
||||
int y = smth;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
public class TestClass {
|
||||
void x() {
|
||||
new Exception() {
|
||||
final int j = doSomething();
|
||||
private final int j = doSomething();
|
||||
|
||||
int doSomething() { return 1; }
|
||||
void a() {
|
||||
|
||||
+9
@@ -134,6 +134,15 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe
|
||||
});
|
||||
}
|
||||
|
||||
public void testReplaceAllIntroduceFieldInLocalClass() throws Exception {
|
||||
doTestReplaceChoice(IntroduceVariableBase.JavaReplaceChoice.ALL, new Pass<AbstractInplaceIntroducer>() {
|
||||
@Override
|
||||
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
|
||||
type("smth");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void testRanges() throws Exception {
|
||||
doTest(new Pass<AbstractInplaceIntroducer>() {
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user