From 22db9ecd2293aba65ecb1fe7265a38fdd5b01283 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 30 Mar 2012 20:35:22 +0200 Subject: [PATCH] [^yole] IDEA-83805 When duplicating a line, Ctrl+D moves cursor to the start of line --- .../codeInsight/DuplicateActionTest.groovy | 14 ++++++++++++++ .../actions/NamedElementDuplicateHandler.java | 14 +++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/DuplicateActionTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/DuplicateActionTest.groovy index ad53d129f964..00e1a2a41cf0 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/DuplicateActionTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/DuplicateActionTest.groovy @@ -20,6 +20,7 @@ xxx private void doTest(String before, @NonNls String ext, String after) { myFixture.configureByText("a." + ext, before); myFixture.performEditorAction(IdeActions.ACTION_EDITOR_DUPLICATE) + println "/" + myFixture.editor.document.text.substring(myFixture.editor.caretModel.offset) + "/" myFixture.checkResult(after); } @@ -33,6 +34,19 @@ class C { void foo() {} void foo() {} } +''' + } + + public void testXmlTag() { + doTest ''' + + + +''', 'xml', ''' + + + + ''' } } diff --git a/platform/lang-impl/src/com/intellij/openapi/editor/actions/NamedElementDuplicateHandler.java b/platform/lang-impl/src/com/intellij/openapi/editor/actions/NamedElementDuplicateHandler.java index 5f0dce66445a..9c65977e5f6e 100644 --- a/platform/lang-impl/src/com/intellij/openapi/editor/actions/NamedElementDuplicateHandler.java +++ b/platform/lang-impl/src/com/intellij/openapi/editor/actions/NamedElementDuplicateHandler.java @@ -50,9 +50,9 @@ public class NamedElementDuplicateHandler extends EditorWriteActionHandler { Pair lines = EditorUtil.calcCaretLinesRange(editor, caret, caret); TextRange toDuplicate = new TextRange(editor.logicalPositionToOffset(lines.first), editor.logicalPositionToOffset(lines.second)); - PsiNamedElement named = findNamedElement(editor, file, toDuplicate); - if (named != null) { - editor.getCaretModel().moveToOffset(named.getTextOffset()); + PsiElement name = findNameIdentifier(editor, file, toDuplicate); + if (name != null) { + editor.getCaretModel().moveToOffset(name.getTextOffset()); } } } @@ -61,17 +61,17 @@ public class NamedElementDuplicateHandler extends EditorWriteActionHandler { } @Nullable - private static PsiNamedElement findNamedElement(Editor editor, PsiFile file, TextRange toDuplicate) { + private static PsiElement findNameIdentifier(Editor editor, PsiFile file, TextRange toDuplicate) { int nonWs = CharArrayUtil.shiftForward(editor.getDocument().getCharsSequence(), toDuplicate.getStartOffset(), "\n\t "); PsiElement psi = file.findElementAt(nonWs); - PsiNamedElement named = null; + PsiElement named = null; while (psi != null) { TextRange range = psi.getTextRange(); if (range == null || psi instanceof PsiFile || !toDuplicate.contains(psi.getTextRange())) { break; } - if (psi instanceof PsiNamedElement) { - named = (PsiNamedElement)psi; + if (psi instanceof PsiNameIdentifierOwner) { + named = ((PsiNameIdentifierOwner)psi).getNameIdentifier(); } psi = psi.getParent(); }