Search in sources :

Example 26 with XmlAttributeValue

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

the class PrefixReferenceProvider method getReferencesByElement.

@Override
@NotNull
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    final XmlAttributeValue value = (XmlAttributeValue) element;
    final String s = value.getValue();
    final int i = s.indexOf(':');
    if (i <= 0 || s.startsWith("xml:")) {
        return PsiReference.EMPTY_ARRAY;
    }
    return new PsiReference[] { new PrefixReference(value, i) };
}
Also used : PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with XmlAttributeValue

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

the class PropertyKeyReferenceProvider method getReferencesByElement.

@NotNull
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) {
    if (myTagMode && element instanceof XmlTag) {
        return getTagReferences(((XmlTag) element));
    }
    if (element instanceof XmlAttributeValue) {
        final XmlAttribute xmlAttribute = (XmlAttribute) element.getParent();
        if (element.getTextLength() < 2) {
            return PsiReference.EMPTY_ARRAY;
        }
        final XmlTag tag = xmlAttribute.getParent();
        String value = null;
        String bundle = tag.getAttributeValue("bundle");
        if ("key".equals(xmlAttribute.getName())) {
            value = xmlAttribute.getValue();
        } else if (myFallbackKeyName.equals(xmlAttribute.getName())) {
            value = xmlAttribute.getValue();
            final String groupBundle = tag.getAttributeValue(myFallbackGroupName);
            if (groupBundle != null) {
                bundle = groupBundle;
            }
        }
        if (value != null) {
            return new PsiReference[] { new MyPropertyReference(value, xmlAttribute.getValueElement(), bundle) };
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with XmlAttributeValue

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

the class AntCreatePropertyFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement psiElement = descriptor.getPsiElement();
    final PsiFile containingFile = psiElement.getContainingFile();
    final FileModificationService modificationService = FileModificationService.getInstance();
    Navigatable result = null;
    if (myPropFile != null) {
        final VirtualFile vFile = myPropFile.getVirtualFile();
        boolean canModify = true;
        if (myPropFile instanceof PsiFile) {
            canModify = modificationService.prepareFileForWrite((PsiFile) myPropFile);
        } else if (vFile != null) {
            canModify = modificationService.prepareVirtualFilesForWrite(project, Collections.singleton(vFile));
        }
        if (canModify) {
            final IProperty generatedProperty = myPropFile.addProperty(myCanonicalText, "");
            result = vFile != null ? new OpenFileDescriptor(project, vFile, generatedProperty.getPsiElement().getTextRange().getEndOffset()) : generatedProperty;
        }
    } else {
        if (containingFile instanceof XmlFile) {
            final XmlFile xmlFile = (XmlFile) containingFile;
            final XmlTag rootTag = xmlFile.getRootTag();
            if (rootTag != null && modificationService.prepareFileForWrite(xmlFile)) {
                final XmlTag propTag = rootTag.createChildTag(PROPERTY, rootTag.getNamespace(), null, false);
                propTag.setAttribute(NAME_ATTR, myCanonicalText);
                propTag.setAttribute(VALUE_ATTR, "");
                final DomElement contextElement = DomUtil.getDomElement(descriptor.getPsiElement());
                PsiElement generated;
                if (contextElement == null) {
                    generated = rootTag.addSubTag(propTag, true);
                } else {
                    final AntDomTarget containingTarget = contextElement.getParentOfType(AntDomTarget.class, false);
                    final DomElement anchor = containingTarget != null ? containingTarget : contextElement;
                    final XmlTag tag = anchor.getXmlTag();
                    if (!rootTag.equals(tag)) {
                        generated = tag.getParent().addBefore(propTag, tag);
                    } else {
                        generated = rootTag.addSubTag(propTag, true);
                    }
                }
                if (generated instanceof XmlTag) {
                    final XmlAttribute valueAttrib = ((XmlTag) generated).getAttribute(VALUE_ATTR);
                    if (valueAttrib != null) {
                        final XmlAttributeValue valueElement = valueAttrib.getValueElement();
                        if (valueElement instanceof Navigatable) {
                            result = (Navigatable) valueElement;
                        }
                    }
                }
                if (result == null && generated instanceof Navigatable) {
                    result = (Navigatable) generated;
                }
            }
        }
    }
    if (result != null) {
        result.navigate(true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) Navigatable(com.intellij.pom.Navigatable) DomElement(com.intellij.util.xml.DomElement) IProperty(com.intellij.lang.properties.IProperty) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PsiElement(com.intellij.psi.PsiElement) FileModificationService(com.intellij.codeInsight.FileModificationService) XmlTag(com.intellij.psi.xml.XmlTag)

Example 29 with XmlAttributeValue

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

the class AntDomTargetDependsListConverter method createReferences.

@NotNull
public PsiReference[] createReferences(GenericDomValue<TargetResolver.Result> value, PsiElement element, ConvertContext context) {
    final XmlElement xmlElement = value.getXmlElement();
    if (!(xmlElement instanceof XmlAttribute)) {
        return PsiReference.EMPTY_ARRAY;
    }
    final XmlAttributeValue valueElement = ((XmlAttribute) xmlElement).getValueElement();
    if (valueElement == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    final String refsString = value.getStringValue();
    if (refsString == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    final List<PsiReference> refs = new ArrayList<>();
    final AntDomTargetReference.ReferenceGroup group = new AntDomTargetReference.ReferenceGroup();
    final TextRange wholeStringRange = ElementManipulators.getValueTextRange(valueElement);
    final StringTokenizer tokenizer = new StringTokenizer(refsString, ",", false);
    while (tokenizer.hasMoreTokens()) {
        final String token = tokenizer.nextToken();
        int tokenStartOffset = tokenizer.getCurrentPosition() - token.length();
        final String ref = token.trim();
        if (ref.length() != token.length()) {
            for (int idx = 0; idx < token.length(); idx++) {
                if (Character.isWhitespace(token.charAt(idx))) {
                    tokenStartOffset++;
                } else {
                    break;
                }
            }
        }
        refs.add(new AntDomTargetReference(element, TextRange.from(wholeStringRange.getStartOffset() + tokenStartOffset, ref.length()), group));
    }
    return refs.toArray(new PsiReference[refs.size()]);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) StringTokenizer(com.intellij.util.text.StringTokenizer) XmlElement(com.intellij.psi.xml.XmlElement) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with XmlAttributeValue

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

the class DartiumDebuggerEngine method isHtmlFileWithDartScript.

private static boolean isHtmlFileWithDartScript(@Nullable PsiFile psiFile) {
    if (psiFile == null || !HtmlUtil.isHtmlFile(psiFile)) {
        return false;
    }
    final String text = psiFile.getText();
    int i = -1;
    while ((i = text.indexOf(DartLanguage.DART_MIME_TYPE, i + 1)) != -1) {
        final PsiElement element = psiFile.findElementAt(i);
        if (element != null && element.getParent() instanceof XmlAttributeValue) {
            if (HtmlUtil.isScriptTag(PsiTreeUtil.getParentOfType(element, XmlTag.class))) {
                return true;
            }
        }
    }
    return false;
}
Also used : XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) 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