Search in sources :

Example 1 with EquivalenceDescriptorProvider

use of com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider 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 EquivalenceDescriptorProvider

use of com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider 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)

Example 3 with EquivalenceDescriptorProvider

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

the class DuplocatorUtil method isIgnoredNode.

public static boolean isIgnoredNode(PsiElement element) {
    if (element instanceof PsiWhiteSpace || element instanceof PsiErrorElement || element instanceof PsiComment) {
        return true;
    }
    if (!(element instanceof LeafElement)) {
        return false;
    }
    if (CharArrayUtil.containsOnlyWhiteSpaces(element.getText())) {
        return true;
    }
    EquivalenceDescriptorProvider descriptorProvider = EquivalenceDescriptorProvider.getInstance(element);
    if (descriptorProvider == null) {
        return false;
    }
    final IElementType elementType = ((LeafElement) element).getElementType();
    return descriptorProvider.getIgnoredTokens().contains(elementType);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiErrorElement(com.intellij.psi.PsiErrorElement) PsiComment(com.intellij.psi.PsiComment) EquivalenceDescriptorProvider(com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

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