-
Notifications
You must be signed in to change notification settings - Fork 31
/
DeletionMarkingVisitor.java
99 lines (85 loc) · 2.76 KB
/
DeletionMarkingVisitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package name.admitriev.jhelper.generation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.jetbrains.cidr.lang.psi.OCCppNamespace;
import com.jetbrains.cidr.lang.psi.OCDeclaration;
import com.jetbrains.cidr.lang.psi.OCDeclarator;
import com.jetbrains.cidr.lang.psi.OCElement;
import com.jetbrains.cidr.lang.psi.OCFunctionDefinition;
import com.jetbrains.cidr.lang.psi.OCStructLike;
import com.jetbrains.cidr.lang.psi.OCTypeElement;
import com.jetbrains.cidr.lang.psi.impl.OCDefineDirectiveImpl;
import com.jetbrains.cidr.lang.psi.visitors.OCVisitor;
import java.util.Collection;
public class DeletionMarkingVisitor extends OCVisitor {
private final Collection<PsiElement> toDelete;
private SearchScope searchScope;
public DeletionMarkingVisitor(Collection<PsiElement> toDelete, SearchScope searchScope) {
//noinspection AssignmentToCollectionOrArrayFieldFromParameter
this.toDelete = toDelete;
this.searchScope = searchScope;
}
private static boolean isParentFor(OCElement potentialParent, PsiElement potentialChild) {
while (potentialChild != null) {
//noinspection ObjectEquality
if (potentialChild == potentialParent) {
return true;
}
potentialChild = potentialChild.getParent();
}
return false;
}
@Override
public void visitFunctionDefinition(OCFunctionDefinition functionDefinition) {
String name = functionDefinition.getName();
if ("main".equals(name)) {
return;
}
if (name != null && name.startsWith("operator")) {
// To workaround a fact that no usages found for operators
return;
}
super.visitFunctionDefinition(functionDefinition);
}
private void removeIfNoReference(OCElement element) {
for (PsiReference reference : ReferencesSearch.search(element, searchScope)) {
PsiElement referenceElement = reference.getElement();
if (!isParentFor(element, referenceElement)) {
return;
}
}
toDelete.add(element);
}
@Override
public void visitNamespace(OCCppNamespace namespace) {
if (namespace.getChildren().length == 0) {
toDelete.add(namespace);
}
else {
namespace.acceptChildren(this);
}
}
@Override
public void visitDefineDirective(OCDefineDirectiveImpl directive) {
removeIfNoReference(directive);
}
@Override
public void visitDeclarator(OCDeclarator declarator) {
removeIfNoReference(declarator);
}
@Override
public void visitStructLike(OCStructLike structLike) {
removeIfNoReference(structLike);
structLike.acceptChildren(this);
}
@Override
public void visitTypeElement(OCTypeElement typeElement) {
typeElement.acceptChildren(this);
}
@Override
public void visitDeclaration(OCDeclaration declaration) {
declaration.acceptChildren(this);
}
}