use of com.intellij.xml.XmlAttributeDescriptor in project intellij-community by JetBrains.
the class GenerateXmlTagAction method generateRaw.
private static void generateRaw(@NotNull final XmlTag newTag) {
XmlElementDescriptor selected = newTag.getDescriptor();
if (selected == null)
return;
switch(selected.getContentType()) {
case XmlElementDescriptor.CONTENT_TYPE_EMPTY:
newTag.collapseIfEmpty();
ASTNode node = newTag.getNode();
assert node != null;
ASTNode elementEnd = node.findChildByType(XmlTokenType.XML_EMPTY_ELEMENT_END);
if (elementEnd == null) {
LeafElement emptyTagEnd = Factory.createSingleLeafElement(XmlTokenType.XML_EMPTY_ELEMENT_END, "/>", 0, 2, null, newTag.getManager());
node.addChild(emptyTagEnd);
}
break;
case XmlElementDescriptor.CONTENT_TYPE_MIXED:
newTag.getValue().setText("");
}
for (XmlAttributeDescriptor descriptor : selected.getAttributesDescriptors(newTag)) {
if (descriptor.isRequired()) {
newTag.setAttribute(descriptor.getName(), "");
}
}
List<XmlElementDescriptor> tags = getRequiredSubTags(selected);
for (XmlElementDescriptor descriptor : tags) {
if (descriptor == null) {
XmlTag tag = XmlElementFactory.getInstance(newTag.getProject()).createTagFromText("<", newTag.getLanguage());
newTag.addSubTag(tag, false);
} else {
XmlTag subTag = newTag.addSubTag(createTag(newTag, descriptor), false);
generateRaw(subTag);
}
}
}
use of com.intellij.xml.XmlAttributeDescriptor in project intellij-community by JetBrains.
the class XmlAttributeValueGetter method getEnumeratedValues.
@NotNull
public static String[] getEnumeratedValues(XmlAttribute attribute) {
final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
if (descriptor == null) {
return ArrayUtil.EMPTY_STRING_ARRAY;
}
String[] result;
if (descriptor instanceof BasicXmlAttributeDescriptor) {
result = ((BasicXmlAttributeDescriptor) descriptor).getEnumeratedValues(attribute);
} else if (descriptor instanceof XmlEnumerationDescriptor) {
result = ((XmlEnumerationDescriptor) descriptor).getValuesForCompletion();
} else {
result = descriptor.getEnumeratedValues();
}
return result != null ? StringUtil.filterEmptyStrings(result) : ArrayUtil.EMPTY_STRING_ARRAY;
}
use of com.intellij.xml.XmlAttributeDescriptor in project intellij-community by JetBrains.
the class XmlPrefixReferenceProvider method getReferencesByElement.
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
XmlAttributeValue attributeValue = (XmlAttributeValue) element;
String value = attributeValue.getValue();
if (value == null)
return PsiReference.EMPTY_ARRAY;
int i = value.indexOf(':');
if (i <= 0)
return PsiReference.EMPTY_ARRAY;
PsiElement parent = attributeValue.getParent();
if (parent instanceof XmlAttribute) {
XmlTag tag = ((XmlAttribute) parent).getParent();
if (tag != null && !XmlNSDescriptorImpl.checkSchemaNamespace(tag)) {
XmlAttributeDescriptor descriptor = ((XmlAttribute) parent).getDescriptor();
if (descriptor instanceof XmlAttributeDescriptorImpl) {
String type = ((XmlAttributeDescriptorImpl) descriptor).getType();
if (type != null && type.endsWith(":QName")) {
String prefix = XmlUtil.findPrefixByQualifiedName(type);
String ns = ((XmlTag) descriptor.getDeclaration()).getNamespaceByPrefix(prefix);
if (XmlNSDescriptorImpl.checkSchemaNamespace(ns)) {
return new PsiReference[] { new SchemaPrefixReference(attributeValue, TextRange.from(1, i), value.substring(0, i), null) };
}
}
}
}
}
return PsiReference.EMPTY_ARRAY;
}
use of com.intellij.xml.XmlAttributeDescriptor in project intellij-community by JetBrains.
the class InsertRequiredAttributeFix method invoke.
@Override
public void invoke(@NotNull final Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") final Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
XmlTag myTag = (XmlTag) startElement;
ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(myTag);
final XmlElementDescriptor descriptor = myTag.getDescriptor();
if (descriptor == null) {
return;
}
final XmlAttributeDescriptor attrDescriptor = descriptor.getAttributeDescriptor(myAttrName, myTag);
final boolean indirectSyntax = XmlExtension.getExtension(myTag.getContainingFile()).isIndirectSyntax(attrDescriptor);
boolean insertShorthand = myTag instanceof HtmlTag && attrDescriptor != null && HtmlUtil.isBooleanAttribute(attrDescriptor, myTag);
PsiElement anchor = SourceTreeToPsiMap.treeElementToPsi(XmlChildRole.EMPTY_TAG_END_FINDER.findChild(treeElement));
final boolean anchorIsEmptyTag = anchor != null;
if (anchor == null) {
anchor = SourceTreeToPsiMap.treeElementToPsi(XmlChildRole.START_TAG_END_FINDER.findChild(treeElement));
}
if (anchor == null)
return;
final Template template = TemplateManager.getInstance(project).createTemplate("", "");
if (indirectSyntax) {
if (anchorIsEmptyTag)
template.addTextSegment(">");
template.addTextSegment("<jsp:attribute name=\"" + myAttrName + "\">");
} else {
template.addTextSegment(" " + myAttrName + (!insertShorthand ? "=\"" : ""));
}
Expression expression = new Expression() {
final TextResult result = new TextResult("");
@Override
public Result calculateResult(ExpressionContext context) {
return result;
}
@Override
public Result calculateQuickResult(ExpressionContext context) {
return null;
}
@Override
public LookupElement[] calculateLookupItems(ExpressionContext context) {
final LookupElement[] items = new LookupElement[myValues.length];
for (int i = 0; i < items.length; i++) {
items[i] = LookupElementBuilder.create(myValues[i]);
}
return items;
}
};
if (!insertShorthand)
template.addVariable(NAME_TEMPLATE_VARIABLE, expression, expression, true);
if (indirectSyntax) {
template.addTextSegment("</jsp:attribute>");
template.addEndVariable();
if (anchorIsEmptyTag)
template.addTextSegment("</" + myTag.getName() + ">");
} else if (!insertShorthand) {
template.addTextSegment("\"");
}
final PsiElement anchor1 = anchor;
final Runnable runnable = () -> ApplicationManager.getApplication().runWriteAction(() -> {
int textOffset = anchor1.getTextOffset();
if (!anchorIsEmptyTag && indirectSyntax)
++textOffset;
editor.getCaretModel().moveToOffset(textOffset);
if (anchorIsEmptyTag && indirectSyntax) {
editor.getDocument().deleteString(textOffset, textOffset + 2);
}
TemplateManager.getInstance(project).startTemplate(editor, template);
});
if (!ApplicationManager.getApplication().isUnitTestMode()) {
Runnable commandRunnable = () -> CommandProcessor.getInstance().executeCommand(project, runnable, getText(), getFamilyName());
ApplicationManager.getApplication().invokeLater(commandRunnable);
} else {
runnable.run();
}
}
use of com.intellij.xml.XmlAttributeDescriptor in project intellij-community by JetBrains.
the class XmlAttributeReferenceCompletionProvider method addVariants.
private static void addVariants(final CompletionResultSet result, final XmlAttribute[] attributes, final XmlAttributeDescriptor[] descriptors, XmlAttribute attribute, @Nullable InsertHandler<LookupElement> replacementInsertHandler) {
final XmlTag tag = attribute.getParent();
final PsiFile file = tag.getContainingFile();
final XmlExtension extension = XmlExtension.getExtension(file);
final String prefix = attribute.getName().contains(":") && ((XmlAttributeImpl) attribute).getRealLocalName().length() > 0 ? attribute.getNamespacePrefix() + ":" : null;
for (XmlAttributeDescriptor descriptor : descriptors) {
if (isValidVariant(attribute, descriptor, attributes, extension)) {
String name = descriptor.getName(tag);
InsertHandler<LookupElement> insertHandler = XmlAttributeInsertHandler.INSTANCE;
if (tag instanceof HtmlTag && HtmlUtil.isShortNotationOfBooleanAttributePreferred() && HtmlUtil.isBooleanAttribute(descriptor, tag)) {
insertHandler = null;
}
if (replacementInsertHandler != null) {
insertHandler = replacementInsertHandler;
} else if (descriptor instanceof NamespaceAwareXmlAttributeDescriptor) {
final String namespace = ((NamespaceAwareXmlAttributeDescriptor) descriptor).getNamespace(tag);
if (file instanceof XmlFile && namespace != null && namespace.length() > 0 && !name.contains(":") && tag.getPrefixByNamespace(namespace) == null) {
insertHandler = new XmlAttributeInsertHandler(namespace);
}
}
if (prefix == null || name.startsWith(prefix)) {
if (prefix != null && name.length() > prefix.length()) {
name = descriptor.getName(tag).substring(prefix.length());
}
LookupElementBuilder element = LookupElementBuilder.create(name);
if (descriptor instanceof PsiPresentableMetaData) {
element = element.withIcon(((PsiPresentableMetaData) descriptor).getIcon());
}
final int separator = name.indexOf(':');
if (separator > 0) {
element = element.withLookupString(name.substring(separator + 1));
}
element = element.withCaseSensitivity(!(descriptor instanceof HtmlAttributeDescriptorImpl)).withInsertHandler(insertHandler);
result.addElement(descriptor.isRequired() ? PrioritizedLookupElement.withPriority(element.appendTailText("(required)", true), 100) : HtmlUtil.isOwnHtmlAttribute(descriptor) ? PrioritizedLookupElement.withPriority(element, 50) : element);
}
}
}
}
Aggregations