diff --git a/src/main/java/io/github/linwancen/plugin/show/cache/LineEndCacheUtils.java b/src/main/java/io/github/linwancen/plugin/show/cache/LineEndCacheUtils.java index 9252098..9ca6b76 100644 --- a/src/main/java/io/github/linwancen/plugin/show/cache/LineEndCacheUtils.java +++ b/src/main/java/io/github/linwancen/plugin/show/cache/LineEndCacheUtils.java @@ -78,7 +78,9 @@ public class LineEndCacheUtils { isRun = true; AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> { try { - cacheUpdate(); + ReadAction.nonBlocking(LineEndCacheUtils::cacheUpdate) + .inSmartMode(project) + .submit(AppExecutorUtil.getAppExecutorService()); } catch (ProcessCanceledException ignored) { } catch (Throwable e) { LOG.info("LineEndCacheUtils checkScheduleAndInit catch Throwable but log to record.", e); @@ -91,62 +93,60 @@ public class LineEndCacheUtils { private static void cacheUpdate() { cache.forEach((project, fileMap) -> { - ReadAction.nonBlocking(() -> { - try { - if (project.isDisposed()) { - cache.remove(project); + try { + if (project.isDisposed()) { + cache.remove(project); + return; + } + fileMap.forEach((file, lineMap) -> lineMap.forEach((lineNumber, lineCache) -> { + @NotNull LineInfo info = lineCache.info; + @Nullable List list = lineCache.map.get(info.text); + if (!(lineCache.needUpdate() || list == null)) { return; } - fileMap.forEach((file, lineMap) -> lineMap.forEach((lineNumber, lineCache) -> { - @NotNull LineInfo info = lineCache.info; - @Nullable List list = lineCache.map.get(info.text); - if (!(lineCache.needUpdate() || list == null)) { + try { + if (project.isDisposed() || DumbService.isDumb(project)) { return; } - try { - if (project.isDisposed() || DumbService.isDumb(project)) { - return; - } - @Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info); - @Nullable LineInfo info2 = LineInfo.of(info, lineNumber); - if (info2 == null || !info2.text.equals(info.text)) { - return; - } + @Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info); + @Nullable LineInfo info2 = LineInfo.of(info, lineNumber); + if (info2 == null || !info2.text.equals(info.text)) { + return; + } + if (list != null) { + list.clear(); + } + // 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) { - list.clear(); - } - // 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) { - list.add(lineExt); - } else { - @NotNull ArrayList 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); + list.add(lineExt); + } else { + @NotNull ArrayList lineExtList = new ArrayList<>(1); + lineExtList.add(lineExt); + lineCache.map.put(info.text, lineExtList); } } - })); - } 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); - } - }).inSmartMode(project).submit(AppExecutorUtil.getAppExecutorService()); + 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); + } + } + })); + } 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); + } }); } } diff --git a/src/main/java/io/github/linwancen/plugin/show/cache/TreeCacheUtils.java b/src/main/java/io/github/linwancen/plugin/show/cache/TreeCacheUtils.java index 753fc25..f8ad869 100644 --- a/src/main/java/io/github/linwancen/plugin/show/cache/TreeCacheUtils.java +++ b/src/main/java/io/github/linwancen/plugin/show/cache/TreeCacheUtils.java @@ -53,7 +53,9 @@ public class TreeCacheUtils { isRun = true; AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> { try { - cacheUpdate(); + ReadAction.nonBlocking(TreeCacheUtils::cacheUpdate) + .inSmartMode(project) + .submit(AppExecutorUtil.getAppExecutorService()); } catch (Throwable e) { LOG.info("TreeCacheUtils checkScheduleAndInit catch Throwable but log to record.", e); } @@ -72,21 +74,20 @@ public class TreeCacheUtils { } nodeCache.forEach((node, treeCache) -> { if (treeCache.needUpdate) { - ReadAction.nonBlocking(() -> { - try { - if (project.isDisposed() || DumbService.isDumb(project)) { - 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); + try { + if (project.isDisposed() || DumbService.isDumb(project)) { + return; } - }).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) { // ignore inSmartMode(project) throw: // @NotNull method com/intellij/openapi/project/impl/ProjectImpl.getEarlyDisposable must not return null