From 4128424a5f8ebeb783a476ebf763ea9548656b3c Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Fri, 13 Nov 2015 18:35:59 +0300 Subject: [PATCH] PY-17593 Remove IMPORT_GROUP_BEGIN marker from import statements after reformat so user could adjust spacing manually and keep it next time formatting is called. --- .../src/com/jetbrains/python/formatter/PyBlock.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index 461df768fb17..1c94b0ecbe1d 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -720,10 +720,17 @@ public class PyBlock implements ASTBlock { if (psi1 instanceof PyImportStatementBase) { if (psi2 instanceof PyImportStatementBase) { - if (psi2.getCopyableUserData(IMPORT_GROUP_BEGIN) != null) { + final Boolean leftImportIsGroupStart = psi1.getCopyableUserData(IMPORT_GROUP_BEGIN); + final Boolean rightImportIsGroupStart = psi2.getCopyableUserData(IMPORT_GROUP_BEGIN); + // Cleanup user data, it's no longer needed + psi1.putCopyableUserData(IMPORT_GROUP_BEGIN, null); + // Don't remove IMPORT_GROUP_BEGIN from the element psi2 yet, because spacing is constructed pairwise: + // it might be needed on the next iteration. + //psi2.putCopyableUserData(IMPORT_GROUP_BEGIN, null); + if (rightImportIsGroupStart != null) { return Spacing.createSpacing(0, 0, 2, true, 1); } - else if (psi1.getCopyableUserData(IMPORT_GROUP_BEGIN) != null) { + else if (leftImportIsGroupStart != null) { // It's a trick to keep spacing consistent when new import statement is inserted // at the beginning of an import group, i.e. if there is a blank line before the next // import we want to save it, but remove line *after* inserted import.