From 6df5bfd10d79bba0e6f2a2bff5810316df87423d Mon Sep 17 00:00:00 2001 From: "Olga.Lavrichenko" Date: Mon, 16 Jan 2023 15:13:23 +0000 Subject: [PATCH] DS-1584 DS-2583 Reformat breaks IPython magic commands and shell commands GitOrigin-RevId: 949528c951d6c9174b1bad21452a90fbdb07b418 --- .../python/PythonCommonFormatterTest.java | 16 +++++++- .../PythonFormattingModelBuilder.java | 3 ++ .../spaceIPythonMagicCommandsJupyter.ipynb | 37 +++++++++++++++++++ ...aceIPythonMagicCommandsJupyter_after.ipynb | 37 +++++++++++++++++++ .../jupyter/spaceShellCommandsJupyter.ipynb | 36 ++++++++++++++++++ .../spaceShellCommandsJupyter_after.ipynb | 36 ++++++++++++++++++ .../formatter/spaceIPythonMagicCommands.py | 2 + .../spaceIPythonMagicCommands_after.py | 2 + 8 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 python/testData/formatter/jupyter/spaceIPythonMagicCommandsJupyter.ipynb create mode 100644 python/testData/formatter/jupyter/spaceIPythonMagicCommandsJupyter_after.ipynb create mode 100644 python/testData/formatter/jupyter/spaceShellCommandsJupyter.ipynb create mode 100644 python/testData/formatter/jupyter/spaceShellCommandsJupyter_after.ipynb create mode 100644 python/testData/formatter/spaceIPythonMagicCommands.py create mode 100644 python/testData/formatter/spaceIPythonMagicCommands_after.py diff --git a/python/python-common-tests/com/jetbrains/python/PythonCommonFormatterTest.java b/python/python-common-tests/com/jetbrains/python/PythonCommonFormatterTest.java index a06c6a3f1276..a197d700deaa 100644 --- a/python/python-common-tests/com/jetbrains/python/PythonCommonFormatterTest.java +++ b/python/python-common-tests/com/jetbrains/python/PythonCommonFormatterTest.java @@ -485,7 +485,11 @@ public abstract class PythonCommonFormatterTest extends PythonCommonTestCase { } private void doTest(final boolean reformatText) { - myFixture.configureByFile("formatter/" + getTestName(true) + ".py"); + doTest(reformatText, "formatter/", ".py"); + } + + private void doTest(final boolean reformatText, final String filePath, final String fileExtension) { + myFixture.configureByFile(filePath + getTestName(true) + fileExtension); WriteCommandAction.runWriteCommandAction(null, () -> { CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myFixture.getProject()); PsiFile file = myFixture.getFile(); @@ -496,7 +500,7 @@ public abstract class PythonCommonFormatterTest extends PythonCommonTestCase { codeStyleManager.reformat(file); } }); - myFixture.checkResultByFile("formatter/" + getTestName(true) + "_after.py"); + myFixture.checkResultByFile(filePath + getTestName(true) + "_after" + fileExtension); } // PY-12861 @@ -588,6 +592,14 @@ public abstract class PythonCommonFormatterTest extends PythonCommonTestCase { doTest(); } + //DS-1584 + public void testSpaceIPythonMagicCommands() { doTest(); } + + public void testSpaceIPythonMagicCommandsJupyter() { doTest(false, "formatter/jupyter/", ".ipynb"); } + + //DS-2583 + public void testSpaceShellCommandsJupyter() { doTest(false, "formatter/jupyter/", ".ipynb"); } + /** * This test merely checks that call to {@link com.intellij.psi.codeStyle.CodeStyleManager#reformat(com.intellij.psi.PsiElement)} * is possible for Python sources. diff --git a/python/python-psi-impl/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java b/python/python-psi-impl/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java index 912d02615a72..8637d96d80a1 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java +++ b/python/python-psi-impl/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java @@ -132,6 +132,9 @@ public class PythonFormattingModelBuilder implements FormattingModelBuilder, Cus .aroundInside(STAR_OPERATORS, STAR_PARAMETERS).none() .aroundInside(STAR_OPERATORS, STAR_PATTERNS).none() .between(EXCEPT_KEYWORD, MULT).none() + .between(PERC, TokenSet.create(PERC, IDENTIFIER, EXPRESSION_STATEMENT)).none() + .between(DIV, IDENTIFIER).spaces(0) + .between(IDENTIFIER, DIV).spaces(0) .around(MULTIPLICATIVE_OPERATIONS).spaceIf(commonSettings.SPACE_AROUND_MULTIPLICATIVE_OPERATORS) .around(EXP).spaceIf(pySettings.SPACE_AROUND_POWER_OPERATOR) .around(SHIFT_OPERATIONS).spaceIf(commonSettings.SPACE_AROUND_SHIFT_OPERATORS) diff --git a/python/testData/formatter/jupyter/spaceIPythonMagicCommandsJupyter.ipynb b/python/testData/formatter/jupyter/spaceIPythonMagicCommandsJupyter.ipynb new file mode 100644 index 000000000000..f44768e95e19 --- /dev/null +++ b/python/testData/formatter/jupyter/spaceIPythonMagicCommandsJupyter.ipynb @@ -0,0 +1,37 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%%time\n", + "%dirs" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/python/testData/formatter/jupyter/spaceIPythonMagicCommandsJupyter_after.ipynb b/python/testData/formatter/jupyter/spaceIPythonMagicCommandsJupyter_after.ipynb new file mode 100644 index 000000000000..f44768e95e19 --- /dev/null +++ b/python/testData/formatter/jupyter/spaceIPythonMagicCommandsJupyter_after.ipynb @@ -0,0 +1,37 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%%time\n", + "%dirs" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/python/testData/formatter/jupyter/spaceShellCommandsJupyter.ipynb b/python/testData/formatter/jupyter/spaceShellCommandsJupyter.ipynb new file mode 100644 index 000000000000..f745688710d3 --- /dev/null +++ b/python/testData/formatter/jupyter/spaceShellCommandsJupyter.ipynb @@ -0,0 +1,36 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "!rm -r path/to/folder" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/python/testData/formatter/jupyter/spaceShellCommandsJupyter_after.ipynb b/python/testData/formatter/jupyter/spaceShellCommandsJupyter_after.ipynb new file mode 100644 index 000000000000..f745688710d3 --- /dev/null +++ b/python/testData/formatter/jupyter/spaceShellCommandsJupyter_after.ipynb @@ -0,0 +1,36 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "!rm -r path/to/folder" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/python/testData/formatter/spaceIPythonMagicCommands.py b/python/testData/formatter/spaceIPythonMagicCommands.py new file mode 100644 index 000000000000..c9fdd778b659 --- /dev/null +++ b/python/testData/formatter/spaceIPythonMagicCommands.py @@ -0,0 +1,2 @@ +%%time +%dirs \ No newline at end of file diff --git a/python/testData/formatter/spaceIPythonMagicCommands_after.py b/python/testData/formatter/spaceIPythonMagicCommands_after.py new file mode 100644 index 000000000000..4e8d6bd18651 --- /dev/null +++ b/python/testData/formatter/spaceIPythonMagicCommands_after.py @@ -0,0 +1,2 @@ +%%time +%dirs