}
@NotNull
public AnalysisContext getAnalysisContext(final @NotNull VirtualFile annotatedFile,
final @NotNull String sdkPath) {
AnalysisContext analysisContext = SoftReference.dereference(myAnalysisContextRef);
final DartUrlResolver dartUrlResolver = DartUrlResolver.getInstance(myProject, annotatedFile);
final VirtualFile yamlFile = dartUrlResolver.getPubspecYamlFile();
final Document cachedDocument = yamlFile == null ? null : FileDocumentManager.getInstance().getCachedDocument(yamlFile);
final long pubspecYamlTimestamp = yamlFile == null ? -1
: cachedDocument == null ? yamlFile.getModificationCount()
: cachedDocument.getModificationStamp();
final VirtualFile[] packageRoots = dartUrlResolver.getPackageRoots();
final VirtualFile contentRoot = ProjectRootManager.getInstance(myProject).getFileIndex().getContentRootForFile(annotatedFile);
final Module module = ModuleUtilCore.findModuleForFile(annotatedFile, myProject);
final boolean useExplicitPackageUriResolver = !ApplicationManager.getApplication().isUnitTestMode() &&
contentRoot != null &&
module != null &&
!DartConfigurable.isCustomPackageRootSet(module) &&
yamlFile == null;
final boolean sameContext = analysisContext != null &&
Comparing.equal(sdkPath, mySdkPath) &&
pubspecYamlTimestamp == myPubspecYamlTimestamp &&
Comparing.haveEqualElements(packageRoots, myDartPackageRoots) &&
(!useExplicitPackageUriResolver || Comparing.equal(contentRoot, myContentRoot));
if (sameContext) {
applyChangeSet(analysisContext, annotatedFile);
myCreatedFiles.clear();
}
else {
final DirectoryBasedDartSdk dirBasedSdk = getDirectoryBasedDartSdkSdk(sdkPath);
final DartUriResolver dartUriResolver = new DartUriResolver(dirBasedSdk);
final DartFileAndPackageUriResolver fileAndPackageUriResolver = new DartFileAndPackageUriResolver(myProject, dartUrlResolver);
final SourceFactory sourceFactory = useExplicitPackageUriResolver
? new SourceFactory(dartUriResolver, fileAndPackageUriResolver,
new ExplicitPackageUriResolver(dirBasedSdk, new File(contentRoot.getPath())))
: new SourceFactory(dartUriResolver, fileAndPackageUriResolver);
analysisContext = AnalysisEngine.getInstance().createAnalysisContext();
analysisContext.setSourceFactory(sourceFactory);
final AnalysisOptionsImpl contextOptions = new AnalysisOptionsImpl();
contextOptions.setEnableAsync(true);
analysisContext.setAnalysisOptions(contextOptions);
mySdkPath = sdkPath;
myPubspecYamlTimestamp = pubspecYamlTimestamp;
myDartPackageRoots = packageRoots;
myContentRoot = contentRoot;