Search in sources :

Example 1 with CachedValue

use of com.intellij.psi.util.CachedValue in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoPackageUtil method getAllPackagesInDirectory.

@NotNull
public static Collection<String> getAllPackagesInDirectory(@Nullable PsiDirectory dir, @Nullable Module contextModule, boolean trimTestSuffices) {
    if (dir == null)
        return Collections.emptyList();
    if (contextModule != null) {
        return getAllPackagesInDirectoryInner(dir, contextModule, trimTestSuffices);
    }
    Key<CachedValue<Collection<String>>> key = trimTestSuffices ? PACKAGES_TEST_TRIMMED_CACHE : PACKAGES_CACHE;
    return CachedValuesManager.getManager(dir.getProject()).getCachedValue(dir, key, () -> {
        Module module = ModuleUtilCore.findModuleForPsiElement(dir);
        GoBuildTargetSettings buildTargetSettings = module != null ? GoModuleSettings.getInstance(module).getBuildTargetSettings() : null;
        // todo[zolotov]: implement package modification tracker
        return buildTargetSettings != null ? CachedValueProvider.Result.create(getAllPackagesInDirectoryInner(dir, module, trimTestSuffices), dir, buildTargetSettings) : CachedValueProvider.Result.create(getAllPackagesInDirectoryInner(dir, null, trimTestSuffices), dir);
    }, false);
}
Also used : GoBuildTargetSettings(com.goide.project.GoBuildTargetSettings) CachedValue(com.intellij.psi.util.CachedValue) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with CachedValue

use of com.intellij.psi.util.CachedValue in project intellij-community by JetBrains.

the class XmlNSDescriptorImpl method doFindIn.

private TypeDescriptor doFindIn(final XmlTag[] tags, final String name, final String namespace, final Pair<QNameKey, XmlTag> pair, final XmlTag rootTag) {
    for (final XmlTag tag : tags) {
        if (equalsToSchemaName(tag, "complexType")) {
            if (name == null) {
                CachedValue<TypeDescriptor> value = createAndPutTypesCachedValue(tag, pair);
                return value.getValue();
            }
            String nameAttribute = tag.getAttributeValue("name");
            if (isSameName(name, namespace, nameAttribute)) {
                CachedValue<TypeDescriptor> cachedValue = createAndPutTypesCachedValue(tag, pair);
                return cachedValue.getValue();
            }
        } else if (equalsToSchemaName(tag, "simpleType")) {
            if (name == null) {
                CachedValue<TypeDescriptor> value = createAndPutTypesCachedValueSimpleType(tag, pair);
                return value.getValue();
            }
            String nameAttribute = tag.getAttributeValue("name");
            if (isSameName(name, namespace, nameAttribute)) {
                CachedValue<TypeDescriptor> cachedValue = createAndPutTypesCachedValue(tag, pair);
                return cachedValue.getValue();
            }
        } else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (equalsToSchemaName(tag, IMPORT_TAG_NAME) && (namespace == null || !namespace.equals(getDefaultNamespace())))) {
            final String schemaLocation = tag.getAttributeValue("schemaLocation");
            if (schemaLocation != null) {
                final XmlFile xmlFile = XmlUtil.findNamespace(rootTag.getContainingFile(), schemaLocation);
                if (xmlFile != null) {
                    final XmlDocument document = xmlFile.getDocument();
                    if (document != null) {
                        final CachedValue<TypeDescriptor> value = CachedValuesManager.getManager(tag.getProject()).createCachedValue(() -> {
                            final String currentName = tag.getAttributeValue("name");
                            if ((currentName != null && !currentName.equals(XmlUtil.findLocalNameByQualifiedName(name))) || !xmlFile.isValid() || xmlFile.getDocument() == null) {
                                myTypesMap.remove(pair);
                                return new CachedValueProvider.Result<>(null, PsiModificationTracker.MODIFICATION_COUNT);
                            }
                            final XmlDocument document1 = xmlFile.getDocument();
                            final XmlNSDescriptorImpl nsDescriptor = findNSDescriptor(tag, document1);
                            if (nsDescriptor == null) {
                                myTypesMap.remove(pair);
                                return new CachedValueProvider.Result<>(null, PsiModificationTracker.MODIFICATION_COUNT);
                            }
                            final XmlTag rTag = document1.getRootTag();
                            final TypeDescriptor complexTypeDescriptor = nsDescriptor.findTypeDescriptorImpl(rTag, name, namespace);
                            return new CachedValueProvider.Result<>(complexTypeDescriptor, rTag);
                        }, false);
                        if (value.getValue() != null) {
                            myTypesMap.put(pair, value);
                            return value.getValue();
                        }
                    }
                }
            }
        } else if (equalsToSchemaName(tag, REDEFINE_TAG_NAME)) {
            final XmlTag[] subTags = tag.getSubTags();
            TypeDescriptor descriptor = doFindIn(subTags, name, namespace, pair, rootTag);
            if (descriptor != null)
                return descriptor;
            final XmlNSDescriptorImpl nsDescriptor = getRedefinedElementDescriptor(tag);
            if (nsDescriptor != null) {
                final XmlTag redefinedRootTag = ((XmlDocument) nsDescriptor.getDeclaration()).getRootTag();
                descriptor = doFindIn(redefinedRootTag.getSubTags(), name, namespace, pair, redefinedRootTag);
                if (descriptor != null)
                    return descriptor;
            }
        }
    }
    return null;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument) CachedValue(com.intellij.psi.util.CachedValue) XmlTag(com.intellij.psi.xml.XmlTag)

Example 3 with CachedValue

use of com.intellij.psi.util.CachedValue in project intellij-community by JetBrains.

the class XmlNSDescriptorImpl method getAttributeImpl.

@Nullable
private XmlAttributeDescriptor getAttributeImpl(String localName, String namespace, @Nullable Set<XmlTag> visited) {
    if (myTag == null)
        return null;
    XmlNSDescriptor nsDescriptor = myTag.getNSDescriptor(namespace, true);
    if (nsDescriptor != this && nsDescriptor instanceof XmlNSDescriptorImpl) {
        return ((XmlNSDescriptorImpl) nsDescriptor).getAttributeImpl(localName, namespace, visited);
    }
    if (visited == null)
        visited = new HashSet<>(1);
    else if (visited.contains(myTag))
        return null;
    visited.add(myTag);
    XmlTag[] tags = myTag.getSubTags();
    for (XmlTag tag : tags) {
        if (equalsToSchemaName(tag, ATTRIBUTE_TAG_NAME)) {
            String name = tag.getAttributeValue("name");
            if (name != null) {
                if (checkElementNameEquivalence(localName, namespace, name, tag)) {
                    return createAttributeDescriptor(tag);
                }
            }
        } else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (equalsToSchemaName(tag, IMPORT_TAG_NAME) && namespace.equals(tag.getAttributeValue("namespace")))) {
            final String schemaLocation = tag.getAttributeValue("schemaLocation");
            if (schemaLocation != null) {
                final XmlFile xmlFile = XmlUtil.findNamespace(myTag.getContainingFile(), schemaLocation);
                if (xmlFile != null) {
                    final XmlDocument includedDocument = xmlFile.getDocument();
                    if (includedDocument != null) {
                        final PsiMetaData data = includedDocument.getMetaData();
                        if (data instanceof XmlNSDescriptorImpl) {
                            final XmlAttributeDescriptor attributeDescriptor = ((XmlNSDescriptorImpl) data).getAttributeImpl(localName, namespace, visited);
                            if (attributeDescriptor != null) {
                                final CachedValue<XmlAttributeDescriptor> value = CachedValuesManager.getManager(includedDocument.getProject()).createCachedValue(() -> {
                                    Object[] deps = attributeDescriptor.getDependences();
                                    if (deps.length == 0) {
                                        LOG.error(attributeDescriptor + " (" + attributeDescriptor.getClass() + ") returned no dependencies");
                                    }
                                    return new CachedValueProvider.Result<>(attributeDescriptor, deps);
                                }, false);
                                return value.getValue();
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : CachedValueProvider(com.intellij.psi.util.CachedValueProvider) XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument) CachedValue(com.intellij.psi.util.CachedValue) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) PsiMetaData(com.intellij.psi.meta.PsiMetaData) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) THashSet(gnu.trove.THashSet) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with CachedValue

use of com.intellij.psi.util.CachedValue in project intellij-community by JetBrains.

the class XmlDocumentImpl method getDefaultNSDescriptor.

@Override
public XmlNSDescriptor getDefaultNSDescriptor(final String namespace, final boolean strict) {
    long curExtResourcesModCount = ExternalResourceManagerEx.getInstanceEx().getModificationCount(getProject());
    if (myExtResourcesModCount != curExtResourcesModCount) {
        myDefaultDescriptorsCacheNotStrict.clear();
        myDefaultDescriptorsCacheStrict.clear();
        myExtResourcesModCount = curExtResourcesModCount;
    }
    final ConcurrentMap<String, CachedValue<XmlNSDescriptor>> defaultDescriptorsCache;
    if (strict) {
        defaultDescriptorsCache = myDefaultDescriptorsCacheStrict;
    } else {
        defaultDescriptorsCache = myDefaultDescriptorsCacheNotStrict;
    }
    CachedValue<XmlNSDescriptor> cachedValue = defaultDescriptorsCache.get(namespace);
    if (cachedValue == null) {
        defaultDescriptorsCache.put(namespace, cachedValue = new PsiCachedValueImpl<>(getManager(), () -> {
            final XmlNSDescriptor defaultNSDescriptorInner = getDefaultNSDescriptorInner(namespace, strict);
            if (isGeneratedFromDtd(defaultNSDescriptorInner)) {
                return new CachedValueProvider.Result<>(defaultNSDescriptorInner, this, ExternalResourceManager.getInstance());
            }
            return new CachedValueProvider.Result<>(defaultNSDescriptorInner, defaultNSDescriptorInner != null ? defaultNSDescriptorInner.getDependences() : ExternalResourceManager.getInstance());
        }));
    }
    return cachedValue.getValue();
}
Also used : PsiCachedValueImpl(com.intellij.psi.impl.PsiCachedValueImpl) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) CachedValue(com.intellij.psi.util.CachedValue)

Example 5 with CachedValue

use of com.intellij.psi.util.CachedValue 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)

Aggregations

CachedValue (com.intellij.psi.util.CachedValue)9 NotNull (org.jetbrains.annotations.NotNull)4 CachedValueProvider (com.intellij.psi.util.CachedValueProvider)3 CachedValuesManager (com.intellij.psi.util.CachedValuesManager)3 XmlFile (com.intellij.psi.xml.XmlFile)3 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 XmlDocument (com.intellij.psi.xml.XmlDocument)2 XmlTag (com.intellij.psi.xml.XmlTag)2 Nullable (org.jetbrains.annotations.Nullable)2 GoBuildTargetSettings (com.goide.project.GoBuildTargetSettings)1 GoTestFinder (com.goide.runconfig.testing.GoTestFinder)1 Filter (com.intellij.execution.filters.Filter)1 RegexpFilter (com.intellij.execution.filters.RegexpFilter)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Module (com.intellij.openapi.module.Module)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 Project (com.intellij.openapi.project.Project)1 ContentIterator (com.intellij.openapi.roots.ContentIterator)1 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)1