@@ -19,7 +19,7 @@ package intellij.haskell.external.component
19
19
import java .util
20
20
import java .util .concurrent .TimeoutException
21
21
22
- import com .github .blemale .scaffeine .{LoadingCache , Scaffeine }
22
+ import com .github .blemale .scaffeine .{AsyncLoadingCache , Scaffeine }
23
23
import com .intellij .openapi .application .ApplicationManager
24
24
import com .intellij .openapi .project .Project
25
25
import com .intellij .psi .PsiFile
@@ -39,52 +39,51 @@ object FileModuleIdentifiers {
39
39
40
40
private type Result = Option [ModuleIdentifiers ]
41
41
42
- private final val Cache : LoadingCache [Key , Result ] = Scaffeine ().build ((k : Key ) => findModuleIdentifiers(k))
42
+ private final val Cache : AsyncLoadingCache [Key , Result ] = Scaffeine ().buildAsync ((k : Key ) => findModuleIdentifiers(k))
43
43
44
44
import scala .concurrent .ExecutionContext .Implicits .global
45
45
46
46
def invalidate (psiFile : PsiFile ): Unit = {
47
- Cache .invalidate(Key (psiFile))
47
+ Cache .synchronous. invalidate(Key (psiFile))
48
48
}
49
49
50
50
def refresh (psiFile : PsiFile ): Unit = {
51
- Cache .refresh(Key (psiFile))
51
+ Cache .synchronous. refresh(Key (psiFile))
52
52
}
53
53
54
54
// Invalidate files which have imported this module
55
55
def invalidate (moduleName : String ): Unit = {
56
- val keys = Cache .asMap().filter { case (_, v) => v.exists(_.exists(_.exists(_.exists(_.moduleName == moduleName)))) }.keys
57
- Cache .invalidateAll(keys)
56
+ val syncCache = Cache .synchronous
57
+ val keys = syncCache.asMap().filter { case (_, v) => v.exists(_.exists(_.exists(_.exists(_.moduleName == moduleName)))) }.keys
58
+ syncCache.invalidateAll(keys)
58
59
}
59
60
60
61
def invalidateAll (project : Project ): Unit = {
61
- Cache .asMap().filter(_._1.psiFile.getProject == project).keys.foreach(Cache .invalidate)
62
+ val syncCache = Cache .synchronous
63
+ syncCache.asMap().filter(_._1.psiFile.getProject == project).keys.foreach(syncCache.invalidate)
62
64
}
63
65
64
66
def findAvailableModuleIdentifiers (psiFile : PsiFile ): Iterable [ModuleIdentifier ] = {
67
+ val message = s " find available module identifiers for ${psiFile.getName}"
65
68
if (ApplicationManager .getApplication.isReadAccessAllowed) {
66
- val moduleIdentifiers = Future (getModuleIdentifiers(psiFile))
67
-
68
- ScalaFutureUtil .waitWithCheckCancelled(psiFile.getProject, moduleIdentifiers, s " in findAvailableModuleIdentifiers to get all module identifiers for ${psiFile.getName}" ) match {
69
- case Some (mids) => mids.getOrElse(Iterable ())
70
- case _ => Iterable ()
71
- }
69
+ val moduleIdentifiers = getModuleIdentifiers(psiFile)
70
+ ScalaFutureUtil .waitWithCheckCancelled(psiFile.getProject, moduleIdentifiers, message).getOrElse(Iterable ())
72
71
} else {
73
- getModuleIdentifiers(psiFile).getOrElse(Iterable ())
72
+ ScalaFutureUtil .waitForValue(psiFile.getProject, getModuleIdentifiers(psiFile), message ).getOrElse(Iterable ())
74
73
}
75
74
}
76
75
77
- private def getModuleIdentifiers (psiFile : PsiFile ): Option [Iterable [ModuleIdentifier ]] = {
76
+ private def getModuleIdentifiers (psiFile : PsiFile ): Future [Iterable [ModuleIdentifier ]] = {
78
77
val key = Key (psiFile)
79
- Cache .get(key) match {
78
+ Cache .get(key) map {
80
79
case Some (mids) =>
81
80
if (mids.toSeq.contains(None )) {
82
- Cache .invalidate(key)
81
+ Cache .synchronous. invalidate(key)
83
82
}
84
- Some ( mids.flatten.flatten)
83
+ mids.flatten.flatten
85
84
case None =>
86
- Cache .invalidate(key)
87
- None
85
+ Cache .synchronous. invalidate(key)
86
+ Iterable ()
88
87
}
89
88
}
90
89
0 commit comments