Skip to content

Commit

Permalink
fix: events agregtate
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jun 15, 2023
1 parent de21506 commit e9c7d3c
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
@Service
public final class PsiMicroProfileProjectManager {

private static final String JAVA_FILE_EXTENSION = "java";

public static PsiMicroProfileProjectManager getInstance(Project project) {
return ServiceManager.getService(project, PsiMicroProfileProjectManager.class);
}
Expand Down Expand Up @@ -105,6 +107,9 @@ private PsiMicroProfileProject getJDTMicroProfileProject(Module project, boolean
}

public boolean isConfigSource(VirtualFile file) {
if (file == null) {
return false;
}
String fileName = file.getName();
for (IConfigSourceProvider provider : IConfigSourceProvider.EP_NAME.getExtensions()) {
if (provider.isConfigSource(fileName)) {
Expand All @@ -114,6 +119,10 @@ public boolean isConfigSource(VirtualFile file) {
return false;
}

public boolean isJavaFile(VirtualFile file) {
return file != null && JAVA_FILE_EXTENSION.equals(file.getExtension());
}

public void initialize() {
if (microprofileProjectListener != null) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
/*******************************************************************************
* Copyright (c) 2019-2020 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.intellij.quarkus;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import org.jetbrains.annotations.NotNull;

public class QuarkusPostStartupActivity implements StartupActivity, DumbAware {
@Override
public void runActivity(@NotNull Project project) {
QuarkusProjectService.getInstance(project);
}
}
/*******************************************************************************
* Copyright (c) 2019-2020 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.intellij.quarkus;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import com.redhat.devtools.intellij.quarkus.classpath.ClasspathResourceChangeManager;
import org.jetbrains.annotations.NotNull;

public class QuarkusPostStartupActivity implements StartupActivity, DumbAware {
@Override
public void runActivity(@NotNull Project project) {
ClasspathResourceChangeManager.getInstance(project);
QuarkusProjectService.getInstance(project);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
******************************************************************************/
package com.redhat.devtools.intellij.quarkus;

import com.intellij.ProjectTopics;
import com.intellij.psi.*;
import com.intellij.workspaceModel.ide.WorkspaceModelTopics;
import com.intellij.json.JsonFileType;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
Expand All @@ -25,10 +26,8 @@
import com.intellij.openapi.roots.impl.libraries.LibraryEx;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.openapi.vfs.newvfs.BulkFileListener;
import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent;
import com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent;
Expand All @@ -38,9 +37,13 @@
import com.intellij.util.ConcurrencyUtil;
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.util.messages.Topic;
import com.intellij.workspaceModel.ide.WorkspaceModelChangeListener;
import com.intellij.workspaceModel.storage.VersionedStorageChange;
import com.intellij.workspaceModel.storage.bridgeEntities.LibraryEntity;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.PropertiesManager;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.project.PsiMicroProfileProjectManager;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.core.ls.PsiUtilsLSImpl;
import com.redhat.devtools.intellij.quarkus.classpath.SourceFileChangeNotifier;
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.lsp4mp.commons.ClasspathKind;
Expand All @@ -53,11 +56,7 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
Expand All @@ -74,18 +73,77 @@ public class QuarkusProjectService implements LibraryTable.Listener, BulkFileLis
private final Map<Module, MutablePair<VirtualFile, Boolean>> schemas = new ConcurrentHashMap<>();

private final ExecutorService executor;
private final ResourcesChangeListener listener;

// private final SourceFileChangeNotifier sourcesChanged;

private Set<Module> modulesBeingEnsured = new HashSet<>();

@Override
public void dispose() {
connection.disconnect();
executor.shutdown();
private class ResourcesChangeListener extends PsiTreeChangeAdapter implements WorkspaceModelChangeListener {

@Override
public void beforeChanged(@NotNull VersionedStorageChange event) {

}

@Override
public void changed(@NotNull VersionedStorageChange event) {
var libraryChanges = event.getChanges(LibraryEntity.class);
if (libraryChanges.isEmpty()) {
return;
}
project.getMessageBus().syncPublisher(TOPIC).libraryUpdated(null);
}

@Override
public void childAdded(@NotNull PsiTreeChangeEvent event) {
handleChangedPsiTree(event);
}

@Override
public void childRemoved(@NotNull PsiTreeChangeEvent event) {
handleChangedPsiTree(event);
}

@Override
public void childReplaced(@NotNull PsiTreeChangeEvent event) {
handleChangedPsiTree(event);
}

@Override
public void childMoved(@NotNull PsiTreeChangeEvent event) {
handleChangedPsiTree(event);
}

@Override
public void childrenChanged(@NotNull PsiTreeChangeEvent event) {
handleChangedPsiTree(event);
}

@Override
public void propertyChanged(@NotNull PsiTreeChangeEvent event) {
handleChangedPsiTree(event);
}

private void handleChangedPsiTree(PsiTreeChangeEvent event) {
PsiFile psiFile = event.getFile();
if (psiFile == null) {
return;
}
VirtualFile file = psiFile.getVirtualFile();
if (file == null || !file.exists()) {
return;
}
if (!isJavaFile(file) && !isConfigSource(file, project)) {
return;
}
//sourcesChanged.addSourceFile(file);
}
}

public interface Listener {
void libraryUpdated(Library library);
void sourceUpdated(List<Pair<Module, VirtualFile>> sources);
void sourceUpdated(Set<VirtualFile> sources);
}

public static QuarkusProjectService getInstance(Project project) {
Expand All @@ -105,10 +163,16 @@ public QuarkusProjectService(Project project) {
1L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(),
r -> new Thread(r, "Quarkus lib pool " + project.getName()));
}
LibraryTablesRegistrar.getInstance().getLibraryTable(project).addListener(this, project);
connection = ApplicationManager.getApplication().getMessageBus().connect(project);
connection.subscribe(VirtualFileManager.VFS_CHANGES, this);
project.getMessageBus().connect().subscribe(ProjectTopics.MODULES, this);
// this.sourcesChanged = new SourceFileChangeNotifier(files -> project.getMessageBus().syncPublisher(TOPIC).sourceUpdated(files));
//LibraryTablesRegistrar.getInstance().getLibraryTable(project).addListener(this, project);
//PsiManager.getInstance(project).addPsiTreeChangeListener(this, project);
//connection = ApplicationManager.getApplication().getMessageBus().connect(project);
//connection.subscribe(VirtualFileManager.VFS_CHANGES, this);
//project.getMessageBus().connect().subscribe(ProjectTopics.MODULES, this);
connection = project.getMessageBus().connect();
listener = new ResourcesChangeListener();
WorkspaceModelTopics.getInstance(project).subscribeAfterModuleLoading(connection, listener);
PsiManager.getInstance(project).addPsiTreeChangeListener(listener, project);
processModules();
}

Expand Down Expand Up @@ -182,7 +246,7 @@ public void after(@NotNull List<? extends VFileEvent> events) {
p.setRight(Boolean.FALSE);
return p;
}));
project.getMessageBus().syncPublisher(TOPIC).sourceUpdated(pairs);
//project.getMessageBus().syncPublisher(TOPIC).sourceUpdated(pairs);
}
}

Expand Down Expand Up @@ -276,4 +340,13 @@ public void moduleAdded(@NotNull Project project, @NotNull Module module) {
public void moduleRemoved(@NotNull Project project, @NotNull Module module) {
moduleChanged(module);
}


@Override
public void dispose() {
PsiManager.getInstance(project).removePsiTreeChangeListener(listener);
connection.disconnect();
executor.shutdown();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.redhat.devtools.intellij.quarkus.classpath;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiManager;
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.util.messages.Topic;
import com.intellij.workspaceModel.ide.WorkspaceModelTopics;
import org.apache.commons.lang3.tuple.Pair;

import java.util.List;
import java.util.Set;

public class ClasspathResourceChangeManager implements Disposable {

public static final Topic<ClasspathResourceChangeManager.Listener> TOPIC = Topic.create(ClasspathResourceChangeManager.class.getName(), ClasspathResourceChangeManager.Listener.class);

private final SourceFileChangeNotifier sourcesChanged;
private final MessageBusConnection connection;
private final ClasspathRessourceChangedListener listener;

public static ClasspathResourceChangeManager getInstance(Project project) {
return ServiceManager.getService(project, ClasspathResourceChangeManager.class);
}

public interface Listener {

void librariesChanged();

void sourceFilesChanged(Set<Pair<VirtualFile, Module>> sources);
}

private final Project project;

public ClasspathResourceChangeManager(Project project) {
this.project = project;
// Send source files changed in debounce mode
this.sourcesChanged = new SourceFileChangeNotifier(files ->
project.getMessageBus().syncPublisher(ClasspathResourceChangeManager.TOPIC).sourceFilesChanged(files)
);
listener = new ClasspathRessourceChangedListener(project, sourcesChanged);
connection = project.getMessageBus().connect();
// Track delete, create, update of file
connection.subscribe(VirtualFileManager.VFS_CHANGES, listener);
// Track end of Java libraries update
WorkspaceModelTopics.getInstance(project).subscribeAfterModuleLoading(connection, listener);
// Track update of update of Psi Java, properties files
PsiManager.getInstance(project).addPsiTreeChangeListener(listener, project);
}

@Override
public void dispose() {
this.sourcesChanged.dispose();
this.connection.disconnect();
PsiManager.getInstance(project).removePsiTreeChangeListener(listener);
}
}
Loading

0 comments on commit e9c7d3c

Please sign in to comment.