private void checkPsiIsCorrect(final FileViewProvider key) {
    PsiFile actualPsi = key.getPsi(key.getBaseLanguage());

    PsiTreeDebugBuilder treeDebugBuilder =
        new PsiTreeDebugBuilder().setShowErrorElements(false).setShowWhiteSpaces(false);

    String actualPsiTree = treeDebugBuilder.psiToString(actualPsi);

    String fileName = key.getVirtualFile().getName();
    PsiFile psi =
        PsiFileFactory.getInstance(myProject)
            .createFileFromText(
                fileName,
                FileTypeManager.getInstance().getFileTypeByFileName(fileName),
                actualPsi.getNode().getText(),
                LocalTimeCounter.currentTime(),
                false);

    if (actualPsi.getClass().equals(psi.getClass())) {
      String expectedPsi = treeDebugBuilder.psiToString(psi);

      if (!expectedPsi.equals(actualPsiTree)) {
        myReformatElements.clear();
        assert expectedPsi.equals(actualPsiTree)
            : "Refactored psi should be the same as result of parsing";
      }
    }
  }
 private static void reportInconsistentLength(
     PsiFile file, CharSequence newFileText, ASTNode node, int start, int end) {
   String message =
       "Index out of bounds: type="
           + node.getElementType()
           + "; file="
           + file
           + "; file.class="
           + file.getClass()
           + "; start="
           + start
           + "; end="
           + end
           + "; length="
           + node.getTextLength();
   String newTextBefore = newFileText.subSequence(0, start).toString();
   String oldTextBefore = file.getText().subSequence(0, start).toString();
   if (oldTextBefore.equals(newTextBefore)) {
     message += "; oldTextBefore==newTextBefore";
   }
   LOG.error(
       message,
       new Attachment(file.getName() + "_oldNodeText.txt", node.getText()),
       new Attachment(file.getName() + "_oldFileText.txt", file.getText()),
       new Attachment(file.getName() + "_newFileText.txt", newFileText.toString()));
 }
Exemplo n.º 3
0
 private static Language findLanguage(PsiFile file) {
   FileViewProvider vp = file.getViewProvider();
   Set<Language> languages = vp.getLanguages();
   for (Language language : languages) {
     if (file.equals(vp.getPsi(language))) {
       return language;
     }
   }
   throw new AssertionError(
       "Non-retrievable file: "
           + file.getClass()
           + "; "
           + file.getLanguage()
           + "; "
           + languages);
 }
  @Nullable
  @Override
  public Document getDocument(@NotNull PsiFile file) {
    if (file instanceof PsiBinaryFile) return null;

    Document document = getCachedDocument(file);
    if (document != null) {
      if (!file.getViewProvider().isPhysical() && document.getUserData(HARD_REF_TO_PSI) == null) {
        PsiUtilCore.ensureValid(file);
        cachePsi(document, file);
      }
      return document;
    }

    FileViewProvider viewProvider = file.getViewProvider();
    if (!viewProvider.isEventSystemEnabled()) return null;

    document = FileDocumentManager.getInstance().getDocument(viewProvider.getVirtualFile());
    if (document != null) {
      if (document.getTextLength() != file.getTextLength()) {
        String message =
            "Document/PSI mismatch: "
                + file
                + " ("
                + file.getClass()
                + "); physical="
                + viewProvider.isPhysical();
        if (document.getTextLength() + file.getTextLength() < 8096) {
          message +=
              "\n=== document ===\n" + document.getText() + "\n=== PSI ===\n" + file.getText();
        }
        throw new AssertionError(message);
      }

      if (!viewProvider.isPhysical()) {
        PsiUtilCore.ensureValid(file);
        cachePsi(document, file);
        file.putUserData(HARD_REF_TO_DOCUMENT, document);
      }
    }

    return document;
  }