feat(zon): Code formatting

This commit is contained in:
FalsePattern 2023-08-10 15:16:47 +02:00 committed by FalsePattern
parent 8814628f04
commit 2614cbb2f0
Signed by: falsepattern
GPG key ID: FDF7126A9E124447
4 changed files with 137 additions and 0 deletions

View file

@ -26,6 +26,7 @@ Changelog structure reference:
- Color settings page
- Brace matching
- Code completion
- Indentation
## [0.4.0]

View file

@ -0,0 +1,90 @@
/*
* Copyright 2023 FalsePattern
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.falsepattern.zigbrains.zon.formatter;
import com.falsepattern.zigbrains.zon.psi.ZonTypes;
import com.intellij.formatting.Alignment;
import com.intellij.formatting.Block;
import com.intellij.formatting.Indent;
import com.intellij.formatting.Spacing;
import com.intellij.formatting.SpacingBuilder;
import com.intellij.formatting.Wrap;
import com.intellij.lang.ASTNode;
import com.intellij.psi.TokenType;
import com.intellij.psi.formatter.common.AbstractBlock;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class ZonBlock extends AbstractBlock {
private final SpacingBuilder spacingBuilder;
protected ZonBlock(@NotNull ASTNode node, @Nullable Wrap wrap, @Nullable Alignment alignment,
SpacingBuilder spacingBuilder) {
super(node, wrap, alignment);
this.spacingBuilder = spacingBuilder;
}
@Override
protected List<Block> buildChildren() {
var blocks = new ArrayList<Block>();
var child = myNode.getFirstChildNode();
while (child != null) {
if ( child.getElementType() != TokenType.WHITE_SPACE) {
var block = new ZonBlock(child, null, null,
spacingBuilder);
blocks.add(block);
}
child = child.getTreeNext();
}
return blocks;
}
@Override
public Indent getIndent() {
if (myNode.getElementType() == ZonTypes.PROPERTY) {
return Indent.getNormalIndent();
} else {
return Indent.getNoneIndent();
}
}
@Override
public @Nullable Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) {
return null;
}
@Override
public boolean isLeaf() {
return myNode.getFirstChildNode() == null;
}
@Override
protected @Nullable Indent getChildIndent() {
if (myNode.getElementType() == ZonTypes.STRUCT) {
return Indent.getNormalIndent();
} else {
return Indent.getNoneIndent();
}
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright 2023 FalsePattern
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.falsepattern.zigbrains.zon.formatter;
import com.falsepattern.zigbrains.zon.ZonLanguage;
import com.intellij.formatting.FormattingContext;
import com.intellij.formatting.FormattingModel;
import com.intellij.formatting.FormattingModelBuilder;
import com.intellij.formatting.FormattingModelProvider;
import com.intellij.formatting.SpacingBuilder;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import org.jetbrains.annotations.NotNull;
public class ZonFormattingModelBuilder implements FormattingModelBuilder {
private static SpacingBuilder createSpaceBuilder(CodeStyleSettings settings) {
return new SpacingBuilder(settings, ZonLanguage.INSTANCE);
}
@Override
public @NotNull FormattingModel createModel(@NotNull FormattingContext formattingContext) {
final var codeStyleSettings = formattingContext.getCodeStyleSettings();
return FormattingModelProvider
.createFormattingModelForPsiFile(formattingContext.getContainingFile(),
new ZonBlock(formattingContext.getNode(),
null,
null,
createSpaceBuilder(codeStyleSettings)),
codeStyleSettings);
}
}

View file

@ -99,6 +99,9 @@
<completion.contributor language="Zon"
implementationClass="com.falsepattern.zigbrains.zon.completion.ZonCompletionContributor"/>
<lang.formatter language="Zon"
implementationClass="com.falsepattern.zigbrains.zon.formatter.ZonFormattingModelBuilder"/>
<!-- endregion Zon -->
</extensions>