diff --git a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java index 03624c511007..0ddf46a7de80 100644 --- a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java +++ b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java @@ -11,10 +11,9 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiWhiteSpace; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtilCore; -import com.jetbrains.python.psi.PyClass; import com.jetbrains.python.psi.PyFile; -import com.jetbrains.python.psi.PyFunction; import com.jetbrains.python.psi.PyStatementList; import java.util.List; @@ -54,14 +53,8 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor { final PsiElement element1 = PsiUtilCore.getElementAtOffset(file, offset); boolean moved = false; if (element instanceof PsiWhiteSpace && element == element1) { - final PsiElement prevSibling = element.getPrevSibling(); - PyStatementList statementList = null; - if (prevSibling instanceof PyFunction) { - statementList = ((PyFunction)prevSibling).getStatementList(); - } - else if (prevSibling instanceof PyClass) { - statementList = ((PyClass)prevSibling).getStatementList(); - } + PyStatementList statementList = PsiTreeUtil + .findElementOfClassAtOffset(file, element.getTextOffset() - 1, PyStatementList.class, false); // Caret beyond actual indent -- move to the actual offset if (statementList != null) { final PsiElement lastChild = statementList.getLastChild(); diff --git a/python/testData/copyPaste/singleLine/IndentInIfInDef.after.py b/python/testData/copyPaste/singleLine/IndentInIfInDef.after.py new file mode 100644 index 000000000000..5cf2aefa0834 --- /dev/null +++ b/python/testData/copyPaste/singleLine/IndentInIfInDef.after.py @@ -0,0 +1,6 @@ +def f(self): + x = 1 + if True: + b = 2 + x = 1 + \ No newline at end of file diff --git a/python/testData/copyPaste/singleLine/IndentInIfInDef.dst.py b/python/testData/copyPaste/singleLine/IndentInIfInDef.dst.py new file mode 100644 index 000000000000..cd51f7265b8f --- /dev/null +++ b/python/testData/copyPaste/singleLine/IndentInIfInDef.dst.py @@ -0,0 +1,5 @@ +def f(self): + x = 1 + if True: + b = 2 + \ No newline at end of file diff --git a/python/testData/copyPaste/singleLine/IndentInIfInDef.src.py b/python/testData/copyPaste/singleLine/IndentInIfInDef.src.py new file mode 100644 index 000000000000..4e2976ce89a9 --- /dev/null +++ b/python/testData/copyPaste/singleLine/IndentInIfInDef.src.py @@ -0,0 +1,4 @@ +def f(self): + x = 1 + if True: + b = 2 diff --git a/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java b/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java index 71b9304804ab..d7e44915ebf9 100644 --- a/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java +++ b/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java @@ -171,6 +171,10 @@ public class PyCopyPasteTest extends PyTestCase { doTestSingleLine(); } + public void testIndentInIfInDef() { //PY-6927 + doTestSingleLine(); + } + private void doTest() { String name = getTestName(false);