use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class XmlUtil method createChildTag.
/**
* @param xmlTag
* @param localName
* @param namespace
* @param bodyText pass null to create collapsed tag, empty string means creating expanded one
* @param enforceNamespacesDeep
* @return
*/
public static XmlTag createChildTag(final XmlTag xmlTag, String localName, String namespace, @Nullable String bodyText, boolean enforceNamespacesDeep) {
String qname;
final String prefix = xmlTag.getPrefixByNamespace(namespace);
if (prefix != null && !prefix.isEmpty()) {
qname = prefix + ":" + localName;
} else {
qname = localName;
}
try {
String tagStart = qname + (!StringUtil.isEmpty(namespace) && xmlTag.getPrefixByNamespace(namespace) == null && !(StringUtil.isEmpty(xmlTag.getNamespacePrefix()) && namespace.equals(xmlTag.getNamespace())) ? " xmlns=\"" + namespace + "\"" : "");
Language language = xmlTag.getLanguage();
if (!(language instanceof HTMLLanguage))
language = XMLLanguage.INSTANCE;
XmlTag retTag;
if (bodyText != null) {
retTag = XmlElementFactory.getInstance(xmlTag.getProject()).createTagFromText("<" + tagStart + ">" + bodyText + "</" + qname + ">", language);
if (enforceNamespacesDeep) {
retTag.acceptChildren(new XmlRecursiveElementVisitor() {
@Override
public void visitXmlTag(XmlTag tag) {
final String namespacePrefix = tag.getNamespacePrefix();
if (namespacePrefix.isEmpty()) {
String qname;
if (prefix != null && !prefix.isEmpty()) {
qname = prefix + ":" + tag.getLocalName();
} else {
qname = tag.getLocalName();
}
try {
tag.setName(qname);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
super.visitXmlTag(tag);
}
});
}
} else {
retTag = XmlElementFactory.getInstance(xmlTag.getProject()).createTagFromText("<" + tagStart + "/>", language);
}
return retTag;
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return null;
}
use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class XmlUtil method getDefaultNamespaces.
@Nullable
@NonNls
public static String[][] getDefaultNamespaces(final XmlDocument document) {
final XmlFile file = getContainingFile(document);
final XmlTag tag = document.getRootTag();
if (tag == null)
return null;
@NotNull final XmlFileNSInfoProvider[] nsProviders = Extensions.getExtensions(XmlFileNSInfoProvider.EP_NAME);
if (file != null) {
NextProvider: for (XmlFileNSInfoProvider nsProvider : nsProviders) {
final String[][] pairs = nsProvider.getDefaultNamespaces(file);
if (pairs != null && pairs.length > 0) {
for (final String[] nsMapping : pairs) {
if (nsMapping == null || nsMapping.length != 2 || nsMapping[0] == null || nsMapping[1] == null) {
LOG.debug("NSInfoProvider " + nsProvider + " gave wrong info about " + file.getVirtualFile());
continue NextProvider;
}
}
return pairs;
}
}
}
String namespace = getDtdUri(document);
if (namespace != null) {
boolean overrideNamespaceFromDocType = false;
if (file != null) {
for (XmlFileNSInfoProvider provider : nsProviders) {
try {
if (provider.overrideNamespaceFromDocType(file)) {
overrideNamespaceFromDocType = true;
break;
}
} catch (AbstractMethodError ignored) {
}
}
}
if (!overrideNamespaceFromDocType)
return new String[][] { new String[] { "", namespace } };
}
if ("taglib".equals(tag.getName())) {
return new String[][] { new String[] { "", TAGLIB_1_2_URI } };
}
if (file != null) {
final Language language = file.getLanguage();
if (language.isKindOf(HTMLLanguage.INSTANCE) || language == XHTMLLanguage.INSTANCE) {
return new String[][] { new String[] { "", XHTML_URI } };
}
}
return null;
}
use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class XmlStructureViewBuilderFactory method getStructureViewBuilderForExtensions.
@Nullable
private static StructureViewBuilder getStructureViewBuilderForExtensions(final PsiFile psiFile) {
for (Language language : XMLLanguage.INSTANCE.getLanguageExtensionsForFile(psiFile)) {
PsiStructureViewFactory factory = LanguageStructureViewBuilder.INSTANCE.forLanguage(language);
if (factory == null)
continue;
final StructureViewBuilder builder = factory.getStructureViewBuilder(psiFile);
if (builder != null) {
return builder;
}
}
return null;
}
use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class RestFileViewProvider method getLanguages.
@Override
@NotNull
public Set<Language> getLanguages() {
if (myLanguages == null) {
final Set<Language> languages = ContainerUtil.newHashSet();
languages.add(getBaseLanguage());
Language djangoTemplateLanguage = Language.findLanguageByID("DjangoTemplate");
if (djangoTemplateLanguage != null) {
languages.add(djangoTemplateLanguage);
}
languages.add(getTemplateDataLanguage());
myLanguages = languages;
}
return myLanguages;
}
use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class HtmlConditionalCommentInjector method getLanguagesToInject.
@Override
public void getLanguagesToInject(@NotNull final MultiHostRegistrar registrar, @NotNull final PsiElement host) {
Pair<ASTNode, ASTNode> pair = parseConditionalCommentBoundaries(host);
if (pair == null) {
return;
}
final TextRange textRange = host.getTextRange();
final int startOffset = textRange.getStartOffset();
Language language = host.getParent().getLanguage();
ASTNode conditionalStart = pair.first;
ASTNode conditionalEnd = pair.second;
TextRange range = new UnfairTextRange(conditionalStart.getTextRange().getEndOffset() - startOffset, conditionalEnd.getStartOffset() - startOffset);
if (range.getStartOffset() < range.getEndOffset()) {
registrar.startInjecting(language).addPlace(null, null, (PsiLanguageInjectionHost) host, range).doneInjecting();
}
}
Aggregations