extracted a bunch of code from XmlEntityRefImpl into XmlEntityCache

This commit is contained in:
Dmitry Jemerov
2013-05-24 18:01:13 +02:00
parent 77af7af646
commit 512cfb28ab
6 changed files with 74 additions and 60 deletions
@@ -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<String> extractEmbeddedFileReferences(XmlFile file, XmlFile context, final String url) {
final Set<String> result = new LinkedHashSet<String>();
if (context != null) {
XmlEntityRefImpl.copyEntityCaches(file, context);
XmlEntityCache.copyEntityCaches(file, context);
}
XmlUtil.processXmlElements(
@@ -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;
@@ -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<Map<String,CachedValue<XmlEntityDecl>>> XML_ENTITY_DECL_MAP = Key.create("XML_ENTITY_DECL_MAP");
@NonNls private static final String GT_ENTITY = "&gt;";
@NonNls private static final String QUOT_ENTITY = "&quot;";
@@ -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<XmlEntityDecl> cachedValue;
synchronized(PsiLock.LOCK) {
final Map<String, CachedValue<XmlEntityDecl>> 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<String, CachedValue<XmlEntityDecl>> 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<XmlEntityDecl>() {
public Result<XmlEntityDecl> compute() {
PsiElement declElement = declPointer.getElement();
if (declElement instanceof XmlEntityDecl && declElement.isValid() && name.equals(((XmlEntityDecl)declElement).getName()))
return new Result<XmlEntityDecl>((XmlEntityDecl)declElement, declElement);
cachingMap.put(name,null);
return new Result<XmlEntityDecl>(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<XmlEntityDecl> value;
synchronized(PsiLock.LOCK) {
Map<String, CachedValue<XmlEntityDecl>> map = getCachingMap(targetElement);
Map<String, CachedValue<XmlEntityDecl>> 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<String, CachedValue<XmlEntityDecl>> getCachingMap(final PsiElement targetElement) {
Map<String, CachedValue<XmlEntityDecl>> map = targetElement.getUserData(XML_ENTITY_DECL_MAP);
if (map == null){
map = new HashMap<String,CachedValue<XmlEntityDecl>>();
targetElement.putUserData(XML_ENTITY_DECL_MAP, map);
}
return map;
}
private static final Key<Boolean> DISABLE_ENTITY_EXPAND = Key.create("disable.entity.expand");
private static CachedValueProvider.Result<XmlEntityDecl> 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<String, CachedValue<XmlEntityDecl>> cachingMap = getCachingMap(file);
for(Map.Entry<String,CachedValue<XmlEntityDecl>> 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);
@@ -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<PsiElement> compute() {
final PsiElement res = entityDecl.parse(targetFile, type, entityRef);
if (res == null) return new Result<PsiElement>(res, targetFile);
if (!entityDecl.isInternalReference()) XmlEntityRefImpl.copyEntityCaches(res.getContainingFile(), targetFile);
if (!entityDecl.isInternalReference()) XmlEntityCache.copyEntityCaches(res.getContainingFile(), targetFile);
return new Result<PsiElement>(res, res.getUserData(XmlElement.DEPENDING_ELEMENT), entityDecl, targetFile, entityRef);
}
}, false);
@@ -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;
}
@@ -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<Map<String,CachedValue<XmlEntityDecl>>> XML_ENTITY_DECL_MAP = Key.create("XML_ENTITY_DECL_MAP");
public static void cacheParticularEntity(PsiFile file, XmlEntityDecl decl) {
synchronized(PsiLock.LOCK) {
final Map<String, CachedValue<XmlEntityDecl>> 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<XmlEntityDecl>() {
public Result<XmlEntityDecl> compute() {
PsiElement declElement = declPointer.getElement();
if (declElement instanceof XmlEntityDecl && declElement.isValid() && name.equals(((XmlEntityDecl)declElement).getName()))
return new Result<XmlEntityDecl>((XmlEntityDecl)declElement, declElement);
cachingMap.put(name,null);
return new Result<XmlEntityDecl>(null,null);
}
},
false
));
}
}
static Map<String, CachedValue<XmlEntityDecl>> getCachingMap(final PsiElement targetElement) {
Map<String, CachedValue<XmlEntityDecl>> map = targetElement.getUserData(XML_ENTITY_DECL_MAP);
if (map == null){
map = new HashMap<String,CachedValue<XmlEntityDecl>>();
targetElement.putUserData(XML_ENTITY_DECL_MAP, map);
}
return map;
}
public static void copyEntityCaches(final PsiFile file, final PsiFile context) {
synchronized (PsiLock.LOCK) {
final Map<String, CachedValue<XmlEntityDecl>> cachingMap = getCachingMap(file);
for(Map.Entry<String,CachedValue<XmlEntityDecl>> entry:getCachingMap(context).entrySet()) {
cachingMap.put(entry.getKey(), entry.getValue());
}
}
}
public static XmlEntityDecl getCachedEntity(PsiFile file, String name) {
CachedValue<XmlEntityDecl> cachedValue;
synchronized(PsiLock.LOCK) {
final Map<String, CachedValue<XmlEntityDecl>> cachingMap = getCachingMap(file);
cachedValue = cachingMap.get(name);
}
return cachedValue != null ? cachedValue.getValue():null;
}
}