From ff716edc19c976f0f2a21150d354e5b3215b87ad Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Fri, 7 Jun 2013 19:38:12 +0400 Subject: [PATCH] fixed PY-9956 Join Lines fails to preserve space between tokens --- .../python/editor/PyJoinLinesHandler.java | 32 ++++++++++++------- .../joinLines/ListComprehension-after.py | 1 + .../testData/joinLines/ListComprehension.py | 2 ++ .../com/jetbrains/python/PyJoinLinesTest.java | 3 +- 4 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 python/testData/joinLines/ListComprehension-after.py create mode 100644 python/testData/joinLines/ListComprehension.py diff --git a/python/src/com/jetbrains/python/editor/PyJoinLinesHandler.java b/python/src/com/jetbrains/python/editor/PyJoinLinesHandler.java index 16dd0faa3127..ddab786f23bd 100644 --- a/python/src/com/jetbrains/python/editor/PyJoinLinesHandler.java +++ b/python/src/com/jetbrains/python/editor/PyJoinLinesHandler.java @@ -60,8 +60,8 @@ public class PyJoinLinesHandler implements JoinRawLinesHandlerDelegate { new BinaryExprJoiner(), new ListLikeExprJoiner(), new CommentJoiner(), }; - - Request request = new Request(document, left_element, left_expr, right_element, right_expr); + + Request request = new Request(document, left_element, left_expr, right_element, right_expr); for (Joiner joiner : joiners) { Result res = joiner.join(request); @@ -79,10 +79,9 @@ public class PyJoinLinesHandler implements JoinRawLinesHandlerDelegate { if (request.leftElem() == request.rightElem()) { IElementType type = request.leftElem().getNode().getElementType(); if (PyTokenTypes.SINGLE_QUOTED_STRING == type || PyTokenTypes.SINGLE_QUOTED_UNICODE == type) { - PyExpression element = leftExpression; - if (element == null) return CANNOT_JOIN; - if (removeBackSlash(document, element, false)) { - return element.getTextOffset(); + if (leftExpression == null) return CANNOT_JOIN; + if (removeBackSlash(document, leftExpression, false)) { + return leftExpression.getTextOffset(); } } } @@ -102,17 +101,26 @@ public class PyJoinLinesHandler implements JoinRawLinesHandlerDelegate { return CANNOT_JOIN; } - private boolean removeBackSlash(Document document, PsiElement element, boolean trim) { + private static boolean removeBackSlash(Document document, PsiElement element, boolean trim) { String[] substrings = element.getText().split("\n"); if (substrings.length != 1) { StringBuilder replacement = new StringBuilder(); - for (String string : substrings) { - if (trim) + for (int i = 0; i < substrings.length; i++) { + String string = substrings[i]; + if (trim) { string = StringUtil.trimLeading(string); - if (string.trim().endsWith("\\")) - replacement.append(string.substring(0, string.length()-1)); - else + } + if (string.trim().endsWith("\\")) { + replacement.append(string.substring(0, string.length() - 1)); + } + else { replacement.append(string); + } + + if (i != substrings.length - 1 && !(element instanceof PyReferenceExpression) && + !(element instanceof PyStringLiteralExpression)) { + replacement.append(" "); + } } document.replaceString(element.getTextOffset(), element.getTextOffset()+element.getTextLength(), replacement); return true; diff --git a/python/testData/joinLines/ListComprehension-after.py b/python/testData/joinLines/ListComprehension-after.py new file mode 100644 index 000000000000..27ee20be3ae2 --- /dev/null +++ b/python/testData/joinLines/ListComprehension-after.py @@ -0,0 +1 @@ +records = [select.query.decode(r) for r in records] \ No newline at end of file diff --git a/python/testData/joinLines/ListComprehension.py b/python/testData/joinLines/ListComprehension.py new file mode 100644 index 000000000000..3f423a55e5df --- /dev/null +++ b/python/testData/joinLines/ListComprehension.py @@ -0,0 +1,2 @@ +records = [select.query.decode(r) for + r in records] \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyJoinLinesTest.java b/python/testSrc/com/jetbrains/python/PyJoinLinesTest.java index cfec42363c17..04ae25ebc941 100644 --- a/python/testSrc/com/jetbrains/python/PyJoinLinesTest.java +++ b/python/testSrc/com/jetbrains/python/PyJoinLinesTest.java @@ -34,7 +34,7 @@ public class PyJoinLinesTest extends PyTestCase { new WriteAction() { @Override protected void run(Result result) throws Throwable { - int ret = handler.tryJoinRawLines(doc, myFixture.getFile(), start, end); + handler.tryJoinRawLines(doc, myFixture.getFile(), start, end); } }.execute().throwException(); myFixture.checkResultByFile(path + getTestName(false) + "-after.py"); @@ -66,4 +66,5 @@ public class PyJoinLinesTest extends PyTestCase { public void testStringWithSlash() { doTest(); } public void testListOfStrings() { doTest(); } public void testLongExpression() { doTest(); } + public void testListComprehension() { doTest(); } }