Filter diagnostics during creating cache

This commit is contained in:
Nikolay Krasko
2015-11-23 20:46:53 +03:00
parent 7baf58b5de
commit 7622e90438
3 changed files with 14 additions and 6 deletions
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.resolve.diagnostics;
import com.intellij.openapi.util.AtomicNotNullLazyValue;
import com.intellij.openapi.util.Condition;
import com.intellij.psi.PsiElement;
import com.intellij.util.containers.ConcurrentMultiMap;
import com.intellij.util.containers.MultiMap;
@@ -27,17 +28,19 @@ import java.util.Collection;
public class DiagnosticsElementsCache {
private final Diagnostics diagnostics;
private final Condition<Diagnostic> filter;
private final AtomicNotNullLazyValue<MultiMap<PsiElement, Diagnostic>> elementToDiagnostic = new AtomicNotNullLazyValue<MultiMap<PsiElement, Diagnostic>>() {
@NotNull
@Override
protected MultiMap<PsiElement, Diagnostic> compute() {
return buildElementToDiagnosticCache(diagnostics);
return buildElementToDiagnosticCache(diagnostics, filter);
}
};
public DiagnosticsElementsCache(Diagnostics diagnostics) {
public DiagnosticsElementsCache(Diagnostics diagnostics, Condition<Diagnostic> filter) {
this.diagnostics = diagnostics;
this.filter = filter;
}
@NotNull
@@ -45,10 +48,12 @@ public class DiagnosticsElementsCache {
return elementToDiagnostic.getValue().get(psiElement);
}
private static MultiMap<PsiElement, Diagnostic> buildElementToDiagnosticCache(Diagnostics diagnostics) {
private static MultiMap<PsiElement, Diagnostic> buildElementToDiagnosticCache(Diagnostics diagnostics, Condition<Diagnostic> filter) {
MultiMap<PsiElement, Diagnostic> elementToDiagnostic = new ConcurrentMultiMap<PsiElement, Diagnostic>();
for (Diagnostic diagnostic : diagnostics) {
elementToDiagnostic.putValue(diagnostic.getPsiElement(), diagnostic);
if (filter.value(diagnostic)) {
elementToDiagnostic.putValue(diagnostic.getPsiElement(), diagnostic);
}
}
return elementToDiagnostic;
@@ -76,7 +76,7 @@ public class DiagnosticsWithSuppression implements Diagnostics {
return !isSuppressed(diagnostic);
}
};
private final DiagnosticsElementsCache elementsCache = new DiagnosticsElementsCache(this);
private final DiagnosticsElementsCache elementsCache = new DiagnosticsElementsCache(this, filter);
public DiagnosticsWithSuppression(@NotNull BindingContext context, @NotNull Collection<Diagnostic> diagnostics) {
this.context = context;
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.resolve.diagnostics
import com.intellij.openapi.util.Condition
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.Diagnostic
import java.util.ArrayList
@@ -23,7 +24,9 @@ import java.util.ArrayList
public class SimpleDiagnostics(diagnostics: Collection<Diagnostic>) : Diagnostics {
//copy to prevent external change
private val diagnostics = ArrayList(diagnostics)
private val elementsCache = DiagnosticsElementsCache(this)
@Suppress("UNCHECKED_CAST")
private val elementsCache = DiagnosticsElementsCache(this, Condition.TRUE as Condition<Diagnostic>?)
override fun all() = diagnostics