|
12 | 12 |
|
13 | 13 | import java.util.Objects; |
14 | 14 |
|
| 15 | +import org.eclipse.core.runtime.CoreException; |
| 16 | +import org.eclipse.jdt.core.IJavaElement; |
| 17 | +import org.eclipse.jdt.core.dom.ASTNode; |
15 | 18 | import org.eclipse.jdt.core.dom.ChildPropertyDescriptor; |
16 | 19 | import org.eclipse.jdt.core.dom.Expression; |
17 | 20 | import org.eclipse.jdt.core.dom.IBinding; |
|
20 | 23 | import org.eclipse.jdt.core.dom.QualifiedName; |
21 | 24 | import org.eclipse.jdt.core.dom.SimpleName; |
22 | 25 | import org.eclipse.jdt.core.dom.VariableDeclaration; |
| 26 | +import org.eclipse.jdt.core.search.SearchMatch; |
23 | 27 | import org.eclipse.jdt.internal.core.LocalVariable; |
24 | 28 | import org.eclipse.jdt.internal.core.search.LocatorResponse; |
25 | 29 |
|
@@ -59,28 +63,71 @@ public LocatorResponse resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBind |
59 | 63 | return toResponse(IMPOSSIBLE_MATCH); |
60 | 64 | } |
61 | 65 | if (Objects.equals(binding.getJavaElement(), getLocalVariable())) { |
62 | | - return toResponse(ACCURATE_MATCH); |
| 66 | + // We need to know if this is a reference request or a declaration request |
| 67 | + if (this.locator.pattern.findReferences) { |
| 68 | + return new LocatorResponse(ACCURATE_MATCH, false, node, false, false); |
| 69 | + } else if (this.locator.pattern.findDeclarations) { |
| 70 | + // we need to make sure the node has a VariableDeclaration in its ancestry |
| 71 | + boolean isDecl = hasVariableDeclarationAncestor(node); |
| 72 | + if( isDecl) { |
| 73 | + return new LocatorResponse(ACCURATE_MATCH, false, node, false, false); |
| 74 | + } |
| 75 | + return toResponse(IMPOSSIBLE_MATCH); |
| 76 | + } |
63 | 77 | } |
64 | 78 | return toResponse(INACCURATE_MATCH); |
65 | 79 | } |
66 | 80 |
|
| 81 | + private boolean hasVariableDeclarationAncestor(ASTNode node) { |
| 82 | + ASTNode working = node; |
| 83 | + while(working != null ) { |
| 84 | + if( working instanceof VariableDeclaration) { |
| 85 | + return true; |
| 86 | + } |
| 87 | + working = working.getParent(); |
| 88 | + } |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
67 | 92 | @Override |
68 | 93 | public LocatorResponse match(VariableDeclaration node, NodeSetWrapper nodeSet, MatchLocator locator) { |
69 | | - int referencesLevel = IMPOSSIBLE_MATCH; |
70 | | - if (this.locator.pattern.findReferences) |
| 94 | + int defaultLevelOnMatch = this.locator.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH; |
| 95 | + if (this.locator.pattern.findReferences) { |
71 | 96 | // must be a write only access with an initializer |
72 | | - if (this.locator.pattern.writeAccess && !this.locator.pattern.readAccess && node.getInitializer() != null) |
73 | | - if (this.locator.matchesName(this.locator.pattern.name, node.getName().getIdentifier().toCharArray())) |
74 | | - referencesLevel = this.locator.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH; |
75 | | - |
76 | | - int declarationsLevel = IMPOSSIBLE_MATCH; |
77 | | - if (this.locator.pattern.findDeclarations) |
78 | | - if (this.locator.matchesName(this.locator.pattern.name, node.getName().getIdentifier().toCharArray())) |
79 | | - if (getLocalVariable() != null && node.getStartPosition() == getLocalVariable().declarationSourceStart) |
80 | | - declarationsLevel = this.locator.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH; |
81 | | - |
82 | | - // Use the stronger match |
83 | | - int level = nodeSet.addMatch(node, referencesLevel >= declarationsLevel ? referencesLevel : declarationsLevel); |
84 | | - return toResponse(level, true); |
| 97 | + if (this.locator.pattern.writeAccess && !this.locator.pattern.readAccess && node.getInitializer() != null) { |
| 98 | + if (this.locator.matchesName(this.locator.pattern.name, node.getName().getIdentifier().toCharArray())) { |
| 99 | + return toResponse(defaultLevelOnMatch, false); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (this.locator.pattern.findDeclarations) { |
| 105 | + if (this.locator.matchesName(this.locator.pattern.name, node.getName().getIdentifier().toCharArray())) { |
| 106 | + LocalVariable lvFromPattern = getLocalVariable(); |
| 107 | + if (lvFromPattern != null ) { |
| 108 | + if(node.getStartPosition() == lvFromPattern.declarationSourceStart) { |
| 109 | + return toResponse(defaultLevelOnMatch, false); |
| 110 | + } else if( node.getName().getStartPosition() == lvFromPattern.nameStart) { |
| 111 | + return new LocatorResponse(defaultLevelOnMatch, true, node.getName(), false, false); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return toResponse(0, false); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void reportSearchMatch(MatchLocator locator, ASTNode node, SearchMatch match) throws CoreException { |
| 121 | + if(this.locator.pattern.findDeclarations && hasVariableDeclarationAncestor(node) ) { |
| 122 | + LocalVariable localVariable = getLocalVariable(); |
| 123 | + int offset = localVariable.nameStart; |
| 124 | + int length = localVariable.nameEnd-offset+1; |
| 125 | + IJavaElement element = localVariable; |
| 126 | + SearchMatch newMatch = locator.newDeclarationMatch(element, null, match.getAccuracy(), offset, length); |
| 127 | + SearchMatchingUtility.reportSearchMatch(locator, newMatch); |
| 128 | + } else { |
| 129 | + SearchMatchingUtility.reportSearchMatch(locator, match); |
| 130 | + } |
85 | 131 | } |
| 132 | + |
86 | 133 | } |
0 commit comments