com.intellij.psi.impl.cache.CacheManager Java Examples

The following examples show how to use com.intellij.psi.impl.cache.CacheManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GlobalWordIndexTest.java    From intellij with Apache License 2.0 6 votes vote down vote up
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));
    }
  }
}
 
Example #2
Source File: CsvAnnotatorTest.java    From intellij-csv-validator with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #3
Source File: CodeInsightTestFixtureImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
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;
  }
 
Example #4
Source File: PsiSearchHelperImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public PsiFile[] findFilesWithPlainTextWords(@Nonnull String word) {
  return CacheManager.getInstance(myManager.getProject()).getFilesWithWord(word, UsageSearchContext.IN_PLAIN_TEXT,
                                                                                   GlobalSearchScope.projectScope(myManager.getProject()),
                                                                                   true);
}
 
Example #5
Source File: PsiSearchHelperImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #6
Source File: PsiSearchHelperImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #7
Source File: PsiSearchHelperImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example #8
Source File: PsiSearchHelperImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example #9
Source File: PsiSearchHelperImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@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);
}