Search in sources :

Example 1 with EquivalenceDescriptor

use of com.intellij.dupLocator.equivalence.EquivalenceDescriptor in project intellij-community by JetBrains.

the class DuplicatesMatchingVisitor method match.

@Override
public boolean match(PsiElement element1, PsiElement element2) {
    if (element1 == null || element2 == null) {
        return element1 == element2;
    }
    if (myDiscardCost > 0) {
        final int cost1 = myTreeHasher.hash(element1, null, myNodeSpecificHasher).getCost();
        final int cost2 = myTreeHasher.hash(element2, null, myNodeSpecificHasher).getCost();
        if (cost1 < myDiscardCost || cost2 < myDiscardCost) {
            return true;
        }
    }
    final DuplicatesProfileBase duplicatesProfile = myNodeSpecificHasher.getDuplicatesProfile();
    final PsiElementRole role1 = duplicatesProfile.getRole(element1);
    final PsiElementRole role2 = duplicatesProfile.getRole(element2);
    final Set<PsiElementRole> skippedRoles = EnumSet.noneOf(PsiElementRole.class);
    final ExternalizableDuplocatorState duplocatorState = duplicatesProfile.getDuplocatorState(duplicatesProfile.getLanguage(element1));
    for (PsiElementRole role : PsiElementRole.values()) {
        if (!duplocatorState.distinguishRole(role)) {
            skippedRoles.add(role);
        }
    }
    if (role1 == role2 && skippedRoles.contains(role1)) {
        return true;
    }
    final EquivalenceDescriptorProvider descriptorProvider = EquivalenceDescriptorProvider.getInstance(element1);
    EquivalenceDescriptor descriptor1 = descriptorProvider != null ? descriptorProvider.buildDescriptor(element1) : null;
    EquivalenceDescriptor descriptor2 = descriptorProvider != null ? descriptorProvider.buildDescriptor(element2) : null;
    PsiElement newElement1 = DuplocatorUtil.skipNodeIfNeccessary(element1, descriptor1, myNodeFilter);
    PsiElement newElement2 = DuplocatorUtil.skipNodeIfNeccessary(element2, descriptor2, myNodeFilter);
    if (newElement1 != element1 || newElement2 != element2) {
        return match(newElement1, newElement2);
    }
    if (!element1.getClass().equals(element2.getClass())) {
        return false;
    }
    if (descriptor1 != null && descriptor2 != null) {
        return DuplocatorUtil.match(descriptor1, descriptor2, this, skippedRoles, duplicatesProfile);
    }
    if (element1 instanceof LeafElement) {
        IElementType elementType1 = ((LeafElement) element1).getElementType();
        IElementType elementType2 = ((LeafElement) element2).getElementType();
        if (!duplocatorState.distinguishLiterals() && duplicatesProfile.getLiterals().contains(elementType1) && duplicatesProfile.getLiterals().contains(elementType2)) {
            return true;
        }
        return element1.getText().equals(element2.getText());
    }
    if (element1.getFirstChild() == null && element1.getTextLength() == 0) {
        return element2.getFirstChild() == null && element2.getTextLength() == 0;
    }
    return matchSequentially(new FilteringNodeIterator(new SiblingNodeIterator(element1.getFirstChild()), getNodeFilter()), new FilteringNodeIterator(new SiblingNodeIterator(element2.getFirstChild()), getNodeFilter()));
}
Also used : IElementType(com.intellij.psi.tree.IElementType) EquivalenceDescriptorProvider(com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider) SiblingNodeIterator(com.intellij.dupLocator.iterators.SiblingNodeIterator) FilteringNodeIterator(com.intellij.dupLocator.iterators.FilteringNodeIterator) EquivalenceDescriptor(com.intellij.dupLocator.equivalence.EquivalenceDescriptor) PsiElement(com.intellij.psi.PsiElement) LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 2 with EquivalenceDescriptor

use of com.intellij.dupLocator.equivalence.EquivalenceDescriptor in project intellij-community by JetBrains.

the class TreeHasherBase method computeHash.

private TreeHashResult computeHash(PsiElement root, PsiFragment upper, NodeSpecificHasher hasher) {
    final EquivalenceDescriptorProvider descriptorProvider = EquivalenceDescriptorProvider.getInstance(root);
    if (descriptorProvider != null) {
        final EquivalenceDescriptor descriptor = descriptorProvider.buildDescriptor(root);
        if (descriptor != null) {
            return computeHash(root, upper, descriptor, hasher);
        }
    }
    if (root instanceof PsiFile) {
        final List<PsiElement> children = hasher.getNodeChildren(root);
        if (children.size() <= 20) {
            return hashCodeBlock(children, upper, hasher, true);
        }
    }
    final NodeSpecificHasherBase ssrNodeSpecificHasher = (NodeSpecificHasherBase) hasher;
    if (shouldBeAnonymized(root, ssrNodeSpecificHasher)) {
        return computeElementHash(root, upper, hasher);
    }
    if (myForIndexing) {
        return computeElementHash(root, upper, hasher);
    }
    final PsiElement element = DuplocatorUtil.getOnlyChild(root, ssrNodeSpecificHasher.getNodeFilter());
    if (element != root) {
        final TreeHashResult result = hash(element, upper, hasher);
        final int cost = hasher.getNodeCost(root);
        return new TreeHashResult(result.getHash(), result.getCost() + cost, result.getFragment());
    }
    return computeElementHash(element, upper, hasher);
}
Also used : EquivalenceDescriptorProvider(com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider) EquivalenceDescriptor(com.intellij.dupLocator.equivalence.EquivalenceDescriptor) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Aggregations

EquivalenceDescriptor (com.intellij.dupLocator.equivalence.EquivalenceDescriptor)2 EquivalenceDescriptorProvider (com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider)2 PsiElement (com.intellij.psi.PsiElement)2 FilteringNodeIterator (com.intellij.dupLocator.iterators.FilteringNodeIterator)1 SiblingNodeIterator (com.intellij.dupLocator.iterators.SiblingNodeIterator)1 PsiFile (com.intellij.psi.PsiFile)1 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)1 IElementType (com.intellij.psi.tree.IElementType)1