Skip to content

Commit 10d385d

Browse files
Merge pull request #6 from protostuff/feature/brace-matching
Add brace matching support
2 parents 3f6a1d9 + 9e34974 commit 10d385d

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.protostuff.jetbrains.plugin;
2+
3+
import com.intellij.lang.BracePair;
4+
import com.intellij.lang.PairedBraceMatcher;
5+
import com.intellij.psi.PsiFile;
6+
import com.intellij.psi.tree.IElementType;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
import static io.protostuff.jetbrains.plugin.ProtoParserDefinition.*;
11+
12+
/**
13+
* @author Kostiantyn Shchepanovskyi
14+
*/
15+
public class ProtoBraceMatcher implements PairedBraceMatcher {
16+
17+
private static final BracePair[] PAIRS = {
18+
new BracePair(LCURLY, RCURLY, true),
19+
new BracePair(LPAREN, RPAREN, false),
20+
new BracePair(LSQUARE, RSQUARE, false),
21+
new BracePair(LT, GT, false)
22+
};
23+
24+
25+
@Override
26+
public BracePair[] getPairs() {
27+
return PAIRS;
28+
}
29+
30+
@Override
31+
public boolean isPairedBracesAllowedBeforeType(@NotNull IElementType iElementType, @Nullable IElementType iElementType1) {
32+
return true;
33+
}
34+
35+
@Override
36+
public int getCodeConstructStart(PsiFile psiFile, int i) {
37+
return i;
38+
}
39+
}

src/main/java/io/protostuff/jetbrains/plugin/ProtoParserDefinition.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ public class ProtoParserDefinition implements ParserDefinition {
3535

3636
public static final TokenIElementType ID;
3737
public static final TokenSet KEYWORDS;
38+
39+
// tokens
40+
41+
public static final TokenIElementType LCURLY;
42+
public static final TokenIElementType RCURLY;
43+
public static final TokenIElementType LPAREN;
44+
public static final TokenIElementType RPAREN;
45+
public static final TokenIElementType LSQUARE;
46+
public static final TokenIElementType RSQUARE;
47+
public static final TokenIElementType LT;
48+
public static final TokenIElementType GT;
49+
3850
// Rules
3951
public static final IElementType R_TYPE_REFERENCE;
4052
public static final IElementType R_NAME;
@@ -44,12 +56,14 @@ public class ProtoParserDefinition implements ParserDefinition {
4456
private static final TokenSet WHITESPACE;
4557
private static final TokenSet STRING;
4658

59+
60+
4761
static {
4862
PSIElementTypeFactory.defineLanguageIElementTypes(ProtoLanguage.INSTANCE,
4963
ProtoParser.tokenNames, ProtoParser.ruleNames);
50-
List<TokenIElementType> tokenIElementTypes =
64+
List<TokenIElementType> tokenTypes =
5165
PSIElementTypeFactory.getTokenIElementTypes(ProtoLanguage.INSTANCE);
52-
ID = tokenIElementTypes.get(ProtoLexer.NAME);
66+
ID = tokenTypes.get(ProtoLexer.NAME);
5367
FILE = new IFileElementType(ProtoLanguage.INSTANCE);
5468
COMMENTS = PSIElementTypeFactory.createTokenSet(ProtoLanguage.INSTANCE, COMMENT, LINE_COMMENT);
5569
WHITESPACE = PSIElementTypeFactory.createTokenSet(ProtoLanguage.INSTANCE, WS);
@@ -102,6 +116,15 @@ public class ProtoParserDefinition implements ParserDefinition {
102116
R_TYPE_REFERENCE = ruleTypes.get(ProtoParser.RULE_typeReference);
103117
R_NAME = ruleTypes.get(ProtoParser.RULE_name);
104118
R_FIELD_MODIFIER = ruleTypes.get(ProtoParser.RULE_fieldModifier);
119+
120+
LCURLY = tokenTypes.get(ProtoLexer.LCURLY);
121+
RCURLY = tokenTypes.get(ProtoLexer.RCURLY);
122+
LPAREN = tokenTypes.get(ProtoLexer.LPAREN);
123+
RPAREN = tokenTypes.get(ProtoLexer.RPAREN);
124+
LSQUARE = tokenTypes.get(ProtoLexer.LSQUARE);
125+
RSQUARE = tokenTypes.get(ProtoLexer.RSQUARE);
126+
LT = tokenTypes.get(ProtoLexer.LT);
127+
GT = tokenTypes.get(ProtoLexer.GT);
105128
}
106129

107130
@NotNull

src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<colorSettingsPage implementation="io.protostuff.jetbrains.plugin.ProtoColorSettingsPage"/>
7979

8080
<lang.commenter language="PROTO" implementationClass="io.protostuff.jetbrains.plugin.ProtoCommenter"/>
81+
<lang.braceMatcher language="PROTO" implementationClass="io.protostuff.jetbrains.plugin.ProtoBraceMatcher"/>
8182
</extensions>
8283

8384
<actions>

0 commit comments

Comments
 (0)