diff --git a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java index 43cc03571d08..91b9fb7778ea 100644 --- a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java +++ b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java @@ -64,7 +64,8 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor { final int lineStartOffset = getLineStartSafeOffset(document, lineNumber); text = addLeadingSpaces(text, NOT_INDENT_FILTER, indentSize, indentChar); - final String indentText = getIndentText(file, document, caretOffset, lineNumber); + int firstLineIndent = StringUtil.findFirst(text, NOT_INDENT_FILTER); + final String indentText = getIndentText(file, document, caretOffset, lineNumber, firstLineIndent); int toRemove = calculateIndentToRemove(text, NOT_INDENT_FILTER); if (StringUtil.isEmptyOrSpaces(indentText) && isApplicable(file, text, caretOffset)) { @@ -105,7 +106,7 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor { private static String getIndentText(@NotNull final PsiFile file, @NotNull final Document document, int caretOffset, - int lineNumber) { + int lineNumber, int firstLineIndent) { PsiElement nonWS = PyUtil.findNextAtOffset(file, caretOffset, PsiWhiteSpace.class); if (nonWS != null) { @@ -132,6 +133,8 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor { indentText = strings.get(0); } } + if (indentText.length() == firstLineIndent) + return ""; } return indentText; } @@ -141,7 +144,7 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor { int minIndent = StringUtil.findFirst(text, filter); for (String s : strings) { final int indent = StringUtil.findFirst(s, filter); - if (indent < minIndent) + if (indent < minIndent && !StringUtil.isEmptyOrSpaces(s)) minIndent = indent; } return minIndent; diff --git a/python/testData/copyPaste/CaretAtTheBeginningOfIndent.after.py b/python/testData/copyPaste/CaretAtTheBeginningOfIndent.after.py new file mode 100644 index 000000000000..9f86fdf7e2f6 --- /dev/null +++ b/python/testData/copyPaste/CaretAtTheBeginningOfIndent.after.py @@ -0,0 +1,16 @@ +# original function # this is line 1 of the code. +def foo(): + print 'f00' + def bar(num): + for _ in range(num): + print 'bar' + + bar(7) + + +def bar(num): + for _ in range(num): + print 'bar' + + +bar(7) diff --git a/python/testData/copyPaste/CaretAtTheBeginningOfIndent.dst.py b/python/testData/copyPaste/CaretAtTheBeginningOfIndent.dst.py new file mode 100644 index 000000000000..ec09360aaeec --- /dev/null +++ b/python/testData/copyPaste/CaretAtTheBeginningOfIndent.dst.py @@ -0,0 +1,10 @@ +# original function # this is line 1 of the code. +def foo(): + print 'f00' + def bar(num): + for _ in range(num): + print 'bar' + + bar(7) + + diff --git a/python/testData/copyPaste/CaretAtTheBeginningOfIndent.src.py b/python/testData/copyPaste/CaretAtTheBeginningOfIndent.src.py new file mode 100644 index 000000000000..5492edd65dd7 --- /dev/null +++ b/python/testData/copyPaste/CaretAtTheBeginningOfIndent.src.py @@ -0,0 +1,7 @@ +# original function # this is line 1 of the code. +def foo(): + print 'f00' + def bar(num): + for _ in range(num): + print 'bar' + bar(7) diff --git a/python/testData/copyPaste/EmptyLineInList.after.py b/python/testData/copyPaste/EmptyLineInList.after.py new file mode 100644 index 000000000000..9f86fdf7e2f6 --- /dev/null +++ b/python/testData/copyPaste/EmptyLineInList.after.py @@ -0,0 +1,16 @@ +# original function # this is line 1 of the code. +def foo(): + print 'f00' + def bar(num): + for _ in range(num): + print 'bar' + + bar(7) + + +def bar(num): + for _ in range(num): + print 'bar' + + +bar(7) diff --git a/python/testData/copyPaste/EmptyLineInList.dst.py b/python/testData/copyPaste/EmptyLineInList.dst.py new file mode 100644 index 000000000000..ec09360aaeec --- /dev/null +++ b/python/testData/copyPaste/EmptyLineInList.dst.py @@ -0,0 +1,10 @@ +# original function # this is line 1 of the code. +def foo(): + print 'f00' + def bar(num): + for _ in range(num): + print 'bar' + + bar(7) + + diff --git a/python/testData/copyPaste/EmptyLineInList.src.py b/python/testData/copyPaste/EmptyLineInList.src.py new file mode 100644 index 000000000000..e9bd47cec7ae --- /dev/null +++ b/python/testData/copyPaste/EmptyLineInList.src.py @@ -0,0 +1,8 @@ +# original function # this is line 1 of the code. +def foo(): + print 'f00' + def bar(num): + for _ in range(num): + print 'bar' + + bar(7) diff --git a/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java b/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java index 62e1f216f1e9..2c80ef0f696e 100644 --- a/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java +++ b/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java @@ -301,6 +301,14 @@ public class PyCopyPasteTest extends PyTestCase { doTest(); } + public void testEmptyLineInList() { + doTest(); + } + + public void testCaretAtTheBeginningOfIndent() { + doTest(); + } + private void doTestTabs() { final CommonCodeStyleSettings.IndentOptions indentOptions = CodeStyleSettingsManager.getSettings(myFixture.getProject()).getIndentOptions(PythonFileType.INSTANCE);