void () { String text = """ import java.util.List;import java.util.ArrayList;import java.util.Collection;import java.util.Collections; import java.util.LinkedList; import java.util.Set; class Test { }"""; init(40, text); final int foldStartOffset = text.indexOf("java.util.Collections"); final int foldEndOffset = text.indexOf("class") - 2; addCollapsedFoldRegion(foldStartOffset, foldEndOffset, "..."); int modificationOffset = text.indexOf("java.util.Set"); WriteCommandAction.runWriteCommandAction(getProject(), () -> getEditor() .getDocument().insertString(modificationOffset, "import java.util.HashSet;\n")); // Used to get StackOverflowError here, hence, no additional checking is performed. }