use of com.intellij.codeInsight.lookup.LookupElementBuilder 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;
});
}
}
use of com.intellij.codeInsight.lookup.LookupElementBuilder 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));
}
use of com.intellij.codeInsight.lookup.LookupElementBuilder in project android by JetBrains.
the class AndroidLayoutXmlTagNameProvider method addTagNameVariants.
@Override
public void addTagNameVariants(List<LookupElement> elements, @NotNull XmlTag tag, String prefix) {
final PsiFile file = tag.getContainingFile();
if (!(file instanceof XmlFile && LayoutDomFileDescription.isLayoutFile((XmlFile) file))) {
// Only use this provider for Android layout files
return;
}
XmlExtension xmlExtension = XmlExtension.getExtension(file);
List<XmlElementDescriptor> variants = TagNameVariantCollector.getTagDescriptors(tag, NAMESPACES, null);
// Find the framework widgets that have a support library alternative
Set<String> supportAlternatives = Sets.newHashSet();
for (XmlElementDescriptor descriptor : variants) {
String qualifiedName = descriptor.getName(tag);
if (qualifiedName.startsWith(ANDROID_SUPPORT_PKG_PREFIX)) {
supportAlternatives.add(AndroidUtils.getUnqualifiedName(qualifiedName));
}
}
final Set<String> addedNames = Sets.newHashSet();
for (XmlElementDescriptor descriptor : variants) {
String qualifiedName = descriptor.getName(tag);
if (!addedNames.add(qualifiedName)) {
continue;
}
final String simpleName = AndroidUtils.getUnqualifiedName(qualifiedName);
// Creating LookupElementBuilder with PsiElement gives an ability to show documentation during completion time
PsiElement declaration = descriptor.getDeclaration();
LookupElementBuilder lookupElement = declaration == null ? LookupElementBuilder.create(qualifiedName) : LookupElementBuilder.create(declaration, qualifiedName);
final boolean isDeprecated = isDeclarationDeprecated(declaration);
if (isDeprecated) {
lookupElement = lookupElement.withStrikeoutness(true);
}
if (simpleName != null) {
lookupElement = lookupElement.withLookupString(simpleName);
}
// This statement preserves them in autocompletion.
if (descriptor instanceof PsiPresentableMetaData) {
lookupElement = lookupElement.withIcon(((PsiPresentableMetaData) descriptor).getIcon());
}
// Using insert handler is required for, e.g. automatic insertion of required fields in Android layout XMLs
if (xmlExtension.useXmlTagInsertHandler()) {
lookupElement = lookupElement.withInsertHandler(XmlTagInsertHandler.INSTANCE);
}
// Deprecated tag names are supposed to be shown below non-deprecated tags
int priority = isDeprecated ? 10 : 100;
if (simpleName == null) {
if (supportAlternatives.contains(qualifiedName)) {
// This component has a support library alternative so lower the priority so the support component is shown at the top.
priority -= 1;
} else {
// The component doesn't have an alternative in the support library so push it to the top.
priority += 10;
}
}
elements.add(PrioritizedLookupElement.withPriority(lookupElement, priority));
}
}
use of com.intellij.codeInsight.lookup.LookupElementBuilder in project android by JetBrains.
the class ResourceReferenceConverter method createLookupElement.
@Nullable
@Override
public LookupElement createLookupElement(ResourceValue resourceValue) {
final String value = resourceValue.toString();
boolean deprecated = false;
String doc = null;
if (myAttributeDefinition != null) {
doc = myAttributeDefinition.getValueDoc(value);
deprecated = myAttributeDefinition.isValueDeprecated(value);
}
LookupElementBuilder builder;
if (doc == null) {
builder = LookupElementBuilder.create(value);
} else {
builder = LookupElementBuilder.create(new DocumentationHolder(value, doc.trim()), value);
}
builder = builder.withCaseSensitivity(true).withStrikeoutness(deprecated);
final String resourceName = resourceValue.getResourceName();
if (resourceName != null) {
builder = builder.withLookupString(resourceName);
}
final int priority;
if (deprecated) {
// Show deprecated values in the end of the autocompletion list
priority = 0;
} else if (TOP_PRIORITY_VALUES.contains(value)) {
// http://b.android.com/189164
// match_parent and wrap_content are shown higher in the list of autocompletions, if they're available
priority = 2;
} else {
priority = 1;
}
return PrioritizedLookupElement.withPriority(builder, priority);
}
use of com.intellij.codeInsight.lookup.LookupElementBuilder in project android by JetBrains.
the class OnClickConverter method createLookupElement.
private static LookupElement createLookupElement(PsiMethod method) {
final LookupElementBuilder builder = LookupElementBuilder.create(method, method.getName()).withIcon(method.getIcon(Iconable.ICON_FLAG_VISIBILITY)).withPresentableText(method.getName());
final PsiClass containingClass = method.getContainingClass();
return containingClass != null ? builder.withTailText(" (" + containingClass.getQualifiedName() + ')') : builder;
}
Aggregations