blob: c3a8887891a1af5ce1d51debeef4c1c6c7af28d2 (
plain) (
blame)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.intellij.schema.findUsages;
import ai.vespa.intellij.schema.model.Function;
import ai.vespa.intellij.schema.model.RankProfile;
import ai.vespa.intellij.schema.model.Schema;
import ai.vespa.intellij.schema.psi.SdFunctionDefinition;
import ai.vespa.intellij.schema.psi.SdRankProfileDefinition;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.progress.ProgressIndicatorProvider;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.usageView.UsageInfo;
import com.intellij.util.Processor;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* An instance created to find usages of a function once.
*
* @author bratseth
*/
public class FunctionUsageFinder extends UsageFinder {
private final SdFunctionDefinition functionToFind;
private final String functionNameToFind;
private final Set<RankProfile> visited = new HashSet<>();
public FunctionUsageFinder(SdFunctionDefinition functionToFind, SearchScope scope, Processor<? super UsageInfo> processor) {
super(scope, processor);
this.functionToFind = functionToFind;
this.functionNameToFind = ReadAction.compute(functionToFind::getName);
}
/**
* Finds usages brute force. There is built-in search functionality in the IntelliJ SDK but I could
* not make it work across files. Since the lexical, scope of a rank profile will be quite small
* brute force might be faster in any case.
*
* Since search is done by a separate thread it cannot safely access the Psi tree.
* This splits Psi tree accesses into smaller chunks which are handed off to the Reader
* on the assumption that this keeps the IDE responsive. I have not found documentation
* on that.
*/
public void findUsages() {
Schema schema = ReadAction.compute(() -> resolveSchema(functionToFind));
var rankProfile = ReadAction.compute(() -> schema.rankProfiles()
.get(PsiTreeUtil.getParentOfType(functionToFind, SdRankProfileDefinition.class).getName()));
findUsagesBelow(rankProfile);
}
private void findUsagesBelow(RankProfile profile) {
ProgressIndicatorProvider.checkCanceled();
if ( ! visited.add(profile)) return;
ReadAction.compute(() -> findUsagesIn(profile));
for (var child : ReadAction.compute(() -> profile.children()))
findUsagesBelow(child);
}
private boolean findUsagesIn(RankProfile profile) {
if ( ! scope().contains(profile.definition().getContainingFile().getVirtualFile())) return false;
Collection<List<Function>> functions = ReadAction.compute(() -> profile.definedFunctions().values());
for (var functionList : functions) {
for (var function : functionList) {
var matchingVisitor = new MatchingVisitor(functionNameToFind,
functionToFind == function.definition(),
processor());
ReadAction.compute(() -> { function.definition().accept(matchingVisitor); return null; } );
}
}
return true;
}
private static class MatchingVisitor extends PsiElementVisitor {
private final String textToMatch;
private final Processor<? super UsageInfo> processor;
private boolean skipNextMatch;
public MatchingVisitor(String textToMatch, boolean skipFirstMatch, Processor<? super UsageInfo> processor) {
this.textToMatch = textToMatch;
this.skipNextMatch = skipFirstMatch;
this.processor = processor;
}
@Override
public void visitElement(PsiElement element) {
if (element instanceof LeafPsiElement)
visitThis(element);
else
element.acceptChildren(this);
}
private void visitThis(PsiElement element) {
if ( ! textToMatch.equals(element.getText())) return;
if (skipNextMatch) {
skipNextMatch = false;
return;
}
processor.process(new UsageInfo(element));
}
}
}
|