Skip to content

Commit ca87829

Browse files
authored
fix: TC-1631 Explicitly synchronize PSI file cached document content and disk file content to avoid analysis happens before changes is saved to disk. (#197)
Signed-off-by: Chao Wang <[email protected]>
1 parent 1861027 commit ca87829

File tree

1 file changed

+19
-11
lines changed
  • src/main/java/org/jboss/tools/intellij/componentanalysis

1 file changed

+19
-11
lines changed

src/main/java/org/jboss/tools/intellij/componentanalysis/CAService.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313

1414
import com.github.benmanes.caffeine.cache.Cache;
1515
import com.github.benmanes.caffeine.cache.Caffeine;
16+
import com.intellij.openapi.application.ApplicationManager;
17+
import com.intellij.openapi.command.WriteCommandAction;
1618
import com.intellij.openapi.components.Service;
1719
import com.intellij.openapi.components.ServiceManager;
1820
import com.intellij.openapi.diagnostic.Logger;
21+
import com.intellij.openapi.editor.Document;
22+
import com.intellij.openapi.fileEditor.FileDocumentManager;
23+
import com.intellij.openapi.project.Project;
1924
import com.intellij.psi.PsiDocumentManager;
2025
import com.intellij.psi.PsiFile;
2126
import com.redhat.exhort.api.v4.AnalysisReport;
@@ -73,20 +78,23 @@ public static boolean performAnalysis(String packageManager,
7378
Set<Dependency> dependencies,
7479
PsiFile file) {
7580
if (dependenciesModified(filePath, dependencies)) {
76-
Path tempManifest;
77-
Path tempDirectory;
7881
ApiService apiService = ServiceManager.getService(ApiService.class);
79-
try {
80-
tempDirectory = Files.createTempDirectory("rhda-idea");
81-
tempManifest = Files.createFile(Path.of(tempDirectory.toString(),fileName));
82-
Files.write(tempManifest,PsiDocumentManager.getInstance(file.getProject()).getCachedDocument(file).getText().getBytes());
83-
} catch (IOException e) {
84-
throw new RuntimeException(e);
82+
Project project = file.getProject();
83+
Document document = PsiDocumentManager.getInstance(project).getDocument(file);
84+
if (document == null) {
85+
throw new RuntimeException("Failed to perform component analysis, document " + file + " is invalid.");
86+
}
87+
// Fix for TC-1631 to avoid analysis happens before changes is saved to disk.
88+
// Explicitly synchronize PSI file cached document content and disk file content
89+
if (FileDocumentManager.getInstance().isDocumentUnsaved(document)) {
90+
ApplicationManager.getApplication().invokeAndWait(() ->
91+
WriteCommandAction.runWriteCommandAction(project, () ->
92+
FileDocumentManager.getInstance().saveDocument(document)
93+
)
94+
);
8595
}
8696

87-
// AnalysisReport report = apiService.getComponentAnalysis(packageManager, fileName, filePath);
88-
AnalysisReport report = apiService.getComponentAnalysis(packageManager, fileName, tempManifest.toString());
89-
deleteTempDir(tempDirectory);
97+
AnalysisReport report = apiService.getComponentAnalysis(packageManager, fileName, filePath);
9098
if (report == null) {
9199
throw new RuntimeException("Failed to perform component analysis, result is invalid.");
92100
}

0 commit comments

Comments
 (0)