下面列出了怎么用com.intellij.psi.html.HtmlTag的API类实例代码及写法,或者点击链接到github查看源代码。
private void checkStructure(PsiElement document, @NotNull AnnotationHolder annotationHolder) {
PsiElement[] children = document.getChildren();
List<String> acceptedTag = Arrays.asList("template","element","script", "style");
for (PsiElement element : children) {
if (element instanceof HtmlTag) {
if (!acceptedTag.contains(((HtmlTag) element).getName().toLowerCase())) {
annotationHolder.createErrorAnnotation(element, "Invalid tag '"
+ ((HtmlTag) element).getName() + "', only the [template, element, script, style] tags are allowed here.");
}
checkAttributes((XmlTag) element, annotationHolder);
} else {
if (!(element instanceof PsiWhiteSpace)
&& !(element instanceof XmlProlog)
&& !(element instanceof XmlText)
&& !(element instanceof XmlComment)) {
String s = element.getText();
if (s.length() > 20) {
s = s.substring(0, 20);
}
annotationHolder.createErrorAnnotation(element, "Invalid content '" + s +
"', only the [template, script, style] tags are allowed here.");
}
}
}
}
public static List<String> loadImportedTags(@NotNull XmlFile file, @NotNull XmlTag context) {
// PsiElement[] arr = file.getRootTag().getChildren();
// Collection<HtmlTag> tags = PsiTreeUtil.findChildrenOfType(file, HtmlTag.class);
PsiElement[] reqTags = PsiTreeUtil.collectElements(file, new PsiElementFilter() {
@Override
public boolean isAccepted(PsiElement element) {
return element instanceof HtmlTag && (((HtmlTag) element).getName().equals(RTTagDescriptorsProvider.RT_REQUIRE) || ((HtmlTag) element).getName().equals(RTTagDescriptorsProvider.RT_IMPORT));
}
});
List<String> importedTags = new ArrayList<String>();
for (PsiElement elem : reqTags) {
String as = ((HtmlTag) elem).getAttributeValue("as");
if (!Strings.isNullOrEmpty(as)) {
importedTags.add(as);
}
}
return importedTags;
}
public static List<String> loadImportedTags(@NotNull XmlFile file, @NotNull XmlTag context) {
// PsiElement[] arr = file.getRootTag().getChildren();
// Collection<HtmlTag> tags = PsiTreeUtil.findChildrenOfType(file, HtmlTag.class);
PsiElement[] reqTags = PsiTreeUtil.collectElements(file, new PsiElementFilter() {
@Override
public boolean isAccepted(PsiElement element) {
return element instanceof HtmlTag && (((HtmlTag) element).getName().equals(RTTagDescriptorsProvider.RT_REQUIRE) || ((HtmlTag) element).getName().equals(RTTagDescriptorsProvider.RT_IMPORT));
}
});
List<String> importedTags = new ArrayList<String>();
for (PsiElement elem : reqTags) {
String as = ((HtmlTag) elem).getAttributeValue("as");
if (!Strings.isNullOrEmpty(as)) {
importedTags.add(as);
}
}
return importedTags;
}
@Nullable
@Override
public XmlElementDescriptor getDescriptor(XmlTag xmlTag) {
// System.out.println("getDescriptor " + xmlTag);
if (!(xmlTag instanceof HtmlTag && RTProjectComponent.isEnabled(xmlTag.getProject()))) {
return null;
}
final String directiveName = DirectiveUtil.normalizeAttributeName(xmlTag.getName());
if (directiveName.equals(RT_REQUIRE)) {
return new RTRequireTagDescriptor(RT_REQUIRE, xmlTag);
}
if (directiveName.equals(RT_IMPORT)) {
return new RTImportTagDescriptor(RT_IMPORT, xmlTag);
}
if (xmlTag.getContainingFile() instanceof XmlFile) {
List<String> tags = RTHtmlExtension.loadImportedTags((XmlFile) xmlTag.getContainingFile(), xmlTag);
for (String tag : tags) {
if (Strings.areEqual(tag, directiveName)) {
return new RTClassTagDescriptor(directiveName, xmlTag);
}
}
}
// TODO: support required tags
//return new AnyXmlElementDescriptor()
return null;
}
@Override
public XmlAttributeDescriptor[] getAttributeDescriptors(XmlTag xmlTag) {
if (xmlTag instanceof HtmlTag /*&& RTFileUtil.hasRTExt(xmlTag.getContainingFile())*/) {
final Project project = xmlTag.getProject();
if (RTActionUtil.isRTEnabled(project)) {
final Map<String, XmlAttributeDescriptor> result = new LinkedHashMap<String, XmlAttributeDescriptor>();
for (String attr : RTAttributes.ALL_ATTRIBUTES) {
result.put(attr, new RTXmlAttributeDescriptor(attr));
}
// result.put("rt-repeat", new RTXmlAttributeDescriptor("rt-repeat")); // x in [javascript]
// result.put("rt-if", new RTXmlAttributeDescriptor("rt-if")); //[javascript]
// result.put("rt-scope", new RTXmlAttributeDescriptor("rt-scope")); //a as b;c as d
// result.put("rt-class", new RTXmlAttributeDescriptor("rt-class")); // [javascript]
// result.put("rt-props", new RTXmlAttributeDescriptor("rt-props")); // [javascript]
return result.values().toArray(new XmlAttributeDescriptor[result.size()]);
// return new XmlAttributeDescriptor[]{
// new AnyXmlAttributeDescriptor("rt-if"),
// new AnyXmlAttributeDescriptor("rt-repeat"),
// new AnyXmlAttributeDescriptor("rt-scope"),
// new AnyXmlAttributeDescriptor("rt-props"),
// new AnyXmlAttributeDescriptor("rt-class"),
// new AnyXmlAttributeDescriptor("rt-require")
// };
}
}
return XmlAttributeDescriptor.EMPTY;
}
@Nullable
@Override
public XmlElementDescriptor getDescriptor(XmlTag tag) {
if (!(tag instanceof HtmlTag)) {
return null;
}
final XmlNSDescriptor nsDescriptor = tag.getNSDescriptor(tag.getNamespace(), false);
final XmlElementDescriptor descriptor = nsDescriptor != null ? nsDescriptor.getElementDescriptor(tag) : null;
if (descriptor != null && !(descriptor instanceof AnyXmlElementDescriptor)) {
return null;
}
return new ISMLTagDescriptor(tag.getName(), tag);
}
@Override
public void addTagNameVariants(List<LookupElement> elements, XmlTag tag, String prefix) {
if (!(tag instanceof HtmlTag)) return;
for (String tagName : ismlTagNames) {
elements.add(LookupElementBuilder.create(tagName));
}
}
@Nullable
@Override
public XmlElementDescriptor getDescriptor(XmlTag xmlTag) {
// System.out.println("getDescriptor " + xmlTag);
if (!(xmlTag instanceof HtmlTag && RTProjectComponent.isEnabled(xmlTag.getProject()))) {
return null;
}
final String directiveName = DirectiveUtil.normalizeAttributeName(xmlTag.getName());
if (directiveName.equals(RT_REQUIRE)) {
return new RTRequireTagDescriptor(RT_REQUIRE, xmlTag);
}
if (directiveName.equals(RT_IMPORT)) {
return new RTImportTagDescriptor(RT_IMPORT, xmlTag);
}
if (xmlTag.getContainingFile() instanceof XmlFile) {
List<String> tags = RTHtmlExtension.loadImportedTags((XmlFile) xmlTag.getContainingFile(), xmlTag);
for (String tag : tags) {
if (Strings.areEqual(tag, directiveName)) {
return new RTClassTagDescriptor(directiveName, xmlTag);
}
}
}
// TODO: support required tags
//return new AnyXmlElementDescriptor()
return null;
}
@Override
public XmlAttributeDescriptor[] getAttributeDescriptors(XmlTag xmlTag) {
if (xmlTag instanceof HtmlTag /*&& RTFileUtil.hasRTExt(xmlTag.getContainingFile())*/) {
final Project project = xmlTag.getProject();
if (RTActionUtil.isRTEnabled(project)) {
final Map<String, XmlAttributeDescriptor> result = new LinkedHashMap<String, XmlAttributeDescriptor>();
for (String attr : RTAttributes.ALL_ATTRIBUTES) {
result.put(attr, new RTXmlAttributeDescriptor(attr));
}
// result.put("rt-repeat", new RTXmlAttributeDescriptor("rt-repeat")); // x in [javascript]
// result.put("rt-if", new RTXmlAttributeDescriptor("rt-if")); //[javascript]
// result.put("rt-scope", new RTXmlAttributeDescriptor("rt-scope")); //a as b;c as d
// result.put("rt-class", new RTXmlAttributeDescriptor("rt-class")); // [javascript]
// result.put("rt-props", new RTXmlAttributeDescriptor("rt-props")); // [javascript]
return result.values().toArray(new XmlAttributeDescriptor[result.size()]);
// return new XmlAttributeDescriptor[]{
// new AnyXmlAttributeDescriptor("rt-if"),
// new AnyXmlAttributeDescriptor("rt-repeat"),
// new AnyXmlAttributeDescriptor("rt-scope"),
// new AnyXmlAttributeDescriptor("rt-props"),
// new AnyXmlAttributeDescriptor("rt-class"),
// new AnyXmlAttributeDescriptor("rt-require")
// };
}
}
return XmlAttributeDescriptor.EMPTY;
}
/**
* <foo>bar</foo>
*/
public static PsiElementPattern.Capture<PsiElement> getTagTextPattern(@NotNull String... tag) {
return XmlPatterns
.psiElement().withParent(
PlatformPatterns.psiElement(XmlText.class).withParent(
PlatformPatterns.psiElement(HtmlTag.class).withName(tag)
)
)
.inFile(XmlPatterns.psiFile()
.withName(XmlPatterns
.string().endsWith(".twig")
)
);
}