From b39d794fa87d2d69ab470004f9b54b9986e80e21 Mon Sep 17 00:00:00 2001 From: Piotr Tomiak Date: Mon, 25 May 2020 13:50:37 +0200 Subject: [PATCH] WEB-39103 WEB-30392 WEB-30024 WEB-41056 Fix issues with indentation on Enter in mixed lang files Use indentation of previous block if language changes and use child block language to calculate child indentation GitOrigin-RevId: 4f15e15bcc0b672edae6fe12f67cf2a2c1dee49b --- .../intellij/formatting/FormatProcessor.java | 19 +++++++++++++++++-- .../formatting/engine/IndentAdjuster.java | 4 ++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java b/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java index 0a2b91ecf380..be1f38f5c9f1 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java @@ -25,6 +25,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; +import java.util.Objects; import static com.intellij.formatting.InitialInfoBuilder.prepareToBuildBlocksSequentially; @@ -169,7 +170,7 @@ public class FormatProcessor { } public IndentInfo getIndentAt(final int offset) { - LeafBlockWrapper current = processBlocksBefore(offset); + LeafBlockWrapper current = adjustAtLanguageBorder(processBlocksBefore(offset), offset); AbstractBlockWrapper parent = getParentFor(offset, current); if (parent == null) { final LeafBlockWrapper previousBlock = current.getPreviousBlock(); @@ -189,7 +190,21 @@ public class FormatProcessor { } IndentAdjuster adjuster = myWrapState.getIndentAdjuster(); - return adjuster.adjustLineIndent(info); + return adjuster.adjustLineIndent(info, current); + } + + private static @NotNull LeafBlockWrapper adjustAtLanguageBorder(@NotNull LeafBlockWrapper current, final int offset) { + if (!current.contains(offset)) { + final LeafBlockWrapper previousBlock = current.getPreviousBlock(); + if (previousBlock != null && !previousBlock.contains(offset) && + !Objects.equals(previousBlock.getLanguage(), current.getLanguage())) { + AbstractBlockWrapper prevParent = getParentFor(offset, (AbstractBlockWrapper)previousBlock); + if (prevParent != null && prevParent.getEndOffset() <= current.getStartOffset()) { + return previousBlock; + } + } + } + return current; } @Nullable diff --git a/platform/lang-impl/src/com/intellij/formatting/engine/IndentAdjuster.java b/platform/lang-impl/src/com/intellij/formatting/engine/IndentAdjuster.java index 2b0dca78ccb2..b263c72d569c 100644 --- a/platform/lang-impl/src/com/intellij/formatting/engine/IndentAdjuster.java +++ b/platform/lang-impl/src/com/intellij/formatting/engine/IndentAdjuster.java @@ -105,14 +105,14 @@ public class IndentAdjuster { } } - public IndentInfo adjustLineIndent(FormatProcessor.ChildAttributesInfo info) { + public IndentInfo adjustLineIndent(FormatProcessor.ChildAttributesInfo info, AbstractBlockWrapper child) { AbstractBlockWrapper parent = info.parent; ChildAttributes childAttributes = info.attributes; int index = info.index; AlignWhiteSpace alignWhiteSpace = getAlignOffsetBefore(childAttributes.getAlignment()); if (alignWhiteSpace == null) { - return parent.calculateChildOffset(myBlockIndentOptions.getIndentOptions(parent), childAttributes, index).createIndentInfo(); + return parent.calculateChildOffset(myBlockIndentOptions.getIndentOptions(child), childAttributes, index).createIndentInfo(); } else { return new IndentInfo(0, alignWhiteSpace.indentSpaces, alignWhiteSpace.alignSpaces);