public static @Nullable TextRange findUrl(@NotNull CharSequence text, int startOffset, int endOffset) {
    Matcher m = URL_WITH_PARENS_PATTERN.matcher(text);
    m.region(startOffset, endOffset);
    if (!m.find()) return null;
    int start = m.start();
    int end = m.end();
    int unmatchedPos = 0;
    int unmatchedCount = 0;
    for (int i = m.end(1); i < end; i++) {
      char c = text.charAt(i);
      if (c == '(') {
        if (unmatchedCount++ == 0) unmatchedPos = i;
      }
      else if (c == ')') {
        if (unmatchedCount-- == 0) return new TextRange(start, i);
      }
    }
    if (unmatchedCount > 0) return new TextRange(start, unmatchedPos);
    return new TextRange(start, end);
  }
public static @Nullable TextRange findUrl(@NotNull CharSequence text, int startOffset, int endOffset)