From d6dc7106ca206f9873044511df397643c6dfac2b Mon Sep 17 00:00:00 2001 From: anna Date: Tue, 22 Feb 2011 17:13:26 +0100 Subject: [PATCH] preserve editor selection in inline rename; make ctrl-A works inside template bounds (IDEA-60743) --- .../codeInsight/template/Template.java | 3 ++ .../template/impl/TemplateImpl.java | 10 +++++ .../template/impl/TemplateState.java | 8 +++- .../impl/editorActions/SelectAllHandler.java | 45 +++++++++++++++++++ .../inplace/VariableInplaceRenamer.java | 16 +++++++ .../intellij/ide/actions/SelectAllAction.java | 25 ++++++----- .../src/META-INF/LangExtensions.xml | 1 + 7 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/template/impl/editorActions/SelectAllHandler.java diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/Template.java b/platform/lang-impl/src/com/intellij/codeInsight/template/Template.java index 0da41ec1cb35..5fa3924895d0 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/Template.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/Template.java @@ -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); } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateImpl.java index a1a9f3bf567c..3d06dcf57232 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateImpl.java @@ -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 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; diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java index 7457d7e91936..50df33f95cc0 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java @@ -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); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/editorActions/SelectAllHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/editorActions/SelectAllHandler.java new file mode 100644 index 000000000000..b19d7da4dbbb --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/editorActions/SelectAllHandler.java @@ -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); + } +} diff --git a/platform/lang-impl/src/com/intellij/refactoring/rename/inplace/VariableInplaceRenamer.java b/platform/lang-impl/src/com/intellij/refactoring/rename/inplace/VariableInplaceRenamer.java index cd01409972b3..bdae42635a79 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/rename/inplace/VariableInplaceRenamer.java +++ b/platform/lang-impl/src/com/intellij/refactoring/rename/inplace/VariableInplaceRenamer.java @@ -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(); @@ -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(); + } } }); diff --git a/platform/platform-impl/src/com/intellij/ide/actions/SelectAllAction.java b/platform/platform-impl/src/com/intellij/ide/actions/SelectAllAction.java index c66184fdb8c3..8b2d47a7deb1 100644 --- a/platform/platform-impl/src/com/intellij/ide/actions/SelectAllAction.java +++ b/platform/platform-impl/src/com/intellij/ide/actions/SelectAllAction.java @@ -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){ diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index a661a767e16c..81efbfedb9da 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -505,6 +505,7 @@ +