From aff595fa9b1d8d4cc27f9097e9ed54b223514007 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 26 Jul 2010 19:02:35 +0100 Subject: [PATCH] insert paired brace before the closing brace or punctuation in the custom file type --- .../psi/CustomHighlighterTokenType.java | 1 + .../custom/CustomFileTypeLexer.java | 1 + .../impl/CustomFileTypeBraceMatcher.java | 8 +++- .../custom/tokens/PunctuationParser.java | 39 +++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/ide/highlighter/custom/tokens/PunctuationParser.java diff --git a/platform/lang-api/src/com/intellij/psi/CustomHighlighterTokenType.java b/platform/lang-api/src/com/intellij/psi/CustomHighlighterTokenType.java index 505b52cbeaa2..41ae1093e409 100644 --- a/platform/lang-api/src/com/intellij/psi/CustomHighlighterTokenType.java +++ b/platform/lang-api/src/com/intellij/psi/CustomHighlighterTokenType.java @@ -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"); diff --git a/platform/lang-impl/src/com/intellij/ide/highlighter/custom/CustomFileTypeLexer.java b/platform/lang-impl/src/com/intellij/ide/highlighter/custom/CustomFileTypeLexer.java index c31f41735c91..6506934c0c73 100644 --- a/platform/lang-impl/src/com/intellij/ide/highlighter/custom/CustomFileTypeLexer.java +++ b/platform/lang-impl/src/com/intellij/ide/highlighter/custom/CustomFileTypeLexer.java @@ -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); } diff --git a/platform/lang-impl/src/com/intellij/ide/highlighter/custom/impl/CustomFileTypeBraceMatcher.java b/platform/lang-impl/src/com/intellij/ide/highlighter/custom/impl/CustomFileTypeBraceMatcher.java index 1c099650b269..684ab7972e54 100644 --- a/platform/lang-impl/src/com/intellij/ide/highlighter/custom/impl/CustomFileTypeBraceMatcher.java +++ b/platform/lang-impl/src/com/intellij/ide/highlighter/custom/impl/CustomFileTypeBraceMatcher.java @@ -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) { diff --git a/platform/lang-impl/src/com/intellij/ide/highlighter/custom/tokens/PunctuationParser.java b/platform/lang-impl/src/com/intellij/ide/highlighter/custom/tokens/PunctuationParser.java new file mode 100644 index 000000000000..e097e4df887b --- /dev/null +++ b/platform/lang-impl/src/com/intellij/ide/highlighter/custom/tokens/PunctuationParser.java @@ -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; + } +}