Document (Project project, HistoryElement historyElement) { final String expression = historyElement != null ? historyElement.expression : ""; final PsiFile file = PsiFileFactory.getInstance(project).createFileFromText("DummyFile.xpath", XPathFileType.XPATH, expression, LocalTimeCounter.currentTime(), true); final Document document = PsiDocumentManager.getInstance(project).getDocument(file); // not sure why this is required... assert document != null; document.setReadOnly(false); assert document.isWritable() : "WTF, document is not writable? Text = <" + expression + ">"; return document; }