diff --git a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java index 49826cb049cd..137d87eea46a 100644 --- a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java +++ b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java @@ -4,6 +4,7 @@ import com.intellij.codeInsight.CodeInsightSettings; import com.intellij.codeInsight.editorActions.CopyPastePreProcessor; import com.intellij.openapi.editor.*; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.CharFilter; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiElement; @@ -58,11 +59,16 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor { final int lineOffset = getLineStartSafeOffset(document, line); final PsiElement ws = file.findElementAt(lineOffset); int offset = ws instanceof PsiWhiteSpace? ws.getTextRange().getEndOffset() : lineOffset; + if (text.equals(selectionModel.getSelectedText())) return text; caretModel.moveToOffset(offset); selectionModel.setSelection(offset, selectionModel.getSelectionEnd()); } - else + else { + if (isNotApplicable(document, caretOffset, lineStartOffset)) { + return text; + } caretModel.moveToOffset(lineStartOffset); + } String spaceString; int indent = 0; @@ -118,8 +124,13 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor { return newText; } + private boolean isNotApplicable(Document document, int caretOffset, int lineStartOffset) { + return !StringUtil.isEmptyOrSpaces(document.getText(TextRange.create(lineStartOffset, caretOffset))); + } + public static int getLineStartSafeOffset(final Document document, int line) { if (line == document.getLineCount()) return document.getTextLength(); + if (line < 0) return 0; return document.getLineStartOffset(line); } diff --git a/python/testData/copyPaste/LineToBegin.after.py b/python/testData/copyPaste/LineToBegin.after.py new file mode 100644 index 000000000000..f9057d4ca5de --- /dev/null +++ b/python/testData/copyPaste/LineToBegin.after.py @@ -0,0 +1,3 @@ +p +print 2rint 1 +print 3 diff --git a/python/testData/copyPaste/LineToBegin.dst.py b/python/testData/copyPaste/LineToBegin.dst.py new file mode 100644 index 000000000000..11c6652a7579 --- /dev/null +++ b/python/testData/copyPaste/LineToBegin.dst.py @@ -0,0 +1,2 @@ +print 1 +print 3 diff --git a/python/testData/copyPaste/LineToBegin.src.py b/python/testData/copyPaste/LineToBegin.src.py new file mode 100644 index 000000000000..e02faf90fbc7 --- /dev/null +++ b/python/testData/copyPaste/LineToBegin.src.py @@ -0,0 +1,3 @@ +print 1 +print 2 +print 3 diff --git a/python/testData/copyPaste/LineToEnd.after.py b/python/testData/copyPaste/LineToEnd.after.py new file mode 100644 index 000000000000..759d05b32e05 --- /dev/null +++ b/python/testData/copyPaste/LineToEnd.after.py @@ -0,0 +1,3 @@ +print 1 +print 2 +print 3 diff --git a/python/testData/copyPaste/LineToEnd.dst.py b/python/testData/copyPaste/LineToEnd.dst.py new file mode 100644 index 000000000000..09602cada9a1 --- /dev/null +++ b/python/testData/copyPaste/LineToEnd.dst.py @@ -0,0 +1,2 @@ +print 1 +print 3 diff --git a/python/testData/copyPaste/LineToEnd.src.py b/python/testData/copyPaste/LineToEnd.src.py new file mode 100644 index 000000000000..e02faf90fbc7 --- /dev/null +++ b/python/testData/copyPaste/LineToEnd.src.py @@ -0,0 +1,3 @@ +print 1 +print 2 +print 3 diff --git a/python/testData/copyPaste/LineToPrev.after.py b/python/testData/copyPaste/LineToPrev.after.py new file mode 100644 index 000000000000..12228e3b63d5 --- /dev/null +++ b/python/testData/copyPaste/LineToPrev.after.py @@ -0,0 +1,3 @@ +print +print 21 +print 3 diff --git a/python/testData/copyPaste/LineToPrev.dst.py b/python/testData/copyPaste/LineToPrev.dst.py new file mode 100644 index 000000000000..29dfffb6f8cd --- /dev/null +++ b/python/testData/copyPaste/LineToPrev.dst.py @@ -0,0 +1,2 @@ +print 1 +print 3 diff --git a/python/testData/copyPaste/LineToPrev.src.py b/python/testData/copyPaste/LineToPrev.src.py new file mode 100644 index 000000000000..e02faf90fbc7 --- /dev/null +++ b/python/testData/copyPaste/LineToPrev.src.py @@ -0,0 +1,3 @@ +print 1 +print 2 +print 3 diff --git a/python/testData/copyPaste/SelectionOneLine.after.py b/python/testData/copyPaste/SelectionOneLine.after.py new file mode 100644 index 000000000000..c637bb0bb095 --- /dev/null +++ b/python/testData/copyPaste/SelectionOneLine.after.py @@ -0,0 +1,4 @@ +class MyClass(object): + member1 = 1 + member2 = 2 + member3 = 3 \ No newline at end of file diff --git a/python/testData/copyPaste/SelectionOneLine.dst.py b/python/testData/copyPaste/SelectionOneLine.dst.py new file mode 100644 index 000000000000..ac50ba3ddd2b --- /dev/null +++ b/python/testData/copyPaste/SelectionOneLine.dst.py @@ -0,0 +1,4 @@ +class MyClass(object): + member1 = 1 + member2 = 2 + member3 = 3 \ No newline at end of file diff --git a/python/testData/copyPaste/SelectionOneLine.src.py b/python/testData/copyPaste/SelectionOneLine.src.py new file mode 100644 index 000000000000..ebe7695fbccd --- /dev/null +++ b/python/testData/copyPaste/SelectionOneLine.src.py @@ -0,0 +1,4 @@ +class MyClass(object): + member1 = 1 + member2 = 2 + member3 = 3 \ No newline at end of file diff --git a/python/testData/copyPaste/SelectionOneLine1.after.py b/python/testData/copyPaste/SelectionOneLine1.after.py new file mode 100644 index 000000000000..c637bb0bb095 --- /dev/null +++ b/python/testData/copyPaste/SelectionOneLine1.after.py @@ -0,0 +1,4 @@ +class MyClass(object): + member1 = 1 + member2 = 2 + member3 = 3 \ No newline at end of file diff --git a/python/testData/copyPaste/SelectionOneLine1.dst.py b/python/testData/copyPaste/SelectionOneLine1.dst.py new file mode 100644 index 000000000000..f3dbc6cdc9ab --- /dev/null +++ b/python/testData/copyPaste/SelectionOneLine1.dst.py @@ -0,0 +1,4 @@ +class MyClass(object): + member1 = 1 + member2 = 2 + member3 = 3 \ No newline at end of file diff --git a/python/testData/copyPaste/SelectionOneLine1.src.py b/python/testData/copyPaste/SelectionOneLine1.src.py new file mode 100644 index 000000000000..ebe7695fbccd --- /dev/null +++ b/python/testData/copyPaste/SelectionOneLine1.src.py @@ -0,0 +1,4 @@ +class MyClass(object): + member1 = 1 + member2 = 2 + member3 = 3 \ No newline at end of file diff --git a/python/testData/copyPaste/SelectionOneLine2.after.py b/python/testData/copyPaste/SelectionOneLine2.after.py new file mode 100644 index 000000000000..c637bb0bb095 --- /dev/null +++ b/python/testData/copyPaste/SelectionOneLine2.after.py @@ -0,0 +1,4 @@ +class MyClass(object): + member1 = 1 + member2 = 2 + member3 = 3 \ No newline at end of file diff --git a/python/testData/copyPaste/SelectionOneLine2.dst.py b/python/testData/copyPaste/SelectionOneLine2.dst.py new file mode 100644 index 000000000000..88b86fab35ad --- /dev/null +++ b/python/testData/copyPaste/SelectionOneLine2.dst.py @@ -0,0 +1,4 @@ +class MyClass(object): + member1 = 1 + member2 = 2 + member3 = 3 \ No newline at end of file diff --git a/python/testData/copyPaste/SelectionOneLine2.src.py b/python/testData/copyPaste/SelectionOneLine2.src.py new file mode 100644 index 000000000000..53d364bd5fdc --- /dev/null +++ b/python/testData/copyPaste/SelectionOneLine2.src.py @@ -0,0 +1,4 @@ +class MyClass(object): + member1 = 1 + member2 = 2 + member3 = 3 \ No newline at end of file diff --git a/python/testData/copyPaste/SelectionOneLine3.after.py b/python/testData/copyPaste/SelectionOneLine3.after.py new file mode 100644 index 000000000000..c637bb0bb095 --- /dev/null +++ b/python/testData/copyPaste/SelectionOneLine3.after.py @@ -0,0 +1,4 @@ +class MyClass(object): + member1 = 1 + member2 = 2 + member3 = 3 \ No newline at end of file diff --git a/python/testData/copyPaste/SelectionOneLine3.dst.py b/python/testData/copyPaste/SelectionOneLine3.dst.py new file mode 100644 index 000000000000..7f6256675644 --- /dev/null +++ b/python/testData/copyPaste/SelectionOneLine3.dst.py @@ -0,0 +1,4 @@ +class MyClass(object): + member1 = 1 + member2 = 2 + member3 = 3 \ No newline at end of file diff --git a/python/testData/copyPaste/SelectionOneLine3.src.py b/python/testData/copyPaste/SelectionOneLine3.src.py new file mode 100644 index 000000000000..53d364bd5fdc --- /dev/null +++ b/python/testData/copyPaste/SelectionOneLine3.src.py @@ -0,0 +1,4 @@ +class MyClass(object): + member1 = 1 + member2 = 2 + member3 = 3 \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java b/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java index a0f89efd79a3..30e63e9a6b01 100644 --- a/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java +++ b/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java @@ -227,6 +227,34 @@ public class PyCopyPasteTest extends PyTestCase { doTest(); } + public void testLineToEnd() { //PY-7524 + doTest(); + } + + public void testLineToPrev() { //PY-7524 + doTest(); + } + + public void testLineToBegin() { //PY-7524 + doTest(); + } + + public void testSelectionOneLine() { //PY-7470 + doTest(); + } + + public void testSelectionOneLine1() { //PY-7470 + doTest(); + } + + public void testSelectionOneLine2() { //PY-7470 + doTest(); + } + + public void testSelectionOneLine3() { //PY-7470 + doTest(); + } + private void doTest() { String name = getTestName(false);