pref: Cache update ReadAction.nonBlocking once

This commit is contained in:
林万程
2025-03-10 05:44:05 +08:00
parent 2d3f8d17de
commit 156ed6f014
2 changed files with 64 additions and 63 deletions

View File

@@ -78,7 +78,9 @@ public class LineEndCacheUtils {
isRun = true; isRun = true;
AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> { AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> {
try { try {
cacheUpdate(); ReadAction.nonBlocking(LineEndCacheUtils::cacheUpdate)
.inSmartMode(project)
.submit(AppExecutorUtil.getAppExecutorService());
} catch (ProcessCanceledException ignored) { } catch (ProcessCanceledException ignored) {
} catch (Throwable e) { } catch (Throwable e) {
LOG.info("LineEndCacheUtils checkScheduleAndInit catch Throwable but log to record.", e); LOG.info("LineEndCacheUtils checkScheduleAndInit catch Throwable but log to record.", e);
@@ -91,62 +93,60 @@ public class LineEndCacheUtils {
private static void cacheUpdate() { private static void cacheUpdate() {
cache.forEach((project, fileMap) -> { cache.forEach((project, fileMap) -> {
ReadAction.nonBlocking(() -> { try {
try { if (project.isDisposed()) {
if (project.isDisposed()) { cache.remove(project);
cache.remove(project); return;
}
fileMap.forEach((file, lineMap) -> lineMap.forEach((lineNumber, lineCache) -> {
@NotNull LineInfo info = lineCache.info;
@Nullable List<LineExtensionInfo> list = lineCache.map.get(info.text);
if (!(lineCache.needUpdate() || list == null)) {
return; return;
} }
fileMap.forEach((file, lineMap) -> lineMap.forEach((lineNumber, lineCache) -> { try {
@NotNull LineInfo info = lineCache.info; if (project.isDisposed() || DumbService.isDumb(project)) {
@Nullable List<LineExtensionInfo> list = lineCache.map.get(info.text);
if (!(lineCache.needUpdate() || list == null)) {
return; return;
} }
try { @Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info);
if (project.isDisposed() || DumbService.isDumb(project)) { @Nullable LineInfo info2 = LineInfo.of(info, lineNumber);
return; if (info2 == null || !info2.text.equals(info.text)) {
} return;
@Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info); }
@Nullable LineInfo info2 = LineInfo.of(info, lineNumber); if (list != null) {
if (info2 == null || !info2.text.equals(info.text)) { list.clear();
return; }
} // fix delete line get doc from before line because PsiFile be not updated
if ("}".equals(info.text.trim())) {
return;
}
if (lineExt != null) {
if (list != null) { if (list != null) {
list.clear(); list.add(lineExt);
} } else {
// fix delete line get doc from before line because PsiFile be not updated @NotNull ArrayList<LineExtensionInfo> lineExtList = new ArrayList<>(1);
if ("}".equals(info.text.trim())) { lineExtList.add(lineExt);
return; lineCache.map.put(info.text, lineExtList);
}
if (lineExt != null) {
if (list != null) {
list.add(lineExt);
} else {
@NotNull ArrayList<LineExtensionInfo> lineExtList = new ArrayList<>(1);
lineExtList.add(lineExt);
lineCache.map.put(info.text, lineExtList);
}
}
lineCache.updated();
} catch (ProcessCanceledException ignore) {
// ignore
} catch (Throwable e) {
@Nullable String msg = e.getMessage();
if (msg == null || !msg.contains("File is not valid")) {
LOG.info("LineEndCacheUtils lineMap.forEach catch Throwable but log to record.", e);
} }
} }
})); lineCache.updated();
} catch (ProcessCanceledException ignored) { } catch (ProcessCanceledException ignore) {
} catch (IllegalStateException ignore) { // ignore
// ignore inSmartMode(project) throw: } catch (Throwable e) {
// @NotNull method com/intellij/openapi/project/impl/ProjectImpl.getEarlyDisposable must not @Nullable String msg = e.getMessage();
// return null if (msg == null || !msg.contains("File is not valid")) {
} catch (Throwable e) { LOG.info("LineEndCacheUtils lineMap.forEach catch Throwable but log to record.", e);
LOG.info("LineEndCacheUtils cache.forEach catch Throwable but log to record.", e); }
} }
}).inSmartMode(project).submit(AppExecutorUtil.getAppExecutorService()); }));
} catch (ProcessCanceledException ignored) {
} catch (IllegalStateException ignore) {
// ignore inSmartMode(project) throw:
// @NotNull method com/intellij/openapi/project/impl/ProjectImpl.getEarlyDisposable must not
// return null
} catch (Throwable e) {
LOG.info("LineEndCacheUtils cache.forEach catch Throwable but log to record.", e);
}
}); });
} }
} }

View File

@@ -53,7 +53,9 @@ public class TreeCacheUtils {
isRun = true; isRun = true;
AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> { AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> {
try { try {
cacheUpdate(); ReadAction.nonBlocking(TreeCacheUtils::cacheUpdate)
.inSmartMode(project)
.submit(AppExecutorUtil.getAppExecutorService());
} catch (Throwable e) { } catch (Throwable e) {
LOG.info("TreeCacheUtils checkScheduleAndInit catch Throwable but log to record.", e); LOG.info("TreeCacheUtils checkScheduleAndInit catch Throwable but log to record.", e);
} }
@@ -72,21 +74,20 @@ public class TreeCacheUtils {
} }
nodeCache.forEach((node, treeCache) -> { nodeCache.forEach((node, treeCache) -> {
if (treeCache.needUpdate) { if (treeCache.needUpdate) {
ReadAction.nonBlocking(() -> { try {
try { if (project.isDisposed() || DumbService.isDumb(project)) {
if (project.isDisposed() || DumbService.isDumb(project)) { return;
return;
}
treeCache.doc = Tree.treeDoc(node, project);
treeCache.needUpdate = false;
} catch (ProcessCanceledException ignore) {
// ignore
} catch (Throwable e) {
LOG.info("TreeCacheUtils nodeCache.forEach catch Throwable but log to record.", e);
} }
}).inSmartMode(project).executeSynchronously(); treeCache.doc = Tree.treeDoc(node, project);
treeCache.needUpdate = false;
} catch (ProcessCanceledException ignore) {
// ignore
} catch (Throwable e) {
LOG.info("TreeCacheUtils nodeCache.forEach catch Throwable but log to record.", e);
}
} }
}); });
} catch (ProcessCanceledException ignored) {
} catch (IllegalStateException ignore) { } catch (IllegalStateException ignore) {
// ignore inSmartMode(project) throw: // ignore inSmartMode(project) throw:
// @NotNull method com/intellij/openapi/project/impl/ProjectImpl.getEarlyDisposable must not return null // @NotNull method com/intellij/openapi/project/impl/ProjectImpl.getEarlyDisposable must not return null