void () { String text = """ import java.util.List; import java.util.ArrayList; class Test { }"""; init(40, text); final int foldStartOffset = "import".length() + 1; int foldEndOffset = text.indexOf("class") - 2; addCollapsedFoldRegion(foldStartOffset, foldEndOffset, "..."); // Simulate addition of the new import that modifies existing fold region. WriteCommandAction.runWriteCommandAction(getProject(), () -> getEditor() .getDocument().insertString(foldEndOffset, "\nimport java.util.Date;\n")); final FoldingModel foldingModel = getEditor().getFoldingModel(); foldingModel.runBatchFoldingOperation(() -> { FoldRegion oldFoldRegion = getFoldRegion(foldStartOffset); assertNotNull(oldFoldRegion); foldingModel.removeFoldRegion(oldFoldRegion); int newFoldEndOffset = getEditor().getDocument().getText().indexOf("class") - 2; FoldRegion newFoldRegion = foldingModel.addFoldRegion(foldStartOffset, newFoldEndOffset, "..."); assertNotNull(newFoldRegion); newFoldRegion.setExpanded(false); }); CodeFoldingManager.getInstance(getProject()).updateFoldRegions(getEditor()); assertEquals(new VisualPosition(2, 0), getEditor().logicalToVisualPosition(new LogicalPosition(5, 0))); }