Search in sources :

Example 21 with XmlText

use of com.intellij.psi.xml.XmlText in project intellij-community by JetBrains.

the class HtmlScriptLanguageInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement host) {
    if (!host.isValid() || !(host instanceof XmlText) || !HtmlUtil.isHtmlTagContainingFile(host)) {
        return;
    }
    XmlTag scriptTag = ((XmlText) host).getParentTag();
    if (scriptTag == null) {
        return;
    }
    final Language language = getScriptLanguageToInject(scriptTag);
    if (language == null || HtmlScriptInjectionBlockerExtension.isInjectionBlocked(scriptTag, language)) {
        return;
    }
    if (LanguageUtil.isInjectableLanguage(language)) {
        registrar.startInjecting(language).addPlace(null, null, (PsiLanguageInjectionHost) host, TextRange.create(0, host.getTextLength())).doneInjecting();
    }
}
Also used : Language(com.intellij.lang.Language) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) XmlText(com.intellij.psi.xml.XmlText) XmlTag(com.intellij.psi.xml.XmlTag)

Example 22 with XmlText

use of com.intellij.psi.xml.XmlText in project intellij-community by JetBrains.

the class XmlTagWriteTest method test3.

public void test3() throws Exception {
    final XmlTag tag = XmlElementFactory.getInstance(getProject()).createTagFromText("<b>\n0123456</b>");
    final XmlText text = (XmlText) tag.getValue().getChildren()[0];
    String textS = text.getText();
    ApplicationManager.getApplication().runWriteAction(() -> text.insertText("lala", 2));
    XmlText text2 = (XmlText) tag.getValue().getChildren()[0];
    assertEquals(textS.substring(0, 2) + "lala" + textS.substring(2), text2.getText());
}
Also used : XmlText(com.intellij.psi.xml.XmlText) XmlTag(com.intellij.psi.xml.XmlTag)

Example 23 with XmlText

use of com.intellij.psi.xml.XmlText in project intellij-community by JetBrains.

the class XmlTagManipulator method getRangeInElement.

@Override
@NotNull
public TextRange getRangeInElement(@NotNull final XmlTag tag) {
    if (tag.getSubTags().length > 0) {
        // Text range in tag with subtags is not supported, return empty range, consider making this function nullable.
        return TextRange.EMPTY_RANGE;
    }
    final XmlTagValue value = tag.getValue();
    final XmlText[] texts = value.getTextElements();
    switch(texts.length) {
        case 0:
            return value.getTextRange().shiftRight(-tag.getTextOffset());
        case 1:
            return getValueRange(texts[0]);
        default:
            return TextRange.EMPTY_RANGE;
    }
}
Also used : XmlTagValue(com.intellij.psi.xml.XmlTagValue) XmlText(com.intellij.psi.xml.XmlText) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with XmlText

use of com.intellij.psi.xml.XmlText in project oxy-template-support-plugin by mutant-industries.

the class OxyTemplateInjectedBlockBuilder method buildInjectedBlocks.

@Nullable
public ASTNode buildInjectedBlocks(@NotNull List<Block> result, @NotNull ASTNode injectedNode, @Nullable Indent indent, @NotNull final TextRange allowedRange) {
    FileViewProvider viewProvider = injectedNode.getPsi().getContainingFile().getViewProvider();
    TextRange allowedRangeLocal = allowedRange;
    PsiElement elementAt = viewProvider.findElementAt(injectedNode.getStartOffset(), HTMLLanguage.INSTANCE);
    while (elementAt != null) {
        if (elementAt.getNode().getElementType() == getOuterLanguageElement()) {
            break;
        } else if (!allowedRangeLocal.contains(elementAt.getTextRange())) {
            /**
             * It is important to cover all content within allowedRange. When the current element doesn't fit in
             * (e.g. unclosed tags etc.), let's just try it's first child.
             */
            while (elementAt.getFirstChild() != null && !allowedRangeLocal.contains((elementAt = elementAt.getFirstChild()).getTextRange())) ;
        }
        /**
         * shift to topmost element, that covers the range, e.g.:
         * ...html...<m:foo... - T_XML_TAG_START -> MACRO_[EMPTY_]TAG
         */
        while (OxyTemplateHelper.checkRangeContainsParent(elementAt, allowedRangeLocal, getOuterLanguageElement())) {
            elementAt = elementAt.getParent();
        }
        if (!FormatterUtil.containsWhiteSpacesOnly(elementAt.getNode())) {
            TextRange intersection = elementAt.getNode().getTextRange().intersection(allowedRangeLocal);
            assert intersection != null;
            result.add(createInjectedBlock(elementAt.getNode(), null, null, indent, intersection));
        }
        while (elementAt.getNextSibling() == null && !(elementAt.getParent() instanceof XmlDocumentImpl)) {
            elementAt = elementAt.getParent();
        }
        elementAt = elementAt.getNextSibling();
        if (elementAt != null) {
            if (elementAt instanceof XmlText && OxyTemplateHelper.containsElement(elementAt, allowedRangeLocal, getOuterLanguageElement())) {
                elementAt = elementAt.getFirstChild();
            }
            if (elementAt.getNode().getStartOffset() >= allowedRange.getEndOffset()) {
                break;
            }
            allowedRangeLocal = TextRange.create(elementAt.getNode().getStartOffset(), allowedRange.getEndOffset());
        }
    }
    return shiftOriginalNode(injectedNode, result);
}
Also used : FileViewProvider(com.intellij.psi.FileViewProvider) TextRange(com.intellij.openapi.util.TextRange) XmlText(com.intellij.psi.xml.XmlText) XmlDocumentImpl(com.intellij.psi.impl.source.xml.XmlDocumentImpl) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlText (com.intellij.psi.xml.XmlText)24 XmlTag (com.intellij.psi.xml.XmlTag)16 PsiElement (com.intellij.psi.PsiElement)12 ASTNode (com.intellij.lang.ASTNode)5 TextRange (com.intellij.openapi.util.TextRange)3 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)3 XmlFile (com.intellij.psi.xml.XmlFile)3 Nullable (org.jetbrains.annotations.Nullable)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 Language (com.intellij.lang.Language)2 Project (com.intellij.openapi.project.Project)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 XmlDocument (com.intellij.psi.xml.XmlDocument)2 XmlTagValue (com.intellij.psi.xml.XmlTagValue)2 DomElement (com.intellij.util.xml.DomElement)2 NotNull (org.jetbrains.annotations.NotNull)2 MavenProjectIndicesManager (org.jetbrains.idea.maven.indices.MavenProjectIndicesManager)2 TypographyDetector (com.android.tools.lint.checks.TypographyDetector)1 NegatingComparable (com.intellij.codeInsight.completion.impl.NegatingComparable)1 LookupElementWeigher (com.intellij.codeInsight.lookup.LookupElementWeigher)1