From 9edc017c1724fdcfe09b3d4f73afe7c19185e99d Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Fri, 25 May 2012 20:43:48 +0400 Subject: [PATCH] ensure caret position invariant during inplace rename --- .../InplaceRenameInvariantTest.groovy | 129 ++++++++++++++++++ .../inplace/VariableInplaceRenamer.java | 5 + 2 files changed, 134 insertions(+) create mode 100644 java/java-tests/testSrc/com/intellij/refactoring/InplaceRenameInvariantTest.groovy diff --git a/java/java-tests/testSrc/com/intellij/refactoring/InplaceRenameInvariantTest.groovy b/java/java-tests/testSrc/com/intellij/refactoring/InplaceRenameInvariantTest.groovy new file mode 100644 index 000000000000..17c4694f3b9e --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/refactoring/InplaceRenameInvariantTest.groovy @@ -0,0 +1,129 @@ +/* + * Copyright 2000-2012 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 + +import com.intellij.codeInsight.TargetElementUtilBase +import com.intellij.codeInsight.template.TemplateManager +import com.intellij.codeInsight.template.impl.TemplateManagerImpl +import com.intellij.codeInsight.template.impl.TemplateState +import com.intellij.psi.PsiElement +import com.intellij.refactoring.rename.inplace.MemberInplaceRenameHandler +import com.intellij.testFramework.LightCodeInsightTestCase + +/** + * User: anna + */ +class InplaceRenameInvariantTest extends LightCodeInsightTestCase { + public void "test start caret position"() { + def text = """\ + class Test { + } + } + """ + + doTestPositionInvariance(text, false) + } + + public void "test middle caret position"() { + def text = """\ + class Test { + } + } + """ + + doTestPositionInvariance(text, false) + } + + public void "test end caret position"() { + def text = """\ + class Test { + } + } + """ + + doTestPositionInvariance(text, false) + } + + + public void "test start caret position preselect"() { + def text = """\ + class Test { + } + } + """ + + doTestPositionInvariance(text, true) + } + + public void "test middle caret position preselect"() { + def text = """\ + class Test { + } + } + """ + + doTestPositionInvariance(text, true) + } + + public void "test end caret position preselect"() { + def text = """\ + class Test { + } + } + """ + + doTestPositionInvariance(text, true) + } + + private doTestPositionInvariance(String text, final boolean preselect) { + configure text + TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(project) + def oldPreselectSetting = myEditor.settings.preselectRename + try { + templateManager.templateTesting = true + myEditor.settings.preselectRename = preselect; + int offset = myEditor.caretModel.offset + final PsiElement element = TargetElementUtilBase.findTargetElement(myEditor, TargetElementUtilBase.getInstance().getAllAccepted()) + + assertNotNull(element) + + MemberInplaceRenameHandler handler = new MemberInplaceRenameHandler() + + + handler.doRename(element, editor, null); + + assertEquals(offset, myEditor.caretModel.offset) + } + finally { + myEditor.settings.preselectRename = oldPreselectSetting + + try { + TemplateState state = TemplateManagerImpl.getTemplateState(editor) + + assertNotNull(state) + + state.gotoEnd(false) + } + finally { + templateManager.templateTesting = false + } + } + } + + private def configure(String text) { + configureFromFileText("a.java", text) + } +} \ No newline at end of file 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 8894c2cc7c71..f52e6635ef16 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 @@ -156,6 +156,11 @@ public class VariableInplaceRenamer extends InplaceRefactoring { } } + @Override + protected int restoreCaretOffset(int offset) { + return offset; + } + @Override protected boolean shouldSelectAll() { if (myEditor.getSettings().isPreselectRename()) return true;