protected int searchForOpenBracket( int startPosition, char openBracket, char closeBracket, IDocument document ) throws BadLocationException {
        int stack = 1;
        int openPos = startPosition - 1;
        char nextChar;

        // Scan backward for the opening bracket.  Ignore "nested" bracket pairs.
        while (openPos >= 0 && stack > 0) {
            nextChar= document.getChar(openPos);
            if (nextChar == closeBracket && nextChar != openBracket)
                stack++;
            else if (nextChar == openBracket)
                stack--;
            openPos--;
        }

        if (stack == 0)
            return openPos + 1;

        return -1;
    }
Returns the position of the open bracket before startPosition.

@param startPosition the starting position for the search
@param openBracket the open bracket character
@param closeBracket the close bracket character
@param document the document being searched
@return the location of the open bracket
plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/syntax/SQLDoubleClickStrategy.java