Refactoring: extract anonymous CachedValueProvider provider

This commit is contained in:
Nikolay Krasko
2013-08-15 18:04:29 +04:00
parent 95559e09eb
commit 58aaa2bc00
@@ -75,7 +75,83 @@ public final class AnalyzerFacadeWithCache {
return CachedValuesManager.getManager(project).getCachedValue( return CachedValuesManager.getManager(project).getCachedValue(
project, project,
ANALYZE_EXHAUST_FULL, ANALYZE_EXHAUST_FULL,
new CachedValueProvider<SLRUCache<JetFile, AnalyzeExhaust>>() { new SLRUCachedAnalyzeExhaustProvider(),
false
).get(file);
}
}
public static BindingContext getContextForElement(@NotNull JetElement jetElement) {
CancelableResolveSession cancelableResolveSession = getLazyResolveSessionForFile((JetFile) jetElement.getContainingFile());
return cancelableResolveSession.resolveToElement(jetElement);
}
public static CancelableResolveSession getLazyResolveSessionForFile(@NotNull JetFile file) {
Project project = file.getProject();
DeclarationsCacheProvider provider = KotlinCacheManager.getInstance(project).getRegisteredProvider(TargetPlatformDetector.getPlatform(file));
if (!provider.areDeclarationsAvailable(file)) {
// There can be request for temp files (in completion) or non-source (in library) files. Create temp sessions for them.
CachedValue<CancelableResolveSession> cachedValue;
synchronized (PER_FILE_SESSION_CACHE) {
cachedValue = PER_FILE_SESSION_CACHE.get(file);
}
return cachedValue.getValue();
}
return provider.getLazyResolveSession();
}
@NotNull
private static AnalyzeExhaust emptyExhaustWithDiagnosticOnFile(JetFile file, Throwable e) {
BindingTraceContext bindingTraceContext = new BindingTraceContext();
bindingTraceContext.report(Errors.EXCEPTION_WHILE_ANALYZING.on(file, e));
AnalyzeExhaust analyzeExhaust = AnalyzeExhaust.error(bindingTraceContext.getBindingContext(), e);
// Force invalidating of headers cache - temp decision for monitoring rewrite slice bug
PsiModificationTracker tracker = PsiManager.getInstance(file.getProject()).getModificationTracker();
((PsiModificationTrackerImpl) tracker).incOutOfCodeBlockModificationCounter();
return analyzeExhaust;
}
private static final SLRUCache<JetFile, CachedValue<CancelableResolveSession>> PER_FILE_SESSION_CACHE = new SLRUCache<JetFile, CachedValue<CancelableResolveSession>>(2, 3) {
@NotNull
@Override
public CachedValue<CancelableResolveSession> createValue(final JetFile file) {
final Project fileProject = file.getProject();
return CachedValuesManager.getManager(fileProject).createCachedValue(
// Each value monitors OUT_OF_CODE_BLOCK_MODIFICATION_COUNT and modification tracker of the stored value
new CachedValueProvider<CancelableResolveSession>() {
@Nullable
@Override
public Result<CancelableResolveSession> compute() {
Project project = file.getProject();
Collection<JetFile> files = JetFilesProvider.getInstance(project).allInScope(GlobalSearchScope.allScope(project));
// Add requested file to the list of files for searching declarations
files.add(file);
if (file != file.getOriginalFile()) {
// Given file can be a non-physical copy of the file in list (completion case). Remove the prototype file.
//noinspection SuspiciousMethodCalls
files.remove(file.getOriginalFile());
}
ResolveSession resolveSession = AnalyzerFacadeProvider.getAnalyzerFacadeForFile(file).getLazyResolveSession(fileProject, files);
return Result.create(new CancelableResolveSession(file, resolveSession), PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
}
},
true);
}
};
private static class SLRUCachedAnalyzeExhaustProvider implements CachedValueProvider<SLRUCache<JetFile, AnalyzeExhaust>> {
@Nullable @Nullable
@Override @Override
public Result<SLRUCache<JetFile, AnalyzeExhaust>> compute() { public Result<SLRUCache<JetFile, AnalyzeExhaust>> compute() {
@@ -106,11 +182,6 @@ public final class AnalyzerFacadeWithCache {
}; };
return Result.create(cache, PsiModificationTracker.MODIFICATION_COUNT); return Result.create(cache, PsiModificationTracker.MODIFICATION_COUNT);
} }
},
false
).get(file);
}
}
private static AnalyzeExhaust emptyExhaust() { private static AnalyzeExhaust emptyExhaust() {
return AnalyzeExhaust.success(BindingContext.EMPTY, ErrorUtils.getErrorModule()); return AnalyzeExhaust.success(BindingContext.EMPTY, ErrorUtils.getErrorModule());
@@ -148,78 +219,9 @@ public final class AnalyzerFacadeWithCache {
analyzeExhaustHeaders.getModuleDescriptor()); analyzeExhaustHeaders.getModuleDescriptor());
} }
@NotNull
private static AnalyzeExhaust emptyExhaustWithDiagnosticOnFile(JetFile file, Throwable e) {
BindingTraceContext bindingTraceContext = new BindingTraceContext();
bindingTraceContext.report(Errors.EXCEPTION_WHILE_ANALYZING.on(file, e));
AnalyzeExhaust analyzeExhaust = AnalyzeExhaust.error(bindingTraceContext.getBindingContext(), e);
// Force invalidating of headers cache - temp decision for monitoring rewrite slice bug
PsiModificationTracker tracker = PsiManager.getInstance(file.getProject()).getModificationTracker();
((PsiModificationTrackerImpl) tracker).incOutOfCodeBlockModificationCounter();
return analyzeExhaust;
}
private static void handleError(@NotNull Throwable e) { private static void handleError(@NotNull Throwable e) {
DiagnosticUtils.throwIfRunningOnServer(e); DiagnosticUtils.throwIfRunningOnServer(e);
LOG.error(e); LOG.error(e);
} }
public static BindingContext getContextForElement(@NotNull JetElement jetElement) {
CancelableResolveSession cancelableResolveSession = getLazyResolveSessionForFile((JetFile) jetElement.getContainingFile());
return cancelableResolveSession.resolveToElement(jetElement);
} }
public static CancelableResolveSession getLazyResolveSessionForFile(@NotNull JetFile file) {
Project project = file.getProject();
DeclarationsCacheProvider provider = KotlinCacheManager.getInstance(project).getRegisteredProvider(TargetPlatformDetector.getPlatform(file));
if (!provider.areDeclarationsAvailable(file)) {
// There can be request for temp files (in completion) or non-source (in library) files. Create temp sessions for them.
CachedValue<CancelableResolveSession> cachedValue;
synchronized (PER_FILE_SESSION_CACHE) {
cachedValue = PER_FILE_SESSION_CACHE.get(file);
}
return cachedValue.getValue();
}
return provider.getLazyResolveSession();
}
private static final SLRUCache<JetFile, CachedValue<CancelableResolveSession>> PER_FILE_SESSION_CACHE = new SLRUCache<JetFile, CachedValue<CancelableResolveSession>>(2, 3) {
@NotNull
@Override
public CachedValue<CancelableResolveSession> createValue(final JetFile file) {
final Project fileProject = file.getProject();
return CachedValuesManager.getManager(fileProject).createCachedValue(
// Each value monitors OUT_OF_CODE_BLOCK_MODIFICATION_COUNT and modification tracker of the stored value
new CachedValueProvider<CancelableResolveSession>() {
@Nullable
@Override
public Result<CancelableResolveSession> compute() {
Project project = file.getProject();
Collection<JetFile> files = JetFilesProvider.getInstance(project).allInScope(GlobalSearchScope.allScope(project));
// Add requested file to the list of files for searching declarations
files.add(file);
if (file != file.getOriginalFile()) {
// Given file can be a non-physical copy of the file in list (completion case). Remove the prototype file.
//noinspection SuspiciousMethodCalls
files.remove(file.getOriginalFile());
}
ResolveSession resolveSession = AnalyzerFacadeProvider.getAnalyzerFacadeForFile(file).getLazyResolveSession(fileProject, files);
return Result.create(new CancelableResolveSession(file, resolveSession), PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
}
},
true);
}
};
} }