Search in sources :

Example 1 with XmlDocumentImpl

use of com.intellij.psi.impl.source.xml.XmlDocumentImpl 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

TextRange (com.intellij.openapi.util.TextRange)1 FileViewProvider (com.intellij.psi.FileViewProvider)1 PsiElement (com.intellij.psi.PsiElement)1 XmlDocumentImpl (com.intellij.psi.impl.source.xml.XmlDocumentImpl)1 XmlText (com.intellij.psi.xml.XmlText)1 Nullable (org.jetbrains.annotations.Nullable)1