Search in sources :

Example 16 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class AddAttributeValueIntentionFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") final Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    final XmlAttribute attribute = PsiTreeUtil.getNonStrictParentOfType(startElement, XmlAttribute.class);
    if (attribute == null || attribute.getValue() != null) {
        return;
    }
    final XmlAttribute attributeWithValue = XmlElementFactory.getInstance(project).createAttribute(attribute.getName(), "", startElement);
    final PsiElement newAttribute = attribute.replace(attributeWithValue);
    if (editor != null && newAttribute != null && newAttribute instanceof XmlAttribute && newAttribute.isValid()) {
        final XmlAttributeValue valueElement = ((XmlAttribute) newAttribute).getValueElement();
        if (valueElement != null) {
            editor.getCaretModel().moveToOffset(valueElement.getTextOffset());
            AutoPopupController.getInstance(newAttribute.getProject()).scheduleAutoPopup(editor);
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 17 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class XmlChooseColorIntentionAction method chooseColor.

public static void chooseColor(JComponent editorComponent, PsiElement element) {
    String caption = CodeInsightBundle.message("intention.color.chooser.dialog");
    final XmlAttributeValue literal = PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class, false);
    if (literal == null)
        return;
    final String text = StringUtil.unquoteString(literal.getValue());
    Color oldColor;
    try {
        oldColor = Color.decode(text);
    } catch (NumberFormatException e) {
        oldColor = JBColor.GRAY;
    }
    Color color = ColorChooser.chooseColor(editorComponent, caption, oldColor, true);
    if (color == null)
        return;
    if (!Comparing.equal(color, oldColor)) {
        if (!FileModificationService.getInstance().preparePsiElementForWrite(element))
            return;
        final String newText = "#" + ColorUtil.toHex(color);
        final PsiManager manager = literal.getManager();
        final XmlAttribute newAttribute = XmlElementFactory.getInstance(manager.getProject()).createAttribute("name", newText, element);
        final Runnable replaceRunnable = () -> {
            final XmlAttributeValue valueElement = newAttribute.getValueElement();
            assert valueElement != null;
            literal.replace(valueElement);
        };
        new WriteCommandAction(element.getProject(), caption) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                replaceRunnable.run();
            }
        }.execute();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlAttribute(com.intellij.psi.xml.XmlAttribute) JBColor(com.intellij.ui.JBColor) PsiManager(com.intellij.psi.PsiManager) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Result(com.intellij.openapi.application.Result)

Example 18 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class FormReferenceProvider method processReferences.

private static void processReferences(final PsiPlainTextFile file, final PsiReferenceProcessor processor) {
    final Project project = file.getProject();
    final XmlTag rootTag = ApplicationManager.getApplication().runReadAction(new Computable<XmlTag>() {

        public XmlTag compute() {
            final XmlFile xmlFile = (XmlFile) PsiFileFactory.getInstance(project).createFileFromText("a.xml", XmlFileType.INSTANCE, file.getViewProvider().getContents());
            return xmlFile.getRootTag();
        }
    });
    if (rootTag == null || !Utils.FORM_NAMESPACE.equals(rootTag.getNamespace())) {
        return;
    }
    @NonNls final String name = rootTag.getName();
    if (!"form".equals(name)) {
        return;
    }
    PsiReference classReference = null;
    final XmlAttribute classToBind = rootTag.getAttribute("bind-to-class", null);
    if (classToBind != null) {
        // reference to class
        final XmlAttributeValue valueElement = classToBind.getValueElement();
        if (valueElement == null) {
            return;
        }
        final String className = valueElement.getValue().replace('$', '.');
        final PsiReference[] referencesByString = new JavaClassReferenceProvider().getReferencesByString(className, file, valueElement.getTextRange().getStartOffset() + 1);
        if (referencesByString.length < 1) {
            // There are no references there
            return;
        }
        for (PsiReference aReferencesByString : referencesByString) {
            processor.execute(aReferencesByString);
        }
        classReference = referencesByString[referencesByString.length - 1];
    }
    final PsiReference finalClassReference = classReference;
    ApplicationManager.getApplication().runReadAction(() -> processReferences(rootTag, finalClassReference, file, processor));
}
Also used : Project(com.intellij.openapi.project.Project) NonNls(org.jetbrains.annotations.NonNls) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag) JavaClassReferenceProvider(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceProvider)

Example 19 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class FormReferenceProvider method getValueRange.

private static TextRange getValueRange(final XmlAttribute classToBind) {
    final XmlAttributeValue valueElement = classToBind.getValueElement();
    final TextRange textRange = valueElement.getTextRange();
    // skip " "
    return new TextRange(textRange.getStartOffset() + 1, textRange.getEndOffset() - 1);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue)

Example 20 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue 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;
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XsltParameter(org.intellij.lang.xpath.xslt.psi.XsltParameter) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)127 XmlAttribute (com.intellij.psi.xml.XmlAttribute)57 XmlTag (com.intellij.psi.xml.XmlTag)50 NotNull (org.jetbrains.annotations.NotNull)38 PsiElement (com.intellij.psi.PsiElement)31 Nullable (org.jetbrains.annotations.Nullable)24 PsiReference (com.intellij.psi.PsiReference)20 XmlFile (com.intellij.psi.xml.XmlFile)19 TextRange (com.intellij.openapi.util.TextRange)18 VirtualFile (com.intellij.openapi.vfs.VirtualFile)15 ArrayList (java.util.ArrayList)13 Project (com.intellij.openapi.project.Project)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 DomElement (com.intellij.util.xml.DomElement)9 PsiFile (com.intellij.psi.PsiFile)8 LazyValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper)7 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 Module (com.intellij.openapi.module.Module)5 ValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)5