From 45403110b78ffdc3117b0203d49ee41e9995611f Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Sat, 5 May 2012 19:59:37 +0400 Subject: [PATCH] fixed PY-6410 Adding/Pasting text at the end of a class method causes the next class method to be moved outside the class --- python/src/META-INF/python-plugin-common.xml | 1 + .../editor/PythonCopyPasteProcessor.java | 52 +++++++++++++++++++ python/testData/copyPaste/Indent3.after.py | 3 ++ python/testData/copyPaste/Indent3.dst.py | 2 + python/testData/copyPaste/Indent3.src.py | 3 ++ .../com/jetbrains/python/PyCopyPasteTest.java | 4 ++ 6 files changed, 65 insertions(+) create mode 100644 python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java create mode 100644 python/testData/copyPaste/Indent3.after.py create mode 100644 python/testData/copyPaste/Indent3.dst.py create mode 100644 python/testData/copyPaste/Indent3.src.py diff --git a/python/src/META-INF/python-plugin-common.xml b/python/src/META-INF/python-plugin-common.xml index 512d14c48026..26fff826ecab 100644 --- a/python/src/META-INF/python-plugin-common.xml +++ b/python/src/META-INF/python-plugin-common.xml @@ -4,6 +4,7 @@ com.jetbrains.python.PyBundle + diff --git a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java new file mode 100644 index 000000000000..87f63c65484f --- /dev/null +++ b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java @@ -0,0 +1,52 @@ +package com.jetbrains.python.editor; + +import com.intellij.codeInsight.editorActions.CopyPastePreProcessor; +import com.intellij.openapi.editor.CaretModel; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.RawText; +import com.intellij.openapi.project.Project; +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.PsiUtilCore; + +/** + * User : catherine + */ +public class PythonCopyPasteProcessor implements CopyPastePreProcessor { + + @Override + public String preprocessOnCopy(PsiFile file, int[] startOffsets, int[] endOffsets, String text) { + return null; + } + + @Override + public String preprocessOnPaste(Project project, + PsiFile file, + Editor editor, + String text, + RawText rawText) { + final CaretModel caretModel = editor.getCaretModel(); + final Document document = editor.getDocument(); + + if (StringUtil.startsWithWhitespace(text) && StringUtil.endsWithLineBreak(text)) { + final int caretOffset = caretModel.getOffset(); + final PsiElement element = PsiUtilCore.getElementAtOffset(file, caretOffset-1); + final int lineNumber = document.getLineNumber(caretOffset); + final int offset = getLineStartSafeOffset(document, lineNumber); + final PsiElement element1 = PsiUtilCore.getElementAtOffset(file, offset); + if (element instanceof PsiWhiteSpace && element == element1) { + caretModel.moveToOffset(offset); + } + } + return text; + } + + public int getLineStartSafeOffset(final Document document, int line) { + if (line == document.getLineCount()) return document.getTextLength(); + return document.getLineStartOffset(line); + } + +} diff --git a/python/testData/copyPaste/Indent3.after.py b/python/testData/copyPaste/Indent3.after.py new file mode 100644 index 000000000000..94362ee09214 --- /dev/null +++ b/python/testData/copyPaste/Indent3.after.py @@ -0,0 +1,3 @@ +class A: + a = 1 + b = 2 diff --git a/python/testData/copyPaste/Indent3.dst.py b/python/testData/copyPaste/Indent3.dst.py new file mode 100644 index 000000000000..596c56a69018 --- /dev/null +++ b/python/testData/copyPaste/Indent3.dst.py @@ -0,0 +1,2 @@ +class A: + b = 2 diff --git a/python/testData/copyPaste/Indent3.src.py b/python/testData/copyPaste/Indent3.src.py new file mode 100644 index 000000000000..ab24cabc8252 --- /dev/null +++ b/python/testData/copyPaste/Indent3.src.py @@ -0,0 +1,3 @@ +class C: + a = 1 + \ 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 0636892e6962..3f13cf2b5598 100644 --- a/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java +++ b/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java @@ -32,6 +32,10 @@ public class PyCopyPasteTest extends PyTestCase { doTest(); } + public void testIndent3() { + doTest(); + } + public void testIndentIncrease() { doTest(); }