Search in sources :

Example 11 with BraceMatcher

use of com.intellij.codeInsight.highlighting.BraceMatcher in project intellij-plugins by JetBrains.

the class CfmlBraceMatcher method isPairBraces.

public boolean isPairBraces(IElementType tokenType1, IElementType tokenType2) {
    PairedBraceMatcher matcher = LanguageBraceMatching.INSTANCE.forLanguage(tokenType1.getLanguage());
    if (matcher != null) {
        BracePair[] pairs = matcher.getPairs();
        for (BracePair pair : pairs) {
            if (pair.getLeftBraceType() == tokenType1)
                return pair.getRightBraceType() == tokenType2;
            if (pair.getRightBraceType() == tokenType1)
                return pair.getLeftBraceType() == tokenType2;
        }
    }
    FileType tokenFileType1 = tokenType1.getLanguage().getAssociatedFileType();
    FileType tokenFileType2 = tokenType2.getLanguage().getAssociatedFileType();
    if (tokenFileType2 != tokenFileType1) {
        return false;
    }
    if (tokenFileType1 != CfmlFileType.INSTANCE && tokenFileType1 != null) {
        for (FileTypeExtensionPoint<BraceMatcher> ext : Extensions.getExtensions(BraceMatcher.EP_NAME)) {
            if (ext.filetype.equals(tokenFileType1.getName())) {
                return ext.getInstance().isPairBraces(tokenType1, tokenType2);
            }
        }
    }
    for (BracePair pair : PAIRS) {
        if (pair.getLeftBraceType() == tokenType1)
            return pair.getRightBraceType() == tokenType2;
        if (pair.getRightBraceType() == tokenType1)
            return pair.getLeftBraceType() == tokenType2;
    }
    return (tokenType1.equals(CfmlTokenTypes.OPENER) && tokenType2.equals(CfmlTokenTypes.CLOSER)) || (tokenType1.equals(CfmlTokenTypes.CLOSER) && tokenType2.equals(CfmlTokenTypes.OPENER)) || (tokenType1.equals(CfmlTokenTypes.R_ANGLEBRACKET) && tokenType2.equals(CfmlTokenTypes.OPENER)) || (tokenType1.equals(CfmlTokenTypes.OPENER) && tokenType2.equals(CfmlTokenTypes.R_ANGLEBRACKET));
}
Also used : PairedBraceMatcher(com.intellij.lang.PairedBraceMatcher) PairedBraceMatcher(com.intellij.lang.PairedBraceMatcher) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) XmlFileType(com.intellij.ide.highlighter.XmlFileType) FileType(com.intellij.openapi.fileTypes.FileType) CfmlFileType(com.intellij.coldFusion.model.files.CfmlFileType) BracePair(com.intellij.lang.BracePair)

Example 12 with BraceMatcher

use of com.intellij.codeInsight.highlighting.BraceMatcher in project oxy-template-support-plugin by mutant-industries.

the class MatchingTagsAndJsBlock method buildBraceMatcherBasedFolding.

private static void buildBraceMatcherBasedFolding(@NotNull List<FoldingDescriptor> descriptors, @NotNull PsiElement root, @NotNull Document document, @NotNull SyntaxHighlighter highlighter) {
    LexerEditorHighlighter editorHighlighter = new LexerEditorHighlighter(highlighter, EditorColorsManager.getInstance().getGlobalScheme());
    editorHighlighter.setText(document.getText());
    FileType fileType = root.getContainingFile().getFileType();
    BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, root.getLanguage());
    TextRange totalRange = root.getTextRange();
    final HighlighterIterator iterator = editorHighlighter.createIterator(totalRange.getStartOffset());
    final LinkedList<Trinity<Integer, Integer, IElementType>> stack = new LinkedList<>();
    String editorText = document.getText();
    TextRange range;
    while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
        final Trinity<Integer, Integer, IElementType> last;
        if (OxyTemplateParserDefinition.PARAMETER_QUOTES.contains(iterator.getTokenType())) {
            iterator.advance();
            continue;
        }
        if (braceMatcher.isLBraceToken(iterator, editorText, fileType)) {
            if (iterator.getTokenType() == OxyTemplateTypes.T_XML_TAG_START) {
                IElementType tokenType = iterator.getTokenType();
                do {
                    iterator.advance();
                } while (!iterator.atEnd() && iterator.getTokenType() != OxyTemplateTypes.T_XML_OPEN_TAG_END && iterator.getTokenType() != OxyTemplateTypes.T_XML_CLOSE_TAG_END && iterator.getTokenType() != OxyTemplateTypes.T_XML_EMPTY_TAG_END);
                if (iterator.atEnd()) {
                    return;
                }
                stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), tokenType));
                iterator.retreat();
            } else {
                stack.addLast(Trinity.create(iterator.getStart() + 2, iterator.getEnd(), iterator.getTokenType()));
            }
        } else if (braceMatcher.isRBraceToken(iterator, editorText, fileType) && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
            stack.removeLast();
            range = new TextRange(last.first, iterator.getEnd() - (iterator.getTokenType() == OxyTemplateTypes.T_XML_CLOSE_TAG_END ? 1 : 2));
            if (last.third != OxyTemplateTypes.T_XML_EMPTY_TAG_END && StringUtil.countChars(document.getText(range), '\n') >= 2) {
                descriptors.add(new FoldingDescriptor(root, range));
            }
        }
        iterator.advance();
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) Trinity(com.intellij.openapi.util.Trinity) TextRange(com.intellij.openapi.util.TextRange) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) LinkedList(java.util.LinkedList) IElementType(com.intellij.psi.tree.IElementType) OxyTemplateFileType(ool.intellij.plugin.file.type.OxyTemplateFileType) FileType(com.intellij.openapi.fileTypes.FileType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Aggregations

BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)12 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)9 FileType (com.intellij.openapi.fileTypes.FileType)9 IElementType (com.intellij.psi.tree.IElementType)8 EditorEx (com.intellij.openapi.editor.ex.EditorEx)6 NontrivialBraceMatcher (com.intellij.codeInsight.highlighting.NontrivialBraceMatcher)3 CfmlFileType (com.intellij.coldFusion.model.files.CfmlFileType)3 XmlFileType (com.intellij.ide.highlighter.XmlFileType)3 BracePair (com.intellij.lang.BracePair)3 PairedBraceMatcher (com.intellij.lang.PairedBraceMatcher)3 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)3 TextRange (com.intellij.openapi.util.TextRange)3 Trinity (com.intellij.openapi.util.Trinity)3 LinkedList (java.util.LinkedList)3 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)2 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)2 PsiFile (com.intellij.psi.PsiFile)2 Language (com.intellij.lang.Language)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1