Search in sources :

Example 31 with XmlAttributeValue

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

the class CreateFlexSkinIntention method getHostComponentClass.

@Nullable
private JSClass getHostComponentClass() {
    final XmlTag tag = myElement instanceof XmlTag ? ((XmlTag) myElement).getParentTag() : myElement instanceof XmlAttributeValue ? (XmlTag) myElement.getParent().getParent() : null;
    final XmlElementDescriptor descriptor = tag == null ? null : tag.getDescriptor();
    if (descriptor instanceof ClassBackedElementDescriptor) {
        final PsiElement declaration = descriptor.getDeclaration();
        if (declaration instanceof JSClass) {
            return (JSClass) declaration;
        }
    }
    return null;
}
Also used : ClassBackedElementDescriptor(com.intellij.javascript.flex.mxml.schema.ClassBackedElementDescriptor) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with XmlAttributeValue

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

the class NlIdPropertyItem method setValue.

@Override
public void setValue(Object value) {
    String newId = value != null ? stripIdPrefix(value.toString()) : "";
    String oldId = getValue();
    XmlTag tag = getTag();
    if (ourRefactoringChoice != REFACTOR_NO && oldId != null && !oldId.isEmpty() && !newId.isEmpty() && !oldId.equals(newId) && tag != null && tag.isValid()) {
        // Offer rename refactoring?
        XmlAttribute attribute = tag.getAttribute(SdkConstants.ATTR_ID, SdkConstants.ANDROID_URI);
        if (attribute != null) {
            Module module = getModel().getModule();
            Project project = module.getProject();
            XmlAttributeValue valueElement = attribute.getValueElement();
            if (valueElement != null && valueElement.isValid()) {
                // Exact replace only, no comment/text occurrence changes since it is non-interactive
                ValueResourceElementWrapper wrapper = new ValueResourceElementWrapper(valueElement);
                RenameProcessor processor = new RenameProcessor(project, wrapper, NEW_ID_PREFIX + newId, false, /*comments*/
                false);
                processor.setPreviewUsages(false);
                // Do a quick usage search to see if we need to ask about renaming
                UsageInfo[] usages = processor.findUsages();
                if (usages.length > 0) {
                    int choice = ourRefactoringChoice;
                    if (choice == REFACTOR_ASK) {
                        DialogBuilder builder = createDialogBuilder(project);
                        builder.setTitle("Update Usages?");
                        // UGH!
                        JPanel panel = new JPanel(new BorderLayout());
                        JLabel label = new JLabel("<html>" + "Update usages as well?<br>" + "This will update all XML references and Java R field references.<br>" + "<br>" + "</html>");
                        panel.add(label, BorderLayout.CENTER);
                        JBCheckBox checkBox = new JBCheckBox("Don't ask again during this session");
                        panel.add(checkBox, BorderLayout.SOUTH);
                        builder.setCenterPanel(panel);
                        builder.setDimensionServiceKey("idPropertyDimension");
                        builder.removeAllActions();
                        DialogBuilder.CustomizableAction yesAction = builder.addOkAction();
                        yesAction.setText(Messages.YES_BUTTON);
                        builder.addActionDescriptor(dialogWrapper -> new AbstractAction(Messages.NO_BUTTON) {

                            @Override
                            public void actionPerformed(ActionEvent actionEvent) {
                                dialogWrapper.close(DialogWrapper.NEXT_USER_EXIT_CODE);
                            }
                        });
                        builder.addCancelAction();
                        int exitCode = builder.show();
                        choice = exitCode == DialogWrapper.OK_EXIT_CODE ? REFACTOR_YES : exitCode == DialogWrapper.NEXT_USER_EXIT_CODE ? REFACTOR_NO : ourRefactoringChoice;
                        //noinspection AssignmentToStaticFieldFromInstanceMethod
                        ourRefactoringChoice = checkBox.isSelected() ? choice : REFACTOR_ASK;
                        if (exitCode == DialogWrapper.CANCEL_EXIT_CODE) {
                            return;
                        }
                    }
                    if (choice == REFACTOR_YES) {
                        processor.run();
                        return;
                    }
                }
            }
        }
    }
    super.setValue(!StringUtil.isEmpty(newId) ? NEW_ID_PREFIX + newId : null);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) RenameProcessor(com.intellij.refactoring.rename.RenameProcessor) ActionEvent(java.awt.event.ActionEvent) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) JBCheckBox(com.intellij.ui.components.JBCheckBox) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) DialogBuilder(com.intellij.openapi.ui.DialogBuilder) UsageInfo(com.intellij.usageView.UsageInfo) XmlTag(com.intellij.psi.xml.XmlTag) ValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)

Example 33 with XmlAttributeValue

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

the class AngularAttributeIndexer method map.

@NotNull
@Override
public Map<String, AngularNamedItemDefinition> map(@NotNull FileContent inputData) {
    final Map<String, AngularNamedItemDefinition> map = new HashMap<>();
    final PsiFile file = inputData.getPsiFile();
    if (file instanceof XmlFile) {
        file.accept(new XmlRecursiveElementWalkingVisitor() {

            @Override
            public void visitXmlAttribute(XmlAttribute attribute) {
                if (myAttributeName.equals(DirectiveUtil.normalizeAttributeName(attribute.getName()))) {
                    final XmlAttributeValue element = attribute.getValueElement();
                    if (element == null) {
                        map.put("", new AngularNamedItemDefinition("", attribute.getTextRange().getStartOffset()));
                    } else {
                        final String name = StringUtil.unquoteString(element.getText());
                        map.put(name, new AngularNamedItemDefinition(name, element.getTextRange().getStartOffset()));
                    }
                }
            }
        });
    }
    return map;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) HashMap(java.util.HashMap) PsiFile(com.intellij.psi.PsiFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlRecursiveElementWalkingVisitor(com.intellij.psi.XmlRecursiveElementWalkingVisitor) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with XmlAttributeValue

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

the class DataBindingConverter method createReferences.

@NotNull
@Override
public PsiReference[] createReferences(final GenericDomValue<PsiClass> value, final PsiElement element, final ConvertContext context) {
    assert element instanceof XmlAttributeValue;
    final XmlAttributeValue attrValue = (XmlAttributeValue) element;
    final String strValue = attrValue.getValue();
    final int start = attrValue.getValueTextRange().getStartOffset() - attrValue.getTextRange().getStartOffset();
    List<PsiReference> result = new ArrayList<>();
    final String[] nameParts = strValue.split("[$.]");
    Module module = context.getModule();
    if (nameParts.length == 0 || module == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    int offset = start;
    // Check if the first namePart is an alias.
    DataBindingInfo bindingInfo = getDataBindingInfo(context);
    // for iterating over the nameParts.
    int idx = 0;
    // difference in lengths of the "type" and the "alias". Used in range computation later.
    int diff = 0;
    String fullType = strValue.replace('$', '.');
    if (bindingInfo != null) {
        String alias = nameParts[idx];
        PsiDataBindingResourceItem anImport = getImport(alias, bindingInfo);
        if (anImport != null) {
            // Found an import matching the first namePart. Add a reference from this to the type.
            idx++;
            TextRange range = new TextRange(offset, offset += alias.length());
            // Skip the next dot or dollar separator (if any)
            offset++;
            String type = anImport.getTypeDeclaration();
            result.add(new AliasedReference(element, range, type, module));
            fullType = type + fullType.substring(alias.length());
            diff = type.length() - alias.length();
        } else {
            //  Check java.lang and primitives
            if (nameParts.length == 1) {
                if (alias.length() > 0) {
                    if (Character.isLowerCase(alias.charAt(0))) {
                        final PsiPrimitiveType primitive = PsiJavaParserFacadeImpl.getPrimitiveType(alias);
                        if (primitive != null) {
                            result.add(new PsiReferenceBase<PsiElement>(element, true) {

                                @Nullable
                                @Override
                                public PsiElement resolve() {
                                    return myElement;
                                }

                                @NotNull
                                @Override
                                public Object[] getVariants() {
                                    return ArrayUtil.EMPTY_OBJECT_ARRAY;
                                }
                            });
                        }
                    } else {
                        // java.lang
                        PsiClass aClass = JavaPsiFacade.getInstance(context.getProject()).findClass(JAVA_LANG + alias, GlobalSearchScope.moduleWithLibrariesScope(module));
                        if (aClass != null) {
                            final TextRange range = new TextRange(offset, offset += alias.length());
                            result.add(new ClassReference(element, range, aClass));
                        }
                    }
                    idx++;
                }
            }
        }
    }
    for (; idx < nameParts.length; idx++, offset++) {
        final String packageName = nameParts[idx];
        if (packageName.length() > 0) {
            final TextRange range = new TextRange(offset, offset += packageName.length());
            result.add(new AliasedReference(element, range, fullType.substring(0, diff + offset - start), module));
        }
    }
    return result.toArray(new PsiReference[result.size()]);
}
Also used : PsiDataBindingResourceItem(com.android.tools.idea.res.PsiDataBindingResourceItem) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) DataBindingInfo(com.android.tools.idea.res.DataBindingInfo) NotNull(org.jetbrains.annotations.NotNull) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Example 35 with XmlAttributeValue

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

the class AndroidXmlDocumentationProvider method generateDoc.

@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    if (element instanceof ProvidedDocumentationPsiElement) {
        return ((ProvidedDocumentationPsiElement) element).getDocumentation();
    }
    if (element instanceof LazyValueResourceElementWrapper) {
        LazyValueResourceElementWrapper wrapper = (LazyValueResourceElementWrapper) element;
        ValueResourceInfo resourceInfo = wrapper.getResourceInfo();
        ResourceType type = resourceInfo.getType();
        String name = resourceInfo.getName();
        Module module = ModuleUtilCore.findModuleForPsiElement(element);
        if (module == null) {
            return null;
        }
        AndroidFacet facet = AndroidFacet.getInstance(element);
        if (facet == null) {
            return null;
        }
        ResourceUrl url;
        ResourceUrl originalUrl = originalElement != null ? ResourceUrl.parse(originalElement.getText()) : null;
        if (originalUrl != null && name.equals(originalUrl.name)) {
            url = originalUrl;
        } else {
            boolean isFramework = false;
            if (originalUrl != null) {
                isFramework = originalUrl.framework;
            } else {
                // Figure out if this resource is a framework file.
                // We really should store that info in the ValueResourceInfo instances themselves.
                // For now, attempt to figure it out
                SystemResourceManager systemResourceManager = facet.getSystemResourceManager();
                VirtualFile containingFile = resourceInfo.getContainingFile();
                if (systemResourceManager != null) {
                    VirtualFile parent = containingFile.getParent();
                    if (parent != null) {
                        VirtualFile resDir = parent.getParent();
                        if (resDir != null) {
                            isFramework = systemResourceManager.isResourceDir(resDir);
                        }
                    }
                }
            }
            url = ResourceUrl.create(type, name, isFramework, false);
        }
        return generateDoc(element, url);
    } else if (element instanceof MyResourceElement) {
        return getResourceDocumentation(element, ((MyResourceElement) element).myResource);
    } else if (element instanceof XmlAttributeValue) {
        return getResourceDocumentation(element, ((XmlAttributeValue) element).getValue());
    }
    if (originalElement instanceof XmlToken) {
        XmlToken token = (XmlToken) originalElement;
        if (token.getTokenType() == XML_ATTRIBUTE_VALUE_START_DELIMITER) {
            PsiElement next = token.getNextSibling();
            if (next instanceof XmlToken) {
                token = (XmlToken) next;
            }
        } else if (token.getTokenType() == XML_ATTRIBUTE_VALUE_END_DELIMITER) {
            PsiElement prev = token.getPrevSibling();
            if (prev instanceof XmlToken) {
                token = (XmlToken) prev;
            }
        }
        if (token.getTokenType() == XML_ATTRIBUTE_VALUE_TOKEN) {
            String documentation = getResourceDocumentation(originalElement, token.getText());
            if (documentation != null) {
                return documentation;
            }
        } else if (token.getTokenType() == XML_DATA_CHARACTERS) {
            String text = token.getText().trim();
            String documentation = getResourceDocumentation(originalElement, text);
            if (documentation != null) {
                return documentation;
            }
        }
    }
    if (element instanceof PomTargetPsiElement && originalElement != null) {
        final PomTarget target = ((PomTargetPsiElement) element).getTarget();
        if (target instanceof DomAttributeChildDescription) {
            synchronized (ANDROID_ATTRIBUTE_DOCUMENTATION_CACHE_KEY) {
                return generateDocForXmlAttribute((DomAttributeChildDescription) target, originalElement);
            }
        }
    }
    if (element instanceof MyDocElement) {
        return ((MyDocElement) element).myDocumentation;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceType(com.android.resources.ResourceType) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) DomAttributeChildDescription(com.intellij.util.xml.reflect.DomAttributeChildDescription) XmlToken(com.intellij.psi.xml.XmlToken) PomTarget(com.intellij.pom.PomTarget) LazyValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) Module(com.intellij.openapi.module.Module) ResourceUrl(com.android.ide.common.resources.ResourceUrl) SystemResourceManager(org.jetbrains.android.resourceManagers.SystemResourceManager) FakePsiElement(com.intellij.psi.impl.FakePsiElement) PsiElement(com.intellij.psi.PsiElement) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) ValueResourceInfo(org.jetbrains.android.resourceManagers.ValueResourceInfo)

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