How can i prevent formatting of my custom language has a syntax errors (or any errors for that matter)?
Hi,
I created a custom language and produced a parser/lexer using bnf/flex. Now, I am creating a plugin that can syntax highlight and format using keyboard shortcuts. When the file has no errors, formatting works great. It indents and puts spaces where it needs to defined in the FormattingModelBuilder. However, when there is a grammar rule error and when I hit the keyboard shortcut to format the file, things get deleted. The first picture shows there is an issue in the file. The second picture is the result of the keyboard shortcut to format the file, performed after the error..


It deletes everything.. lol
I can prevent the formatting to happen when I use following block of code in `buildChildren` function, but only works for blocks that are not complete because user has yet to type something in the future like a parenthesis or something to finish the block.
protected List<Block> buildChildren() {
if(this.isIncomplete()){
return AbstractBlock.EMPTY;
}
.
.
.

It doesn't handle when the block has an issue in the middle of it. Is there an method I can leverage that says "hey there is an issue in the file, so dont format anything"?
Please sign in to leave a comment.
Fixed it.
When building the children if the child doesn't match any IElementType see if the child.getPsi() is of type PsiErrorElement. If so, you want to return back an empty list
```
protected List<Block> buildChildren() {
if(this.isIncomplete()){
return AbstractBlock.EMPTY;
}
List<Block> blocks = new ArrayList<Block>();
ASTNode child = myNode.getFirstChildNode();
while (child != null) {
if (child.getElementType() == SimpleTypes.MAPPER) {
Block block = new MapperBlock(child, Wrap.createWrap(WrapType.NONE, false), Alignment.createAlignment(),
spacingBuilder);
blocks.add(block);
} else if (child.getElementType() == SimpleTypes.MAPPER_MAPPINGS) {
Block block = new MappingListBlock(child, Wrap.createWrap(WrapType.NONE, false), Alignment.createAlignment(),
spacingBuilder);
blocks.add(block);
} else if (child.getElementType() == SimpleTypes.SOURCE_WORD) {
Block block = new MapperSourceWordBlock(
child,
Wrap.createWrap(WrapType.NONE, false),
Alignment.createAlignment(),
spacingBuilder);
blocks.add(block);
} else if (child.getElementType() == SimpleTypes.MAPPER_TABLE_NAME) {
Block block = new MapperTableNameBlock(
child,
Wrap.createWrap(WrapType.NONE, false),
Alignment.createAlignment(),
spacingBuilder);
blocks.add(block);
} else if (child.getElementType() == SimpleTypes.OPEN_PARENTH) {
Block block = new MapperOpenParenthBlock(
child,
Wrap.createWrap(WrapType.NONE, false),
Alignment.createAlignment(),
spacingBuilder);
blocks.add(block);
} else if (child.getElementType() == SimpleTypes.CLOSE_PARENTH) {
Block block = new MapperCloseParenthBlock(
child,
Wrap.createWrap(WrapType.NONE, false),
Alignment.createAlignment(),
spacingBuilder);
blocks.add(block);
} else if (child.getElementType() == SimpleTypes.FROM_WORD) {
Block block = new MapperFromWordBlock(
child,
Wrap.createWrap(WrapType.NONE, false),
Alignment.createAlignment(),
spacingBuilder);
blocks.add(block);
} else if (child.getElementType() == SimpleTypes.MAPPER_FROM_FIELD) {
Block block = new MapperFromFieldBlock(
child,
Wrap.createWrap(WrapType.NONE, false),
Alignment.createAlignment(),
spacingBuilder);
blocks.add(block);
} else if (child.getPsi() instanceof PsiErrorElement){
return new ArrayList<Block>();
}
child = child.getTreeNext();
}
return blocks;
}
```