pull OccurrencesChooser for in-place refactorings to platfrom

This commit is contained in:
Dmitry Jemerov
2011-08-25 16:08:01 +02:00
parent 740bd231dc
commit d3cc942d07
5 changed files with 38 additions and 37 deletions

View File

@@ -51,6 +51,7 @@ import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.refactoring.*;
import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer;
import com.intellij.refactoring.introduce.inplace.OccurrencesChooser;
import com.intellij.refactoring.introduceField.ElementToWorkOn;
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
import com.intellij.refactoring.util.CommonRefactoringUtil;
@@ -514,7 +515,7 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase impleme
final LinkedHashMap<OccurrencesChooser.ReplaceChoice, PsiExpression[]> occurrencesMap =
new LinkedHashMap<OccurrencesChooser.ReplaceChoice, PsiExpression[]>();
final boolean hasWriteAccess = OccurrencesChooser.fillChoices(expr, occurrences, occurrencesMap);
final boolean hasWriteAccess = fillChoices(expr, occurrences, occurrencesMap);
final PsiElement nameSuggestionContext = editor != null ? file.findElementAt(editor.getCaretModel().getOffset()) : null;
final RefactoringSupportProvider supportProvider = LanguageRefactoringSupport.INSTANCE.forLanguage(expr.getLanguage());
@@ -583,6 +584,30 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase impleme
return wasSucceed[0];
}
/**
* @return true if write usages found
*/
private static boolean fillChoices(final PsiExpression expr,
final PsiExpression[] occurrences,
final LinkedHashMap<OccurrencesChooser.ReplaceChoice, PsiExpression[]> occurrencesMap) {
occurrencesMap.put(OccurrencesChooser.ReplaceChoice.NO, new PsiExpression[]{expr});
final List<PsiExpression> nonWrite = new ArrayList<PsiExpression>();
for (PsiExpression occurrence : occurrences) {
if (!RefactoringUtil.isAssignmentLHS(occurrence)) {
nonWrite.add(occurrence);
}
}
final boolean hasWriteAccess = occurrences.length > nonWrite.size() && occurrences.length > 1;
if (hasWriteAccess) {
occurrencesMap.put(OccurrencesChooser.ReplaceChoice.NO_WRITE, nonWrite.toArray(new PsiExpression[nonWrite.size()]));
}
if (occurrences.length > 1) {
occurrencesMap.put(OccurrencesChooser.ReplaceChoice.ALL, occurrences);
}
return hasWriteAccess;
}
private static Runnable introduce(final Project project,
final PsiExpression expr,

View File

@@ -28,6 +28,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.refactoring.HelpID;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.introduce.inplace.OccurrencesChooser;
import com.intellij.refactoring.ui.ConflictsDialog;
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
import com.intellij.refactoring.util.CommonRefactoringUtil;

View File

@@ -6,10 +6,10 @@ import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiType;
import com.intellij.refactoring.introduce.inplace.OccurrencesChooser;
import com.intellij.refactoring.introduceVariable.InputValidator;
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase;
import com.intellij.refactoring.introduceVariable.IntroduceVariableSettings;
import com.intellij.refactoring.introduceVariable.OccurrencesChooser;
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
import com.intellij.testFramework.LightCodeInsightTestCase;
import com.intellij.util.containers.MultiMap;

View File

@@ -3,10 +3,10 @@ package com.intellij.refactoring;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.refactoring.introduce.inplace.OccurrencesChooser;
import com.intellij.refactoring.introduceVariable.InputValidator;
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase;
import com.intellij.refactoring.introduceVariable.IntroduceVariableSettings;
import com.intellij.refactoring.introduceVariable.OccurrencesChooser;
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
import com.intellij.util.containers.MultiMap;
import junit.framework.Assert;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.refactoring.introduceVariable;
package com.intellij.refactoring.introduce.inplace;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.colors.EditorColors;
@@ -24,8 +24,7 @@ import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.LightweightWindowEvent;
import com.intellij.openapi.util.Pass;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiExpression;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.ui.components.JBList;
import javax.swing.*;
@@ -33,14 +32,15 @@ import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.text.MessageFormat;
import java.util.*;
import java.util.List;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* User: anna
* Date: 10/14/10
*/
public class OccurrencesChooser {
public class OccurrencesChooser<T extends PsiElement> {
public static enum ReplaceChoice {
NO("Replace this occurrence only"), NO_WRITE("Replace all occurrences but write"), ALL("Replace all {0} occurrences");
@@ -66,7 +66,7 @@ public class OccurrencesChooser {
}
public void showChooser(final Pass<ReplaceChoice> callback,
final Map<ReplaceChoice, PsiExpression[]> occurrencesMap) {
final Map<ReplaceChoice, T[]> occurrencesMap) {
if (occurrencesMap.size() == 1) {
callback.pass(occurrencesMap.keySet().iterator().next());
return;
@@ -101,8 +101,8 @@ public class OccurrencesChooser {
if (value == null) return;
dropHighlighters();
final MarkupModel markupModel = myEditor.getMarkupModel();
final PsiExpression[] psiExpressions = occurrencesMap.get(value);
for (PsiExpression psiExpression : psiExpressions) {
final T[] psiExpressions = occurrencesMap.get(value);
for (T psiExpression : psiExpressions) {
final TextRange textRange = psiExpression.getTextRange();
final RangeHighlighter rangeHighlighter = markupModel.addRangeHighlighter(
textRange.getStartOffset(), textRange.getEndOffset(), HighlighterLayer.SELECTION - 1, myAttributes,
@@ -131,31 +131,6 @@ public class OccurrencesChooser {
.createPopup().showInBestPositionFor(myEditor);
}
/**
* @return true if write usages found
*/
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>();
for (PsiExpression occurrence : occurrences) {
if (!RefactoringUtil.isAssignmentLHS(occurrence)) {
nonWrite.add(occurrence);
}
}
final boolean hasWriteAccess = occurrences.length > nonWrite.size() && occurrences.length > 1;
if (hasWriteAccess) {
occurrencesMap.put(ReplaceChoice.NO_WRITE, nonWrite.toArray(new PsiExpression[nonWrite.size()]));
}
if (occurrences.length > 1) {
occurrencesMap.put(ReplaceChoice.ALL, occurrences);
}
return hasWriteAccess;
}
private void dropHighlighters() {
for (RangeHighlighter highlight : myRangeHighlighters) {
highlight.dispose();