preserve selection during inline rename/ refixed

This commit is contained in:
anna
2011-02-23 12:31:26 +01:00
parent 53fbb28fda
commit 33f885c204
4 changed files with 13 additions and 31 deletions
@@ -66,6 +66,4 @@ public abstract class Template {
public abstract boolean isToShortenLongNames();
public abstract void setToShortenLongNames(boolean toShortenLongNames);
public abstract void setSubSelection(RangeMarker selection);
}
@@ -42,7 +42,6 @@ public class TemplateImpl extends Template implements SchemeElement {
private ArrayList<Segment> mySegments = null;
private String myTemplateText = null;
private String myId;
private RangeMarker mySubSelection;
public boolean equals(Object o) {
if (this == o) return true;
@@ -452,14 +451,6 @@ public class TemplateImpl extends Template implements SchemeElement {
return myVariables.get(i).skipOnStart();
}
public RangeMarker getSubSelection() {
return mySubSelection;
}
public void setSubSelection(RangeMarker selection) {
mySubSelection = selection;
}
private static class Segment {
public String name;
public int offset;
@@ -459,12 +459,8 @@ public class TemplateState implements Disposable {
myEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
myEditor.getSelectionModel().removeSelection();
final RangeMarker selection = myTemplate.getSubSelection();
if (selection != null && selection.getStartOffset() >= start && selection.getEndOffset() <= end) {
myEditor.getSelectionModel().setSelection(selection.getStartOffset(), selection.getEndOffset());
} else {
myEditor.getSelectionModel().setSelection(start, end);
}
myEditor.getSelectionModel().setSelection(start, end);
Expression expressionNode = myTemplate.getExpressionAt(myCurrentVariableNumber);
final ExpressionContext context = createExpressionContext(start);
@@ -185,13 +185,6 @@ public class VariableInplaceRenamer {
PsiElement selectedElement = getSelectedInEditorElement(nameIdentifier, refs, offset);
if (!CommonRefactoringUtil.checkReadOnlyStatus(myProject, myElementToRename)) return true;
final RangeMarker selection;
final SelectionModel selectionModel = myEditor.getSelectionModel();
if (selectionModel.hasSelection()) {
selection = myEditor.getDocument().createRangeMarker(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
} else {
selection = null;
}
if (nameIdentifier != null) addVariable(nameIdentifier, selectedElement, builder, nameSuggestions);
for (PsiReference ref : refs) {
addVariable(ref, selectedElement, builder, offset, nameSuggestions);
@@ -205,9 +198,16 @@ public class VariableInplaceRenamer {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
final int offset = myEditor.getCaretModel().getOffset();
final SelectionModel selectionModel = myEditor.getSelectionModel();
final TextRange selectedRange;
if (selectionModel.hasSelection()) {
selectedRange = new TextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
}
else {
selectedRange = null;
}
Template template = builder.buildInlineTemplate();
template.setToShortenLongNames(false);
template.setSubSelection(selection);
TextRange range = scope1.getTextRange();
assert range != null;
myHighlighters = new ArrayList<RangeHighlighter>();
@@ -240,17 +240,11 @@ public class VariableInplaceRenamer {
performAutomaticRename(myNewName, PsiTreeUtil.getParentOfType(containingFile.findElementAt(renameOffset), PsiNameIdentifierOwner.class));
}
moveOffsetAfter(true);
if (selection != null) {
selection.dispose();
}
}
public void templateCancelled(Template template) {
finish();
moveOffsetAfter(false);
if (selection != null) {
selection.dispose();
}
}
});
@@ -258,6 +252,9 @@ public class VariableInplaceRenamer {
Runnable runnable = new Runnable() {
public void run() {
myEditor.getCaretModel().moveToOffset(offset);
if (selectedRange != null){
myEditor.getSelectionModel().setSelection(selectedRange.getStartOffset(), selectedRange.getEndOffset());
}
}
};