ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / lang-impl / src / com / intellij / psi / impl / search / CachesBasedRefSearcher.java
blob417b071c001e1945b15a0bcf6680f3498421eb31
1 /*
2 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
3 */
4 package com.intellij.psi.impl.search;
6 import com.intellij.openapi.application.ApplicationManager;
7 import com.intellij.openapi.progress.ProgressManager;
8 import com.intellij.openapi.util.Computable;
9 import com.intellij.openapi.util.text.StringUtil;
10 import com.intellij.openapi.vfs.VirtualFile;
11 import com.intellij.psi.*;
12 import com.intellij.psi.meta.PsiMetaData;
13 import com.intellij.psi.meta.PsiMetaOwner;
14 import com.intellij.psi.search.*;
15 import com.intellij.psi.search.searches.ReferencesSearch;
16 import com.intellij.util.Processor;
17 import com.intellij.util.QueryExecutor;
19 /**
20 * @author max
22 public class CachesBasedRefSearcher implements QueryExecutor<PsiReference, ReferencesSearch.SearchParameters> {
23 public static boolean DEBUG = false;
25 public boolean execute(final ReferencesSearch.SearchParameters p, final Processor<PsiReference> consumer) {
26 final PsiElement refElement = p.getElementToSearch();
28 String text = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
29 public String compute() {
30 String text = null;
31 if (refElement instanceof PsiFile) {
32 final VirtualFile vFile = ((PsiFile)refElement).getVirtualFile();
33 if (vFile != null) {
34 text = vFile.getNameWithoutExtension();
37 else if (refElement instanceof PsiNamedElement) {
38 text = ((PsiNamedElement)refElement).getName();
39 if (refElement instanceof PsiMetaOwner) {
40 final PsiMetaData metaData = ((PsiMetaOwner)refElement).getMetaData();
41 if (metaData != null) text = metaData.getName();
45 if (text == null && refElement instanceof PsiMetaOwner) {
46 final PsiMetaData metaData = ((PsiMetaOwner)refElement).getMetaData();
47 if (metaData != null) text = metaData.getName();
49 return text;
51 });
52 if (StringUtil.isEmpty(text)) return true;
53 if (DEBUG) System.out.println("Searching for :"+text);
55 SearchScope searchScope = ApplicationManager.getApplication().runReadAction(new Computable<SearchScope>() {
56 public SearchScope compute() {
57 return p.getEffectiveSearchScope();
59 });
60 final boolean ignoreInjectedPsi = searchScope instanceof LocalSearchScope && ((LocalSearchScope)searchScope).isIgnoreInjectedPsi();
62 final TextOccurenceProcessor processor = new TextOccurenceProcessor() {
63 public boolean execute(PsiElement element, int offsetInElement) {
64 ProgressManager.checkCanceled();
65 if (DEBUG) {
66 System.out.println("!!! About to check "+element);
68 if (ignoreInjectedPsi && element instanceof PsiLanguageInjectionHost) return true;
69 final PsiReference[] refs = element.getReferences();
70 for (PsiReference ref : refs) {
71 if (DEBUG) {
72 System.out.println("!!!!!!!!!!!!!! Ref "+ref);
74 if (ref.getRangeInElement().contains(offsetInElement)) {
75 if (DEBUG) {
76 System.out.println("!!!!!!!!!!!!!!!!!!!!! Ref "+ref + " contains");
78 if (ref.isReferenceTo(refElement)) {
79 if (DEBUG) {
80 System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Found ref "+ref);
82 return consumer.process(ref);
86 return true;
90 short searchContext = UsageSearchContext.IN_CODE | UsageSearchContext.IN_FOREIGN_LANGUAGES | UsageSearchContext.IN_COMMENTS;
92 final PsiSearchHelper helper = PsiManager.getInstance(refElement.getProject()).getSearchHelper();
93 return helper.processElementsWithWord(processor, searchScope, text, searchContext,
94 refElement.getLanguage().isCaseSensitive());