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));
}
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();
}
}
Aggregations