From 512cfb28ab7944af022a608789195aa577e850ae Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 23 May 2013 21:39:16 +0200 Subject: [PATCH] extracted a bunch of code from XmlEntityRefImpl into XmlEntityCache --- .../impl/quickfix/FetchExtResourceAction.java | 4 +- .../source/xml/XmlConditionalSectionImpl.java | 2 +- .../psi/impl/source/xml/XmlEntityRefImpl.java | 54 +-------------- .../src/com/intellij/xml/util/XmlPsiUtil.java | 6 +- .../src/com/intellij/xml/util/XmlUtil.java | 3 +- .../psi/impl/source/xml/XmlEntityCache.java | 65 +++++++++++++++++++ 6 files changed, 74 insertions(+), 60 deletions(-) create mode 100644 xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlEntityCache.java diff --git a/xml/impl/src/com/intellij/codeInsight/daemon/impl/quickfix/FetchExtResourceAction.java b/xml/impl/src/com/intellij/codeInsight/daemon/impl/quickfix/FetchExtResourceAction.java index c86533c23e21..8e6a4ff00d16 100644 --- a/xml/impl/src/com/intellij/codeInsight/daemon/impl/quickfix/FetchExtResourceAction.java +++ b/xml/impl/src/com/intellij/codeInsight/daemon/impl/quickfix/FetchExtResourceAction.java @@ -41,7 +41,7 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; import com.intellij.psi.PsiReference; -import com.intellij.psi.impl.source.xml.XmlEntityRefImpl; +import com.intellij.psi.impl.source.xml.XmlEntityCache; import com.intellij.psi.search.PsiElementProcessor; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.xml.*; @@ -395,7 +395,7 @@ public class FetchExtResourceAction extends BaseExtResourceAction implements Wat private static Set extractEmbeddedFileReferences(XmlFile file, XmlFile context, final String url) { final Set result = new LinkedHashSet(); if (context != null) { - XmlEntityRefImpl.copyEntityCaches(file, context); + XmlEntityCache.copyEntityCaches(file, context); } XmlUtil.processXmlElements( diff --git a/xml/impl/src/com/intellij/psi/impl/source/xml/XmlConditionalSectionImpl.java b/xml/impl/src/com/intellij/psi/impl/source/xml/XmlConditionalSectionImpl.java index 426e221d50c4..26c0bec1f622 100644 --- a/xml/impl/src/com/intellij/psi/impl/source/xml/XmlConditionalSectionImpl.java +++ b/xml/impl/src/com/intellij/psi/impl/source/xml/XmlConditionalSectionImpl.java @@ -52,7 +52,7 @@ public class XmlConditionalSectionImpl extends XmlElementImpl implements XmlCond final String text = xmlEntityRef.getText(); String name = text.substring(1,text.length() - 1); - PsiElement psiElement = targetFile != null ? XmlEntityRefImpl.getCachedEntity( targetFile, name): null; + PsiElement psiElement = targetFile != null ? XmlEntityCache.getCachedEntity(targetFile, name): null; if (psiElement instanceof XmlEntityDecl) { final XmlEntityDecl decl = (XmlEntityDecl)psiElement; diff --git a/xml/impl/src/com/intellij/psi/impl/source/xml/XmlEntityRefImpl.java b/xml/impl/src/com/intellij/psi/impl/source/xml/XmlEntityRefImpl.java index 4bbebb78341b..6d32e94f03aa 100644 --- a/xml/impl/src/com/intellij/psi/impl/source/xml/XmlEntityRefImpl.java +++ b/xml/impl/src/com/intellij/psi/impl/source/xml/XmlEntityRefImpl.java @@ -33,7 +33,6 @@ import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -41,7 +40,6 @@ import java.util.Map; * @author mike */ public class XmlEntityRefImpl extends XmlElementImpl implements XmlEntityRef { - private static final Key>> XML_ENTITY_DECL_MAP = Key.create("XML_ENTITY_DECL_MAP"); @NonNls private static final String GT_ENTITY = ">"; @NonNls private static final String QUOT_ENTITY = """; @@ -57,37 +55,6 @@ public class XmlEntityRefImpl extends XmlElementImpl implements XmlEntityRef { return resolveEntity(this, text, targetFile); } - public static XmlEntityDecl getCachedEntity(PsiFile file, String name) { - CachedValue cachedValue; - synchronized(PsiLock.LOCK) { - final Map> cachingMap = getCachingMap(file); - cachedValue = cachingMap.get(name); - } - return cachedValue != null ? cachedValue.getValue():null; - } - - public static void cacheParticularEntity(PsiFile file, XmlEntityDecl decl) { - synchronized(PsiLock.LOCK) { - final Map> cachingMap = getCachingMap(file); - final String name = decl.getName(); - if (cachingMap.containsKey(name)) return; - final SmartPsiElementPointer declPointer = SmartPointerManager.getInstance(file.getProject()).createSmartPsiElementPointer(decl); - - cachingMap.put( - name, CachedValuesManager.getManager(file.getProject()).createCachedValue(new CachedValueProvider() { - public Result compute() { - PsiElement declElement = declPointer.getElement(); - if (declElement instanceof XmlEntityDecl && declElement.isValid() && name.equals(((XmlEntityDecl)declElement).getName())) - return new Result((XmlEntityDecl)declElement, declElement); - cachingMap.put(name,null); - return new Result(null,null); - } - }, - false - )); - } - } - public static XmlEntityDecl resolveEntity(final XmlElement element, final String text, PsiFile targetFile) { if (targetFile instanceof XmlFile) { XmlDocument document = ((XmlFile)targetFile).getDocument(); @@ -99,7 +66,7 @@ public class XmlEntityRefImpl extends XmlElementImpl implements XmlEntityRef { final PsiElement targetElement = targetFile != null ? targetFile : element; CachedValue value; synchronized(PsiLock.LOCK) { - Map> map = getCachingMap(targetElement); + Map> map = XmlEntityCache.getCachingMap(targetElement); value = map.get(entityName); final PsiFile containingFile = element.getContainingFile(); @@ -122,15 +89,6 @@ public class XmlEntityRefImpl extends XmlElementImpl implements XmlEntityRef { return value.getValue(); } - private static Map> getCachingMap(final PsiElement targetElement) { - Map> map = targetElement.getUserData(XML_ENTITY_DECL_MAP); - if (map == null){ - map = new HashMap>(); - targetElement.putUserData(XML_ENTITY_DECL_MAP, map); - } - return map; - } - private static final Key DISABLE_ENTITY_EXPAND = Key.create("disable.entity.expand"); private static CachedValueProvider.Result resolveEntity(final PsiElement targetElement, final String entityName, PsiFile contextFile) { @@ -253,16 +211,6 @@ public class XmlEntityRefImpl extends XmlElementImpl implements XmlEntityRef { } } - public static void copyEntityCaches(final PsiFile file, final PsiFile context) { - synchronized (PsiLock.LOCK) { - final Map> cachingMap = getCachingMap(file); - for(Map.Entry> entry:getCachingMap(context).entrySet()) { - cachingMap.put(entry.getKey(), entry.getValue()); - } - } - - } - public static void setNoEntityExpandOutOfDocument(XmlDocument doc, boolean b) { if (b) doc.putUserData(DISABLE_ENTITY_EXPAND, Boolean.TRUE); else doc.putUserData(DISABLE_ENTITY_EXPAND, null); diff --git a/xml/impl/src/com/intellij/xml/util/XmlPsiUtil.java b/xml/impl/src/com/intellij/xml/util/XmlPsiUtil.java index b86d6e0da7fb..57cff1c08069 100644 --- a/xml/impl/src/com/intellij/xml/util/XmlPsiUtil.java +++ b/xml/impl/src/com/intellij/xml/util/XmlPsiUtil.java @@ -4,7 +4,7 @@ import com.intellij.openapi.util.Key; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiLock; -import com.intellij.psi.impl.source.xml.XmlEntityRefImpl; +import com.intellij.psi.impl.source.xml.XmlEntityCache; import com.intellij.psi.search.PsiElementProcessor; import com.intellij.psi.util.CachedValue; import com.intellij.psi.util.CachedValueProvider; @@ -118,7 +118,7 @@ public class XmlPsiUtil { } if (targetFile != null && child instanceof XmlEntityDecl) { XmlEntityDecl xmlEntityDecl = (XmlEntityDecl)child; - XmlEntityRefImpl.cacheParticularEntity(targetFile, xmlEntityDecl); + XmlEntityCache.cacheParticularEntity(targetFile, xmlEntityDecl); } return true; } @@ -207,7 +207,7 @@ public class XmlPsiUtil { public Result compute() { final PsiElement res = entityDecl.parse(targetFile, type, entityRef); if (res == null) return new Result(res, targetFile); - if (!entityDecl.isInternalReference()) XmlEntityRefImpl.copyEntityCaches(res.getContainingFile(), targetFile); + if (!entityDecl.isInternalReference()) XmlEntityCache.copyEntityCaches(res.getContainingFile(), targetFile); return new Result(res, res.getUserData(XmlElement.DEPENDING_ELEMENT), entityDecl, targetFile, entityRef); } }, false); diff --git a/xml/impl/src/com/intellij/xml/util/XmlUtil.java b/xml/impl/src/com/intellij/xml/util/XmlUtil.java index 24dafa15c7ba..ca79e8801a3d 100644 --- a/xml/impl/src/com/intellij/xml/util/XmlUtil.java +++ b/xml/impl/src/com/intellij/xml/util/XmlUtil.java @@ -51,6 +51,7 @@ import com.intellij.psi.filters.position.FilterPattern; import com.intellij.psi.impl.source.html.HtmlDocumentImpl; import com.intellij.psi.impl.source.tree.CompositeElement; import com.intellij.psi.impl.source.tree.LeafElement; +import com.intellij.psi.impl.source.xml.XmlEntityCache; import com.intellij.psi.impl.source.xml.XmlEntityRefImpl; import com.intellij.psi.scope.processor.FilterElementProcessor; import com.intellij.psi.search.PsiElementProcessor; @@ -1057,7 +1058,7 @@ public class XmlUtil { if (currentElement != null) { final String name = _element.getName(); if (_element instanceof XmlEntityDecl) { - final XmlEntityDecl cachedEntity = XmlEntityRefImpl.getCachedEntity((PsiFile)currentElement, name); + final XmlEntityDecl cachedEntity = XmlEntityCache.getCachedEntity((PsiFile)currentElement, name); if (cachedEntity != null) return cachedEntity; } diff --git a/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlEntityCache.java b/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlEntityCache.java new file mode 100644 index 000000000000..b361463749a3 --- /dev/null +++ b/xml/xml-psi-impl/src/com/intellij/psi/impl/source/xml/XmlEntityCache.java @@ -0,0 +1,65 @@ +package com.intellij.psi.impl.source.xml; + +import com.intellij.openapi.util.Key; +import com.intellij.psi.*; +import com.intellij.psi.util.CachedValue; +import com.intellij.psi.util.CachedValueProvider; +import com.intellij.psi.util.CachedValuesManager; +import com.intellij.psi.xml.XmlEntityDecl; + +import java.util.HashMap; +import java.util.Map; + +public class XmlEntityCache { + private static final Key>> XML_ENTITY_DECL_MAP = Key.create("XML_ENTITY_DECL_MAP"); + + public static void cacheParticularEntity(PsiFile file, XmlEntityDecl decl) { + synchronized(PsiLock.LOCK) { + final Map> cachingMap = getCachingMap(file); + final String name = decl.getName(); + if (cachingMap.containsKey(name)) return; + final SmartPsiElementPointer declPointer = SmartPointerManager.getInstance(file.getProject()).createSmartPsiElementPointer(decl); + + cachingMap.put( + name, CachedValuesManager.getManager(file.getProject()).createCachedValue(new CachedValueProvider() { + public Result compute() { + PsiElement declElement = declPointer.getElement(); + if (declElement instanceof XmlEntityDecl && declElement.isValid() && name.equals(((XmlEntityDecl)declElement).getName())) + return new Result((XmlEntityDecl)declElement, declElement); + cachingMap.put(name,null); + return new Result(null,null); + } + }, + false + )); + } + } + + static Map> getCachingMap(final PsiElement targetElement) { + Map> map = targetElement.getUserData(XML_ENTITY_DECL_MAP); + if (map == null){ + map = new HashMap>(); + targetElement.putUserData(XML_ENTITY_DECL_MAP, map); + } + return map; + } + + public static void copyEntityCaches(final PsiFile file, final PsiFile context) { + synchronized (PsiLock.LOCK) { + final Map> cachingMap = getCachingMap(file); + for(Map.Entry> entry:getCachingMap(context).entrySet()) { + cachingMap.put(entry.getKey(), entry.getValue()); + } + } + + } + + public static XmlEntityDecl getCachedEntity(PsiFile file, String name) { + CachedValue cachedValue; + synchronized(PsiLock.LOCK) { + final Map> cachingMap = getCachingMap(file); + cachedValue = cachingMap.get(name); + } + return cachedValue != null ? cachedValue.getValue():null; + } +}