fix: More indenting fixes (zig fmt parity)

This commit is contained in:
FalsePattern 2024-07-17 22:36:58 +02:00
parent c4c533eeb4
commit 7231d1b6a2
Signed by: falsepattern
GPG key ID: E930CDEC50C50E23
3 changed files with 49 additions and 6 deletions

View file

@ -21,6 +21,7 @@ Changelog structure reference:
- Zig - Zig
- Color settings has more accurate color preview text. - Color settings has more accurate color preview text.
- Better builtin indentation
### Changed ### Changed

View file

@ -168,9 +168,9 @@
Root ::= CONTAINER_DOC_COMMENT? ContainerMembers? Root ::= CONTAINER_DOC_COMMENT? ContainerMembers?
// *** Top level *** // *** Top level ***
private ContainerMembers ::= ContainerDeclarations (ContainerField COMMA)* (ContainerField | ContainerDeclarations)? ContainerMembers ::= ContainerDeclarations? (ContainerField COMMA)* (ContainerField | ContainerDeclarations)?
ContainerDeclarations ::= (TestDecl | ComptimeDecl | DOC_COMMENT? KEYWORD_PUB? Decl)* ContainerDeclarations ::= (TestDecl | ComptimeDecl | DOC_COMMENT? KEYWORD_PUB? Decl)+
TestDecl ::= DOC_COMMENT? KEYWORD_TEST (STRING_LITERAL_SINGLE | IDENTIFIER)? Block {pin=2} TestDecl ::= DOC_COMMENT? KEYWORD_TEST (STRING_LITERAL_SINGLE | IDENTIFIER)? Block {pin=2}

View file

@ -33,10 +33,23 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.ADDITION_EXPR;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.ASM_EXPR;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.ASM_INPUT_LIST;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.ASM_OUTPUT_LIST;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.BITWISE_EXPR;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.BIT_SHIFT_EXPR;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.BLOCK; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.BLOCK;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.BOOL_AND_EXPR;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.BOOL_OR_EXPR;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.COMPARE_EXPR;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.CONTAINER_DECLARATIONS;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.CONTAINER_DECL_AUTO; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.CONTAINER_DECL_AUTO;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.CONTAINER_DECL_TYPE; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.CONTAINER_DECL_TYPE;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.CONTAINER_DOC_COMMENT; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.CONTAINER_DOC_COMMENT;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.CONTAINER_FIELD;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.CONTAINER_MEMBERS;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.EXPR;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.EXPR_LIST; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.EXPR_LIST;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.FN_CALL_ARGUMENTS; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.FN_CALL_ARGUMENTS;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.FN_PROTO; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.FN_PROTO;
@ -44,9 +57,12 @@ import static com.falsepattern.zigbrains.zig.psi.ZigTypes.IF_EXPR;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.IF_PREFIX; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.IF_PREFIX;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.IF_STATEMENT; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.IF_STATEMENT;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.INIT_LIST; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.INIT_LIST;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.KEYWORD_ASM;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.KEYWORD_ELSE; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.KEYWORD_ELSE;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.KEYWORD_VOLATILE;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.LBRACE; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.LBRACE;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.LPAREN; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.LPAREN;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.MULTIPLY_EXPR;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.PARAM_DECL_LIST; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.PARAM_DECL_LIST;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.RBRACE; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.RBRACE;
import static com.falsepattern.zigbrains.zig.psi.ZigTypes.RPAREN; import static com.falsepattern.zigbrains.zig.psi.ZigTypes.RPAREN;
@ -92,14 +108,16 @@ public class ZigBlock extends AbstractBlock {
@Override @Override
protected @Nullable Indent getChildIndent() { protected @Nullable Indent getChildIndent() {
return getIndentBasedOnParentType(getNode().getElementType(), PLACEHOLDER); val node = getNode();
return getIndentBasedOnParentType(node, null, node.getElementType(), PLACEHOLDER);
} }
@Override @Override
public Indent getIndent() { public Indent getIndent() {
val parent = getNode().getTreeParent(); val node = getNode();
val parent = node.getTreeParent();
if (parent != null) { if (parent != null) {
return getIndentBasedOnParentType(parent.getElementType(), getNode().getElementType()); return getIndentBasedOnParentType(parent, node, parent.getElementType(), node.getElementType());
} }
return Indent.getNoneIndent(); return Indent.getNoneIndent();
} }
@ -112,8 +130,12 @@ public class ZigBlock extends AbstractBlock {
return element == LPAREN || element == RPAREN; return element == LPAREN || element == RPAREN;
} }
private static boolean isEmpty(ASTNode child) {
return child.getFirstChildNode() == null;
}
private static Indent getIndentBasedOnParentType(IElementType parentElementType, IElementType childElementType) {
private static @NotNull Indent getIndentBasedOnParentType(@NotNull ASTNode parent, @Nullable ASTNode child, @NotNull IElementType parentElementType, @NotNull IElementType childElementType) {
//Statement blocks //Statement blocks
if (parentElementType == BLOCK && !isBrace(childElementType)) if (parentElementType == BLOCK && !isBrace(childElementType))
return Indent.getNormalIndent(); return Indent.getNormalIndent();
@ -132,6 +154,17 @@ public class ZigBlock extends AbstractBlock {
parentElementType == FN_PROTO && childElementType == PLACEHOLDER) parentElementType == FN_PROTO && childElementType == PLACEHOLDER)
return Indent.getNormalIndent(); return Indent.getNormalIndent();
//Chained operations on newlines
if ((parentElementType == BOOL_OR_EXPR ||
parentElementType == BOOL_AND_EXPR ||
parentElementType == COMPARE_EXPR ||
parentElementType == BITWISE_EXPR ||
parentElementType == BIT_SHIFT_EXPR ||
parentElementType == ADDITION_EXPR ||
parentElementType == MULTIPLY_EXPR) &&
parent.getFirstChildNode() != child)
return Indent.getNormalIndent();
//Switch prongs //Switch prongs
if (parentElementType == SWITCH_PRONG_LIST || if (parentElementType == SWITCH_PRONG_LIST ||
parentElementType == SWITCH_EXPR && childElementType == PLACEHOLDER) parentElementType == SWITCH_EXPR && childElementType == PLACEHOLDER)
@ -141,9 +174,18 @@ public class ZigBlock extends AbstractBlock {
if ((parentElementType == IF_EXPR || parentElementType == IF_STATEMENT) && childElementType != KEYWORD_ELSE && childElementType != IF_PREFIX) if ((parentElementType == IF_EXPR || parentElementType == IF_STATEMENT) && childElementType != KEYWORD_ELSE && childElementType != IF_PREFIX)
return Indent.getNormalIndent(); return Indent.getNormalIndent();
//Struct members
if (parentElementType == CONTAINER_DECL_AUTO && childElementType != CONTAINER_DECL_TYPE && childElementType != CONTAINER_DOC_COMMENT && !isBrace(childElementType)) if (parentElementType == CONTAINER_DECL_AUTO && childElementType != CONTAINER_DECL_TYPE && childElementType != CONTAINER_DOC_COMMENT && !isBrace(childElementType))
return Indent.getNormalIndent(); return Indent.getNormalIndent();
//Inline assembly body
if (parentElementType == ASM_EXPR && childElementType != KEYWORD_ASM && childElementType != KEYWORD_VOLATILE && !isParen(childElementType))
return Indent.getNormalIndent();
//Assembly params
if (parentElementType == ASM_INPUT_LIST || parentElementType == ASM_OUTPUT_LIST)
return Indent.getSpaceIndent(2);
return Indent.getNoneIndent(); return Indent.getNoneIndent();
} }
} }