Skip to content

Commit

Permalink
fix: Java file diagnostics is not refreshed when properties file is
Browse files Browse the repository at this point in the history
saved

Fixes #860

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Aug 8, 2023
1 parent 70b5a55 commit 82c76ab
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -100,4 +103,5 @@ public interface IConfigSource {
*/
Set<String> getAllKeys();

void reload(PsiFile file);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -190,8 +194,19 @@ public List<IConfigSource> 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<IConfigSource> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void sourceFilesChanged(Set<Pair<VirtualFile, Module>> sources) {
Module javaProject = pair.getSecond();
PsiMicroProfileProject mpProject = getJDTMicroProfileProject(javaProject);
if (mpProject != null) {
mpProject.evictConfigSourcesCache();
mpProject.evictConfigSourcesCache(file);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 82c76ab

Please sign in to comment.