下面列出了怎么用com.intellij.psi.impl.cache.CacheManager的API类实例代码及写法,或者点击链接到github查看源代码。
private void assertContainsWords(
VirtualFile file,
@MagicConstant(flagsFromClass = UsageSearchContext.class) short occurenceMask,
String... words) {
for (String word : words) {
VirtualFile[] files =
CacheManager.SERVICE
.getInstance(getProject())
.getVirtualFilesWithWord(
word, occurenceMask, GlobalSearchScope.fileScope(getProject(), file), true);
if (!Arrays.asList(files).contains(file)) {
Assert.fail(String.format("Word '%s' not found in file '%s'", word, file));
}
}
}
private long collectAndCheckHighlighting(@NotNull ExpectedHighlightingDataWrapper data) {
Project project = myFixture.getProject();
EdtTestUtil.runInEdtAndWait(() -> {
PsiDocumentManager.getInstance(project).commitAllDocuments();
});
PsiFileImpl file = (PsiFileImpl)this.getHostFile();
FileElement hardRefToFileElement = file.calcTreeElement();
if (!DumbService.isDumb(project)) {
ServiceManager.getService(project, CacheManager.class).getFilesWithWord("XXX", (short)2, GlobalSearchScope.allScope(project), true);
}
long start = System.currentTimeMillis();
Disposable disposable = Disposer.newDisposable();
List<HighlightInfo> infos;
try {
infos = myFixture.doHighlighting();
this.removeDuplicatedRangesForInjected(infos);
} finally {
Disposer.dispose(disposable);
}
long elapsed = System.currentTimeMillis() - start;
data.checkResultWrapper(file, infos, file.getText());
hardRefToFileElement.hashCode();
return elapsed;
}
private long collectAndCheckHighlighting(@Nonnull ExpectedHighlightingData data) {
final Project project = getProject();
PsiDocumentManager.getInstance(project).commitAllDocuments();
PsiFileImpl file = (PsiFileImpl)getHostFile();
FileElement hardRefToFileElement = file.calcTreeElement();//to load text
//to initialize caches
if (!DumbService.isDumb(project)) {
CacheManager.getInstance(project).getFilesWithWord(XXX, UsageSearchContext.IN_COMMENTS, GlobalSearchScope.allScope(project), true);
}
List<HighlightInfo> infos;
final long start = System.currentTimeMillis();
try {
((PsiManagerImpl)PsiManager.getInstance(project)).setAssertOnFileLoadingFilter(myJavaFilesFilter, myTestRootDisposable);
// ProfilingUtil.startCPUProfiling();
infos = doHighlighting();
removeDuplicatedRangesForInjected(infos);
// ProfilingUtil.captureCPUSnapshot("testing");
}
finally {
((PsiManagerImpl)PsiManager.getInstance(project)).setAssertOnFileLoadingFilter(VirtualFileFilter.NONE, myTestRootDisposable);
}
final long elapsed = System.currentTimeMillis() - start;
data.checkResult(infos, file.getText());
hardRefToFileElement.hashCode(); // use it so gc won't collect it
return elapsed;
}
@Override
@Nonnull
public PsiFile[] findFilesWithPlainTextWords(@Nonnull String word) {
return CacheManager.getInstance(myManager.getProject()).getFilesWithWord(word, UsageSearchContext.IN_PLAIN_TEXT,
GlobalSearchScope.projectScope(myManager.getProject()),
true);
}
@Override
public boolean processAllFilesWithWord(@Nonnull String word,
@Nonnull GlobalSearchScope scope,
@Nonnull Processor<PsiFile> processor,
final boolean caseSensitively) {
return CacheManager.getInstance(myManager.getProject()).processFilesWithWord(processor, word, UsageSearchContext.IN_CODE, scope, caseSensitively);
}
@Override
public boolean processAllFilesWithWordInText(@Nonnull final String word,
@Nonnull final GlobalSearchScope scope,
@Nonnull final Processor<PsiFile> processor,
final boolean caseSensitively) {
return CacheManager.getInstance(myManager.getProject()).processFilesWithWord(processor, word, UsageSearchContext.IN_PLAIN_TEXT, scope, caseSensitively);
}
@Override
public boolean processUsagesInNonJavaFiles(@Nullable final PsiElement originalElement,
@Nonnull String qName,
@Nonnull final PsiNonJavaFileReferenceProcessor processor,
@Nonnull final GlobalSearchScope initialScope) {
if (qName.isEmpty()) {
throw new IllegalArgumentException("Cannot search for elements with empty text. Element: "+originalElement+ "; "+(originalElement == null ? null : originalElement.getClass()));
}
final ProgressIndicator progress = getOrCreateIndicator();
int dotIndex = qName.lastIndexOf('.');
int dollarIndex = qName.lastIndexOf('$');
int maxIndex = Math.max(dotIndex, dollarIndex);
final String wordToSearch = maxIndex >= 0 ? qName.substring(maxIndex + 1) : qName;
ThrowableComputable<GlobalSearchScope, RuntimeException> action1 = () -> {
if (originalElement != null && myManager.isInProject(originalElement) && initialScope.isSearchInLibraries()) {
return initialScope.intersectWith(GlobalSearchScope.projectScope(myManager.getProject()));
}
return initialScope;
};
final GlobalSearchScope theSearchScope = AccessRule.read(action1);
PsiFile[] files = myDumbService.runReadActionInSmartMode(() -> CacheManager.getInstance(myManager.getProject()).getFilesWithWord(wordToSearch, UsageSearchContext.IN_PLAIN_TEXT, theSearchScope, true));
final StringSearcher searcher = new StringSearcher(qName, true, true, false);
progress.pushState();
final Ref<Boolean> cancelled = Ref.create(Boolean.FALSE);
try {
progress.setText(PsiBundle.message("psi.search.in.non.java.files.progress"));
final SearchScope useScope = originalElement == null ? null : myDumbService.runReadActionInSmartMode(() -> getUseScope(originalElement));
final int patternLength = qName.length();
for (int i = 0; i < files.length; i++) {
progress.checkCanceled();
final PsiFile psiFile = files[i];
if (psiFile instanceof PsiBinaryFile) continue;
ThrowableComputable<CharSequence, RuntimeException> action = () -> psiFile.getViewProvider().getContents();
final CharSequence text = AccessRule.read(action);
LowLevelSearchUtil.processTextOccurrences(text, 0, text.length(), searcher, progress, index -> {
boolean isReferenceOK = myDumbService.runReadActionInSmartMode(() -> {
PsiReference referenceAt = psiFile.findReferenceAt(index);
return referenceAt == null || useScope == null || !PsiSearchScopeUtil.isInScope(useScope.intersectWith(initialScope), psiFile);
});
if (isReferenceOK && !processor.process(psiFile, index, index + patternLength)) {
cancelled.set(Boolean.TRUE);
return false;
}
return true;
});
if (cancelled.get()) break;
progress.setFraction((double)(i + 1) / files.length);
}
}
finally {
progress.popState();
}
return !cancelled.get();
}
@Override
public boolean processAllFilesWithWordInComments(@Nonnull String word,
@Nonnull GlobalSearchScope scope,
@Nonnull Processor<PsiFile> processor) {
return CacheManager.getInstance(myManager.getProject()).processFilesWithWord(processor, word, UsageSearchContext.IN_COMMENTS, scope, true);
}
@Override
public boolean processAllFilesWithWordInLiterals(@Nonnull String word,
@Nonnull GlobalSearchScope scope,
@Nonnull Processor<PsiFile> processor) {
return CacheManager.getInstance(myManager.getProject()).processFilesWithWord(processor, word, UsageSearchContext.IN_STRINGS, scope, true);
}