use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.
the class JavaFxEventHandlerInspection method collectFieldTypeFixes.
private static void collectFieldTypeFixes(@NotNull XmlAttribute attribute, @NotNull PsiClassType eventType, @NotNull List<LocalQuickFix> quickFixes) {
final XmlTag xmlTag = attribute.getParent();
if (xmlTag == null)
return;
final XmlAttribute idAttribute = xmlTag.getAttribute(FxmlConstants.FX_ID);
if (idAttribute == null)
return;
final XmlAttributeValue idValue = idAttribute.getValueElement();
if (idValue == null)
return;
final PsiReference reference = idValue.getReference();
if (reference == null)
return;
final PsiElement element = reference.resolve();
if (!(element instanceof PsiField))
return;
final PsiField tagField = (PsiField) element;
if (tagField.hasModifierProperty(PsiModifier.STATIC) || !JavaFxPsiUtil.isVisibleInFxml(tagField))
return;
final PsiType tagFieldType = tagField.getType();
if (!(tagFieldType instanceof PsiClassType))
return;
final PsiClassType rawFieldType = ((PsiClassType) tagFieldType).rawType();
final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(eventType);
final PsiClass eventClass = resolveResult.getElement();
if (eventClass == null)
return;
final PsiSubstitutor eventSubstitutor = resolveResult.getSubstitutor();
for (PsiTypeParameter typeParameter : PsiUtil.typeParametersIterable(eventClass)) {
final PsiType eventTypeArgument = eventSubstitutor.substitute(typeParameter);
final PsiClassType rawEventArgument = eventTypeArgument instanceof PsiClassType ? ((PsiClassType) eventTypeArgument).rawType() : null;
if (rawFieldType.equals(rawEventArgument)) {
final List<IntentionAction> fixes = HighlightUtil.getChangeVariableTypeFixes(tagField, eventTypeArgument);
for (IntentionAction action : fixes) {
if (action instanceof LocalQuickFix) {
quickFixes.add((LocalQuickFix) action);
}
}
break;
}
}
}
use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.
the class JavaFxControllerBasedReferenceProvider method getReferencesByElement.
@NotNull
@Override
public final PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
final XmlAttributeValue xmlAttrVal = (XmlAttributeValue) element;
final PsiFile containingFile = xmlAttrVal.getContainingFile();
if (!JavaFxFileTypeFactory.isFxml(containingFile))
return PsiReference.EMPTY_ARRAY;
final PsiClass controllerClass = JavaFxPsiUtil.getControllerClass(containingFile);
return controllerClass != null ? getReferencesByElement(controllerClass, xmlAttrVal, context) : new PsiReference[] { new PsiReferenceBase.Immediate<>(xmlAttrVal, xmlAttrVal) };
}
use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.
the class JavaFxControllerFieldSearcher method execute.
@Override
public boolean execute(@NotNull final ReferencesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiReference> consumer) {
final PsiElement elementToSearch = queryParameters.getElementToSearch();
if (elementToSearch instanceof PsiField) {
final PsiField field = (PsiField) elementToSearch;
final PsiClass containingClass = ReadAction.compute(() -> field.getContainingClass());
if (containingClass != null) {
final String qualifiedName = ReadAction.compute(() -> containingClass.getQualifiedName());
if (qualifiedName != null) {
Project project = PsiUtilCore.getProjectInReadAction(containingClass);
final List<PsiFile> fxmlWithController = JavaFxControllerClassIndex.findFxmlWithController(project, qualifiedName);
for (final PsiFile file : fxmlWithController) {
ApplicationManager.getApplication().runReadAction(() -> {
final String fieldName = field.getName();
if (fieldName == null)
return;
final VirtualFile virtualFile = file.getViewProvider().getVirtualFile();
final SearchScope searchScope = queryParameters.getEffectiveSearchScope();
if (searchScope.contains(virtualFile)) {
file.accept(new XmlRecursiveElementVisitor() {
@Override
public void visitXmlAttributeValue(final XmlAttributeValue value) {
final PsiReference reference = value.getReference();
if (reference != null) {
final PsiElement resolve = reference.resolve();
if (resolve instanceof XmlAttributeValue) {
final PsiElement parent = resolve.getParent();
if (parent instanceof XmlAttribute) {
final XmlAttribute attribute = (XmlAttribute) parent;
if (FxmlConstants.FX_ID.equals(attribute.getName()) && fieldName.equals(attribute.getValue())) {
consumer.process(reference);
}
}
}
}
}
});
}
});
}
}
}
}
return true;
}
use of com.intellij.psi.xml.XmlAttributeValue 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.xml.XmlAttributeValue in project intellij-community by JetBrains.
the class XmlImplementationTextSelectioner method getTextEndOffset.
@Override
public int getTextEndOffset(@NotNull PsiElement element) {
if (element instanceof XmlAttributeValue) {
// for convenience
final XmlTag xmlTag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
if (xmlTag != null)
return xmlTag.getTextRange().getEndOffset();
LOG.assertTrue(false);
}
return element.getTextRange().getEndOffset();
}
Aggregations