vue: avoid recursion in getting tag's descriptor

- reuse solution from angular, extract code into common place
- in XmlTagImpl, use RecursionGuard because it is very likely to have such problems with some other tag & descriptor combinations

EA-116625 - SOE: FlexMxmlNSDescriptor.getElementDescriptor
This commit is contained in:
Irina.Chernushina
2018-03-22 09:35:05 +01:00
parent 8bcd17315c
commit 975aa31856
2 changed files with 55 additions and 18 deletions
@@ -0,0 +1,45 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.psi.impl.source.xml;
import com.intellij.html.impl.DelegatingRelaxedHtmlElementDescriptor;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlDocument;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.xml.XmlElementDescriptor;
import com.intellij.xml.XmlNSDescriptor;
import com.intellij.xml.XmlNSDescriptorEx;
import org.jetbrains.annotations.NotNull;
import static com.intellij.xml.XmlElementDescriptor.EMPTY_ARRAY;
/**
* @author Irina.Chernushina on 3/21/2018.
*/
public class XmlDescriptorUtil {
public static XmlElementDescriptor[] getElementsDescriptors(XmlTag context) {
XmlDocumentImpl xmlDocument = PsiTreeUtil.getParentOfType(context, XmlDocumentImpl.class);
if (xmlDocument == null) return EMPTY_ARRAY;
return ContainerUtil.map2Array(xmlDocument.getRootTagNSDescriptor().getRootElementsDescriptors(xmlDocument),
XmlElementDescriptor.class, descriptor -> wrapInDelegating(descriptor));
}
public static XmlElementDescriptor getElementDescriptor(XmlTag childTag, XmlTag contextTag) {
final XmlDocument document = PsiTreeUtil.getParentOfType(contextTag, XmlDocument.class);
if (document == null) {
return null;
}
final XmlNSDescriptor nsDescriptor = document.getDefaultNSDescriptor(childTag.getNamespace(), true);
if (nsDescriptor instanceof XmlNSDescriptorEx) {
XmlElementDescriptor descriptor = ((XmlNSDescriptorEx)nsDescriptor).getElementDescriptor(childTag.getLocalName(), childTag.getNamespace());
return descriptor != null ? wrapInDelegating(descriptor) : null;
}
return null;
}
@NotNull
public static DelegatingRelaxedHtmlElementDescriptor wrapInDelegating(XmlElementDescriptor descriptor) {
return descriptor instanceof DelegatingRelaxedHtmlElementDescriptor ? (DelegatingRelaxedHtmlElementDescriptor)descriptor :
new DelegatingRelaxedHtmlElementDescriptor(descriptor);
}
}
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.psi.impl.source.xml;
import com.intellij.javaee.ExternalResourceManager;
@@ -494,9 +480,15 @@ public class XmlTagImpl extends XmlElementImpl implements XmlTag, HintedReferenc
@Override
public XmlElementDescriptor getDescriptor() {
return CachedValuesManager.getCachedValue(this, () ->
Result.create(computeElementDescriptor(),
PsiModificationTracker.MODIFICATION_COUNT, externalResourceModificationTracker()));
return CachedValuesManager.getCachedValue(this, () -> {
final RecursionGuard.StackStamp stamp = ourGuard.markStack();
final XmlElementDescriptor descriptor = ourGuard.doPreventingRecursion(this, true, this::computeElementDescriptor);
if (stamp.mayCacheNow()) {
return Result.create(descriptor, PsiModificationTracker.MODIFICATION_COUNT, externalResourceModificationTracker());
}
// = do not cache
return Result.create(descriptor, ModificationTracker.EVER_CHANGED);
});
}
private ModificationTracker externalResourceModificationTracker() {