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;
});
}
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);
}
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);
}
}
}
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);
}
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);
}
});
}
Aggregations