int (@NotNull HighlighterIterator iterator, @NotNull IElementType targetParenTokenType, @NotNull CharSequence fileText, @NotNull FileType fileType, boolean searchingForRight, boolean stopOnFirstFinishedGroup) { int lastBraceOffset = -1; Stack<IElementType> braceStack = new Stack<>(); while (!iterator.atEnd()) { IElementType tokenType = iterator.getTokenType(); if (isBraceToken(iterator, fileText, fileType, searchingForRight)) { if (braceStack.isEmpty()) { if (tokenType != targetParenTokenType) break; //unmatched braces lastBraceOffset = iterator.getStart(); if (stopOnFirstFinishedGroup) break; //first brace group finished. stop searching } else { IElementType topToken = braceStack.pop(); if (!isPairBraces(tokenType, topToken, fileType)) break; // unmatched braces } } else if (isBraceToken(iterator, fileText, fileType, !searchingForRight)) { braceStack.push(iterator.getTokenType()); } advance(iterator, searchingForRight); } return lastBraceOffset; }