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
This commit is contained in:
Piotr Tomiak
2020-05-26 09:49:11 +03:00
committed by intellij-monorepo-bot
parent 8ec4f3b419
commit b39d794fa8
2 changed files with 19 additions and 4 deletions
@@ -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
@@ -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);