public static void buildBraceMatcherBasedFolding(List<? super FoldingDescriptor> descriptors,
                                                   PsiElement root,
                                                   Document document,
                                                   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();
    while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
      final Trinity<Integer, Integer, IElementType> last;
      if (braceMatcher.isLBraceToken(iterator, editorText, fileType) &&
          braceMatcher.isStructuralBrace(iterator, editorText, fileType)) {
        stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), iterator.getTokenType()));
      }
      else if (braceMatcher.isRBraceToken(iterator, editorText, fileType) &&
               braceMatcher.isStructuralBrace(iterator, editorText, fileType)
               && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
        stack.removeLast();
        TextRange range = new TextRange(last.first, iterator.getEnd());
        if (StringUtil.countChars(document.getText(range), '\n') >= 3) {
          descriptors.add(new FoldingDescriptor(root, range));
        }
      }
      iterator.advance();
    }
  }
public static void buildBraceMatcherBasedFolding(List<? super FoldingDescriptor> descriptors,
                                                   PsiElement root,
                                                   Document document,
                                                   SyntaxHighlighter highlighter)