private static @NotNull PsiFile createOrMergeInjectedFile(@NotNull PsiFile hostPsiFile,
                                                            @NotNull PsiDocumentManagerBase documentManager,
                                                            @NotNull Place place,
                                                            @NotNull DocumentWindowImpl documentWindow,
                                                            @NotNull PsiFile injectedPsiFile,
                                                            @NotNull InjectedFileViewProvider injectedViewProvider) {
    cacheEverything(place, documentWindow, injectedViewProvider, injectedPsiFile);

    VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(documentWindow);
    PsiFile cachedPsiFile = ((PsiManagerEx)injectedPsiFile.getManager()).getFileManager().findCachedViewProvider(virtualFile).getPsi(injectedPsiFile.getLanguage());
    assert cachedPsiFile == injectedPsiFile : "Cached psi :"+ cachedPsiFile +" instead of "+injectedPsiFile;

    assert place.isValid();
    assert injectedViewProvider.isValid();

    List<? extends InjectedLanguageUtilBase.TokenInfo> newTokens = InjectedLanguageUtilBase.getHighlightTokens(injectedPsiFile);
    PsiFile newFile = registerDocument(documentWindow, injectedPsiFile, place, hostPsiFile, documentManager);
    boolean mergeHappened = newFile != injectedPsiFile;
    Place mergedPlace = place;
    if (mergeHappened) {
      InjectedLanguageUtilBase.clearCaches(injectedPsiFile.getProject(), documentWindow);
      injectedPsiFile = newFile;
      injectedViewProvider = (InjectedFileViewProvider)injectedPsiFile.getViewProvider();
      documentWindow = injectedViewProvider.getDocument();
      boolean shredsReused = !cacheEverything(place, documentWindow, injectedViewProvider, injectedPsiFile);
      if (shredsReused) {
        place.dispose();
        mergedPlace = documentWindow.getShreds();
      }
      InjectedLanguageUtilBase.setHighlightTokens(injectedPsiFile, newTokens);
    }

    assert injectedPsiFile.isValid();
    assert mergedPlace.isValid();
    assert injectedViewProvider.isValid();
    return injectedPsiFile;
  }
private static @NotNull PsiFile createOrMergeInjectedFile(@NotNull PsiFile hostPsiFile,
                                                            @NotNull PsiDocumentManagerBase documentManager,
                                                            @NotNull Place place,
                                                            @NotNull DocumentWindowImpl documentWindow,
                                                            @NotNull PsiFile injectedPsiFile,
                                                            @NotNull InjectedFileViewProvider injectedViewProvider)