From eb600f704620efd0038809deae4f2f195fb0d332 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Thu, 14 Sep 2017 17:52:31 +0300 Subject: [PATCH] PY-21823 Align bound and stride expressions inside slices --- python/src/com/jetbrains/python/formatter/PyBlock.java | 6 +++++- python/testData/formatter/sliceAlignment.py | 10 ++++++++++ python/testData/formatter/sliceAlignment_after.py | 10 ++++++++++ .../testSrc/com/jetbrains/python/PyFormatterTest.java | 5 +++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 python/testData/formatter/sliceAlignment.py create mode 100644 python/testData/formatter/sliceAlignment_after.py diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index 818e43150737..dc1a5a3032d6 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -327,6 +327,10 @@ public class PyBlock implements ASTBlock { childIndent = Indent.getNoneIndent(); } } + // Note that colons are aligned together with bounds and stride + else if (myNode.getElementType() == PyElementTypes.SLICE_ITEM) { + childAlignment = getChildAlignment(); + } else if (parentType == PyElementTypes.GENERATOR_EXPRESSION || parentType == PyElementTypes.PARENTHESIZED_EXPRESSION) { final boolean tupleOrGenerator = parentType == PyElementTypes.GENERATOR_EXPRESSION || myNode.getPsi(PyParenthesizedExpression.class).getContainedExpression() instanceof PyTupleExpression; @@ -934,7 +938,7 @@ public class PyBlock implements ASTBlock { @Nullable private Alignment getChildAlignment() { - if (ourListElementTypes.contains(myNode.getElementType())) { + if (ourListElementTypes.contains(myNode.getElementType()) || myNode.getElementType() == PyElementTypes.SLICE_ITEM) { if (isInControlStatement()) { return null; } diff --git a/python/testData/formatter/sliceAlignment.py b/python/testData/formatter/sliceAlignment.py new file mode 100644 index 000000000000..b976e62eab69 --- /dev/null +++ b/python/testData/formatter/sliceAlignment.py @@ -0,0 +1,10 @@ +xs1 = ys[42: +5: +-1] + +xs2 = ys[: +2: +3] + +xs3 = ys[:: +3] diff --git a/python/testData/formatter/sliceAlignment_after.py b/python/testData/formatter/sliceAlignment_after.py new file mode 100644 index 000000000000..3fe27e377656 --- /dev/null +++ b/python/testData/formatter/sliceAlignment_after.py @@ -0,0 +1,10 @@ +xs1 = ys[42: + 5: + -1] + +xs2 = ys[: + 2: + 3] + +xs3 = ys[:: + 3] diff --git a/python/testSrc/com/jetbrains/python/PyFormatterTest.java b/python/testSrc/com/jetbrains/python/PyFormatterTest.java index f905bc5660e4..9c4b0fa6a8c6 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -847,6 +847,11 @@ public class PyFormatterTest extends PyTestCase { doTest(); } + // PY-21823 + public void testSliceAlignment() { + doTest(); + } + public void testVariableAnnotations() { runWithLanguageLevel(LanguageLevel.PYTHON36, this::doTest); }