private static void processReplacements(@NotNull SnippetVisitor visitor, @NotNull PlainText plainText, @NotNull List<LocationMarkupNode> flatActive) {
    Map<Boolean, List<LocationMarkupNode>> map = StreamEx.of(flatActive).partitioningBy(n -> n instanceof Replace);
    flatActive = map.get(false);
    String content = plainText.content();
    for (LocationMarkupNode markupNode : map.get(true)) {
      Replace replace = (Replace)markupNode;
      Selector selector = replace.selector();
      String replacement = replace.replacement();
      if (selector instanceof Regex regex) {
        boolean addLineBreak = false;
        if (content.endsWith("\n")) {
          content = content.substring(0, content.length() - 1);
          addLineBreak = true;
        }
        try {
          content = regex.pattern().matcher(StringUtil.newBombedCharSequence(content, 1000)).replaceAll(replacement);
        }
        catch (StackOverflowError | ProcessCanceledException e) {
          ErrorMarkup replacementError = new ErrorMarkup(
            replace.range(), JavaBundle.message("javadoc.snippet.error.regex.too.complex", "replace", regex.pattern().pattern()));
          visitor.visitError(replacementError);
        }
        catch (IllegalArgumentException | IndexOutOfBoundsException e) {
          ErrorMarkup replacementError = new ErrorMarkup(
            replace.range(), JavaBundle.message("javadoc.snippet.error.malformed.replacement", "replace", replacement, e.getMessage()));
          visitor.visitError(replacementError);
        }
        if (addLineBreak) {
          content += "\n";
        }
      }
      else if (selector instanceof Substring substring) {
        content = content.replace(substring.substring(), replacement);
      }
      else {
        content = replacement;
      }
    }
    visitor.visitPlainText(new PlainText(plainText.range(), content), flatActive);
  }
private static void processReplacements(@NotNull SnippetVisitor visitor, @NotNull PlainText plainText, @NotNull List<LocationMarkupNode> flatActive)