insert paired brace before the closing brace or punctuation in the custom file type

This commit is contained in:
peter
2010-07-28 23:49:43 +01:00
parent a8ebc6cbd8
commit aff595fa9b
4 changed files with 47 additions and 2 deletions
@@ -45,6 +45,7 @@ public interface CustomHighlighterTokenType {
IElementType MULTI_LINE_COMMENT = new CustomElementType("MULTI_LINE_COMMENT");
IElementType WHITESPACE = new CustomElementType("WHITESPACE");
IElementType CHARACTER = new CustomElementType("CHARACTER");
IElementType PUNCTUATION = new CustomElementType("PUNCTUATION");
IElementType L_BRACE = new CustomElementType("L_BRACE");
IElementType R_BRACE = new CustomElementType("R_BRACE");
@@ -65,6 +65,7 @@ public final class CustomFileTypeLexer extends AbstractCustomLexer {
tokenParsers.add(multilineCommentParser);
}
tokenParsers.add(numberParser);
tokenParsers.add(new PunctuationParser());
if (hexNumberParser != null) {
tokenParsers.add(hexNumberParser);
}
@@ -42,8 +42,10 @@ class CustomFileTypeBraceMatcher implements BraceMatcher {
}
public boolean isRBraceToken(HighlighterIterator iterator, CharSequence fileText, FileType fileType) {
final IElementType tokenType = iterator.getTokenType();
return isRBraceToken(iterator.getTokenType());
}
private static boolean isRBraceToken(IElementType tokenType) {
return tokenType == CustomHighlighterTokenType.R_BRACKET ||
tokenType == CustomHighlighterTokenType.R_PARENTH ||
tokenType == CustomHighlighterTokenType.R_BRACE;
@@ -80,7 +82,9 @@ class CustomFileTypeBraceMatcher implements BraceMatcher {
}
public boolean isPairedBracesAllowedBeforeType(@NotNull final IElementType lbraceType, @Nullable final IElementType contextType) {
return contextType == CustomHighlighterTokenType.WHITESPACE;
return contextType == CustomHighlighterTokenType.PUNCTUATION ||
contextType == CustomHighlighterTokenType.WHITESPACE ||
isRBraceToken(contextType);
}
public int getCodeConstructStart(final PsiFile file, final int openingBraceOffset) {
@@ -0,0 +1,39 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* 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.intellij.ide.highlighter.custom.tokens;
import com.intellij.psi.CustomHighlighterTokenType;
/**
* @author peter
*/
public class PunctuationParser extends BaseTokenParser {
@Override
public boolean hasToken(int position) {
final char c = myBuffer.charAt(position);
if (".,:;".indexOf(c) >= 0) {
myTokenInfo.updateData(position, position+1, CustomHighlighterTokenType.PUNCTUATION);
return true;
}
return false;
}
@Override
public int getSmartUpdateShift() {
return 0;
}
}