Search in sources :

Example 1 with LayoutElement

use of org.jetbrains.android.dom.layout.LayoutElement in project android by JetBrains.

the class AndroidCompletionContributor method customizeAddedAttributes.

private static void customizeAddedAttributes(final AndroidFacet facet, CompletionParameters parameters, final XmlAttribute attribute, final CompletionResultSet resultSet) {
    final PsiElement gp = attribute.getParent();
    if (gp == null) {
        return;
    }
    final XmlTag tag = (XmlTag) gp;
    final DomElement element = DomManager.getDomManager(gp.getProject()).getDomElement(tag);
    if (!(element instanceof LayoutElement)) {
        return;
    }
    final boolean localNameCompletion;
    if (attribute.getName().contains(":")) {
        final String nsPrefix = attribute.getNamespacePrefix();
        if (nsPrefix.length() == 0) {
            return;
        }
        if (!SdkConstants.NS_RESOURCES.equals(tag.getNamespaceByPrefix(nsPrefix))) {
            return;
        } else {
            localNameCompletion = true;
        }
    } else {
        localNameCompletion = false;
    }
    final Map<String, String> prefix2ns = new HashMap<String, String>();
    resultSet.runRemainingContributors(parameters, new Consumer<CompletionResult>() {

        @Override
        public void consume(CompletionResult result) {
            LookupElement lookupElement = result.getLookupElement();
            final Object obj = lookupElement.getObject();
            if (obj instanceof String) {
                final String s = (String) obj;
                final int index = s.indexOf(':');
                final String attributeName = s.substring(index + 1);
                if (index > 0) {
                    final String prefix = s.substring(0, index);
                    String ns = prefix2ns.get(prefix);
                    if (ns == null) {
                        ns = tag.getNamespaceByPrefix(prefix);
                        prefix2ns.put(prefix, ns);
                    }
                    if (SdkConstants.NS_RESOURCES.equals(ns)) {
                        final boolean deprecated = isFrameworkAttributeDeprecated(facet, attribute, attributeName);
                        result = customizeLayoutAttributeLookupElement(lookupElement, result, attributeName, deprecated);
                    }
                } else if (localNameCompletion) {
                    result = customizeLayoutAttributeLookupElement(lookupElement, result, attributeName, false);
                }
            }
            resultSet.passResult(result);
        }
    });
}
Also used : LayoutElement(org.jetbrains.android.dom.layout.LayoutElement) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement)

Example 2 with LayoutElement

use of org.jetbrains.android.dom.layout.LayoutElement in project android by JetBrains.

the class AndroidCompletionContributor method addDesignTimeAttributes.

/**
   * For every regular layout element attribute, add it with "tools:" prefix
   * (or whatever user uses for tools namespace)
   * <p/>
   * <a href="https://developer.android.com/studio/write/tool-attributes.html#design-time_view_attributes">Designtime attributes docs</a>
   */
private static void addDesignTimeAttributes(@NotNull final String namespacePrefix, @NotNull final PsiElement psiElement, @NotNull final AndroidFacet facet, @NotNull final XmlAttribute attribute, @NotNull final CompletionResultSet resultSet) {
    final XmlTag tag = attribute.getParent();
    final DomElement element = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
    final Set<XmlName> registeredAttributes = new HashSet<>();
    if (element instanceof LayoutElement) {
        AttributeProcessingUtil.processLayoutAttributes(facet, tag, (LayoutElement) element, registeredAttributes, (xmlName, attrDef, parentStyleableName) -> {
            if (SdkConstants.ANDROID_URI.equals(xmlName.getNamespaceKey())) {
                final String localName = xmlName.getLocalName();
                // Lookup string is something that would be inserted when attribute is completed, so we want to use
                // local name as an argument of .create(), otherwise we'll end up with getting completions like
                // "tools:tools:src". However, we want to show "tools:" prefix in the completion list, and for that
                // .withPresentableText is used
                final LookupElementBuilder lookupElement = LookupElementBuilder.create(psiElement, localName).withInsertHandler(XmlAttributeInsertHandler.INSTANCE).withPresentableText(namespacePrefix + ":" + localName);
                resultSet.addElement(lookupElement);
            }
            return null;
        });
    }
}
Also used : LayoutElement(org.jetbrains.android.dom.layout.LayoutElement) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) HashSet(com.intellij.util.containers.HashSet)

Example 3 with LayoutElement

use of org.jetbrains.android.dom.layout.LayoutElement in project android by JetBrains.

the class AndroidCompletionContributor method addAndroidPrefixElement.

private static void addAndroidPrefixElement(PsiElement position, PsiElement parent, CompletionResultSet resultSet) {
    if (position.getText().startsWith(SdkConstants.ANDROID_NS_NAME_PREFIX)) {
        return;
    }
    final PsiElement grandparent = parent.getParent();
    if (!(grandparent instanceof XmlTag)) {
        return;
    }
    final DomElement element = DomManager.getDomManager(grandparent.getProject()).getDomElement((XmlTag) grandparent);
    if (!(element instanceof LayoutElement) && !(element instanceof PreferenceElement)) {
        return;
    }
    final String prefix = ((XmlTag) grandparent).getPrefixByNamespace(SdkConstants.NS_RESOURCES);
    if (prefix == null || prefix.length() < 3) {
        return;
    }
    final LookupElementBuilder e = LookupElementBuilder.create(prefix + ":").withTypeText("[Namespace Prefix]", true);
    resultSet.addElement(PrioritizedLookupElement.withPriority(e, Double.MAX_VALUE));
}
Also used : LayoutElement(org.jetbrains.android.dom.layout.LayoutElement) PreferenceElement(org.jetbrains.android.dom.xml.PreferenceElement) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiElement(com.intellij.psi.PsiElement)

Aggregations

LayoutElement (org.jetbrains.android.dom.layout.LayoutElement)3 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)2 PsiElement (com.intellij.psi.PsiElement)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 HashSet (com.intellij.util.containers.HashSet)1 PreferenceElement (org.jetbrains.android.dom.xml.PreferenceElement)1