From 475253f9515eac6a0fb5182f54d2e3386eff8b1c Mon Sep 17 00:00:00 2001 From: m0rkeulv Date: Mon, 5 Feb 2024 20:02:23 +0100 Subject: [PATCH] Avoid formatting anonymous types as line breaks in method declarations etc looks strange --- .../haxe/ide/formatter/HaxeSpacingProcessor.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/intellij/plugins/haxe/ide/formatter/HaxeSpacingProcessor.java b/src/main/java/com/intellij/plugins/haxe/ide/formatter/HaxeSpacingProcessor.java index f36fee1cf..8056cd148 100644 --- a/src/main/java/com/intellij/plugins/haxe/ide/formatter/HaxeSpacingProcessor.java +++ b/src/main/java/com/intellij/plugins/haxe/ide/formatter/HaxeSpacingProcessor.java @@ -24,12 +24,14 @@ import com.intellij.lang.ASTNode; import com.intellij.openapi.util.TextRange; import com.intellij.plugins.haxe.ide.formatter.settings.HaxeCodeStyleSettings; +import com.intellij.plugins.haxe.lang.psi.HaxeTypeTag; import com.intellij.plugins.haxe.metadata.util.HaxeMetadataUtils; import com.intellij.psi.PsiElement; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.intellij.psi.formatter.common.AbstractBlock; import com.intellij.psi.tree.IElementType; +import com.intellij.psi.util.PsiTreeUtil; import lombok.CustomLog; import java.util.List; @@ -161,14 +163,18 @@ public Spacing getSpacingInternal(Block child1, Block child2) { if (isClassBodyType(type1)) { // End of the class body. (After the right brace.) return Spacing.createSpacing(0, 0, 1, false, mySettings.KEEP_BLANK_LINES_IN_CODE); } + // avoid multi-line formatting types (anonymous structures have brackets) + boolean isType = parentType == ANONYMOUS_TYPE || PsiTreeUtil.getParentOfType(myNode.getPsi(), HaxeTypeTag.class) != null; if (type1 == PLCURLY && isClassBodyType(elementType) && isFirstChild(child1)) { - int lineFeeds = max(1, (isFieldDeclaration(type2) ? mySettings.BLANK_LINES_AROUND_FIELD : mySettings.BLANK_LINES_AROUND_METHOD)); + int settings = isFieldDeclaration(type2) ? mySettings.BLANK_LINES_AROUND_FIELD : mySettings.BLANK_LINES_AROUND_METHOD; + int lineFeeds = isType ? 0 : max(1, settings); return Spacing.createSpacing(0, 0, lineFeeds, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); } if (type2 == PRCURLY && isClassBodyType(elementType) && isLastChild(child2)) { - return Spacing.createSpacing(0, 0, max(1, mySettings.BLANK_LINES_BEFORE_CLASS_END), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); + int lineFeeds = isType ? 0 : max(1, mySettings.BLANK_LINES_BEFORE_CLASS_END); + return Spacing.createSpacing(0, 0, lineFeeds, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); } if (isMethodDeclarationOrConstructorDeclaration(type1) && isMethodDeclarationOrConstructorDeclaration(type2)) {