|
13 | 13 |
|
14 | 14 | import com.github.benmanes.caffeine.cache.Cache; |
15 | 15 | import com.github.benmanes.caffeine.cache.Caffeine; |
| 16 | +import com.intellij.openapi.application.ApplicationManager; |
| 17 | +import com.intellij.openapi.command.WriteCommandAction; |
16 | 18 | import com.intellij.openapi.components.Service; |
17 | 19 | import com.intellij.openapi.components.ServiceManager; |
18 | 20 | 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; |
19 | 24 | import com.intellij.psi.PsiDocumentManager; |
20 | 25 | import com.intellij.psi.PsiFile; |
21 | 26 | import com.redhat.exhort.api.v4.AnalysisReport; |
@@ -73,20 +78,23 @@ public static boolean performAnalysis(String packageManager, |
73 | 78 | Set<Dependency> dependencies, |
74 | 79 | PsiFile file) { |
75 | 80 | if (dependenciesModified(filePath, dependencies)) { |
76 | | - Path tempManifest; |
77 | | - Path tempDirectory; |
78 | 81 | 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 | + ); |
85 | 95 | } |
86 | 96 |
|
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); |
90 | 98 | if (report == null) { |
91 | 99 | throw new RuntimeException("Failed to perform component analysis, result is invalid."); |
92 | 100 | } |
|
0 commit comments