diff options
Diffstat (limited to 'plugins/ui-designer/src/com/intellij/uiDesigner/binding/FormReferencesSearcher.java')
-rw-r--r-- | plugins/ui-designer/src/com/intellij/uiDesigner/binding/FormReferencesSearcher.java | 192 |
1 files changed, 108 insertions, 84 deletions
diff --git a/plugins/ui-designer/src/com/intellij/uiDesigner/binding/FormReferencesSearcher.java b/plugins/ui-designer/src/com/intellij/uiDesigner/binding/FormReferencesSearcher.java index 179f47d57a47..d40be21e1786 100644 --- a/plugins/ui-designer/src/com/intellij/uiDesigner/binding/FormReferencesSearcher.java +++ b/plugins/ui-designer/src/com/intellij/uiDesigner/binding/FormReferencesSearcher.java @@ -1,3 +1,18 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.intellij.uiDesigner.binding; import com.intellij.lang.properties.IProperty; @@ -15,7 +30,6 @@ import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.NullableComputable; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; -import com.intellij.psi.impl.PsiManagerImpl; import com.intellij.psi.impl.cache.CacheManager; import com.intellij.psi.impl.search.PsiSearchHelperImpl; import com.intellij.psi.search.*; @@ -27,16 +41,19 @@ import com.intellij.util.QueryExecutor; import com.intellij.util.text.CharArrayUtil; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; import java.util.List; /** * @author max */ public class FormReferencesSearcher implements QueryExecutor<PsiReference, ReferencesSearch.SearchParameters> { + @Override public boolean execute(@NotNull final ReferencesSearch.SearchParameters p, @NotNull final Processor<PsiReference> consumer) { if (!scopeCanContainForms(p.getScope())) return true; final PsiElement refElement = p.getElementToSearch(); final PsiFile psiFile = ApplicationManager.getApplication().runReadAction(new Computable<PsiFile>() { + @Override public PsiFile compute() { if (!refElement.isValid()) return null; return refElement.getContainingFile(); @@ -45,31 +62,38 @@ public class FormReferencesSearcher implements QueryExecutor<PsiReference, Refer if (psiFile == null) return true; final VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile == null) return true; - Module module = ProjectRootManager.getInstance(refElement.getProject()).getFileIndex().getModuleForFile(virtualFile); + Project project = ApplicationManager.getApplication().runReadAction(new Computable<Project>() { + @Override + public Project compute() { + return psiFile.getProject(); + } + }); + Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(virtualFile); if (module == null) return true; final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesScope(module); final LocalSearchScope filterScope = p.getScope() instanceof LocalSearchScope ? (LocalSearchScope) p.getScope() : null; + PsiManager psiManager = PsiManager.getInstance(project); if (refElement instanceof PsiPackage) { //no need to do anything //if (!UIFormUtil.processReferencesInUIForms(consumer, (PsiPackage)refElement, scope)) return false; } else if (refElement instanceof PsiClass) { - if (!processReferencesInUIForms(consumer, (PsiClass)refElement, scope, filterScope)) return false; + if (!processReferencesInUIForms(consumer, psiManager,(PsiClass)refElement, scope, filterScope)) return false; } else if (refElement instanceof PsiEnumConstant) { - if (!processReferencesInUIForms(consumer, (PsiEnumConstant)refElement, scope, filterScope)) return false; + if (!processReferencesInUIForms(consumer, psiManager, (PsiEnumConstant)refElement, scope, filterScope)) return false; } else if (refElement instanceof PsiField) { - if (!processReferencesInUIForms(consumer, (PsiField)refElement, scope, filterScope)) return false; + if (!processReferencesInUIForms(consumer, psiManager, (PsiField)refElement, scope, filterScope)) return false; } else if (refElement instanceof IProperty) { - if (!processReferencesInUIForms(consumer, (Property)refElement, scope, filterScope)) return false; + if (!processReferencesInUIForms(consumer, psiManager, (Property)refElement, scope, filterScope)) return false; } else if (refElement instanceof PropertiesFile) { - if (!processReferencesInUIForms(consumer, (PropertiesFile)refElement, scope, filterScope)) return false; + if (!processReferencesInUIForms(consumer, psiManager, (PropertiesFile)refElement, scope, filterScope)) return false; } return true; @@ -79,31 +103,37 @@ public class FormReferencesSearcher implements QueryExecutor<PsiReference, Refer if (!(scope instanceof LocalSearchScope)) return true; LocalSearchScope localSearchScope = (LocalSearchScope) scope; final PsiElement[] elements = localSearchScope.getScope(); - for (PsiElement element : elements) { + for (final PsiElement element : elements) { if (element instanceof PsiDirectory) return true; - PsiFile file; - if (element instanceof PsiFile) { - file = (PsiFile) element; - } - else { - if (!element.isValid()) continue; - file = element.getContainingFile(); - } - if (file.getFileType() == StdFileTypes.GUI_DESIGNER_FORM) return true; + boolean isForm = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() { + @Override + public Boolean compute() { + PsiFile file; + if (element instanceof PsiFile) { + file = (PsiFile)element; + } + else { + if (!element.isValid()) return false; + file = element.getContainingFile(); + } + return file.getFileType() == StdFileTypes.GUI_DESIGNER_FORM; + } + }); + if (isForm) return true; } return false; } private static boolean processReferencesInUIForms(Processor<PsiReference> processor, - final PsiClass aClass, - GlobalSearchScope scope, final LocalSearchScope filterScope) { - PsiManagerImpl manager = (PsiManagerImpl)aClass.getManager(); + PsiManager psiManager, final PsiClass aClass, + GlobalSearchScope scope, final LocalSearchScope filterScope) { String className = getQualifiedName(aClass); - return className == null || processReferencesInUIFormsInner(className, aClass, processor, scope, manager, filterScope); + return className == null || processReferencesInUIFormsInner(className, aClass, processor, scope, psiManager, filterScope); } public static String getQualifiedName(final PsiClass aClass) { return ApplicationManager.getApplication().runReadAction(new Computable<String>() { + @Override public String compute() { if (!aClass.isValid()) return null; return aClass.getQualifiedName(); @@ -112,83 +142,56 @@ public class FormReferencesSearcher implements QueryExecutor<PsiReference, Refer } private static boolean processReferencesInUIForms(Processor<PsiReference> processor, - final PsiEnumConstant enumConstant, + PsiManager psiManager, final PsiEnumConstant enumConstant, GlobalSearchScope scope, final LocalSearchScope filterScope) { - PsiManagerImpl manager = (PsiManagerImpl)enumConstant.getManager(); String className = ApplicationManager.getApplication().runReadAction(new Computable<String>() { @Override public String compute() { return enumConstant.getName(); } }); - return className == null || processReferencesInUIFormsInner(className, enumConstant, processor, scope, manager, filterScope); - + return className == null || processReferencesInUIFormsInner(className, enumConstant, processor, scope, psiManager, filterScope); } private static boolean processReferencesInUIFormsInner(String name, PsiElement element, Processor<PsiReference> processor, GlobalSearchScope scope1, - PsiManagerImpl manager, + PsiManager manager, final LocalSearchScope filterScope) { GlobalSearchScope scope = GlobalSearchScope.projectScope(manager.getProject()).intersectWith(scope1); - manager.startBatchFilesProcessingMode(); - - try { - List<PsiFile> files = FormClassIndex.findFormsBoundToClass(manager.getProject(), name, scope); - - for (PsiFile file : files) { - ProgressManager.checkCanceled(); + List<PsiFile> files = FormClassIndex.findFormsBoundToClass(manager.getProject(), name, scope); - if (file.getFileType() != StdFileTypes.GUI_DESIGNER_FORM) continue; - if (!processReferences(processor, file, name, element, filterScope)) return false; - } - } - finally { - manager.finishBatchFilesProcessingMode(); - } - - return true; + return processReferencesInFiles(files, manager, name, element, filterScope, processor); } private static boolean processReferencesInUIForms(Processor<PsiReference> processor, - PsiField field, - GlobalSearchScope scope1, - LocalSearchScope filterScope) { - GlobalSearchScope scope = GlobalSearchScope.projectScope(field.getProject()).intersectWith(scope1); - PsiManagerImpl manager = (PsiManagerImpl)field.getManager(); + PsiManager psiManager, + PsiField field, + GlobalSearchScope scope1, + LocalSearchScope filterScope) { + GlobalSearchScope scope = GlobalSearchScope.projectScope(psiManager.getProject()).intersectWith(scope1); + final AccessToken token = ReadAction.start(); PsiClass containingClass = field.getContainingClass(); if (containingClass == null) return true; String fieldName; - final AccessToken token = ReadAction.start(); try { fieldName = field.getName(); } finally { token.finish(); } - manager.startBatchFilesProcessingMode(); - - try { - final List<PsiFile> files = FormClassIndex.findFormsBoundToClass(containingClass, scope); - - for (PsiFile file : files) { - ProgressManager.checkCanceled(); - - if (file.getFileType() != StdFileTypes.GUI_DESIGNER_FORM) continue; - if (!processReferences(processor, file, fieldName, field, filterScope)) return false; - } - } - finally { - manager.finishBatchFilesProcessingMode(); - } - - return true; + final List<PsiFile> files = FormClassIndex.findFormsBoundToClass(psiManager.getProject(), containingClass, scope); + return processReferencesInFiles(files, psiManager, fieldName, field, filterScope, processor); } - private static boolean processReferences(final Processor<PsiReference> processor, final PsiFile file, String name, final PsiElement element, + private static boolean processReferences(final Processor<PsiReference> processor, + final PsiFile file, + String name, + final PsiElement element, final LocalSearchScope filterScope) { CharSequence chars = ApplicationManager.getApplication().runReadAction(new NullableComputable<CharSequence>() { + @Override public CharSequence compute() { if (filterScope != null) { boolean isInScope = false; @@ -211,6 +214,7 @@ public class FormReferencesSearcher implements QueryExecutor<PsiReference, Refer if (index < 0) break; final int finalIndex = index; final Boolean searchDone = ApplicationManager.getApplication().runReadAction(new NullableComputable<Boolean>() { + @Override public Boolean compute() { final PsiReference ref = file.findReferenceAt(finalIndex + offset + 1); if (ref != null && ref.isReferenceTo(element)) { @@ -227,13 +231,13 @@ public class FormReferencesSearcher implements QueryExecutor<PsiReference, Refer } private static boolean processReferencesInUIForms(final Processor<PsiReference> processor, - final Property property, - final GlobalSearchScope globalSearchScope, - final LocalSearchScope filterScope) { + PsiManager psiManager, + final Property property, + final GlobalSearchScope globalSearchScope, + final LocalSearchScope filterScope) { + final Project project = psiManager.getProject(); - final Project project = property.getProject(); final GlobalSearchScope scope = GlobalSearchScope.projectScope(project).intersectWith(globalSearchScope); - final PsiManagerImpl manager = (PsiManagerImpl)property.getManager(); String name = ApplicationManager.getApplication().runReadAction(new Computable<String>() { @Override public String compute() { @@ -242,7 +246,7 @@ public class FormReferencesSearcher implements QueryExecutor<PsiReference, Refer }); if (name == null) return true; - manager.startBatchFilesProcessingMode(); + psiManager.startBatchFilesProcessingMode(); try { CommonProcessors.CollectProcessor<VirtualFile> collector = new CommonProcessors.CollectProcessor<VirtualFile>() { @@ -268,34 +272,54 @@ public class FormReferencesSearcher implements QueryExecutor<PsiReference, Refer } } finally { - manager.finishBatchFilesProcessingMode(); + psiManager.finishBatchFilesProcessingMode(); } return true; } - private static boolean processReferencesInUIForms(final Processor<PsiReference> processor, final PropertiesFile propFile, final GlobalSearchScope globalSearchScope, - final LocalSearchScope filterScope) { - final Project project = propFile.getProject(); + private static boolean processReferencesInUIForms(final Processor<PsiReference> processor, + PsiManager psiManager, + final PropertiesFile propFile, + final GlobalSearchScope globalSearchScope, + final LocalSearchScope filterScope) { + final Project project = psiManager.getProject(); GlobalSearchScope scope = GlobalSearchScope.projectScope(project).intersectWith(globalSearchScope); - PsiManagerImpl manager = (PsiManagerImpl)propFile.getContainingFile().getManager(); - final String baseName = propFile.getResourceBundle().getBaseName(); - manager.startBatchFilesProcessingMode(); + final String baseName = ApplicationManager.getApplication().runReadAction(new Computable<String>() { + @Override + public String compute() { + return propFile.getResourceBundle().getBaseName(); + } + }); + PsiFile containingFile = ApplicationManager.getApplication().runReadAction(new Computable<PsiFile>() { + @Override + public PsiFile compute() { + return propFile.getContainingFile(); + } + }); - try { - PsiFile[] files = CacheManager.SERVICE.getInstance(project).getFilesWithWord(baseName, UsageSearchContext.IN_PLAIN_TEXT, scope, true); + List<PsiFile> files = Arrays.asList(CacheManager.SERVICE.getInstance(project).getFilesWithWord(baseName, UsageSearchContext.IN_PLAIN_TEXT, scope, true)); + return processReferencesInFiles(files, psiManager, baseName, containingFile, filterScope, processor); + } + private static boolean processReferencesInFiles(List<PsiFile> files, + PsiManager psiManager, String baseName, + PsiElement element, + LocalSearchScope filterScope, + Processor<PsiReference> processor) { + psiManager.startBatchFilesProcessingMode(); + + try { for (PsiFile file : files) { ProgressManager.checkCanceled(); if (file.getFileType() != StdFileTypes.GUI_DESIGNER_FORM) continue; - if (!processReferences(processor, file, baseName, propFile.getContainingFile(), filterScope)) return false; + if (!processReferences(processor, file, baseName, element, filterScope)) return false; } } finally { - manager.finishBatchFilesProcessingMode(); + psiManager.finishBatchFilesProcessingMode(); } - return true; } } |