Search in sources :

Example 1 with CachedValueProvider

use of com.intellij.psi.util.CachedValueProvider in project android by JetBrains.

the class AndroidXmlSchemaProvider method getSchema.

@Override
public XmlFile getSchema(@NotNull @NonNls String url, @Nullable final Module module, @NotNull PsiFile baseFile) {
    if (module == null || AndroidFacet.getInstance(module) == null)
        return null;
    Map<String, CachedValue<XmlFile>> descriptors = module.getUserData(DESCRIPTORS_MAP_IN_MODULE);
    if (descriptors == null) {
        descriptors = new THashMap<String, CachedValue<XmlFile>>();
        module.putUserData(DESCRIPTORS_MAP_IN_MODULE, descriptors);
    }
    CachedValue<XmlFile> reference = descriptors.get(url);
    if (reference != null) {
        return reference.getValue();
    }
    CachedValuesManager manager = CachedValuesManager.getManager(module.getProject());
    reference = manager.createCachedValue(new CachedValueProvider<XmlFile>() {

        @Override
        public Result<XmlFile> compute() {
            final URL resource = AndroidXmlSchemaProvider.class.getResource("android.xsd");
            final VirtualFile fileByURL = VfsUtil.findFileByURL(resource);
            XmlFile result = (XmlFile) PsiManager.getInstance(module.getProject()).findFile(fileByURL).copy();
            return new Result<XmlFile>(result, PsiModificationTracker.MODIFICATION_COUNT);
        }
    }, false);
    descriptors.put(url, reference);
    return reference.getValue();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CachedValueProvider(com.intellij.psi.util.CachedValueProvider) XmlFile(com.intellij.psi.xml.XmlFile) CachedValuesManager(com.intellij.psi.util.CachedValuesManager) CachedValue(com.intellij.psi.util.CachedValue) URL(java.net.URL)

Example 2 with CachedValueProvider

use of com.intellij.psi.util.CachedValueProvider in project kotlin by JetBrains.

the class ProjectStructureUtil method getCachedPlatformForModule.

@NotNull
static /* package */
TargetPlatform getCachedPlatformForModule(@NotNull final Module module) {
    CachedValue<TargetPlatform> result = module.getUserData(PLATFORM_FOR_MODULE);
    if (result == null) {
        result = CachedValuesManager.getManager(module.getProject()).createCachedValue(new CachedValueProvider<TargetPlatform>() {

            @Override
            public Result<TargetPlatform> compute() {
                TargetPlatform configuredInFacet = getPlatformConfiguredInFacet(module);
                TargetPlatform platform = configuredInFacet != null ? configuredInFacet : hasJsStandardLibraryInDependencies(module) ? JsPlatform.INSTANCE : JvmPlatform.INSTANCE;
                return Result.create(platform, ProjectRootModificationTracker.getInstance(module.getProject()));
            }
        }, false);
        module.putUserData(PLATFORM_FOR_MODULE, result);
    }
    return result.getValue();
}
Also used : CachedValueProvider(com.intellij.psi.util.CachedValueProvider) TargetPlatform(org.jetbrains.kotlin.resolve.TargetPlatform) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with CachedValueProvider

use of com.intellij.psi.util.CachedValueProvider in project android by JetBrains.

the class ManifestInnerClass method getFields.

@NotNull
@Override
public PsiField[] getFields() {
    if (myFieldsCache == null) {
        myFieldsCache = CachedValuesManager.getManager(getProject()).createCachedValue(new CachedValueProvider<PsiField[]>() {

            @Override
            public Result<PsiField[]> compute() {
                final Manifest manifest = myFacet.getManifest();
                if (manifest == null) {
                    return Result.create(PsiField.EMPTY_ARRAY, PsiModificationTracker.MODIFICATION_COUNT);
                }
                final List<Pair<String, String>> pairs = doGetFields(manifest);
                final PsiField[] result = new PsiField[pairs.size()];
                final PsiClassType stringType = PsiType.getJavaLangString(myManager, GlobalSearchScope.allScope(getProject()));
                final PsiElementFactory factory = JavaPsiFacade.getElementFactory(getProject());
                int i = 0;
                for (Pair<String, String> pair : pairs) {
                    final AndroidLightField field = new AndroidLightField(pair.getFirst(), ManifestInnerClass.this, stringType, true, pair.getSecond());
                    field.setInitializer(factory.createExpressionFromText("\"" + pair.getSecond() + "\"", field));
                    result[i++] = field;
                }
                final XmlElement xmlElement = manifest.getXmlElement();
                final PsiFile psiManifestFile = xmlElement != null ? xmlElement.getContainingFile() : null;
                return Result.create(result, psiManifestFile != null ? psiManifestFile : PsiModificationTracker.MODIFICATION_COUNT);
            }
        });
    }
    return myFieldsCache.getValue();
}
Also used : CachedValueProvider(com.intellij.psi.util.CachedValueProvider) Manifest(org.jetbrains.android.dom.manifest.Manifest) XmlElement(com.intellij.psi.xml.XmlElement) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CachedValueProvider (com.intellij.psi.util.CachedValueProvider)3 NotNull (org.jetbrains.annotations.NotNull)2 Pair (com.intellij.openapi.util.Pair)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 CachedValue (com.intellij.psi.util.CachedValue)1 CachedValuesManager (com.intellij.psi.util.CachedValuesManager)1 XmlElement (com.intellij.psi.xml.XmlElement)1 XmlFile (com.intellij.psi.xml.XmlFile)1 URL (java.net.URL)1 Manifest (org.jetbrains.android.dom.manifest.Manifest)1 TargetPlatform (org.jetbrains.kotlin.resolve.TargetPlatform)1