From 77425f305d4e7bc9b28f44a822bb35662830bc25 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Mon, 4 Apr 2016 21:32:29 +0300 Subject: [PATCH] PY-18522 PY-19053 Paste raw text if there are non whitespace symbols before caret on line This previously existed check was lost after the recent fixes for PY-18522. It also caused failures of PyCopyPasteTest.testEmpty(), so I reverted previous fix for the test. --- .../python/editor/PythonCopyPasteProcessor.java | 15 ++++++++------- .../SimpleExpressionPartCaretAtLineEnd.after.py | 2 ++ .../SimpleExpressionPartCaretAtLineEnd.dst.py | 2 ++ .../SimpleExpressionPartCaretAtLineEnd.src.py | 1 + .../com/jetbrains/python/PyCopyPasteTest.java | 5 +++++ 5 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 python/testData/copyPaste/SimpleExpressionPartCaretAtLineEnd.after.py create mode 100644 python/testData/copyPaste/SimpleExpressionPartCaretAtLineEnd.dst.py create mode 100644 python/testData/copyPaste/SimpleExpressionPartCaretAtLineEnd.src.py diff --git a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java index 350639e37ce4..9e9cd3d9fefa 100644 --- a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java +++ b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java @@ -82,6 +82,9 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor { final int lineStartOffset = getLineStartSafeOffset(document, lineNumber); final int lineEndOffset = document.getLineEndOffset(lineNumber); + final String linePrefix = document.getText(TextRange.create(lineStartOffset, caretOffset)); + if (!StringUtil.isEmptyOrSpaces(linePrefix)) return text; + final PsiElement element = file.findElementAt(caretOffset); if (PsiTreeUtil.getParentOfType(element, PyStringLiteralExpression.class) != null) return text; @@ -186,15 +189,13 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor { } private static boolean isApplicable(@NotNull final PsiFile file, @NotNull String text, int caretOffset) { - final boolean useTabs = CodeStyleSettingsManager.getSettings(file.getProject()).useTabCharacter(PythonFileType.INSTANCE); + final boolean useTabs = + CodeStyleSettingsManager.getSettings(file.getProject()).useTabCharacter(PythonFileType.INSTANCE); final PsiElement nonWS = PyUtil.findNextAtOffset(file, caretOffset, PsiWhiteSpace.class); - if (nonWS == null) { - return !StringUtil.isEmptyOrSpaces(text); - } - if (text.endsWith("\n") || - (inStatementList(file, caretOffset) && (text.startsWith(useTabs ? "\t" : " ") || StringUtil.split(text, "\n").size() > 1))) { + if (nonWS == null || text.endsWith("\n")) + return true; + if (inStatementList(file, caretOffset) && (text.startsWith(useTabs ? "\t" : " ") || StringUtil.split(text, "\n").size() > 1)) return true; - } return false; } diff --git a/python/testData/copyPaste/SimpleExpressionPartCaretAtLineEnd.after.py b/python/testData/copyPaste/SimpleExpressionPartCaretAtLineEnd.after.py new file mode 100644 index 000000000000..89d84c6688ff --- /dev/null +++ b/python/testData/copyPaste/SimpleExpressionPartCaretAtLineEnd.after.py @@ -0,0 +1,2 @@ +def foo(): + x = 10 * 20 \ No newline at end of file diff --git a/python/testData/copyPaste/SimpleExpressionPartCaretAtLineEnd.dst.py b/python/testData/copyPaste/SimpleExpressionPartCaretAtLineEnd.dst.py new file mode 100644 index 000000000000..11b7c04d8b42 --- /dev/null +++ b/python/testData/copyPaste/SimpleExpressionPartCaretAtLineEnd.dst.py @@ -0,0 +1,2 @@ +def foo(): + x = 10 * \ No newline at end of file diff --git a/python/testData/copyPaste/SimpleExpressionPartCaretAtLineEnd.src.py b/python/testData/copyPaste/SimpleExpressionPartCaretAtLineEnd.src.py new file mode 100644 index 000000000000..7e415111b629 --- /dev/null +++ b/python/testData/copyPaste/SimpleExpressionPartCaretAtLineEnd.src.py @@ -0,0 +1 @@ +20 \ 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 185f54b0e15f..92bd0a81cb94 100644 --- a/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java +++ b/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java @@ -391,4 +391,9 @@ public class PyCopyPasteTest extends PyTestCase { public void testEmptyFunctionCaretAtBodyIndent() { doTest(); } + + // PY-19053 + public void testSimpleExpressionPartCaretAtLineEnd() { + doTest(); + } }