PsiFile (@NotNull PsiFile hostPsiFile, @NotNull PsiDocumentManagerBase documentManager, @NotNull Place place, @NotNull DocumentWindowImpl documentWindow, @NotNull PsiFile psiFile, @NotNull InjectedFileViewProvider viewProvider) { cacheEverything(place, documentWindow, viewProvider, psiFile); VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(documentWindow); PsiFile cachedPsiFile = ((PsiManagerEx)psiFile.getManager()).getFileManager().findCachedViewProvider(virtualFile).getPsi(psiFile.getLanguage()); assert cachedPsiFile == psiFile : "Cached psi :"+ cachedPsiFile +" instead of "+psiFile; assert place.isValid(); assert viewProvider.isValid(); List<InjectedLanguageUtilBase.TokenInfo> newTokens = InjectedLanguageUtilBase.getHighlightTokens(psiFile); PsiFile newFile = registerDocument(documentWindow, psiFile, place, hostPsiFile, documentManager); boolean mergeHappened = newFile != psiFile; Place mergedPlace = place; if (mergeHappened) { InjectedLanguageUtilBase.clearCaches(psiFile.getProject(), documentWindow); psiFile = newFile; viewProvider = (InjectedFileViewProvider)psiFile.getViewProvider(); documentWindow = viewProvider.getDocument(); boolean shredsReused = !cacheEverything(place, documentWindow, viewProvider, psiFile); if (shredsReused) { place.dispose(); mergedPlace = documentWindow.getShreds(); } InjectedLanguageUtilBase.setHighlightTokens(psiFile, newTokens); } assert psiFile.isValid(); assert mergedPlace.isValid(); assert viewProvider.isValid(); return psiFile; }