From e4f8eaf5cefcc9acaca802ac242c0431988d4304 Mon Sep 17 00:00:00 2001 From: Dmitry Trofimov Date: Mon, 3 Feb 2014 22:47:26 +0400 Subject: [PATCH] Don't wrap at the start of string literal (PY-9436). --- .../python/formatter/PyLineWrapPositionStrategy.java | 10 ++++++++++ python/testData/wrap/DontWrapStartOfString.after.py | 2 ++ python/testData/wrap/DontWrapStartOfString.py | 1 + python/testSrc/com/jetbrains/python/PyWrapTest.java | 5 ++++- 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 python/testData/wrap/DontWrapStartOfString.after.py create mode 100644 python/testData/wrap/DontWrapStartOfString.py diff --git a/python/src/com/jetbrains/python/formatter/PyLineWrapPositionStrategy.java b/python/src/com/jetbrains/python/formatter/PyLineWrapPositionStrategy.java index d7fa5f0f5e7b..419d97c9e5d4 100644 --- a/python/src/com/jetbrains/python/formatter/PyLineWrapPositionStrategy.java +++ b/python/src/com/jetbrains/python/formatter/PyLineWrapPositionStrategy.java @@ -24,6 +24,7 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.text.CharArrayUtil; +import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.psi.StringLiteralExpression; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -75,11 +76,20 @@ public class PyLineWrapPositionStrategy extends GenericLineWrapPositionStrategy int maxPreferredOffset, boolean allowToBeyondMaxPreferredOffset, boolean virtual) { + int wrapPosition = super.calculateWrapPosition(document, project, startOffset, endOffset, maxPreferredOffset, allowToBeyondMaxPreferredOffset, virtual); if (wrapPosition < 0) return wrapPosition; final CharSequence text = document.getCharsSequence(); + if (wrapPosition > 0) { + char charBefore = text.charAt(wrapPosition - 1); + if (charBefore == '\'' || charBefore == '"') { + //don't wrap the first char of string literal + return wrapPosition + 1; + } + } + char c = text.charAt(wrapPosition); if (!StringUtil.isWhiteSpace(c) || project == null) { return wrapPosition; diff --git a/python/testData/wrap/DontWrapStartOfString.after.py b/python/testData/wrap/DontWrapStartOfString.after.py new file mode 100644 index 000000000000..5fb7919f6566 --- /dev/null +++ b/python/testData/wrap/DontWrapStartOfString.after.py @@ -0,0 +1,2 @@ +call_command('.' + 'regressiontests_some_long_text_here_to_test_right_margin_some_long_text_here_to_test_right_margin_some_long_text_here_to_test_right_margin') \ No newline at end of file diff --git a/python/testData/wrap/DontWrapStartOfString.py b/python/testData/wrap/DontWrapStartOfString.py new file mode 100644 index 000000000000..06487362ca6a --- /dev/null +++ b/python/testData/wrap/DontWrapStartOfString.py @@ -0,0 +1 @@ +call_command('.regressiontests') \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyWrapTest.java b/python/testSrc/com/jetbrains/python/PyWrapTest.java index 39606c3b5ca2..05c21b8af21a 100644 --- a/python/testSrc/com/jetbrains/python/PyWrapTest.java +++ b/python/testSrc/com/jetbrains/python/PyWrapTest.java @@ -64,6 +64,10 @@ public class PyWrapTest extends PyTestCase { doTest(" AND field"); } + public void testDontWrapStartOfString() { // PY-9436 + doTest("_some_long_text_here_to_test_right_margin_some_long_text_here_to_test_right_margin_some_long_text_here_to_test_right_margin"); + } + public void testWrapRightMargin() { final CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(myFixture.getProject()).getCurrentSettings(); @@ -83,7 +87,6 @@ public class PyWrapTest extends PyTestCase { settings.RIGHT_MARGIN = oldValue; settings.WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN = oldMarginValue; } - } private void doTest(final String textToType) {