Template (Template template) { template.setInline(true); if (myFile.isPhysical()) { ApplicationManager.getApplication().assertWriteAccessAllowed(); } //this is kinda hacky way of doing things, but have not got a better idea DocumentUtil.executeInBulk(myDocument, true, () -> { for (RangeMarker element : myElements) { if (element != myEndElement) { myDocument.deleteString(element.getStartOffset(), element.getEndOffset()); } } }); return template; }