Java Code Examples for com.intellij.openapi.vfs.VirtualFile#replace()

The following examples show how to use com.intellij.openapi.vfs.VirtualFile#replace() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: consulo   File: DocumentImpl.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * makes range marker without creating document (which could be expensive)
 */
@Nonnull
static RangeMarker createRangeMarkerForVirtualFile(@Nonnull VirtualFile file, int startOffset, int endOffset, int startLine, int startCol, int endLine, int endCol, boolean persistent) {
  RangeMarkerImpl marker = persistent ? new PersistentRangeMarker(file, startOffset, endOffset, startLine, startCol, endLine, endCol, false) : new RangeMarkerImpl(file, startOffset, endOffset, false);
  Key<Reference<RangeMarkerTree<RangeMarkerEx>>> key = persistent ? PERSISTENT_RANGE_MARKERS_KEY : RANGE_MARKERS_KEY;
  RangeMarkerTree<RangeMarkerEx> tree;
  while (true) {
    Reference<RangeMarkerTree<RangeMarkerEx>> oldRef = file.getUserData(key);
    tree = SoftReference.dereference(oldRef);
    if (tree != null) break;
    tree = new RangeMarkerTree<>();
    RMTreeReference reference = new RMTreeReference(tree, file);
    if (file.replace(key, oldRef, reference)) break;
  }
  tree.addInterval(marker, startOffset, endOffset, false, false, false, 0);

  return marker;

}
 
Example 2
Source Project: consulo   File: LanguageSubstitutors.java    License: Apache License 2.0 5 votes vote down vote up
private static void processLanguageSubstitution(@Nonnull final VirtualFile file,
                                                @Nonnull Language originalLang,
                                                @Nonnull final Language substitutedLang) {
  if (file instanceof VirtualFileWindow) {
    // Injected files are created with substituted language, no need to reparse:
    //   com.intellij.psi.impl.source.tree.injected.MultiHostRegistrarImpl#doneInjecting
    return;
  }
  Language prevSubstitutedLang = SUBSTITUTED_LANG_KEY.get(file);
  final Language prevLang = ObjectUtil.notNull(prevSubstitutedLang, originalLang);
  if (!prevLang.is(substitutedLang)) {
    if (file.replace(SUBSTITUTED_LANG_KEY, prevSubstitutedLang, substitutedLang)) {
      if (prevSubstitutedLang == null) {
        return; // no need to reparse for the first language substitution
      }
      if (ApplicationManager.getApplication().isUnitTestMode()) {
        return;
      }
      file.putUserData(REPARSING_SCHEDULED, true);
      ApplicationManager.getApplication().invokeLater(new Runnable() {
        @Override
        public void run() {
          if (file.replace(REPARSING_SCHEDULED, true, null)) {
            LOG.info("Reparsing " + file.getPath() + " because of language substitution " +
                     prevLang.getID() + "->" + substitutedLang.getID());
            FileContentUtilCore.reparseFiles(file);
          }
        }
      }, ModalityState.defaultModalityState());
    }
  }
}
 
Example 3
Source Project: consulo   File: DocumentImpl.java    License: Apache License 2.0 5 votes vote down vote up
private void getSaveRMTree(@Nonnull VirtualFile f, @Nonnull Key<Reference<RangeMarkerTree<RangeMarkerEx>>> key, @Nonnull RangeMarkerTree<RangeMarkerEx> tree) {
  RMTreeReference freshRef = new RMTreeReference(tree, f);
  Reference<RangeMarkerTree<RangeMarkerEx>> oldRef;
  do {
    oldRef = f.getUserData(key);
  }
  while (!f.replace(key, oldRef, freshRef));
  RangeMarkerTree<RangeMarkerEx> oldTree = SoftReference.dereference(oldRef);

  if (oldTree == null) {
    // no tree was saved in virtual file before. happens when created new document.
    // or the old tree got gc-ed, because no reachable markers retaining it are left alive. good riddance.
    return;
  }

  // old tree was saved in the virtual file. Have to transfer markers from there.
  TextRange myDocumentRange = new TextRange(0, getTextLength());
  oldTree.processAll(r -> {
    if (r.isValid() && myDocumentRange.contains(r)) {
      registerRangeMarker(r, r.getStartOffset(), r.getEndOffset(), r.isGreedyToLeft(), r.isGreedyToRight(), 0);
    }
    else {
      ((RangeMarkerImpl)r).invalidate("document was gc-ed and re-created");
    }
    return true;
  });
}
 
Example 4
Source Project: consulo   File: FileManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
private boolean evaluateValidity(@Nonnull AbstractFileViewProvider viewProvider) {
  ApplicationManager.getApplication().assertReadAccessAllowed();

  VirtualFile file = viewProvider.getVirtualFile();
  if (getRawCachedViewProvider(file) != viewProvider) {
    return false;
  }

  if (viewProvider.getUserData(IN_COMA) == null) {
    return true;
  }

  if (shouldResurrect(viewProvider, file)) {
    viewProvider.putUserData(IN_COMA, null);
    LOG.assertTrue(getRawCachedViewProvider(file) == viewProvider);

    for (PsiFile psiFile : viewProvider.getCachedPsiFiles()) {
      // update "myPossiblyInvalidated" fields in files by calling "isValid"
      // that will call us recursively again, but since we're not IN_COMA now, we'll exit earlier and avoid SOE
      if (!psiFile.isValid()) {
        LOG.error(new PsiInvalidElementAccessException(psiFile));
      }
    }
    return true;
  }

  getVFileToViewProviderMap().remove(file, viewProvider);
  file.replace(myPsiHardRefKey, viewProvider, null);
  viewProvider.putUserData(IN_COMA, null);

  return false;
}
 
Example 5
Source Project: consulo   File: SmartPointerTracker.java    License: Apache License 2.0 4 votes vote down vote up
private void disconnectTracker(VirtualFile file, Key<SmartPointerTracker> key) {
  if (!file.replace(key, this, null)) {
    throw new IllegalStateException("Couldn't clear smart pointer tracker " + this + ", current " + file.getUserData(key));
  }
}