preserve editor selection in inline rename; make ctrl-A works inside template bounds (IDEA-60743)

This commit is contained in:
anna
2011-02-22 17:36:28 +01:00
parent bbd0b72f38
commit d6dc7106ca
7 changed files with 95 additions and 13 deletions
@@ -17,6 +17,7 @@
package com.intellij.codeInsight.template;
import com.intellij.codeInsight.template.impl.Variable;
import com.intellij.openapi.editor.RangeMarker;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -65,4 +66,6 @@ public abstract class Template {
public abstract boolean isToShortenLongNames();
public abstract void setToShortenLongNames(boolean toShortenLongNames);
public abstract void setSubSelection(RangeMarker selection);
}
@@ -19,6 +19,7 @@ package com.intellij.codeInsight.template.impl;
import com.intellij.codeInsight.template.Expression;
import com.intellij.codeInsight.template.Template;
import com.intellij.codeInsight.template.TemplateContextType;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.options.SchemeElement;
import com.intellij.openapi.util.text.StringUtil;
@@ -41,6 +42,7 @@ 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;
@@ -450,6 +452,14 @@ 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,8 +459,12 @@ public class TemplateState implements Disposable {
myEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
myEditor.getSelectionModel().removeSelection();
myEditor.getSelectionModel().setSelection(start, end);
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);
}
Expression expressionNode = myTemplate.getExpressionAt(myCurrentVariableNumber);
final ExpressionContext context = createExpressionContext(start);
@@ -0,0 +1,45 @@
/*
* 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.codeInsight.template.impl.editorActions;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.util.TextRange;
public class SelectAllHandler extends EditorActionHandler {
private final EditorActionHandler myOriginalHandler;
public SelectAllHandler(final EditorActionHandler originalHandler) {
myOriginalHandler = originalHandler;
}
public void execute(Editor editor, DataContext dataContext) {
final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
if (templateState != null && !templateState.isFinished()) {
final TextRange range = templateState.getCurrentVariableRange();
final int caretOffset = editor.getCaretModel().getOffset();
if (range != null && range.getStartOffset() <= caretOffset && caretOffset <= range.getEndOffset()) {
editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
return;
}
}
myOriginalHandler.execute(editor, dataContext);
}
}
@@ -32,6 +32,8 @@ import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.markup.RangeHighlighter;
@@ -183,6 +185,13 @@ 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);
@@ -198,6 +207,7 @@ public class VariableInplaceRenamer {
final int offset = myEditor.getCaretModel().getOffset();
Template template = builder.buildInlineTemplate();
template.setToShortenLongNames(false);
template.setSubSelection(selection);
TextRange range = scope1.getTextRange();
assert range != null;
myHighlighters = new ArrayList<RangeHighlighter>();
@@ -230,11 +240,17 @@ 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();
}
}
});
@@ -19,24 +19,27 @@ import com.intellij.ide.IdeBundle;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.actions.TextComponentEditorAction;
import com.intellij.openapi.project.DumbAware;
public class SelectAllAction extends AnAction implements DumbAware {
public class SelectAllAction extends TextComponentEditorAction implements DumbAware {
public SelectAllAction() {
super(new Handler());
setEnabledInModalContext(true);
}
public void actionPerformed(AnActionEvent e) {
final DataContext dataContext = e.getDataContext();
final Editor editor = TextComponentEditorAction.getEditorFromContext(dataContext);
if (editor == null) return;
CommandProcessor processor = CommandProcessor.getInstance();
processor.executeCommand(PlatformDataKeys.PROJECT.getData(dataContext), new Runnable() {
public void run() {
editor.getSelectionModel().setSelection(0, editor.getDocument().getTextLength());
}
}, IdeBundle.message("command.select.all"), null);
private static class Handler extends EditorActionHandler {
@Override
public void execute(final Editor editor, DataContext dataContext) {
CommandProcessor processor = CommandProcessor.getInstance();
processor.executeCommand(PlatformDataKeys.PROJECT.getData(dataContext), new Runnable() {
public void run() {
editor.getSelectionModel().setSelection(0, editor.getDocument().getTextLength());
}
}, IdeBundle.message("command.select.all"), null);
}
}
public void update(AnActionEvent event){
@@ -505,6 +505,7 @@
<editorActionHandler action="EditorTab" implementationClass="com.intellij.codeInsight.template.impl.editorActions.TabHandler"/>
<editorActionHandler action="EditorLineStart" implementationClass="com.intellij.codeInsight.template.impl.editorActions.HomeHandler"/>
<editorActionHandler action="EditorLineEnd" implementationClass="com.intellij.codeInsight.template.impl.editorActions.EndHandler"/>
<editorActionHandler action="$SelectAll" implementationClass="com.intellij.codeInsight.template.impl.editorActions.SelectAllHandler"/>
<editorTypedHandler implementationClass="com.intellij.codeInsight.template.impl.editorActions.SpaceHandler"/>
<!-- HighlightManagerActions -->