Search in sources :

Example 26 with Language

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;
}
Also used : Language(com.intellij.lang.Language) XMLLanguage(com.intellij.lang.xml.XMLLanguage) XHTMLLanguage(com.intellij.lang.xhtml.XHTMLLanguage) HTMLLanguage(com.intellij.lang.html.HTMLLanguage) XHTMLLanguage(com.intellij.lang.xhtml.XHTMLLanguage) HTMLLanguage(com.intellij.lang.html.HTMLLanguage) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 27 with Language

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;
}
Also used : Language(com.intellij.lang.Language) XMLLanguage(com.intellij.lang.xml.XMLLanguage) XHTMLLanguage(com.intellij.lang.xhtml.XHTMLLanguage) HTMLLanguage(com.intellij.lang.html.HTMLLanguage) NotNull(org.jetbrains.annotations.NotNull) NonNls(org.jetbrains.annotations.NonNls) Nullable(org.jetbrains.annotations.Nullable)

Example 28 with Language

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;
}
Also used : PsiStructureViewFactory(com.intellij.lang.PsiStructureViewFactory) LanguageStructureViewBuilder(com.intellij.lang.LanguageStructureViewBuilder) TreeBasedStructureViewBuilder(com.intellij.ide.structureView.TreeBasedStructureViewBuilder) StructureViewBuilder(com.intellij.ide.structureView.StructureViewBuilder) Language(com.intellij.lang.Language) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with Language

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;
}
Also used : Language(com.intellij.lang.Language) PythonLanguage(com.jetbrains.python.PythonLanguage) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with Language

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();
    }
}
Also used : Language(com.intellij.lang.Language) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange)

Aggregations

Language (com.intellij.lang.Language)292 NotNull (org.jetbrains.annotations.NotNull)58 Nullable (org.jetbrains.annotations.Nullable)49 PsiElement (com.intellij.psi.PsiElement)46 PsiFile (com.intellij.psi.PsiFile)45 FileType (com.intellij.openapi.fileTypes.FileType)31 Project (com.intellij.openapi.project.Project)31 TextRange (com.intellij.openapi.util.TextRange)25 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)21 FileViewProvider (com.intellij.psi.FileViewProvider)21 HbLanguage (com.dmarcotte.handlebars.HbLanguage)20 JavaLanguage (com.intellij.lang.java.JavaLanguage)16 Editor (com.intellij.openapi.editor.Editor)14 XMLLanguage (com.intellij.lang.xml.XMLLanguage)12 TemplateLanguageFileViewProvider (com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider)11 List (java.util.List)11 HTMLLanguage (com.intellij.lang.html.HTMLLanguage)9 JavascriptLanguage (com.intellij.lang.javascript.JavascriptLanguage)9 Document (com.intellij.openapi.editor.Document)9