use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class FormPropertyUsageTest method doPropertyUsageTest.
private void doPropertyUsageTest(final String propertyFileName) {
PropertiesFile propFile = (PropertiesFile) myPsiManager.findFile(myTestProjectRoot.findChild(propertyFileName));
assertNotNull(propFile);
final Property prop = (Property) propFile.findPropertyByKey("key");
assertNotNull(prop);
final Query<PsiReference> query = ReferencesSearch.search(prop);
final Collection<PsiReference> result = query.findAll();
assertEquals(1, result.size());
verifyReference(result, 0, "form.form", 960);
}
use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class PyCallExpressionHelper method getSuperCallType.
@NotNull
private static Maybe<PyType> getSuperCallType(@NotNull PyCallExpression call, TypeEvalContext context) {
final PyExpression callee = call.getCallee();
if (callee instanceof PyReferenceExpression) {
PsiElement must_be_super_init = ((PyReferenceExpression) callee).getReference().resolve();
if (must_be_super_init instanceof PyFunction) {
PyClass must_be_super = ((PyFunction) must_be_super_init).getContainingClass();
if (must_be_super == PyBuiltinCache.getInstance(call).getClass(PyNames.SUPER)) {
final PyArgumentList argumentList = call.getArgumentList();
if (argumentList != null) {
final PyClass containingClass = PsiTreeUtil.getParentOfType(call, PyClass.class);
PyExpression[] args = argumentList.getArguments();
if (args.length > 1) {
PyExpression first_arg = args[0];
if (first_arg instanceof PyReferenceExpression) {
final PyReferenceExpression firstArgRef = (PyReferenceExpression) first_arg;
final PyExpression qualifier = firstArgRef.getQualifier();
if (qualifier != null && PyNames.__CLASS__.equals(firstArgRef.getReferencedName())) {
final PsiReference qRef = qualifier.getReference();
final PsiElement element = qRef == null ? null : qRef.resolve();
if (element instanceof PyParameter) {
final PyParameterList parameterList = PsiTreeUtil.getParentOfType(element, PyParameterList.class);
if (parameterList != null && element == parameterList.getParameters()[0]) {
return new Maybe<>(getSuperCallTypeForArguments(context, containingClass, args[1]));
}
}
}
PsiElement possible_class = firstArgRef.getReference().resolve();
if (possible_class instanceof PyClass && ((PyClass) possible_class).isNewStyleClass(context)) {
final PyClass first_class = (PyClass) possible_class;
return new Maybe<>(getSuperCallTypeForArguments(context, first_class, args[1]));
}
}
} else if ((call.getContainingFile() instanceof PyFile) && ((PyFile) call.getContainingFile()).getLanguageLevel().isPy3K() && (containingClass != null)) {
return new Maybe<>(getSuperClassUnionType(containingClass, context));
}
}
}
}
}
return new Maybe<>();
}
use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class VariableInlineHandler method invoke.
public static void invoke(@NotNull final XPathVariable variable, Editor editor) {
final String type = LanguageFindUsages.INSTANCE.forLanguage(variable.getLanguage()).getType(variable);
final Project project = variable.getProject();
final XmlTag tag = ((XsltElement) variable).getTag();
final String expression = tag.getAttributeValue("select");
if (expression == null) {
CommonRefactoringUtil.showErrorHint(project, editor, MessageFormat.format("{0} ''{1}'' has no value.", StringUtil.capitalize(type), variable.getName()), TITLE, null);
return;
}
final Collection<PsiReference> references = ReferencesSearch.search(variable, new LocalSearchScope(tag.getParentTag()), false).findAll();
if (references.size() == 0) {
CommonRefactoringUtil.showErrorHint(project, editor, MessageFormat.format("{0} ''{1}'' is never used.", variable.getName()), TITLE, null);
return;
}
boolean hasExternalRefs = false;
if (XsltSupport.isTopLevelElement(tag)) {
final Query<PsiReference> query = ReferencesSearch.search(variable, GlobalSearchScope.allScope(project), false);
hasExternalRefs = !query.forEach(new Processor<PsiReference>() {
int allRefs = 0;
public boolean process(PsiReference psiReference) {
if (++allRefs > references.size()) {
return false;
} else if (!references.contains(psiReference)) {
return false;
}
return true;
}
});
}
final HighlightManager highlighter = HighlightManager.getInstance(project);
final ArrayList<RangeHighlighter> highlighters = new ArrayList<>();
final PsiReference[] psiReferences = references.toArray(new PsiReference[references.size()]);
TextRange[] ranges = ContainerUtil.map2Array(psiReferences, TextRange.class, s -> {
final PsiElement psiElement = s.getElement();
final XmlAttributeValue context = PsiTreeUtil.getContextOfType(psiElement, XmlAttributeValue.class, true);
if (psiElement instanceof XPathElement && context != null) {
return XsltCodeInsightUtil.getRangeInsideHostingFile((XPathElement) psiElement).cutOut(s.getRangeInElement());
}
return psiElement.getTextRange().cutOut(s.getRangeInElement());
});
final Editor e = editor instanceof EditorWindow ? ((EditorWindow) editor).getDelegate() : editor;
for (TextRange range : ranges) {
final TextAttributes textAttributes = EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes();
final Color color = getScrollmarkColor(textAttributes);
highlighter.addOccurrenceHighlight(e, range.getStartOffset(), range.getEndOffset(), textAttributes, HighlightManagerImpl.HIDE_BY_ESCAPE, highlighters, color);
}
highlighter.addOccurrenceHighlights(e, new PsiElement[] { ((XsltVariable) variable).getNameIdentifier() }, EditorColors.WRITE_SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
if (!hasExternalRefs) {
if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(MessageFormat.format("Inline {0} ''{1}''? ({2} occurrence{3})", type, variable.getName(), String.valueOf(references.size()), references.size() > 1 ? "s" : ""), TITLE, Messages.getQuestionIcon()) != Messages.YES) {
return;
}
} else {
if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(MessageFormat.format("Inline {0} ''{1}''? ({2} local occurrence{3})\n" + "\nWarning: It is being used in external files. Its declaration will not be removed.", type, variable.getName(), String.valueOf(references.size()), references.size() > 1 ? "s" : ""), TITLE, Messages.getWarningIcon()) != Messages.YES) {
return;
}
}
final boolean hasRefs = hasExternalRefs;
new WriteCommandAction.Simple(project, "XSLT.Inline", tag.getContainingFile()) {
@Override
protected void run() throws Throwable {
try {
for (PsiReference psiReference : references) {
final PsiElement element = psiReference.getElement();
if (element instanceof XPathElement) {
final XPathElement newExpr = XPathChangeUtil.createExpression(element, expression);
element.replace(newExpr);
} else {
assert false;
}
}
if (!hasRefs) {
tag.delete();
}
} catch (IncorrectOperationException e) {
Logger.getInstance(VariableInlineHandler.class.getName()).error(e);
}
}
}.execute();
}
use of com.intellij.psi.PsiReference 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.PsiReference in project intellij-community by JetBrains.
the class XsltVariableContext method isReferenceTo.
public boolean isReferenceTo(PsiElement element, XPathVariableReference reference) {
if (element instanceof XsltParameter) {
final XsltTemplate template = XsltCodeInsightUtil.getTemplate(element, false);
if (template == null || template.getMatchExpression() == null)
return false;
final XPathVariable t = reference.resolve();
final PsiReference[] references = element.getReferences();
for (PsiReference r : references) {
if (r.isReferenceTo(t))
return true;
}
}
return false;
}
Aggregations