diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/AbstractConfigSource.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/AbstractConfigSource.java index d89e1d8c2..f0037f027 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/AbstractConfigSource.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/AbstractConfigSource.java @@ -14,11 +14,16 @@ import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiFile; +import io.quarkus.deployment.util.IoUtil; +import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringReader; import java.util.List; import java.util.Map; @@ -147,6 +152,11 @@ public String getSourceConfigFileURI() { return null; } + @Override + public boolean isSourceConfigFile(VirtualFile file) { + return file.equals(sourceConfigFile); + } + private static String fixURI(String uri) { return VfsUtil.toUri(uri).toString(); } @@ -164,7 +174,7 @@ protected final T getConfig() { } try { long currentLastModified = configFile.getModificationStamp(); - if (currentLastModified != lastModified) { + if (currentLastModified > lastModified) { reset(); try (InputStream input = configFile.getInputStream()) { config = loadConfig(input); @@ -180,6 +190,19 @@ protected final T getConfig() { return config; } + @Override + public void reload(PsiFile file) { + reset(); + String content = file.getText(); + try (InputStream input = IOUtils.toInputStream(content)) { + config = loadConfig(input); + lastModified = System.currentTimeMillis(); + } catch (IOException e) { + reset(); + LOGGER.error("Error while loading properties from '" + sourceConfigFile + "'.", e); + } + } + @Override public Integer getPropertyAsInt(String key) { String property = getProperty(key); diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/IConfigSource.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/IConfigSource.java index c09c70497..96cd5edd1 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/IConfigSource.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/IConfigSource.java @@ -10,6 +10,7 @@ package com.redhat.devtools.intellij.lsp4mp4ij.psi.core.project; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiFile; import java.util.List; import java.util.Map; @@ -60,6 +61,8 @@ public interface IConfigSource { */ String getSourceConfigFileURI(); + boolean isSourceConfigFile(VirtualFile file); + /** * Returns a list of all values for properties and different profiles that are * defined in this config source. @@ -100,4 +103,5 @@ public interface IConfigSource { */ Set getAllKeys(); + void reload(PsiFile file); } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/PsiMicroProfileProject.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/PsiMicroProfileProject.java index de88dd155..1fd13b0e9 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/PsiMicroProfileProject.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/PsiMicroProfileProject.java @@ -9,9 +9,13 @@ *******************************************************************************/ package com.redhat.devtools.intellij.lsp4mp4ij.psi.core.project; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.compiler.CompilerPaths; import com.intellij.openapi.module.Module; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiDocumentManager; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiManager; import com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.core.project.ConfigSourcePropertiesProvider; import org.eclipse.lsp4mp.commons.utils.ConfigSourcePropertiesProviderUtils; import org.eclipse.lsp4mp.commons.utils.IConfigSourcePropertiesProvider; @@ -190,8 +194,19 @@ public List getConfigSources() { * Evict the config sources cache and related cached information as soon as one * of properties, yaml file is saved. */ - public void evictConfigSourcesCache() { - configSources = null; + public void evictConfigSourcesCache(VirtualFile file) { + List configSources = getConfigSources(); + for (IConfigSource configSource: configSources) { + if (configSource.isSourceConfigFile(file)) { + ReadAction.compute(() -> { + PsiFile psiFile = PsiManager.getInstance(javaProject.getProject()).findFile(file); + if (psiFile != null) { + configSource.reload(psiFile); + } + return null; + }); + } + } propertyValueExpander = null; aggregatedPropertiesProvider = null; } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/PsiMicroProfileProjectManager.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/PsiMicroProfileProjectManager.java index 117a82920..b4e2f8af7 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/PsiMicroProfileProjectManager.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/project/PsiMicroProfileProjectManager.java @@ -63,7 +63,7 @@ public void sourceFilesChanged(Set> sources) { Module javaProject = pair.getSecond(); PsiMicroProfileProject mpProject = getJDTMicroProfileProject(javaProject); if (mpProject != null) { - mpProject.evictConfigSourcesCache(); + mpProject.evictConfigSourcesCache(file); } } } diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusPostStartupActivity.java b/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusPostStartupActivity.java index 58997c435..e4719bace 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusPostStartupActivity.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusPostStartupActivity.java @@ -14,12 +14,20 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.startup.StartupActivity; import com.redhat.devtools.intellij.lsp4mp4ij.classpath.ClasspathResourceChangedManager; +import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.project.PsiMicroProfileProjectManager; import org.jetbrains.annotations.NotNull; public class QuarkusPostStartupActivity implements StartupActivity, DumbAware { @Override public void runActivity(@NotNull Project project) { ClasspathResourceChangedManager.getInstance(project); + // Force the instantiation of the manager to be sure that classpath listener + // are registered before QuarkusLanguageClient classpath listener + // When an application.properties changed + // - the manager need to update the properties cache + // - and after the QuarkusLanguageClient throws an event to trigger Java validation. + // As java validation requires the properties cache, it needs that cache must be updated before. + PsiMicroProfileProjectManager.getInstance(project); QuarkusProjectService.getInstance(project); } }