-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: debounce classpath/source change events (#916)
Fixes #916 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
ce55178
commit 579b51b
Showing
9 changed files
with
463 additions
and
186 deletions.
There are no files selected for viewing
234 changes: 131 additions & 103 deletions
234
...om/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/PsiMicroProfileProjectManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,154 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* 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.lsp4mp4ij.psi.core.project; | ||
|
||
import com.intellij.ProjectTopics; | ||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.Disposable; | ||
import com.intellij.openapi.components.Service; | ||
import com.intellij.openapi.components.ServiceManager; | ||
import com.intellij.openapi.module.Module; | ||
import com.intellij.openapi.project.ModuleListener; | ||
import com.intellij.openapi.project.Project; | ||
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; | ||
import com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent; | ||
import com.intellij.openapi.vfs.newvfs.events.VFileEvent; | ||
import com.intellij.util.messages.MessageBusConnection; | ||
import com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.core.ls.PsiUtilsLSImpl; | ||
import com.redhat.devtools.intellij.quarkus.classpath.ClasspathResourceChangeManager; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* {@link PsiMicroProfileProject} manager. | ||
* | ||
* | ||
* @author Angelo ZERR | ||
* @see <a href="https://github.com/redhat-developer/quarkus-ls/blob/master/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/project/JDTMicroProfileProjectManager.java">https://github.com/redhat-developer/quarkus-ls/blob/master/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/project/JDTMicroProfileProjectManager.java</a> | ||
* | ||
*/ | ||
@Service | ||
public final class PsiMicroProfileProjectManager { | ||
|
||
public static PsiMicroProfileProjectManager getInstance(Project project) { | ||
return ServiceManager.getService(project, PsiMicroProfileProjectManager.class); | ||
} | ||
|
||
private Project project; | ||
|
||
private final Map<Module, PsiMicroProfileProject> projects; | ||
private MicroProfileProjectListener microprofileProjectListener; | ||
|
||
private class MicroProfileProjectListener implements ModuleListener, BulkFileListener { | ||
@Override | ||
public void after(@NotNull List<? extends VFileEvent> events) { | ||
for(VFileEvent event : events) { | ||
if ((event instanceof VFileDeleteEvent || event instanceof VFileContentChangeEvent || | ||
event instanceof VFileCreateEvent) && isConfigSource(event.getFile())) { | ||
Module javaProject = PsiUtilsLSImpl.getInstance(project).getModule(event.getFile()); | ||
if (javaProject != null) { | ||
PsiMicroProfileProject mpProject = getJDTMicroProfileProject(javaProject); | ||
if (mpProject != null) { | ||
mpProject.evictConfigSourcesCache(); | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void beforeModuleRemoved(@NotNull Project project, @NotNull Module module) { | ||
evict(module); | ||
} | ||
|
||
private void evict(Module javaProject) { | ||
if (javaProject != null) { | ||
// Remove the JDTMicroProfile project instance from the cache. | ||
projects.remove(javaProject); | ||
} | ||
} | ||
} | ||
|
||
private PsiMicroProfileProjectManager(Project project) { | ||
this.project = project; | ||
this.projects = new HashMap<>(); | ||
initialize(); | ||
} | ||
|
||
public PsiMicroProfileProject getJDTMicroProfileProject(Module project) { | ||
return getJDTMicroProfileProject(project, true); | ||
} | ||
|
||
private PsiMicroProfileProject getJDTMicroProfileProject(Module project, boolean create) { | ||
Module javaProject = project; | ||
PsiMicroProfileProject info = projects.get(javaProject); | ||
if (info == null) { | ||
if (!create) { | ||
return null; | ||
} | ||
info = new PsiMicroProfileProject(javaProject); | ||
projects.put(javaProject, info); | ||
} | ||
return info; | ||
} | ||
|
||
public boolean isConfigSource(VirtualFile file) { | ||
String fileName = file.getName(); | ||
for (IConfigSourceProvider provider : IConfigSourceProvider.EP_NAME.getExtensions()) { | ||
if (provider.isConfigSource(fileName)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public void initialize() { | ||
if (microprofileProjectListener != null) { | ||
return; | ||
} | ||
microprofileProjectListener = new MicroProfileProjectListener(); | ||
MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect(project); | ||
connection.subscribe(VirtualFileManager.VFS_CHANGES, microprofileProjectListener); | ||
project.getMessageBus().connect(project).subscribe(ProjectTopics.MODULES, microprofileProjectListener); | ||
} | ||
public final class PsiMicroProfileProjectManager implements Disposable { | ||
|
||
private static final String JAVA_FILE_EXTENSION = "java"; | ||
|
||
private MessageBusConnection connection; | ||
|
||
public static PsiMicroProfileProjectManager getInstance(Project project) { | ||
return ServiceManager.getService(project, PsiMicroProfileProjectManager.class); | ||
} | ||
|
||
private Project project; | ||
|
||
private final Map<Module, PsiMicroProfileProject> projects; | ||
private MicroProfileProjectListener microprofileProjectListener; | ||
|
||
private class MicroProfileProjectListener implements ModuleListener, ClasspathResourceChangeManager.Listener { | ||
|
||
@Override | ||
public void librariesChanged() { | ||
// Do nothing | ||
} | ||
|
||
@Override | ||
public void sourceFilesChanged(Set<Pair<VirtualFile, Module>> sources) { | ||
for (var pair : sources) { | ||
VirtualFile file = pair.getLeft(); | ||
if (isConfigSource(file)) { | ||
// A microprofile config file properties file source has been updated, evict the cache of the properties | ||
Module javaProject = pair.getRight(); | ||
PsiMicroProfileProject mpProject = getJDTMicroProfileProject(javaProject); | ||
if (mpProject != null) { | ||
mpProject.evictConfigSourcesCache(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void beforeModuleRemoved(@NotNull Project project, @NotNull Module module) { | ||
evict(module); | ||
} | ||
|
||
private void evict(Module javaProject) { | ||
if (javaProject != null) { | ||
// Remove the JDTMicroProfile project instance from the cache. | ||
projects.remove(javaProject); | ||
} | ||
} | ||
} | ||
|
||
private PsiMicroProfileProjectManager(Project project) { | ||
this.project = project; | ||
this.projects = new HashMap<>(); | ||
initialize(); | ||
} | ||
|
||
public PsiMicroProfileProject getJDTMicroProfileProject(Module project) { | ||
return getJDTMicroProfileProject(project, true); | ||
} | ||
|
||
private PsiMicroProfileProject getJDTMicroProfileProject(Module project, boolean create) { | ||
Module javaProject = project; | ||
PsiMicroProfileProject info = projects.get(javaProject); | ||
if (info == null) { | ||
if (!create) { | ||
return null; | ||
} | ||
info = new PsiMicroProfileProject(javaProject); | ||
projects.put(javaProject, info); | ||
} | ||
return info; | ||
} | ||
|
||
/** | ||
* Returns true if the given file is a MicroProfile config properties file (microprofile-config.properties, application.properties, application.yaml, etc) and false otherwise. | ||
* | ||
* @param file the file to check. | ||
* @return true if the given file is a MicroProfile config properties file (microprofile-config.properties, application.properties, application.yaml, etc) and false otherwise. | ||
*/ | ||
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)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Returns true if the given file is a Java file and false otherwise. | ||
* | ||
* @param file the file to check. | ||
* @return true if the given file is a Java file and false otherwise. | ||
*/ | ||
public boolean isJavaFile(VirtualFile file) { | ||
return file != null && JAVA_FILE_EXTENSION.equals(file.getExtension()); | ||
} | ||
|
||
public void initialize() { | ||
if (microprofileProjectListener != null) { | ||
return; | ||
} | ||
connection = project.getMessageBus().connect(project); | ||
microprofileProjectListener = new MicroProfileProjectListener(); | ||
connection.subscribe(ClasspathResourceChangeManager.TOPIC, microprofileProjectListener); | ||
connection.subscribe(ProjectTopics.MODULES, microprofileProjectListener); | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
if (connection != null) { | ||
connection.disconnect(); | ||
} | ||
} | ||
} |
52 changes: 27 additions & 25 deletions
52
src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusPostStartupActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.