diff --git a/build.gradle b/build.gradle index dabe009..9c9b3c0 100644 --- a/build.gradle +++ b/build.gradle @@ -82,6 +82,7 @@ tasks.withType(Javadoc) { patchPluginXml { // The performance of 2019.3 has been greatly improved. // change plugins without restarting the IDE in 2020.1. + // 2020.2 JCEF, 2022.3 JDK17, 2023.3 AI sinceBuild = '201.1' untilBuild = '' changeNotes = """ diff --git a/src/main/java/io/github/linwancen/plugin/show/cache/CacheUpdateEditorListener.java b/src/main/java/io/github/linwancen/plugin/show/cache/CacheUpdateEditorListener.java index e5841ba..5bcecf8 100644 --- a/src/main/java/io/github/linwancen/plugin/show/cache/CacheUpdateEditorListener.java +++ b/src/main/java/io/github/linwancen/plugin/show/cache/CacheUpdateEditorListener.java @@ -18,8 +18,6 @@ import java.util.Map; */ public class CacheUpdateEditorListener implements FileEditorManagerListener { - private static final Logger LOG = LoggerFactory.getLogger(CacheUpdateEditorListener.class); - @Override public void selectionChanged(@NotNull FileEditorManagerEvent event) { @NotNull Project project = event.getManager().getProject(); @@ -52,8 +50,10 @@ public class CacheUpdateEditorListener implements FileEditorManagerListener { LineEndCacheUtils.cache.remove(project); return; } - Map> fileMap = LineEndCacheUtils.cache.get(project); - fileMap.remove(file); + @Nullable Map> fileMap = LineEndCacheUtils.cache.get(project); + if (fileMap != null) { + fileMap.remove(file); + } }); } } diff --git a/src/main/java/io/github/linwancen/plugin/show/cache/LineEndCache.java b/src/main/java/io/github/linwancen/plugin/show/cache/LineEndCache.java index 29a3107..b6659a6 100644 --- a/src/main/java/io/github/linwancen/plugin/show/cache/LineEndCache.java +++ b/src/main/java/io/github/linwancen/plugin/show/cache/LineEndCache.java @@ -5,23 +5,18 @@ import io.github.linwancen.plugin.show.bean.LineInfo; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.List; public class LineEndCache { - @NotNull public String code; - @NotNull public List lineExtList; - public boolean selectChanged = false; + @NotNull public volatile String code; + @NotNull public final List lineExtList = new ArrayList<>(1); + public volatile boolean selectChanged = false; /** null if updated */ @Nullable public volatile LineInfo info; - public LineEndCache(@NotNull String code, @NotNull List right, @NotNull LineInfo info) { + public LineEndCache(@NotNull String code, @NotNull LineInfo info) { this.code = code; - this.lineExtList = right; this.info = info; } - - public void updated() { - selectChanged = false; - info = null; - } } 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 599e976..2dc0f3e 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 @@ -29,15 +29,17 @@ public class LineEndCacheUtils { @NotNull LineEndCache lineCache = cache .computeIfAbsent(info.project, a -> new ConcurrentHashMap<>()) .computeIfAbsent(info.file, a -> new ConcurrentHashMap<>()) - .computeIfAbsent(info.lineNumber, a -> new LineEndCache(info.text, new ArrayList<>(1), info)); - if (lineCache.selectChanged || !info.text.equals(lineCache.code)) { - lineCache.lineExtList.clear(); + .computeIfAbsent(info.lineNumber, a -> new LineEndCache(info.text, info)); + if (lineCache.selectChanged) { lineCache.info = info; + } else if (!info.text.equals(lineCache.code)) { + lineCache.info = info; + lineCache.lineExtList.clear(); } checkScheduleAndInit(); return lineCache.lineExtList; } catch (Throwable e) { - LOG.info("LineEndCache catch Throwable but log to record.", e); + LOG.info("LineEndCacheUtils catch Throwable but log to record.", e); return null; } } @@ -51,9 +53,9 @@ public class LineEndCacheUtils { isRun = true; AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> { try { - ApplicationManager.getApplication().runReadAction(LineEndCacheUtils::cacheUpdate); + cacheUpdate(); } catch (Exception e) { - LOG.info("LineEndCache checkScheduleAndInit catch Throwable but log to record.", e); + LOG.info("LineEndCacheUtils checkScheduleAndInit catch Throwable but log to record.", e); } }, 0L, 1L, TimeUnit.SECONDS); } @@ -71,26 +73,34 @@ public class LineEndCacheUtils { if (DumbService.isDumb(project)) { return; } - fileMap.forEach((file, lineMap) -> - lineMap.forEach((lineNumber, lineEndCache) -> { - try { - @Nullable LineInfo info = lineEndCache.info; - if (info == null) { - return; - } - @Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info); - lineEndCache.updated(); - lineEndCache.code = info.text; - if (lineExt == null) { - return; - } - lineEndCache.lineExtList.add(lineExt); - } catch (Exception e) { - LOG.info("LineEndCache lineMap.forEach catch Throwable but log to record.", e); + fileMap.forEach((file, lineMap) -> lineMap.forEach((lineNumber, lineEndCache) -> { + if (lineEndCache.info == null) { + return; + } + ApplicationManager.getApplication().runReadAction(() -> { + try { + @Nullable LineInfo info = lineEndCache.info; + if (info == null) { + return; } - })); + lineEndCache.info = null; + if (lineEndCache.selectChanged) { + lineEndCache.selectChanged = false; + lineEndCache.lineExtList.clear(); + } + @Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info); + if (lineExt != null) { + lineEndCache.lineExtList.add(lineExt); + } + // change after ext is updated + lineEndCache.code = info.text; + } catch (Exception e) { + LOG.info("LineEndCacheUtils lineMap.forEach catch Throwable but log to record.", e); + } + }); + })); } catch (Exception e) { - LOG.info("LineEndCache cache.forEach catch Throwable but log to record.", 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/TreeCache.java b/src/main/java/io/github/linwancen/plugin/show/cache/TreeCache.java index a4be54b..5dc2c3d 100644 --- a/src/main/java/io/github/linwancen/plugin/show/cache/TreeCache.java +++ b/src/main/java/io/github/linwancen/plugin/show/cache/TreeCache.java @@ -4,6 +4,6 @@ import org.jetbrains.annotations.Nullable; public class TreeCache { @Nullable - public String doc; + public volatile String doc; public volatile boolean needUpdate = true; } 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 5399199..bd7a8b2 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 @@ -1,10 +1,8 @@ package io.github.linwancen.plugin.show.cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; import com.intellij.ide.projectView.ProjectViewNode; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; import com.intellij.util.concurrency.AppExecutorUtil; @@ -32,8 +30,10 @@ public class TreeCacheUtils { treeCache.needUpdate = true; checkScheduleAndInit(); return treeCache.doc; + } catch (ProcessCanceledException e) { + return null; } catch (Throwable e) { - LOG.info("LineEndCache catch Throwable but log to record.", e); + LOG.info("TreeCacheUtils catch Throwable but log to record.", e); return null; } } @@ -47,7 +47,7 @@ public class TreeCacheUtils { isRun = true; AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> { try { - ApplicationManager.getApplication().runReadAction(TreeCacheUtils::cacheUpdate); + cacheUpdate(); } catch (Exception e) { LOG.info("TreeCacheUtils checkScheduleAndInit catch Throwable but log to record.", e); } @@ -68,13 +68,15 @@ public class TreeCacheUtils { return; } nodeCache.forEach((node, treeCache) -> { - try { - if (treeCache.needUpdate) { - treeCache.needUpdate = false; - treeCache.doc = Tree.treeDoc(node, project); - } - } catch (Exception e) { - LOG.info("TreeCacheUtils nodeCache.forEach catch Throwable but log to record.", e); + if (treeCache.needUpdate) { + treeCache.needUpdate = false; + ApplicationManager.getApplication().runReadAction(() -> { + try { + treeCache.doc = Tree.treeDoc(node, project); + } catch (Exception e) { + LOG.info("TreeCacheUtils nodeCache.forEach catch Throwable but log to record.", e); + } + }); } }); } catch (Exception e) {