PsiFile (PsiFile file, boolean forbidCaching) { final VirtualFile virtualFile = file.getVirtualFile(); boolean mayCacheCopy = !forbidCaching && file.isPhysical() && // we don't want to cache code fragment copies even if they appear to be physical virtualFile != null && virtualFile.isInLocalFileSystem(); if (mayCacheCopy) { final Pair<PsiFile, Document> cached = dereference(file.getUserData(FILE_COPY_KEY)); if (cached != null && isCopyUpToDate(cached.second, cached.first, file)) { PsiFile copy = cached.first; CompletionAssertions.assertCorrectOriginalFile("Cached", file, copy); return copy; } } final PsiFile copy = (PsiFile)file.copy(); if (copy.isPhysical() || copy.getViewProvider().isEventSystemEnabled()) { LOG.error("File copy should be non-physical and non-event-system-enabled! Language=" + file.getLanguage() + "; file=" + file + " of " + file.getClass()); } CompletionAssertions.assertCorrectOriginalFile("New", file, copy); if (mayCacheCopy) { final Document document = copy.getViewProvider().getDocument(); assert document != null; syncAcceptSlashR(file.getViewProvider().getDocument(), document); file.putUserData(FILE_COPY_KEY, new SoftReference<>(Pair.create(copy, document))); } return copy; }