Search in sources :

Example 11 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class XsltImplicitUsagesProvider method isImplicitUsage.

public boolean isImplicitUsage(PsiElement element) {
    if (!(element instanceof XmlAttribute)) {
        return false;
    }
    final XmlAttribute attr = (XmlAttribute) element;
    if (!attr.isNamespaceDeclaration()) {
        return false;
    }
    final PsiFile file = attr.getContainingFile();
    if (!(file instanceof XmlFile)) {
        return false;
    }
    // ContextProvider.hasXPathInjections() is an optimization that avoids to run the references search on totally XPath-free XML files
    if (!ContextProvider.hasXPathInjections((XmlFile) file) && !XsltSupport.isXsltFile(file)) {
        return false;
    }
    // This need to catch both prefix references from injected XPathFiles and prefixes from mode declarations/references:
    // <xsl:template match="*" mode="prefix:name" />
    // BTW: Almost the same logic applies to other XML dialects (RELAX-NG).
    // Pull this class into the platform?
    final String prefix = attr.getLocalName();
    final SchemaPrefix target = new SchemaPrefix(attr, TextRange.from("xmlns:".length(), prefix.length()), prefix);
    final Query<PsiReference> q = ReferencesSearch.search(target, new LocalSearchScope(attr.getParent()));
    return !q.forEach(psiReference -> {
        if (psiReference.getElement() == attr) {
            return true;
        }
        return false;
    });
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) SchemaPrefix(com.intellij.psi.impl.source.xml.SchemaPrefix) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XsltSupport(org.intellij.lang.xpath.xslt.XsltSupport) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Query(com.intellij.util.Query) Processor(com.intellij.util.Processor) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider) ContextProvider(org.intellij.lang.xpath.context.ContextProvider) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) SchemaPrefix(com.intellij.psi.impl.source.xml.SchemaPrefix) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile)

Example 12 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class XsltParameterImpl method getLocalUseScope.

@NotNull
@Override
public SearchScope getLocalUseScope() {
    final XmlTag tag = getTag();
    if (!tag.isValid()) {
        return getDefaultUseScope();
    }
    final XsltTemplate template = getTemplate();
    if (template == null) {
        return getDefaultUseScope();
    }
    if (template.getName() == null) {
        return getDefaultUseScope();
    }
    final XmlFile file = (XmlFile) tag.getContainingFile();
    if (!XsltIncludeIndex.processBackwardDependencies(file, new CommonProcessors.FindFirstProcessor<>())) {
        // processor found something
        return getDefaultUseScope();
    }
    return new LocalSearchScope(file);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) XmlFile(com.intellij.psi.xml.XmlFile) XsltTemplate(org.intellij.lang.xpath.xslt.psi.XsltTemplate) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class GroovyInlineMethodUtil method replaceAllOccurrencesWithExpression.

private static void replaceAllOccurrencesWithExpression(GrMethod method, GrCallExpression call, GrExpression oldExpression, GrParameter parameter) {
    Collection<PsiReference> refs = ReferencesSearch.search(parameter, new LocalSearchScope(method), false).findAll();
    final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(call.getProject());
    GrExpression expression = elementFactory.createExpressionFromText(oldExpression.getText());
    if (GroovyRefactoringUtil.hasSideEffect(expression) && refs.size() > 1 || !hasUnresolvableWriteAccess(refs, oldExpression)) {
        final String oldName = parameter.getName();
        final String newName = InlineMethodConflictSolver.suggestNewName(oldName, method, call);
        expression = elementFactory.createExpressionFromText(newName);
        final GrOpenBlock body = method.getBlock();
        final GrStatement[] statements = body.getStatements();
        GrStatement anchor = null;
        if (statements.length > 0) {
            anchor = statements[0];
        }
        body.addStatementBefore(elementFactory.createStatementFromText(createVariableDefinitionText(parameter, oldExpression, newName)), anchor);
    }
    for (PsiReference ref : refs) {
        PsiElement element = ref.getElement();
        if (element instanceof GrReferenceExpression) {
            ((GrReferenceExpression) element).replaceWithExpression(expression, true);
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 14 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class GroovyRefactoringSupportProvider method isInplaceRenameAvailable.

@Override
public boolean isInplaceRenameAvailable(@NotNull PsiElement elementToRename, PsiElement nameSuggestionContext) {
    if (nameSuggestionContext != null && nameSuggestionContext.getContainingFile() != elementToRename.getContainingFile())
        return false;
    if (!(elementToRename instanceof GrLabeledStatement)) {
        return false;
    }
    SearchScope useScope = PsiSearchHelper.SERVICE.getInstance(elementToRename.getProject()).getUseScope(elementToRename);
    if (!(useScope instanceof LocalSearchScope))
        return false;
    PsiElement[] scopeElements = ((LocalSearchScope) useScope).getScope();
    if (scopeElements.length > 1) {
        return false;
    }
    PsiFile containingFile = elementToRename.getContainingFile();
    return PsiTreeUtil.isAncestor(containingFile, scopeElements[0], false);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiFile(com.intellij.psi.PsiFile) GrLabeledStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement) PsiElement(com.intellij.psi.PsiElement)

Example 15 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class PropertiesReferenceContributor method registerReferenceProviders.

@Override
public void registerReferenceProviders(@NotNull final PsiReferenceRegistrar registrar) {
    registrar.registerReferenceProvider(literalExpression(), new PropertiesReferenceProvider(true));
    registrar.registerReferenceProvider(literalExpression().withParent(psiNameValuePair().withName(AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER)), new ResourceBundleReferenceProvider());
    registrar.registerReferenceProvider(literalExpression(), new PsiReferenceProvider() {

        private final PsiReferenceProvider myUnderlying = new ResourceBundleReferenceProvider();

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            final PsiElement parent = element.getParent();
            if (!(parent instanceof PsiField)) {
                return PsiReference.EMPTY_ARRAY;
            }
            final PsiField field = (PsiField) parent;
            if (field.getInitializer() != element || !field.hasModifierProperty(PsiModifier.FINAL) || !field.getType().equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
                return PsiReference.EMPTY_ARRAY;
            }
            List<PsiReference> references = new ArrayList<>();
            final PsiClass propertyKeyAnnotation = JavaPsiFacade.getInstance(element.getProject()).findClass(AnnotationUtil.PROPERTY_KEY, element.getResolveScope());
            if (propertyKeyAnnotation != null) {
                AnnotatedElementsSearch.searchPsiParameters(propertyKeyAnnotation, new LocalSearchScope(element.getContainingFile())).forEach(parameter -> {
                    final PsiModifierList list = parameter.getModifierList();
                    LOG.assertTrue(list != null);
                    final PsiAnnotation annotation = list.findAnnotation(AnnotationUtil.PROPERTY_KEY);
                    LOG.assertTrue(annotation != null);
                    for (PsiNameValuePair pair : annotation.getParameterList().getAttributes()) {
                        if (AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER.equals(pair.getName())) {
                            final PsiAnnotationMemberValue value = pair.getValue();
                            if (value instanceof PsiReferenceExpression && ((PsiReferenceExpression) value).resolve() == field) {
                                Collections.addAll(references, myUnderlying.getReferencesByElement(element, context));
                                return false;
                            }
                        }
                    }
                    return true;
                });
            }
            return references.toArray(new PsiReference[references.size()]);
        }
    });
    registrar.registerReferenceProvider(PsiJavaPatterns.psiElement(PropertyValueImpl.class), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            String text = element.getText();
            String[] words = text.split("\\s");
            if (words.length != 1)
                return PsiReference.EMPTY_ARRAY;
            return CLASS_REFERENCE_PROVIDER.getReferencesByString(words[0], element, 0);
        }
    });
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ProcessingContext(com.intellij.util.ProcessingContext) PsiJavaPatterns.psiNameValuePair(com.intellij.patterns.PsiJavaPatterns.psiNameValuePair) PsiJavaPatterns.literalExpression(com.intellij.patterns.PsiJavaPatterns.literalExpression) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ArrayList(java.util.ArrayList) PropertyValueImpl(com.intellij.lang.properties.psi.impl.PropertyValueImpl) List(java.util.List) AnnotatedElementsSearch(com.intellij.psi.search.searches.AnnotatedElementsSearch) AnnotationUtil(com.intellij.codeInsight.AnnotationUtil) com.intellij.psi(com.intellij.psi) JavaClassReferenceProvider(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceProvider) Logger(com.intellij.openapi.diagnostic.Logger) PsiJavaPatterns(com.intellij.patterns.PsiJavaPatterns) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) ProcessingContext(com.intellij.util.ProcessingContext) NotNull(org.jetbrains.annotations.NotNull) ArrayList(java.util.ArrayList) List(java.util.List) PropertyValueImpl(com.intellij.lang.properties.psi.impl.PropertyValueImpl)

Aggregations

LocalSearchScope (com.intellij.psi.search.LocalSearchScope)113 SearchScope (com.intellij.psi.search.SearchScope)31 NotNull (org.jetbrains.annotations.NotNull)22 PsiElement (com.intellij.psi.PsiElement)19 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 Project (com.intellij.openapi.project.Project)18 Nullable (org.jetbrains.annotations.Nullable)13 ArrayList (java.util.ArrayList)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 UsageInfo (com.intellij.usageView.UsageInfo)11 TextRange (com.intellij.openapi.util.TextRange)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 com.intellij.psi (com.intellij.psi)8 PsiReference (com.intellij.psi.PsiReference)8 PsiFile (com.intellij.psi.PsiFile)7 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)7 AnalysisScope (com.intellij.analysis.AnalysisScope)6 Processor (com.intellij.util.Processor)6 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)5 Ref (com.intellij.openapi.util.Ref)4