use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class MavenFilteredPropertyPsiReferenceProvider method getReferencesByElement.
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
if (!shouldAddReference(element)) {
// Add reference to element with one child or leaf element only to avoid performance problem.
return PsiReference.EMPTY_ARRAY;
}
if (!MavenDomUtil.isFilteredResourceFile(element))
return PsiReference.EMPTY_ARRAY;
String text = element.getText();
if (StringUtil.isEmptyOrSpaces(text))
return PsiReference.EMPTY_ARRAY;
MavenProject mavenProject = MavenDomUtil.findContainingProject(element);
if (mavenProject == null)
return PsiReference.EMPTY_ARRAY;
List<PsiReference> res = null;
Pattern pattern = getDelimitersPattern(mavenProject);
Matcher matcher = pattern.matcher(text);
int groupCount = matcher.groupCount();
while (matcher.find()) {
String propertyName = null;
int from = 0;
for (int i = 0; i < groupCount; i++) {
propertyName = matcher.group(i + 1);
if (propertyName != null) {
from = matcher.start(i + 1);
break;
}
}
assert propertyName != null;
if (res == null) {
res = new ArrayList<>();
}
TextRange range = TextRange.from(from, propertyName.length());
res.add(new MavenFilteredPropertyPsiReference(mavenProject, element, propertyName, range));
}
return res == null ? PsiReference.EMPTY_ARRAY : res.toArray(new PsiReference[res.size()]);
}
use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class XsltResolveTest method doVariableResolveTest.
private XsltVariable doVariableResolveTest(boolean global) throws Throwable {
final PsiReference reference = findInjectedReferenceAtCaret();
final PsiElement element = reference.resolve();
assertTrue(element instanceof XsltVariable);
final XsltVariable var = (XsltVariable) element;
assertEquals(var.getName(), ((XPathVariableReference) reference).getReferencedName());
assertEquals(global, XsltSupport.isTopLevelElement(var.getTag()));
return var;
}
use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class XsltValidator method checkUnusedVariable.
public static void checkUnusedVariable(XsltVariable variable, ProblemsHolder holder) {
if (variable instanceof XsltParameter) {
if (((XsltParameter) variable).isAbstract()) {
return;
}
} else {
if (variable.isVoid()) {
return;
}
}
final XmlTag tag = variable.getTag();
final XmlTag templateTag = XsltCodeInsightUtil.getTemplateTag(tag, false);
if (templateTag == null) {
return;
}
final XmlAttribute attribute = tag.getAttribute("name");
if (attribute == null) {
return;
}
final PsiElement token = XsltSupport.getAttValueToken(attribute);
if (token == null) {
return;
}
final SearchScope scope = new LocalSearchScope(templateTag);
final Query<PsiReference> refs = ReferencesSearch.search(variable, scope, false);
if (isUnused(variable, refs)) {
final String name = variable.getName();
assert name != null;
final LocalQuickFix[] fixes;
if (variable instanceof XsltParameter) {
fixes = new LocalQuickFix[] { new DeleteUnusedParameterFix(name, (XsltParameter) variable) };
} else {
fixes = new LocalQuickFix[] { new DeleteUnusedVariableFix(name, variable) };
}
holder.registerProblem(token, ((DeleteUnusedElementBase) fixes[0]).getType() + " '" + name + "' is never used", ProblemHighlightType.LIKE_UNUSED_SYMBOL, fixes);
}
}
use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class XsltValidator method isUnused.
private static boolean isUnused(PsiElement obj, Query<PsiReference> query) {
if (obj instanceof XsltParameter) {
final Collection<PsiReference> references = query.findAll();
int n = references.size();
for (PsiReference reference : references) {
final PsiElement element = reference.getElement();
if (element instanceof XmlAttributeValue) {
final XmlAttribute parent = (XmlAttribute) element.getParent();
if ("name".equals(parent.getName())) {
final XmlTag tag = parent.getParent();
if (tag != null && "with-param".equals(tag.getLocalName())) {
n--;
}
}
}
}
return n == 0;
} else {
return query.findFirst() == null;
}
}
use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class XsltRefactoringTest method findVariable.
@Nullable
private XPathVariable findVariable() {
final PsiElement elementAtCaret = myFixture.getElementAtCaret();
XPathVariableReference ref = PsiTreeUtil.getParentOfType(elementAtCaret, XPathVariableReference.class, false);
if (ref != null) {
return ref.resolve();
}
XPathVariable variable = PsiTreeUtil.getParentOfType(elementAtCaret, XPathVariable.class, false);
if (variable != null) {
return variable;
}
final PsiReference[] references = elementAtCaret.getReferences();
for (PsiReference reference : references) {
final PsiElement var = reference.resolve();
if (var instanceof XPathVariable) {
return (XPathVariable) var;
}
}
return null;
}
Aggregations