feat(zon): Brace matching

This commit is contained in:
FalsePattern 2023-08-10 08:18:30 +02:00 committed by FalsePattern
parent 828bd1304c
commit 211f2c8065
Signed by: falsepattern
GPG key ID: FDF7126A9E124447
3 changed files with 55 additions and 0 deletions

View file

@ -24,6 +24,7 @@ Changelog structure reference:
- Basic parser and PSI tree
- Basic syntax highlighting
- Color settings page
- Brace matching
## [0.4.0]

View file

@ -0,0 +1,52 @@
/*
* 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.braces;
import com.falsepattern.zigbrains.zon.psi.ZonTypes;
import com.intellij.lang.BracePair;
import com.intellij.lang.PairedBraceMatcher;
import com.intellij.psi.PsiFile;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ZonBraceMatcher implements PairedBraceMatcher {
public static final BracePair PAIR = new BracePair(ZonTypes.LBRACE, ZonTypes.RBRACE, true);
private static final BracePair[] PAIRS = new BracePair[]{PAIR};
@Override
public BracePair @NotNull [] getPairs() {
return PAIRS;
}
@Override
public boolean isPairedBracesAllowedBeforeType(@NotNull IElementType lbraceType, @Nullable IElementType contextType) {
return true;
}
@Override
public int getCodeConstructStart(PsiFile file, int openingBraceOffset) {
var theBrace = file.findElementAt(openingBraceOffset);
if (theBrace == null) {
return openingBraceOffset;
}
var parent = theBrace.getParent();
if (parent == null) {
return openingBraceOffset;
}
return parent.getTextOffset();
}
}

View file

@ -94,6 +94,8 @@
<colorSettingsPage implementation="com.falsepattern.zigbrains.zon.highlight.ZonColorSettingsPage"/>
<lang.braceMatcher language="Zon"
implementationClass="com.falsepattern.zigbrains.zon.braces.ZonBraceMatcher"/>
<!-- endregion Zon -->
</extensions>