Search in sources :

Example 31 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute 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 32 with XmlAttribute

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

the class Html5CustomAttributeDescriptorsProvider method getAttributeDescriptors.

@Override
public XmlAttributeDescriptor[] getAttributeDescriptors(XmlTag tag) {
    if (tag == null || !HtmlUtil.isHtml5Context(tag)) {
        return XmlAttributeDescriptor.EMPTY;
    }
    final List<String> currentAttrs = new ArrayList<>();
    for (XmlAttribute attribute : tag.getAttributes()) {
        currentAttrs.add(attribute.getName());
    }
    final Project project = tag.getProject();
    final Collection<String> keys = CachedValuesManager.getManager(project).getCachedValue(project, () -> {
        final Collection<String> keys1 = FileBasedIndex.getInstance().getAllKeys(Html5CustomAttributesIndex.INDEX_ID, project);
        final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
        return CachedValueProvider.Result.<Collection<String>>create(ContainerUtil.filter(keys1, key -> !FileBasedIndex.getInstance().processValues(Html5CustomAttributesIndex.INDEX_ID, key, null, (file, value) -> false, scope)), PsiModificationTracker.MODIFICATION_COUNT);
    });
    if (keys.isEmpty())
        return XmlAttributeDescriptor.EMPTY;
    final List<XmlAttributeDescriptor> result = new ArrayList<>();
    for (String key : keys) {
        boolean add = true;
        for (String attr : currentAttrs) {
            if (attr.startsWith(key)) {
                add = false;
            }
        }
        if (add) {
            result.add(new AnyXmlAttributeDescriptor(key));
        }
    }
    return result.toArray(new XmlAttributeDescriptor[result.size()]);
}
Also used : HtmlUtil(com.intellij.xml.util.HtmlUtil) Html5CustomAttributesIndex(com.intellij.html.index.Html5CustomAttributesIndex) XmlAttribute(com.intellij.psi.xml.XmlAttribute) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) XmlAttributeDescriptorsProvider(com.intellij.xml.XmlAttributeDescriptorsProvider) ContainerUtil(com.intellij.util.containers.ContainerUtil) CachedValuesManager(com.intellij.psi.util.CachedValuesManager) ArrayList(java.util.ArrayList) CachedValueProvider(com.intellij.psi.util.CachedValueProvider) PsiModificationTracker(com.intellij.psi.util.PsiModificationTracker) List(java.util.List) Project(com.intellij.openapi.project.Project) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) AnyXmlAttributeDescriptor(com.intellij.xml.impl.schema.AnyXmlAttributeDescriptor) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) XmlTag(com.intellij.psi.xml.XmlTag) AnyXmlAttributeDescriptor(com.intellij.xml.impl.schema.AnyXmlAttributeDescriptor) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) AnyXmlAttributeDescriptor(com.intellij.xml.impl.schema.AnyXmlAttributeDescriptor) Collection(java.util.Collection)

Example 33 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute 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 34 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute 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 35 with XmlAttribute

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

the class FormReferenceProvider method processNestedFormReference.

private static void processNestedFormReference(final XmlTag tag, final PsiReferenceProcessor processor, final PsiPlainTextFile file) {
    final XmlAttribute formFileAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_FORM_FILE, null);
    if (formFileAttribute != null) {
        processPackageReferences(file, processor, formFileAttribute);
        processor.execute(new ResourceFileReference(file, getValueRange(formFileAttribute)));
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute)

Aggregations

XmlAttribute (com.intellij.psi.xml.XmlAttribute)220 XmlTag (com.intellij.psi.xml.XmlTag)116 PsiElement (com.intellij.psi.PsiElement)60 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)57 NotNull (org.jetbrains.annotations.NotNull)44 XmlFile (com.intellij.psi.xml.XmlFile)37 Nullable (org.jetbrains.annotations.Nullable)27 Project (com.intellij.openapi.project.Project)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)20 TextRange (com.intellij.openapi.util.TextRange)18 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)18 ArrayList (java.util.ArrayList)18 PsiFile (com.intellij.psi.PsiFile)17 PsiReference (com.intellij.psi.PsiReference)17 IncorrectOperationException (com.intellij.util.IncorrectOperationException)10 DomElement (com.intellij.util.xml.DomElement)10 Result (com.intellij.openapi.application.Result)9 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)9 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)8 XmlElement (com.intellij.psi.xml.XmlElement)6