mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
inplace refactorings: introduce parameter: enable inplace for private methods, without any settings only (in progress)
This commit is contained in:
+255
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
package com.intellij.refactoring.introduceParameter;
|
||||
|
||||
import com.intellij.codeInsight.intention.impl.TypeExpression;
|
||||
import com.intellij.ide.IdeTooltipManager;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.RangeMarker;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.popup.Balloon;
|
||||
import com.intellij.openapi.ui.popup.BalloonBuilder;
|
||||
import com.intellij.openapi.ui.popup.JBPopupFactory;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.util.Pass;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.JavaRefactoringSettings;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.introduceVariable.OccurrencesChooser;
|
||||
import com.intellij.refactoring.introduceVariable.VariableInplaceIntroducer;
|
||||
import com.intellij.refactoring.rename.inplace.VariableInplaceRenamer;
|
||||
import com.intellij.refactoring.ui.NameSuggestionsGenerator;
|
||||
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
|
||||
import com.intellij.ui.NonFocusableCheckBox;
|
||||
import com.intellij.ui.awt.RelativePoint;
|
||||
import gnu.trove.TIntArrayList;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
* Date: 2/25/11
|
||||
*/
|
||||
class InplaceIntroduceParameterPopup extends JPanel {
|
||||
private JCheckBox myDelegateCb;
|
||||
private Balloon myBalloon;
|
||||
private final Project myProject;
|
||||
private final Editor myEditor;
|
||||
private final TypeSelectorManagerImpl myTypeSelectorManager;
|
||||
private final NameSuggestionsGenerator myNameSuggestionsGenerator;
|
||||
private final PsiExpression myExpr;
|
||||
private final PsiLocalVariable myLocalVar;
|
||||
private final PsiMethod myMethod;
|
||||
private final PsiMethod myMethodToSearchFor;
|
||||
private final PsiExpression[] myOccurrences;
|
||||
private final TIntArrayList myParametersToRemove;
|
||||
private final boolean myMustBeFinal;
|
||||
private final RangeMarker myExprMarker;
|
||||
private final List<RangeMarker> myOccurrenceMarkers;
|
||||
|
||||
|
||||
InplaceIntroduceParameterPopup(final Project project,
|
||||
final Editor editor,
|
||||
final TypeSelectorManagerImpl typeSelectorManager,
|
||||
final NameSuggestionsGenerator nameSuggestionsGenerator,
|
||||
final PsiExpression expr,
|
||||
final PsiLocalVariable localVar,
|
||||
final PsiMethod method,
|
||||
final PsiMethod methodToSearchFor,
|
||||
final PsiExpression[] occurrences,
|
||||
final TIntArrayList parametersToRemove,
|
||||
final boolean mustBeFinal) {
|
||||
super(new GridBagLayout());
|
||||
myProject = project;
|
||||
myEditor = editor;
|
||||
myTypeSelectorManager = typeSelectorManager;
|
||||
myNameSuggestionsGenerator = nameSuggestionsGenerator;
|
||||
myExpr = expr;
|
||||
myLocalVar = localVar;
|
||||
myMethod = method;
|
||||
myMethodToSearchFor = methodToSearchFor;
|
||||
myOccurrences = occurrences;
|
||||
myParametersToRemove = parametersToRemove;
|
||||
myMustBeFinal = mustBeFinal;
|
||||
myExprMarker = myEditor.getDocument().createRangeMarker(myExpr.getTextRange());
|
||||
myOccurrenceMarkers = new ArrayList<RangeMarker>();
|
||||
|
||||
setBorder(BorderFactory.createTitledBorder(IntroduceParameterHandler.REFACTORING_NAME));
|
||||
myDelegateCb = new NonFocusableCheckBox(RefactoringBundle.message("delegation.panel.delegate.via.overloading.method"));
|
||||
final GridBagConstraints gc =
|
||||
new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 0), 0, 0);
|
||||
add(myDelegateCb, gc);
|
||||
}
|
||||
|
||||
|
||||
void inplaceIntroduceParameter() {
|
||||
final LinkedHashMap<OccurrencesChooser.ReplaceChoice, PsiExpression[]> occurrencesMap =
|
||||
new LinkedHashMap<OccurrencesChooser.ReplaceChoice, PsiExpression[]>();
|
||||
|
||||
for (PsiExpression occurrence : myOccurrences) {
|
||||
myOccurrenceMarkers.add(myEditor.getDocument().createRangeMarker(occurrence.getTextRange()));
|
||||
}
|
||||
|
||||
OccurrencesChooser.fillChoices(myExpr, myOccurrences, occurrencesMap);
|
||||
new OccurrencesChooser(myEditor).showChooser(new IntroduceParameterPass(), occurrencesMap);
|
||||
}
|
||||
|
||||
private class ParameterInplaceIntroducer extends VariableInplaceIntroducer {
|
||||
private String myParameterName;
|
||||
private SmartTypePointer myParameterTypePointer;
|
||||
private SmartTypePointer myDefaultParameterTypePointer;
|
||||
private final PsiParameter myParameter;
|
||||
private final int myParameterIndex;
|
||||
|
||||
private final SmartPsiElementPointer<PsiExpression> myExpressionPointer;
|
||||
private final OccurrencesChooser.ReplaceChoice myReplaceChoice;
|
||||
private boolean myFinal;
|
||||
|
||||
public ParameterInplaceIntroducer(PsiParameter parameter,
|
||||
OccurrencesChooser.ReplaceChoice replaceChoice) {
|
||||
super(myProject, new TypeExpression(myProject, myTypeSelectorManager.getTypesForAll()),
|
||||
myEditor, parameter, myMustBeFinal,
|
||||
myTypeSelectorManager.getTypesForAll().length > 1, myExprMarker, myOccurrenceMarkers);
|
||||
myParameter = parameter;
|
||||
myReplaceChoice = replaceChoice;
|
||||
myExpressionPointer = SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(myExpr);
|
||||
myParameterIndex = myMethod.getParameterList().getParameterIndex(myParameter);
|
||||
myDefaultParameterTypePointer = SmartTypePointerManager.getInstance(myProject).createSmartTypePointer(parameter.getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PsiVariable getVariable() {
|
||||
return myMethod.getParameterList().getParameters()[myParameterIndex];
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void saveSettings(PsiVariable psiVariable) {
|
||||
JavaRefactoringSettings.getInstance().INTRODUCE_PARAMETER_CREATE_FINALS = psiVariable.hasModifierProperty(PsiModifier.FINAL);
|
||||
TypeSelectorManagerImpl.typeSelected(psiVariable.getType(), myDefaultParameterTypePointer.getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void moveOffsetAfter(boolean success) {
|
||||
if (success) {
|
||||
final IntroduceParameterProcessor processor =
|
||||
new IntroduceParameterProcessor(myProject, myMethod,
|
||||
myMethodToSearchFor, myExpressionPointer.getElement(), myExpressionPointer.getElement(),
|
||||
myLocalVar, true, myParameterName,
|
||||
myReplaceChoice == OccurrencesChooser.ReplaceChoice.ALL,
|
||||
0, myMustBeFinal || myFinal, myDelegateCb.isSelected(),
|
||||
myParameterTypePointer.getType(),
|
||||
myParametersToRemove) {
|
||||
@Override
|
||||
protected PsiElement[] getOccurrences() {
|
||||
return myOccurrences;
|
||||
}
|
||||
};
|
||||
processor.run();
|
||||
}
|
||||
super.moveOffsetAfter(success);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
super.finish();
|
||||
myBalloon.hide();
|
||||
final PsiParameter psiParameter = myMethod.getParameterList().getParameters()[myParameterIndex];
|
||||
myParameterName = psiParameter.getName();
|
||||
myFinal = psiParameter.hasModifierProperty(PsiModifier.FINAL);
|
||||
myParameterTypePointer = SmartTypePointerManager.getInstance(myProject).createSmartTypePointer(psiParameter.getType());
|
||||
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
public void run() {
|
||||
if (psiParameter.isValid()) {
|
||||
psiParameter.delete();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
public boolean createFinals() {
|
||||
return hasFinalModifier();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasFinalModifier() {
|
||||
final Boolean createFinals = JavaRefactoringSettings.getInstance().INTRODUCE_PARAMETER_CREATE_FINALS;
|
||||
return createFinals == null ? CodeStyleSettingsManager.getSettings(myProject).GENERATE_FINAL_PARAMETERS : createFinals.booleanValue();
|
||||
}
|
||||
|
||||
private class IntroduceParameterPass extends Pass<OccurrencesChooser.ReplaceChoice> {
|
||||
|
||||
@Override
|
||||
public void pass(final OccurrencesChooser.ReplaceChoice replaceChoice) {
|
||||
CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {
|
||||
public void run() {
|
||||
myTypeSelectorManager.setAllOccurences(replaceChoice != OccurrencesChooser.ReplaceChoice.NO);
|
||||
final PsiType defaultType = myTypeSelectorManager.getTypeSelector().getSelectedType();
|
||||
final String[] names = myNameSuggestionsGenerator.getSuggestedNameInfo(defaultType).names;
|
||||
final PsiParameter parameter = createParameterToStartTemplateOn(names, defaultType);
|
||||
if (parameter != null) {
|
||||
myEditor.getCaretModel().moveToOffset(parameter.getTextOffset());
|
||||
showSettingsPopup();
|
||||
final VariableInplaceRenamer renamer =
|
||||
new ParameterInplaceIntroducer(parameter, replaceChoice);
|
||||
renamer.performInplaceRename(false, new LinkedHashSet<String>(Arrays.asList(names)));
|
||||
}
|
||||
}
|
||||
}, IntroduceParameterHandler.REFACTORING_NAME, null);
|
||||
}
|
||||
|
||||
private PsiParameter createParameterToStartTemplateOn(final String[] names,
|
||||
final PsiType defaultType) {
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(myMethod.getProject());
|
||||
return ApplicationManager.getApplication().runWriteAction(new Computable<PsiParameter>() {
|
||||
@Override
|
||||
public PsiParameter compute() {
|
||||
final PsiParameter psiParameter = (PsiParameter)myMethod.getParameterList()
|
||||
.addAfter(elementFactory.createParameter(names[0], defaultType),
|
||||
JavaIntroduceParameterMethodUsagesProcessor.getAnchorParameter(myMethod));
|
||||
PsiUtil.setModifierProperty(psiParameter, PsiModifier.FINAL, hasFinalModifier());
|
||||
return psiParameter;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void showSettingsPopup() {
|
||||
BalloonBuilder balloonBuilder = JBPopupFactory.getInstance().createBalloonBuilder(InplaceIntroduceParameterPopup.this);
|
||||
balloonBuilder.setFadeoutTime(0);
|
||||
balloonBuilder.setFillColor(IdeTooltipManager.GRAPHITE_COLOR);
|
||||
balloonBuilder.setAnimationCycle(0);
|
||||
balloonBuilder.setHideOnClickOutside(false);
|
||||
balloonBuilder.setHideOnKeyOutside(false);
|
||||
balloonBuilder.setHideOnAction(false);
|
||||
balloonBuilder.setCloseButtonEnabled(true);
|
||||
final RelativePoint target = JBPopupFactory.getInstance().guessBestPopupLocation(myEditor);
|
||||
final Point screenPoint = target.getScreenPoint();
|
||||
myBalloon = balloonBuilder.createBalloon();
|
||||
myBalloon
|
||||
.show(new RelativePoint(new Point(screenPoint.x, screenPoint.y - myEditor.getLineHeight())), Balloon.Position.above);
|
||||
}
|
||||
}
|
||||
}
|
||||
+20
-9
@@ -42,7 +42,6 @@ import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.*;
|
||||
import com.intellij.refactoring.introduceField.ElementToWorkOn;
|
||||
import com.intellij.refactoring.ui.NameSuggestionsGenerator;
|
||||
import com.intellij.refactoring.ui.TypeSelectorManager;
|
||||
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
@@ -53,13 +52,13 @@ import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class IntroduceParameterHandler extends IntroduceHandlerBase implements RefactoringActionHandler {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.introduceParameter.IntroduceParameterHandler");
|
||||
private static final String REFACTORING_NAME = RefactoringBundle.message("introduce.parameter.title");
|
||||
static final String REFACTORING_NAME = RefactoringBundle.message("introduce.parameter.title");
|
||||
private Project myProject;
|
||||
|
||||
public void invoke(@NotNull final Project project, final Editor editor, PsiFile file, DataContext dataContext) {
|
||||
@@ -153,9 +152,6 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase implements R
|
||||
expressionToRemoveParamFrom = localVar.getInitializer();
|
||||
}
|
||||
TIntArrayList parametersToRemove = expressionToRemoveParamFrom == null ? new TIntArrayList() : Util.findParametersToRemove(method, expressionToRemoveParamFrom, occurences);
|
||||
if (editor != null) {
|
||||
RefactoringUtil.highlightAllOccurences(myProject, occurences, editor);
|
||||
}
|
||||
|
||||
boolean mustBeFinal = false;
|
||||
if (localVar != null) {
|
||||
@@ -198,7 +194,7 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase implements R
|
||||
final String propName = localVar != null ? JavaCodeStyleManager.getInstance(myProject).variableNameToPropertyName(localVar.getName(), VariableKind.LOCAL_VARIABLE) : null;
|
||||
final PsiType initializerType = IntroduceParameterProcessor.getInitializerType(null, expr, localVar);
|
||||
|
||||
TypeSelectorManager typeSelectorManager = expr != null
|
||||
TypeSelectorManagerImpl typeSelectorManager = expr != null
|
||||
? new TypeSelectorManagerImpl(project, initializerType, expr, occurences)
|
||||
: new TypeSelectorManagerImpl(project, initializerType, occurences);
|
||||
|
||||
@@ -211,8 +207,23 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase implements R
|
||||
}
|
||||
|
||||
};
|
||||
new IntroduceParameterDialog(myProject, classMemberRefs, occurences.length, localVar, expr, nameSuggestionsGenerator,
|
||||
typeSelectorManager, methodToSearchFor, method, parametersToRemove, mustBeFinal).show();
|
||||
boolean isInplaceAvailableOnDataContext = editor != null && editor.getSettings().isVariableInplaceRenameEnabled()
|
||||
&& method == methodToSearchFor && method.hasModifierProperty(PsiModifier.PRIVATE) &&
|
||||
parametersToRemove.isEmpty() && (localVar == null || expr == null) &&
|
||||
!Util.anyFieldsWithGettersPresent(classMemberRefs);
|
||||
|
||||
if (!isInplaceAvailableOnDataContext) {
|
||||
if (editor != null) {
|
||||
RefactoringUtil.highlightAllOccurences(myProject, occurences, editor);
|
||||
}
|
||||
new IntroduceParameterDialog(myProject, classMemberRefs, occurences.length, localVar, expr, nameSuggestionsGenerator,
|
||||
typeSelectorManager, methodToSearchFor, method, parametersToRemove, mustBeFinal).show();
|
||||
} else {
|
||||
new InplaceIntroduceParameterPopup(project, editor,
|
||||
typeSelectorManager, nameSuggestionsGenerator,
|
||||
expr, localVar, method, methodToSearchFor, occurences, parametersToRemove,
|
||||
mustBeFinal).inplaceIntroduceParameter();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
+12
-9
@@ -170,15 +170,7 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem
|
||||
}
|
||||
|
||||
if (myReplaceAllOccurences) {
|
||||
final OccurenceManager occurenceManager;
|
||||
if (myLocalVariable == null) {
|
||||
occurenceManager = new ExpressionOccurenceManager(myExpressionToSearch, myMethodToReplaceIn, null);
|
||||
}
|
||||
else {
|
||||
occurenceManager = new LocalVariableOccurenceManager(myLocalVariable, null);
|
||||
}
|
||||
PsiElement[] exprs = occurenceManager.getOccurences();
|
||||
for (PsiElement expr : exprs) {
|
||||
for (PsiElement expr : getOccurrences()) {
|
||||
result.add(new InternalUsageInfo(expr));
|
||||
}
|
||||
}
|
||||
@@ -192,6 +184,17 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem
|
||||
return UsageViewUtil.removeDuplicatedUsages(usageInfos);
|
||||
}
|
||||
|
||||
protected PsiElement[] getOccurrences() {
|
||||
final OccurenceManager occurenceManager;
|
||||
if (myLocalVariable == null) {
|
||||
occurenceManager = new ExpressionOccurenceManager(myExpressionToSearch, myMethodToReplaceIn, null);
|
||||
}
|
||||
else {
|
||||
occurenceManager = new LocalVariableOccurenceManager(myLocalVariable, null);
|
||||
}
|
||||
return occurenceManager.getOccurences();
|
||||
}
|
||||
|
||||
private static class ReferencedElementsCollector extends JavaRecursiveElementWalkingVisitor {
|
||||
private final Set<PsiElement> myResult = new HashSet<PsiElement>();
|
||||
|
||||
|
||||
+1
-1
@@ -209,7 +209,7 @@ public class JavaIntroduceParameterMethodUsagesProcessor implements IntroducePar
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiParameter getAnchorParameter(PsiMethod methodToReplaceIn) {
|
||||
public static PsiParameter getAnchorParameter(PsiMethod methodToReplaceIn) {
|
||||
PsiParameterList parameterList = methodToReplaceIn.getParameterList();
|
||||
final PsiParameter anchorParameter;
|
||||
final PsiParameter[] parameters = parameterList.getParameters();
|
||||
|
||||
+3
-3
@@ -134,9 +134,9 @@ public class OccurrencesChooser {
|
||||
/**
|
||||
* @return true if write usages found
|
||||
*/
|
||||
static boolean fillChoices(final PsiExpression expr,
|
||||
final PsiExpression[] occurrences,
|
||||
final LinkedHashMap<ReplaceChoice, PsiExpression[]> occurrencesMap) {
|
||||
public static boolean fillChoices(final PsiExpression expr,
|
||||
final PsiExpression[] occurrences,
|
||||
final LinkedHashMap<ReplaceChoice, PsiExpression[]> occurrencesMap) {
|
||||
occurrencesMap.put(ReplaceChoice.NO, new PsiExpression[]{expr});
|
||||
|
||||
final List<PsiExpression> nonWrite = new ArrayList<PsiExpression>();
|
||||
|
||||
+48
-39
@@ -53,7 +53,7 @@ import java.util.List;
|
||||
* User: anna
|
||||
* Date: 12/8/10
|
||||
*/
|
||||
class VariableInplaceIntroducer extends VariableInplaceRenamer {
|
||||
public class VariableInplaceIntroducer extends VariableInplaceRenamer {
|
||||
private final PsiVariable myElementToRename;
|
||||
private final Editor myEditor;
|
||||
private final TypeExpression myExpression;
|
||||
@@ -85,7 +85,7 @@ class VariableInplaceIntroducer extends VariableInplaceRenamer {
|
||||
myDefaultType = elementToRename.getType();
|
||||
|
||||
final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(elementToRename, PsiDeclarationStatement.class);
|
||||
myPointer = SmartPointerManager.getInstance(project).createSmartPsiElementPointer(declarationStatement);
|
||||
myPointer = declarationStatement != null ? SmartPointerManager.getInstance(project).createSmartPsiElementPointer(declarationStatement) : null;
|
||||
editor.putUserData(ReassignVariableUtil.DECLARATION_KEY, myPointer);
|
||||
editor.putUserData(ReassignVariableUtil.OCCURRENCES_KEY,
|
||||
occurrenceMarkers.toArray(new RangeMarker[occurrenceMarkers.size()]));
|
||||
@@ -100,16 +100,14 @@ class VariableInplaceIntroducer extends VariableInplaceRenamer {
|
||||
createExpression(myExpression, typeElement.getText(), !myCantChangeFinalModifier), true,
|
||||
true);
|
||||
if (!myCantChangeFinalModifier) {
|
||||
builder.replaceElement(myElementToRename.getModifierList(), "_FINAL_", new FinalExpression(myProject), false, true);
|
||||
builder.replaceElement(myElementToRename.getModifierList(), "_FINAL_", new FinalExpression(), false, true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LookupElement[] createLookupItems(LookupElement[] lookupItems, String name) {
|
||||
TemplateState templateState = TemplateManagerImpl.getTemplateState(myEditor);
|
||||
final PsiDeclarationStatement declarationStatement = myPointer.getElement();
|
||||
final PsiVariable psiVariable =
|
||||
declarationStatement != null ? (PsiVariable)declarationStatement.getDeclaredElements()[0] : null;
|
||||
final PsiVariable psiVariable = getVariable();
|
||||
if (psiVariable != null) {
|
||||
final TextResult insertedValue =
|
||||
templateState != null ? templateState.getVariableValue(PRIMARY_VARIABLE_NAME) : null;
|
||||
@@ -133,6 +131,12 @@ class VariableInplaceIntroducer extends VariableInplaceRenamer {
|
||||
return super.createLookupItems(lookupItems, name);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected PsiVariable getVariable() {
|
||||
final PsiDeclarationStatement declarationStatement = myPointer.getElement();
|
||||
return declarationStatement != null ? (PsiVariable)declarationStatement.getDeclaredElements()[0] : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TextRange preserveSelectedRange(SelectionModel selectionModel) {
|
||||
return null;
|
||||
@@ -143,33 +147,32 @@ class VariableInplaceIntroducer extends VariableInplaceRenamer {
|
||||
try {
|
||||
if (success) {
|
||||
final Document document = myEditor.getDocument();
|
||||
final PsiDeclarationStatement declarationStatement = myPointer.getElement();
|
||||
if (declarationStatement == null) return;
|
||||
final PsiElement[] declaredElements = declarationStatement.getDeclaredElements();
|
||||
final @Nullable PsiVariable psiVariable = declaredElements.length > 0 ? (PsiVariable)declaredElements[0] : null;
|
||||
if (psiVariable != null) {
|
||||
JavaRefactoringSettings.getInstance().INTRODUCE_LOCAL_CREATE_FINALS = psiVariable.hasModifierProperty(PsiModifier.FINAL);
|
||||
FinalExpression.adjustLine(psiVariable, document);
|
||||
final @Nullable PsiVariable psiVariable = getVariable();
|
||||
if (psiVariable == null) {
|
||||
return;
|
||||
}
|
||||
saveSettings(psiVariable);
|
||||
adjustLine(psiVariable, document);
|
||||
int startOffset = myExprMarker.getStartOffset();
|
||||
final PsiFile file = declarationStatement.getContainingFile();
|
||||
final PsiFile file = psiVariable.getContainingFile();
|
||||
final PsiReference referenceAt = file.findReferenceAt(startOffset);
|
||||
if (referenceAt != null && referenceAt.resolve() instanceof PsiLocalVariable) {
|
||||
startOffset = referenceAt.getElement().getTextRange().getEndOffset();
|
||||
}
|
||||
else if (declarationStatement != null) {
|
||||
startOffset = declarationStatement.getTextRange().getEndOffset();
|
||||
else {
|
||||
final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(psiVariable, PsiDeclarationStatement.class);
|
||||
if (declarationStatement != null) {
|
||||
startOffset = declarationStatement.getTextRange().getEndOffset();
|
||||
}
|
||||
}
|
||||
myEditor.getCaretModel().moveToOffset(startOffset);
|
||||
final PsiType selectedType = ReassignVariableUtil.getVariableType(declarationStatement);
|
||||
if (selectedType != null) {
|
||||
TypeSelectorManagerImpl.typeSelected(selectedType, myDefaultType);
|
||||
if (psiVariable.getInitializer() != null) {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
public void run() {
|
||||
appendTypeCasts(myOccurrenceMarkers, file, myProject, psiVariable);
|
||||
}
|
||||
});
|
||||
}
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
public void run() {
|
||||
appendTypeCasts(myOccurrenceMarkers, file, myProject, psiVariable);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
finally {
|
||||
@@ -182,6 +185,11 @@ class VariableInplaceIntroducer extends VariableInplaceRenamer {
|
||||
}
|
||||
}
|
||||
|
||||
protected void saveSettings(PsiVariable psiVariable) {
|
||||
JavaRefactoringSettings.getInstance().INTRODUCE_LOCAL_CREATE_FINALS = psiVariable.hasModifierProperty(PsiModifier.FINAL);
|
||||
TypeSelectorManagerImpl.typeSelected(psiVariable.getType(), myDefaultType);
|
||||
}
|
||||
|
||||
private static void appendTypeCasts(List<RangeMarker> occurrenceMarkers,
|
||||
PsiFile file,
|
||||
Project project,
|
||||
@@ -282,16 +290,15 @@ class VariableInplaceIntroducer extends VariableInplaceRenamer {
|
||||
};
|
||||
}
|
||||
|
||||
private static class FinalExpression extends Expression {
|
||||
private final Project myProject;
|
||||
protected boolean createFinals() {
|
||||
return IntroduceVariableBase.createFinals(myProject);
|
||||
}
|
||||
|
||||
public FinalExpression(Project project) {
|
||||
myProject = project;
|
||||
}
|
||||
private class FinalExpression extends Expression {
|
||||
|
||||
@Override
|
||||
public Result calculateResult(ExpressionContext context) {
|
||||
return new TextResult(IntroduceVariableBase.createFinals(myProject) ? PsiKeyword.FINAL : "");
|
||||
return new TextResult(createFinals() ? PsiKeyword.FINAL : "");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -306,16 +313,18 @@ class VariableInplaceIntroducer extends VariableInplaceRenamer {
|
||||
lookupElements[1] = LookupElementBuilder.create(PsiModifier.FINAL + " ");
|
||||
return lookupElements;
|
||||
}
|
||||
}
|
||||
|
||||
public static void adjustLine(final PsiVariable psiVariable, final Document document) {
|
||||
final int modifierListOffset = psiVariable.getTextRange().getStartOffset();
|
||||
final int varLineNumber = document.getLineNumber(modifierListOffset);
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() { //adjust line indent if final was inserted and then deleted
|
||||
public static void adjustLine(final PsiVariable psiVariable, final Document document) {
|
||||
final int modifierListOffset = psiVariable.getTextRange().getStartOffset();
|
||||
final int varLineNumber = document.getLineNumber(modifierListOffset);
|
||||
|
||||
public void run() {
|
||||
CodeStyleManager.getInstance(psiVariable.getProject()).adjustLineIndent(document, document.getLineStartOffset(varLineNumber));
|
||||
}
|
||||
});
|
||||
}
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() { //adjust line indent if final was inserted and then deleted
|
||||
|
||||
public void run() {
|
||||
PsiDocumentManager.getInstance(psiVariable.getProject()).doPostponedOperationsAndUnblockDocument(document);
|
||||
CodeStyleManager.getInstance(psiVariable.getProject()).adjustLineIndent(document, document.getLineStartOffset(varLineNumber));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -262,7 +262,7 @@ public class VariableInplaceRenamer {
|
||||
performAutomaticRename(myNewName, PsiTreeUtil.getParentOfType(containingFile.findElementAt(renameOffset),
|
||||
PsiNameIdentifierOwner.class));
|
||||
}
|
||||
moveOffsetAfter(true);
|
||||
moveOffsetAfter(!brokenOff);
|
||||
}
|
||||
|
||||
public void templateCancelled(Template template) {
|
||||
|
||||
Reference in New Issue
Block a user