custom language formatter intent problem Follow
Answered
Hi, I've written a plugin for a custom language and I implement a simple formatting function. It works but has a little problem I dont' know how to fix it.
Here is the problem:
When I press the enter, it will go to a newline with eight spaces (sometimes four) wide extra indent. And my default indent is four spaces wide. Then I type any statement and format the code, the intent will back to normal. Why the newline will with extra intents?
Here is the Block class:
public class YJSBlock extends AbstractBlock {
private final SpacingBuilder spacingBuilder;
protected YJSBlock(@NotNull ASTNode node,
@Nullable Wrap wrap,
@Nullable Alignment alignment,
SpacingBuilder spacingBuilder) {
super(node, wrap, alignment);
this.spacingBuilder = spacingBuilder;
}
@Override
protected List<Block> buildChildren() {
List<Block> blocks = new ArrayList<>();
ASTNode child = myNode.getFirstChildNode();
while (child != null) {
if (child.getElementType() != TokenType.WHITE_SPACE) {
Block block = new YJSBlock(child, null, null, spacingBuilder);
blocks.add(block);
}
child = child.getTreeNext();
}
return blocks;
}
@Override
public Indent getIndent() {
if (isEmpty(myNode)) {
return null;
}
String parentType;
if(myNode.getTreeParent() != null) {
parentType = myNode.getTreeParent().getElementType().toString();
} else {
parentType = "";
}
String firstChildType;
if(myNode.getFirstChildNode() != null) {
firstChildType = myNode.getFirstChildNode().getElementType().toString();
} else {
firstChildType = "";
}
String type = myNode.getElementType().toString();
if(type.equals("propertyAssignment"))
return Indent.getNormalIndent();
if(type.equals("clzOrFunctionDeclaration"))
return Indent.getNormalIndent();
if(type.equals("functionBody"))
return Indent.getNormalIndent();
if(type.equals("statementList") && parentType.equals("block"))
return Indent.getNormalIndent();
if (type.equals("statement") && parentType.equals("ifStatement") && !firstChildType.equals("block"))
return Indent .getNormalIndent();
return Indent.getNoneIndent();
}
@Nullable
@Override
public Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) {
return spacingBuilder.getSpacing(this, child1, child2);
}
@Override
public boolean isLeaf() {
return myNode.getFirstChildNode() == null;
}
private static boolean isEmpty(ASTNode node) {
return node.getElementType() == TokenType.WHITE_SPACE || node.getTextLength() == 0;
}
}
Please sign in to leave a comment.
Hi,
Please take a look at
Block.getChildAttributes()
and adjust it to your needs.