void (final Object document, final String value) { if (document instanceof PlainDocument) { try { ((PlainDocument)document).remove(0, ((PlainDocument)document).getLength()); ((PlainDocument)document).insertString(0, value, null); } catch (BadLocationException e) { throw new RuntimeException(e); } } else { WriteCommandAction.runWriteCommandAction(project, () -> ((com.intellij.openapi.editor.Document)document) .replaceString(0, ((com.intellij.openapi.editor.Document)document).getTextLength(), value)); } }