public static void replaceInContext(@NotNull PsiElement element, @NotNull String text, TextRange range) {
    final PsiFile file = element.getContainingFile();
    text = escapeForContext(text, file);
    final Document document = file.getViewProvider().getDocument();
    assert document != null;
    final TextRange replaceRange = element.getTextRange();
    final int startOffset = replaceRange.getStartOffset();
    if (range != null) {
      document.replaceString(startOffset + range.getStartOffset(), startOffset + range.getEndOffset(), text);
    }
    else {
      document.replaceString(startOffset, replaceRange.getEndOffset(), text);
    }
  }
public static void replaceInContext(@NotNull PsiElement element, @NotNull String text, TextRange range)