diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/editorActions/BlockIndentOnPasteTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/editorActions/BlockIndentOnPasteTest.groovy index 23e2dcadaf3f..2b537de76906 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/editorActions/BlockIndentOnPasteTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/editorActions/BlockIndentOnPasteTest.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * 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. @@ -618,7 +618,7 @@ class Test { doTest(before, toPaste2, expected) } - def testPlainTextPaste() { + void testPlainTextPaste() { def before = '''\ line1 line2 @@ -628,7 +628,7 @@ class Test { def toPaste = '''\ line to paste #1 - line to paste #2 + line to paste #2\ ''' @@ -641,10 +641,33 @@ line to paste #1 doTest(before, toPaste, expected, StdFileTypes.PLAIN_TEXT) } - def testPlainTextPasteWithCompleteReplacement() { + void "test plain text when pasted string ends by line feed"() { + def before = '''\ + line1 + line2 + +''' + + def toPaste = + '''\ +line to paste #1 +line to paste #2 +''' + + def expected = '''\ + line1 + line2 + line to paste #1 + line to paste #2 + +''' + doTest(before, toPaste, expected, StdFileTypes.PLAIN_TEXT) + } + + void "test plain text when caret is after selection"() { def before = '''\ line1 - line2\ +\ ''' def toPaste = @@ -655,32 +678,14 @@ line to paste #2 def expected = '''\ + line1 line to paste #1 line to paste #2 ''' doTest(before, toPaste, expected, StdFileTypes.PLAIN_TEXT) } - def testPlainTextMultilinePasteWithCaretAfterSelection() { - def before = '''\ - line1\ -''' - - def toPaste = - '''\ -line to paste #1 -line to paste #2 -''' - - - def expected = '''\ -line to paste #1 -line to paste #2 -''' - doTest(before, toPaste, expected, StdFileTypes.PLAIN_TEXT) - } - - def testPlainTextThatStartsByLineFeed() { + void testPlainTextThatStartsByLineFeed() { def before = '''\ line 1 # item1 @@ -715,6 +720,37 @@ line 1 doTest(before, toPaste2, expected2, StdFileTypes.PLAIN_TEXT) } + void "test formatter-based paste that starts with white space"() { + def before = '''\ +class Test { + int i; + int j; + + void test() { + + } +} +''' + + def toPaste = '''\ + int i; + int j;\ +''' + + def expected = '''\ +class Test { + int i; + int j; + + void test() { + int i; + int j; + } +} +''' + doTest(before, toPaste, expected) + } + def doTest(String before, toPaste, expected, FileType fileType = StdFileTypes.JAVA) { myFixture.configureByText(fileType, before) @@ -732,6 +768,7 @@ line 1 finally { settings.REFORMAT_ON_PASTE = old } + myFixture.editor.selectionModel.removeSelection() myFixture.checkResult(expected) } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/PasteHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/PasteHandler.java index 4b65a2c9716c..d2a99b30e253 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/PasteHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/PasteHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * 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. @@ -379,7 +379,7 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser // Algorithm: the main idea is to process the first line of the pasted block, adjust its indent if necessary, calculate indent // adjustment string and apply to each line of the pasted block starting from the second one. // - // We differentiate three possible states here: + // We differentiate the following possible states here: // --- pasted block doesn't start new line, i.e. there are non-white space symbols before it at the first line. // Example: // old content [pasted line 1 @@ -460,7 +460,7 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser final int i = CharArrayUtil.shiftBackward(chars, startOffset - 1, " \t"); - // Handle situation when pasted block doesn't start new line. + // Handle a situation when pasted block doesn't start a new line. if (chars.charAt(startOffset) != '\n' && i > 0 && chars.charAt(i) != '\n') { int firstNonWsOffset = CharArrayUtil.shiftForward(chars, firstLineStart, " \t"); if (firstNonWsOffset > firstLineStart) { @@ -550,7 +550,7 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser } } else { - CharSequence toInsert = chars.subSequence(anchorLineStart, j + diff); + CharSequence toInsert = chars.subSequence(anchorLineStart, diff + startOffset); for (int line = anchorLine + 1; line <= lastLine; line++) { document.insertString(document.getLineStartOffset(line), toInsert); }