com.intellij.openapi.vfs.AsyncFileListener Java Examples

The following examples show how to use com.intellij.openapi.vfs.AsyncFileListener. 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 File: RefreshQueueImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected void tryProcessingEvents(@Nonnull RefreshSessionImpl session, @Nullable TransactionId transaction) {
  List<? extends VFileEvent> events = ContainerUtil.filter(session.getEvents(), e -> {
    VirtualFile file = e instanceof VFileCreateEvent ? ((VFileCreateEvent)e).getParent() : e.getFile();
    return file == null || file.isValid();
  });

  List<AsyncFileListener.ChangeApplier> appliers = AsyncEventSupport.runAsyncListeners(events);

  long stamp = myWriteActionCounter.get();
  TransactionGuard.getInstance().submitTransaction(ApplicationManager.getApplication(), transaction, () -> {
    if (stamp == myWriteActionCounter.get()) {
      session.fireEvents(events, appliers);
    }
    else {
      scheduleAsynchronousPreprocessing(session, transaction);
    }
  });
}
 
Example #2
Source File: RefreshSessionImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void fireEventsInWriteAction(List<? extends VFileEvent> events, @Nullable List<? extends AsyncFileListener.ChangeApplier> appliers) {
  final VirtualFileManagerEx manager = (VirtualFileManagerEx)VirtualFileManager.getInstance();

  manager.fireBeforeRefreshStart(myIsAsync);
  try {
    AsyncEventSupport.processEvents(events, appliers);
  }
  catch (AssertionError e) {
    if (FileStatusMap.CHANGES_NOT_ALLOWED_DURING_HIGHLIGHTING.equals(e.getMessage())) {
      throw new AssertionError("VFS changes are not allowed during highlighting", myStartTrace);
    }
    throw e;
  }
  finally {
    try {
      manager.fireAfterRefreshFinish(myIsAsync);
    }
    finally {
      if (myFinishRunnable != null) {
        myFinishRunnable.run();
      }
    }
  }
}
 
Example #3
Source File: AsyncEventSupport.java    From consulo with Apache License 2.0 6 votes vote down vote up
static void processEvents(List<? extends VFileEvent> events, @Nullable List<? extends AsyncFileListener.ChangeApplier> appliers) {
  ApplicationManager.getApplication().assertWriteAccessAllowed();
  if (appliers != null) {
    beforeVfsChange(appliers);
    ourSuppressAppliers = true;
  }
  try {
    PersistentFS.getInstance().processEvents(events);
  }
  finally {
    ourSuppressAppliers = false;
  }
  if (appliers != null) {
    afterVfsChange(appliers);
  }
}
 
Example #4
Source File: FileBasedIndexImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public AsyncFileListener.ChangeApplier prepareChange(@Nonnull List<? extends VFileEvent> events) {
  boolean shouldCleanup = ContainerUtil.exists(events, ChangedFilesCollector::memoryStorageCleaningNeeded);
  ChangeApplier superApplier = super.prepareChange(events);

  return new AsyncFileListener.ChangeApplier() {
    @Override
    public void beforeVfsChange() {
      if (shouldCleanup) {
        myManager.cleanupMemoryStorage(false);
      }
      superApplier.beforeVfsChange();
    }

    @Override
    public void afterVfsChange() {
      superApplier.afterVfsChange();
      if (myManager.myInitialized) ensureUpToDateAsync();
    }
  };
}
 
Example #5
Source File: ScratchTreeStructureProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void registerUpdaters(@Nonnull Project project, @Nonnull consulo.disposer.Disposable disposable, @Nonnull Runnable onUpdate) {
  String scratchPath = FileUtil.toSystemIndependentName(FileUtil.toCanonicalPath(ContainerPathManager.get().getScratchPath()));
  VirtualFileManager.getInstance().addAsyncFileListener(events -> {
    boolean update = JBIterable.from(events).find(e -> {
      ProgressManager.checkCanceled();

      final boolean isDirectory = isDirectory(e);
      final VirtualFile parent = getNewParent(e);
      return parent != null && (ScratchUtil.isScratch(parent) || isDirectory && parent.getPath().startsWith(scratchPath));
    }) != null;

    return !update ? null : new AsyncFileListener.ChangeApplier() {
      @Override
      public void afterVfsChange() {
        onUpdate.run();
      }
    };
  }, disposable);
  ConcurrentMap<RootType, Disposable> disposables = ConcurrentFactoryMap.createMap(o -> Disposable.newDisposable(o.getDisplayName()));
  for (RootType rootType : RootType.getAllRootTypes()) {
    registerRootTypeUpdater(project, rootType, onUpdate, disposable, disposables);
  }
}
 
Example #6
Source File: RefreshSessionImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
void fireEvents(@Nonnull List<? extends VFileEvent> events, @Nullable List<? extends AsyncFileListener.ChangeApplier> appliers) {
  try {
    if ((myFinishRunnable != null || !events.isEmpty()) && !ApplicationManager.getApplication().isDisposed()) {
      if (LOG.isDebugEnabled()) LOG.debug("events are about to fire: " + events);
      WriteAction.run(() -> fireEventsInWriteAction(events, appliers));
    }
  }
  finally {
    mySemaphore.up();
  }
}
 
Example #7
Source File: AsyncEventSupport.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static List<AsyncFileListener.ChangeApplier> runAsyncListeners(@Nonnull List<? extends VFileEvent> events) {
  if (events.isEmpty()) return Collections.emptyList();

  if (LOG.isDebugEnabled()) {
    LOG.debug("Processing " + events);
  }

  List<AsyncFileListener.ChangeApplier> appliers = new ArrayList<>();
  List<AsyncFileListener> allListeners = ContainerUtil.concat(EP_NAME.getExtensionList(), ((VirtualFileManagerImpl)VirtualFileManager.getInstance()).getAsyncFileListeners());
  for (AsyncFileListener listener : allListeners) {
    ProgressManager.checkCanceled();
    long startNs = System.nanoTime();
    boolean canceled = false;
    try {
      ReadAction.run(() -> ContainerUtil.addIfNotNull(appliers, listener.prepareChange(events)));
    }
    catch (ProcessCanceledException e) {
      canceled = true;
      throw e;
    }
    catch (Throwable e) {
      LOG.error(e);
    }
    finally {
      long elapsedMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
      if (elapsedMs > 10_000) {
        LOG.warn(listener + " took too long (" + elapsedMs + "ms) on " + events.size() + " events" + (canceled ? ", canceled" : ""));
      }
    }
  }
  return appliers;
}
 
Example #8
Source File: AsyncEventSupport.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void beforeVfsChange(List<? extends AsyncFileListener.ChangeApplier> appliers) {
  for (AsyncFileListener.ChangeApplier applier : appliers) {
    try {
      applier.beforeVfsChange();
    }
    catch (Throwable e) {
      LOG.error(e);
    }
  }
}
 
Example #9
Source File: AsyncEventSupport.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void afterVfsChange(List<? extends AsyncFileListener.ChangeApplier> appliers) {
  for (AsyncFileListener.ChangeApplier applier : appliers) {
    try {
      applier.afterVfsChange();
    }
    catch (Throwable e) {
      LOG.error(e);
    }
  }
}
 
Example #10
Source File: Unity3dMetaManager.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
@Inject
public Unity3dMetaManager(Project project, VirtualFileManager virtualFileManager)
{
	myProject = project;
	myProject.getMessageBus().connect().subscribe(PsiModificationTracker.TOPIC, () -> myGUIDs.clear());
	virtualFileManager.addAsyncFileListener(new AsyncFileListener()
	{
		@Nonnull
		@Override
		public ChangeApplier prepareChange(@Nonnull List<? extends VFileEvent> list)
		{
			return new ChangeApplier()
			{
				@Override
				public void afterVfsChange()
				{
					for(VFileEvent vFileEvent : list)
					{
						VirtualFile file = vFileEvent.getFile();
						if(file == null)
						{
							continue;
						}

						if(clearIfNeed(file))
						{
							break;
						}
					}
				}

				private boolean clearIfNeed(@Nonnull VirtualFile virtualFile)
				{
					if(virtualFile.getFileType() == Unity3dMetaFileType.INSTANCE)
					{
						myAttaches.clear();
						return true;
					}
					return false;
				}
			};
		}
	}, this);

	LowMemoryWatcher.register(this::clear, this);
}