From af2fc97a8e8f5226b2c72cc1023e67f3371cd545 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 7 Aug 2012 14:39:20 +0200 Subject: [PATCH] handle backslashes in introduce variable initializer correctly (PY-6908); formatter removes spaces around dot --- .../PythonFormattingModelBuilder.java | 1 + .../introduce/IntroduceHandler.java | 2 +- python/testData/formatter/spaceAroundDot.py | 1 + .../testData/formatter/spaceAroundDot_after.py | 1 + .../introduceVariable/backslash.after.py | 3 +++ .../refactoring/introduceVariable/backslash.py | 3 +++ .../com/jetbrains/python/PyFormatterTest.java | 18 +++++++++++------- .../refactoring/PyIntroduceVariableTest.java | 4 ++++ 8 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 python/testData/formatter/spaceAroundDot.py create mode 100644 python/testData/formatter/spaceAroundDot_after.py create mode 100644 python/testData/refactoring/introduceVariable/backslash.after.py create mode 100644 python/testData/refactoring/introduceVariable/backslash.py diff --git a/python/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java b/python/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java index cca6044b7358..f69f627f9c54 100644 --- a/python/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java +++ b/python/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java @@ -69,6 +69,7 @@ public class PythonFormattingModelBuilder implements FormattingModelBuilder, Cus .before(COLON).spaceIf(pySettings.SPACE_BEFORE_PY_COLON) .after(COMMA).spaceIf(commonSettings.SPACE_AFTER_COMMA) .before(COMMA).spaceIf(commonSettings.SPACE_BEFORE_COMMA) + .around(DOT).spaces(0) .before(SEMICOLON).spaceIf(commonSettings.SPACE_BEFORE_SEMICOLON) .withinPairInside(LPAR, RPAR, ARGUMENT_LIST).spaceIf(commonSettings.SPACE_WITHIN_METHOD_CALL_PARENTHESES) .before(LBRACKET).spaceIf(pySettings.SPACE_BEFORE_LBRACKET) diff --git a/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java b/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java index dbf51bb1a95e..72f9416fd1d2 100644 --- a/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java +++ b/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java @@ -435,7 +435,7 @@ abstract public class IntroduceHandler implements RefactoringActionHandler { @Override public void visitWhiteSpace(PsiWhiteSpace space) { - myResult.append(space.getText().replace('\n', ' ')); + myResult.append(space.getText().replace('\n', ' ').replace("\\", "")); } @Override diff --git a/python/testData/formatter/spaceAroundDot.py b/python/testData/formatter/spaceAroundDot.py new file mode 100644 index 000000000000..220956833d76 --- /dev/null +++ b/python/testData/formatter/spaceAroundDot.py @@ -0,0 +1 @@ +foo . bar \ No newline at end of file diff --git a/python/testData/formatter/spaceAroundDot_after.py b/python/testData/formatter/spaceAroundDot_after.py new file mode 100644 index 000000000000..4d5f9756e551 --- /dev/null +++ b/python/testData/formatter/spaceAroundDot_after.py @@ -0,0 +1 @@ +foo.bar \ No newline at end of file diff --git a/python/testData/refactoring/introduceVariable/backslash.after.py b/python/testData/refactoring/introduceVariable/backslash.after.py new file mode 100644 index 000000000000..324232e5f27a --- /dev/null +++ b/python/testData/refactoring/introduceVariable/backslash.after.py @@ -0,0 +1,3 @@ +def f(x): + a = x.foo.bar + return a.baz() \ No newline at end of file diff --git a/python/testData/refactoring/introduceVariable/backslash.py b/python/testData/refactoring/introduceVariable/backslash.py new file mode 100644 index 000000000000..611cd134f551 --- /dev/null +++ b/python/testData/refactoring/introduceVariable/backslash.py @@ -0,0 +1,3 @@ +def f(x): + return x.foo\ + .bar.baz() \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyFormatterTest.java b/python/testSrc/com/jetbrains/python/PyFormatterTest.java index d450579fb31f..d579e7cc7693 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -50,13 +50,13 @@ public class PyFormatterTest extends PyTestCase { public void testBlankLineBeforeFunction() { doTest(); } - + public void testStarArgument() { // PY-1395 doTest(); } public void testDictLiteral() { // PY-1461 - doTest(); + doTest(); } public void testListAssignment() { // PY-1522 @@ -121,12 +121,12 @@ public class PyFormatterTest extends PyTestCase { CodeStyleSettingsManager.getInstance().getSettings(myFixture.getProject()).SPACE_BEFORE_METHOD_PARENTHESES = true; doTest(); } - + public void testOptionalAlignForMethodParameters() { // PY-3995 CodeStyleSettingsManager.getInstance().getSettings(myFixture.getProject()).ALIGN_MULTILINE_PARAMETERS = false; doTest(); } - + public void testNoAlignForMethodArguments() { // PY-3995 doTest(); } @@ -135,7 +135,7 @@ public class PyFormatterTest extends PyTestCase { CodeStyleSettingsManager.getInstance().getSettings(myFixture.getProject()).ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true; doTest(); } - + public void testLambdaColon() { doTest(); } @@ -148,6 +148,10 @@ public class PyFormatterTest extends PyTestCase { doTest(); } + public void testSpaceAroundDot() { // PY-6908 + doTest(); + } + public void testPsiFormatting() { // IDEA-69724 String initial = "def method_name(\n" + @@ -158,7 +162,7 @@ public class PyFormatterTest extends PyTestCase { " # Extract from here to ...\n" + " desired_impulse_response = {'dirac, 'gaussian', logistic_derivative'}\n" + "return desired, o"; - + final PsiFile file = PyElementGenerator.getInstance(myFixture.getProject()).createDummyFile(LanguageLevel.PYTHON30, initial); final PsiElement reformatted = CodeStyleManager.getInstance(myFixture.getProject()).reformat(file); @@ -176,7 +180,7 @@ public class PyFormatterTest extends PyTestCase { assertEquals(expected, reformatted.getText()); } - private void doTest() { + private void doTest() { myFixture.configureByFile("formatter/" + getTestName(true) + ".py"); ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java index 83af8626b36a..f1cbd040ffa7 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java @@ -80,6 +80,10 @@ public class PyIntroduceVariableTest extends PyIntroduceTestCase { doTest(); } + public void testBackslash() { // PY-6908 + doTest(); + } + private void doTestCannotPerform() { boolean thrownExpectedException = false; try {