Skip to content

Commit ea403ce

Browse files
committed
draft Bulk mode for files update
1 parent 5b3d8df commit ea403ce

File tree

5 files changed

+144
-74
lines changed

5 files changed

+144
-74
lines changed

src/main/java/ai/deepcode/jbplugin/core/AnalysisData.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,17 @@ static void removeFilesFromCache(@NotNull Collection<PsiFile> files) {
129129
}
130130
}
131131

132+
static void removeAllFilesFromCache(@NotNull Project project) {
133+
removeFilesFromCache(cachedFilesOfProject(project));
134+
}
135+
132136
static void removeProjectFromCache(@NotNull Project project) {
133137
// lets all running ProgressIndicators release MUTEX first
134138
RunUtils.cancelRunningIndicators(project);
135139
if (mapProject2BundleId.remove(project) != null) {
136140
info("Removed from cache: " + project);
137141
}
138-
removeFilesFromCache(cachedFilesOfProject(project));
142+
removeAllFilesFromCache(project);
139143
}
140144

141145
private static Collection<PsiFile> cachedFilesOfProject(@NotNull Project project) {
@@ -285,16 +289,14 @@ private static Map<PsiFile, List<SuggestionForFile>> retrieveSuggestions(
285289
CreateBundleResponse createBundleResponse = makeNewBundle(project, mapPath2Hash, filesToRemove);
286290
if (isNotSucceed(project, createBundleResponse, "Bad Create/Extend Bundle request: "))
287291
return EMPTY_MAP;
292+
final String bundleId = createBundleResponse.getBundleId();
293+
final List<String> missingFiles = createBundleResponse.getMissingFiles();
288294
info(
289295
"--- Create/Extend Bundle took: "
290296
+ (System.currentTimeMillis() - startTime)
291-
+ " milliseconds");
292-
293-
final String bundleId = createBundleResponse.getBundleId();
294-
info("bundleId: " + bundleId);
295-
296-
final List<String> missingFiles = createBundleResponse.getMissingFiles();
297-
info("missingFiles: " + missingFiles.size());
297+
+ " milliseconds"
298+
+ "\nbundleId: " + bundleId
299+
+ "\nmissingFiles: " + missingFiles.size());
298300

299301
// ---------------------------------------- Upload Files
300302
startTime = System.currentTimeMillis();
@@ -609,6 +611,8 @@ private static FileContent createFileContent(PsiFile psiFile) {
609611
public static Set<PsiFile> getAllFilesWithSuggestions(@NotNull final Project project) {
610612
return mapFile2Suggestions.entrySet().stream()
611613
.filter(e -> e.getKey().getProject().equals(project))
614+
// otherwise ai.deepcode.jbplugin.ui.TodoTreeBuilder.getAllFiles will fail
615+
.filter(e -> e.getKey().isValid())
612616
.filter(e -> !e.getValue().isEmpty())
613617
.map(Map.Entry::getKey)
614618
.collect(Collectors.toSet());
@@ -618,6 +622,7 @@ public static boolean isFileInCache(@NotNull PsiFile psiFile) {
618622
return mapFile2Suggestions.containsKey(psiFile);
619623
}
620624

625+
/** Remove project from all Caches and <b>CANCEL</b> all background tasks for it */
621626
public static void clearCache(@Nullable final Project project) {
622627
info("Cache clearance requested for project: " + project);
623628
mapPsiFile2Hash.clear();

src/main/java/ai/deepcode/jbplugin/core/LoginUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ private LoginUtils() {}
2828
/** network request! */
2929
public static boolean isLogged(@Nullable Project project, boolean userActionNeeded) {
3030
final String sessionToken = DeepCodeParams.getSessionToken();
31+
ProgressManager.checkCanceled();
3132
final EmptyResponse response = DeepCodeRestApi.checkSession(sessionToken);
3233
boolean isLogged = response.getStatusCode() == 200;
3334
String message = response.getStatusDescription();

src/main/java/ai/deepcode/jbplugin/core/MyBulkFileListener.java

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package ai.deepcode.jbplugin.core;
22

33
import com.intellij.openapi.project.Project;
4+
import com.intellij.openapi.project.ProjectManager;
45
import com.intellij.openapi.vfs.newvfs.BulkFileListener;
5-
import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent;
6-
import com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent;
7-
import com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent;
8-
import com.intellij.openapi.vfs.newvfs.events.VFileEvent;
6+
import com.intellij.openapi.vfs.newvfs.events.*;
97
import com.intellij.psi.PsiFile;
108
import com.intellij.psi.PsiManager;
119
import com.intellij.psi.util.PsiTreeUtil;
@@ -26,15 +24,20 @@ public class MyBulkFileListener implements BulkFileListener {
2624
@Override
2725
public void after(@NotNull List<? extends VFileEvent> events) {
2826
// fixme debug only
29-
DCLogger.info("MyBulkFileListener.after begins for: " + events);
27+
DCLogger.info("MyBulkFileListener.after begins for " + events.size() + " events " + events);
28+
for (Project project : ProjectManager.getInstance().getOpenProjects()) {
29+
/*
3030
for (Project project : AnalysisData.getAllCachedProject()) {
3131
RunUtils.runInBackground(
3232
project,
3333
() -> {
34+
*/
3435
Set<PsiFile> filesChangedOrCreated =
36+
/*
3537
RunUtils.computeInReadActionInSmartMode(
3638
project,
3739
() ->
40+
*/
3841
getFilteredFilesByEventTypes(
3942
project,
4043
events,
@@ -43,23 +46,38 @@ public void after(@NotNull List<? extends VFileEvent> events) {
4346
// to prevent updating files already done by
4447
// MyPsiTreeChangeAdapter
4548
// fixme: doesn't work, try to use isFromSave or isFromRefresh
46-
&& AnalysisData.isHashChanged(psiFile)),
49+
//&& AnalysisData.isHashChanged(psiFile)
50+
),
4751
VFileContentChangeEvent.class,
48-
// fixme doen't work for copy-past file ( VFileMoveEvent ?)
49-
VFileCreateEvent.class));
52+
VFileMoveEvent.class,
53+
VFileCopyEvent.class,
54+
VFileCreateEvent.class);
5055
if (!filesChangedOrCreated.isEmpty()) {
5156
DCLogger.info(
5257
filesChangedOrCreated.size() + " files changed: " + filesChangedOrCreated);
53-
for (PsiFile psiFile : filesChangedOrCreated) {
54-
RunUtils.runInBackgroundCancellable(
55-
psiFile,
58+
if (filesChangedOrCreated.size() > 10) {
59+
// if too many files changed then it's easier to do full rescan
60+
RunUtils.setBulkMode(project);
61+
RunUtils.runInBackground(
62+
project,
5663
() -> {
57-
AnalysisData.removeFilesFromCache(Collections.singleton(psiFile));
58-
RunUtils.asyncAnalyseAndUpdatePanel(project, Collections.singleton(psiFile));
64+
AnalysisData.removeFilesFromCache(filesChangedOrCreated);
65+
RunUtils.updateCachedAnalysisResults(project, filesChangedOrCreated);
66+
RunUtils.unsetBulkMode(project);
5967
});
68+
} else {
69+
for (PsiFile psiFile : filesChangedOrCreated) {
70+
RunUtils.runInBackgroundCancellable(
71+
psiFile,
72+
() -> {
73+
AnalysisData.removeFilesFromCache(Collections.singleton(psiFile));
74+
RunUtils.updateCachedAnalysisResults(
75+
project, Collections.singleton(psiFile));
76+
});
77+
}
6078
}
6179
}
62-
});
80+
// });
6381

6482
Set<PsiFile> gcignoreChangedFiles =
6583
getFilteredFilesByEventTypes(
@@ -69,50 +87,58 @@ public void after(@NotNull List<? extends VFileEvent> events) {
6987
VFileContentChangeEvent.class,
7088
VFileCreateEvent.class);
7189
if (!gcignoreChangedFiles.isEmpty()) {
90+
RunUtils.setBulkMode(project);
7291
RunUtils.runInBackground(
7392
project,
7493
() -> {
7594
gcignoreChangedFiles.forEach(DeepCodeIgnoreInfoHolder::update_dcignoreFileContent);
76-
// small delay to prevent duplicated delete with MyPsiTreeChangeAdapter
77-
RunUtils.rescanProject(project, 100);
95+
// ??? small delay to prevent duplicated delete with MyPsiTreeChangeAdapter
96+
RunUtils.rescanProject(project, 0);
97+
RunUtils.unsetBulkMode(project);
7898
});
7999
}
80100
}
81101
// fixme debug only
82-
DCLogger.info("MyBulkFileListener.after ends for: " + events);
102+
DCLogger.info("MyBulkFileListener.after ends");
83103
}
84104

85105
@Override
86106
public void before(@NotNull List<? extends VFileEvent> events) {
87-
DCLogger.info("MyBulkFileListener.before begins for: " + events);
88-
for (Project project : AnalysisData.getAllCachedProject()) {
107+
DCLogger.info("MyBulkFileListener.before begins for " + events.size() + " events " + events);
108+
for (Project project : ProjectManager.getInstance().getOpenProjects()) {
109+
//for (Project project : AnalysisData.getAllCachedProject()) {
89110
if (project.isDisposed()) continue;
90111
Set<PsiFile> filesRemoved =
91112
getFilteredFilesByEventTypes(
92113
project, events, DeepCodeUtils::isSupportedFileFormat, VFileDeleteEvent.class);
93114
if (!filesRemoved.isEmpty()) {
115+
DCLogger.info("Found " + filesRemoved.size() + " files to remove: " + filesRemoved);
116+
RunUtils.setBulkMode(project);
94117
RunUtils.runInBackground(
95118
project,
96119
() -> {
97120
AnalysisData.removeFilesFromCache(filesRemoved);
98-
RunUtils.asyncAnalyseAndUpdatePanel(project, Collections.emptyList(), filesRemoved);
121+
RunUtils.updateCachedAnalysisResults(project, Collections.emptyList(), filesRemoved);
122+
RunUtils.unsetBulkMode(project);
99123
});
100124
}
101125

102126
Set<PsiFile> ignoreFilesToRemove =
103127
getFilteredFilesByEventTypes(
104128
project, events, DeepCodeIgnoreInfoHolder::is_ignoreFile, VFileDeleteEvent.class);
105129
if (!ignoreFilesToRemove.isEmpty()) {
130+
RunUtils.setBulkMode(project);
106131
RunUtils.runInBackground(
107132
project,
108133
() -> {
109134
ignoreFilesToRemove.forEach(DeepCodeIgnoreInfoHolder::remove_dcignoreFileContent);
110-
// small delay to prevent duplicated delete with MyPsiTreeChangeAdapter
111-
RunUtils.rescanProject(project, 100);
135+
// ??? small delay to prevent duplicated delete with MyPsiTreeChangeAdapter
136+
RunUtils.rescanProject(project, 0);
137+
RunUtils.unsetBulkMode(project);
112138
});
113139
}
114140
}
115-
DCLogger.info("MyBulkFileListener.before ends for: " + events);
141+
DCLogger.info("MyBulkFileListener.before ends");
116142
}
117143

118144
private Set<PsiFile> getFilteredFilesByEventTypes(

src/main/java/ai/deepcode/jbplugin/core/MyProjectManagerListener.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.intellij.openapi.editor.Document;
44
import com.intellij.openapi.fileEditor.FileDocumentManager;
5-
import com.intellij.openapi.progress.ProgressManager;
65
import com.intellij.openapi.project.Project;
76
import com.intellij.openapi.project.ProjectManagerListener;
87
import com.intellij.openapi.vfs.VirtualFile;
@@ -32,16 +31,14 @@ public void projectOpened(@NotNull Project project) {
3231

3332
@Override
3433
public void projectClosing(@NotNull Project project) {
35-
// lets all running ProgressIndicators release MUTEX first
36-
// RunUtils.cancelRunningIndicators(project);
3734
RunUtils.runInBackground(project, () -> AnalysisData.removeProjectFromCache(project));
3835
}
3936

4037
private static class MyPsiTreeChangeAdapter extends PsiTreeChangeAdapter {
4138
@Override
4239
public void beforeChildrenChange(@NotNull PsiTreeChangeEvent event) {
4340
final PsiFile psiFile = event.getFile();
44-
if (psiFile == null) return;
41+
if (psiFile == null || RunUtils.inBulkMode(psiFile.getProject())) return;
4542
if (AnalysisData.isFileInCache(psiFile)) {
4643
// ?? immediate delete for visual updates in Panel, annotations, etc.
4744
// should be done in background to wait MUTEX released in case of currently running update
@@ -67,7 +64,7 @@ public void beforeChildrenChange(@NotNull PsiTreeChangeEvent event) {
6764
@Override
6865
public void childrenChanged(@NotNull PsiTreeChangeEvent event) {
6966
final PsiFile psiFile = event.getFile();
70-
if (psiFile == null) return;
67+
if (psiFile == null || RunUtils.inBulkMode(psiFile.getProject())) return;
7168

7269
if (DeepCodeUtils.isSupportedFileFormat(psiFile)) {
7370
RunUtils.runInBackgroundCancellable(
@@ -79,7 +76,7 @@ public void childrenChanged(@NotNull PsiTreeChangeEvent event) {
7976
// but in case of update finished between beforeChildrenChange and now.
8077
AnalysisData.removeFilesFromCache(psiFileSet);
8178
}
82-
RunUtils.asyncAnalyseAndUpdatePanel(psiFile.getProject(), psiFileSet);
79+
RunUtils.updateCachedAnalysisResults(psiFile.getProject(), psiFileSet);
8380
});
8481
}
8582

@@ -103,7 +100,9 @@ public void childrenChanged(@NotNull PsiTreeChangeEvent event) {
103100
@Override
104101
public void beforeChildRemoval(@NotNull PsiTreeChangeEvent event) {
105102
PsiFile psiFile = (event.getChild() instanceof PsiFile) ? (PsiFile) event.getChild() : null;
106-
if (psiFile != null && DeepCodeIgnoreInfoHolder.is_ignoreFile(psiFile)) {
103+
if (psiFile == null || RunUtils.inBulkMode(psiFile.getProject())) return;
104+
105+
if (DeepCodeIgnoreInfoHolder.is_ignoreFile(psiFile)) {
107106
DeepCodeIgnoreInfoHolder.remove_dcignoreFileContent(psiFile);
108107
// small delay to prevent duplicated delete with MyBulkFileListener
109108
RunUtils.rescanProject(psiFile.getProject(), 100);

0 commit comments

Comments
 (0)