private static CompletionContext createCompletionContext(
      PsiFile hostCopy, int hostStartOffset, OffsetMap hostMap, PsiFile originalFile) {
    CompletionAssertions.assertHostInfo(hostCopy, hostMap);

    InjectedLanguageManager injectedLanguageManager =
        InjectedLanguageManager.getInstance(hostCopy.getProject());
    CompletionContext context;
    PsiFile injected = InjectedLanguageUtil.findInjectedPsiNoCommit(hostCopy, hostStartOffset);
    if (injected != null) {
      if (injected instanceof PsiFileImpl) {
        ((PsiFileImpl) injected).setOriginalFile(originalFile);
      }
      DocumentWindow documentWindow = InjectedLanguageUtil.getDocumentWindow(injected);
      CompletionAssertions.assertInjectedOffsets(
          hostStartOffset, injectedLanguageManager, injected, documentWindow);

      context =
          new CompletionContext(injected, translateOffsetMapToInjected(hostMap, documentWindow));
    } else {
      context = new CompletionContext(hostCopy, hostMap);
    }

    CompletionAssertions.assertFinalOffsets(originalFile, context, injected);

    return context;
  }
 @Override
 protected PsiFile getPsiInner(@NotNull final Language target) {
   PsiFileImpl file = myRoots.get(target);
   if (file == null) {
     if (isPhysical()) {
       VirtualFile virtualFile = getVirtualFile();
       if (isIgnored()) return null;
       VirtualFile parent = virtualFile.getParent();
       if (parent != null) {
         getManager().findDirectory(parent);
       }
     }
     if (target != getBaseLanguage() && !getLanguages().contains(target)) {
       return null;
     }
     file = (PsiFileImpl) createFile(target);
     if (file == null) return null;
     if (myOriginal != null) {
       final PsiFile originalFile = myOriginal.getPsi(target);
       if (originalFile != null) {
         file.setOriginalFile(originalFile);
       }
     }
     file = ConcurrencyUtil.cacheOrGet(myRoots, target, file);
   }
   return file;
 }
  @NotNull
  private static DiffLog makeFullParse(
      @NotNull PsiFileImpl fileImpl,
      @NotNull CharSequence newFileText,
      @NotNull ProgressIndicator indicator,
      @NotNull CharSequence lastCommittedText) {
    if (fileImpl instanceof PsiCodeFragment) {
      FileElement parent = fileImpl.getTreeElement();
      final FileElement holderElement =
          new DummyHolder(fileImpl.getManager(), null).getTreeElement();
      holderElement.rawAddChildren(
          fileImpl.createContentLeafElement(
              holderElement.getCharTable().intern(newFileText, 0, newFileText.length())));
      DiffLog diffLog = new DiffLog();
      diffLog.appendReplaceFileElement(parent, (FileElement) holderElement.getFirstChildNode());

      return diffLog;
    } else {
      FileViewProvider viewProvider = fileImpl.getViewProvider();
      viewProvider.getLanguages();
      FileType fileType = viewProvider.getVirtualFile().getFileType();
      String fileName = fileImpl.getName();
      final LightVirtualFile lightFile =
          new LightVirtualFile(
              fileName,
              fileType,
              newFileText,
              viewProvider.getVirtualFile().getCharset(),
              fileImpl.getViewProvider().getModificationStamp());
      lightFile.setOriginalFile(viewProvider.getVirtualFile());

      FileViewProvider copy = viewProvider.createCopy(lightFile);
      if (copy.isEventSystemEnabled()) {
        throw new AssertionError(
            "Copied view provider must be non-physical for reparse to deliver correct events: "
                + viewProvider);
      }
      copy.getLanguages();
      SingleRootFileViewProvider.doNotCheckFileSizeLimit(
          lightFile); // optimization: do not convert file contents to bytes to determine if we
                      // should codeinsight it
      PsiFileImpl newFile = getFileCopy(fileImpl, copy);

      newFile.setOriginalFile(fileImpl);

      final FileElement newFileElement = (FileElement) newFile.getNode();
      final FileElement oldFileElement = (FileElement) fileImpl.getNode();
      if (!lastCommittedText.toString().equals(oldFileElement.getText())) {
        throw new IncorrectOperationException();
      }
      DiffLog diffLog =
          mergeTrees(fileImpl, oldFileElement, newFileElement, indicator, lastCommittedText);

      ((PsiManagerEx) fileImpl.getManager()).getFileManager().setViewProvider(lightFile, null);
      return diffLog;
    }
  }
 @Nullable
 private PsiFileSystemItem doResolve(FileIncludeInfo info, PsiFile context) {
   PsiFileImpl psiFile =
       (PsiFileImpl)
           myPsiFileFactory.createFileFromText("dummy.txt", StdFileTypes.PLAIN_TEXT, info.path);
   psiFile.setOriginalFile(context);
   return new FileReferenceSet(psiFile) {
     @Override
     protected boolean useIncludingFileAsContext() {
       return false;
     }
   }.resolve();
 }
  @Override
  public PsiElement getMirror() {
    TreeElement mirrorTreeElement = myMirrorFileElement;
    if (mirrorTreeElement == null) {
      synchronized (myMirrorLock) {
        mirrorTreeElement = myMirrorFileElement;
        if (mirrorTreeElement == null) {
          VirtualFile file = getVirtualFile();
          PsiClass[] classes = getClasses();
          String fileName =
              (classes.length > 0 ? classes[0].getName() : file.getNameWithoutExtension())
                  + JavaFileType.DOT_DEFAULT_EXTENSION;

          final Document document = FileDocumentManager.getInstance().getDocument(file);
          assert document != null : file.getUrl();

          CharSequence mirrorText = document.getImmutableCharSequence();
          boolean internalDecompiler = StringUtil.startsWith(mirrorText, BANNER);
          PsiFileFactory factory = PsiFileFactory.getInstance(getManager().getProject());
          PsiFile mirror =
              factory.createFileFromText(fileName, JavaLanguage.INSTANCE, mirrorText, false, false);
          mirror.putUserData(PsiUtil.FILE_LANGUAGE_LEVEL_KEY, getLanguageLevel());

          mirrorTreeElement = SourceTreeToPsiMap.psiToTreeNotNull(mirror);
          try {
            final TreeElement finalMirrorTreeElement = mirrorTreeElement;
            ProgressManager.getInstance()
                .executeNonCancelableSection(
                    new Runnable() {
                      @Override
                      public void run() {
                        setMirror(finalMirrorTreeElement);
                        putUserData(CLS_DOCUMENT_LINK_KEY, document);
                      }
                    });
          } catch (InvalidMirrorException e) {
            //noinspection ThrowableResultOfMethodCallIgnored
            LOG.error(file.getUrl(), internalDecompiler ? e : wrapException(e, file));
          }

          ((PsiFileImpl) mirror).setOriginalFile(this);
          myMirrorFileElement = mirrorTreeElement;
        }
      }
    }
    return mirrorTreeElement.getPsi();
  }
 protected PsiFile getPsiInner(final Language target) {
   PsiFile file = myRoots.get(target);
   if (file == null) {
     if (isPhysical()) {
       VirtualFile virtualFile = getVirtualFile();
       VirtualFile parent = virtualFile.getParent();
       if (parent != null) {
         getManager().findDirectory(parent);
       }
     }
     file = createFile(target);
     if (file == null) return null;
     if (myOriginal != null) {
       final PsiFile originalFile = myOriginal.getPsi(target);
       if (originalFile != null) {
         ((PsiFileImpl) file).setOriginalFile(originalFile);
       }
     }
     file = ConcurrencyUtil.cacheOrGet(myRoots, target, file);
   }
   return file;
 }