use of com.intellij.openapi.roots.ModuleRootEvent in project intellij-plugins by JetBrains.
the class NotIndexedCucumberExtension method getDataObject.
public Object getDataObject(@NotNull final Project project) {
final DataObject result = new DataObject();
result.myUpdateQueue.setPassThrough(false);
PsiManager.getInstance(project).addPsiTreeChangeListener(result.myCucumberPsiTreeListener);
PsiManager.getInstance(project).addPsiTreeChangeListener(new PsiTreeChangeAdapter() {
@Override
public void childAdded(@NotNull PsiTreeChangeEvent event) {
final PsiElement parent = event.getParent();
PsiElement child = event.getChild();
if (isStepLikeFile(child, parent)) {
final PsiFile file = (PsiFile) child;
result.myUpdateQueue.queue(new Update(parent) {
public void run() {
if (file.isValid()) {
reloadAbstractStepDefinitions(file);
createWatcher(file);
}
}
});
}
}
@Override
public void childRemoved(@NotNull PsiTreeChangeEvent event) {
final PsiElement parent = event.getParent();
final PsiElement child = event.getChild();
if (isStepLikeFile(child, parent)) {
result.myUpdateQueue.queue(new Update(parent) {
public void run() {
removeAbstractStepDefinitionsRelatedTo((PsiFile) child);
}
});
}
}
});
// clear caches after modules roots were changed
final MessageBusConnection connection = project.getMessageBus().connect();
connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() {
final List<VirtualFile> myPreviousStepDefsProviders = new ArrayList<>();
public void beforeRootsChange(ModuleRootEvent event) {
myPreviousStepDefsProviders.clear();
collectAllStepDefsProviders(myPreviousStepDefsProviders, project);
}
public void rootsChanged(ModuleRootEvent event) {
// compare new and previous content roots
final List<VirtualFile> newStepDefsProviders = new ArrayList<>();
collectAllStepDefsProviders(newStepDefsProviders, project);
if (!compareRoots(newStepDefsProviders)) {
// clear caches on roots changed
reset(project);
}
}
private boolean compareRoots(final List<VirtualFile> newStepDefsProviders) {
if (myPreviousStepDefsProviders.size() != newStepDefsProviders.size()) {
return false;
}
for (VirtualFile root : myPreviousStepDefsProviders) {
if (!newStepDefsProviders.contains(root)) {
return false;
}
}
return true;
}
});
Disposer.register(project, connection);
return result;
}
Aggregations