Search in sources :

Example 1 with XmlTextImpl

use of com.intellij.psi.impl.source.xml.XmlTextImpl in project intellij-plugins by JetBrains.

the class AngularJSInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
    // check that we have angular directives indexed before injecting
    final Project project = context.getProject();
    if (!AngularIndexUtil.hasAngularJS(project))
        return;
    final PsiElement parent = context.getParent();
    if (context instanceof XmlAttributeValueImpl && parent instanceof XmlAttribute) {
        final String value = context.getText();
        final int start = value.startsWith("'") || value.startsWith("\"") ? 1 : 0;
        final int end = value.endsWith("'") || value.endsWith("\"") ? 1 : 0;
        final int length = value.length();
        if (AngularAttributesRegistry.isAngularExpressionAttribute((XmlAttribute) parent) && length > 1) {
            registrar.startInjecting(AngularJSLanguage.INSTANCE).addPlace(null, null, (PsiLanguageInjectionHost) context, new TextRange(start, length - end)).doneInjecting();
            return;
        }
        if (AngularAttributesRegistry.isJSONAttribute((XmlAttribute) parent) && length > 1) {
            registrar.startInjecting(JsonLanguage.INSTANCE).addPlace(null, null, (PsiLanguageInjectionHost) context, new TextRange(start, length - end)).doneInjecting();
            return;
        }
    }
    if (context instanceof XmlTextImpl || context instanceof XmlAttributeValueImpl) {
        final String start = AngularJSBracesUtil.getInjectionStart(project);
        final String end = AngularJSBracesUtil.getInjectionEnd(project);
        if (AngularJSBracesUtil.hasConflicts(start, end, context))
            return;
        final String text = context.getText();
        int endIndex = -1;
        int afterStart = -1;
        while (true) {
            final int startIdx = text.indexOf(start, Math.max(endIndex, afterStart));
            afterStart = startIdx < 0 ? -1 : (startIdx + start.length());
            if (afterStart < 0)
                return;
            endIndex = afterStart;
            endIndex = InjectorMatchingEndFinder.findMatchingEnd(start, end, text, endIndex);
            endIndex = endIndex > 0 ? endIndex : ElementManipulators.getValueTextRange(context).getEndOffset();
            final PsiElement injectionCandidate = afterStart >= 0 ? context.findElementAt(afterStart) : null;
            if (injectionCandidate != null && injectionCandidate.getNode().getElementType() != XmlTokenType.XML_COMMENT_CHARACTERS && !(injectionCandidate instanceof OuterLanguageElement)) {
                if (afterStart > endIndex) {
                    if (ApplicationManager.getApplication().isInternal()) {
                        LOG.error("Braces: " + start + "," + end + "\n" + "Text: \"" + text + "\"" + "\n" + "Interval: (" + afterStart + "," + endIndex + ")" + "\n" + "File: " + context.getContainingFile().getName() + ", language:" + context.getContainingFile().getLanguage());
                    }
                    return;
                }
                registrar.startInjecting(AngularJSLanguage.INSTANCE).addPlace(null, null, (PsiLanguageInjectionHost) context, new TextRange(afterStart, endIndex)).doneInjecting();
            }
        }
    }
}
Also used : XmlTextImpl(com.intellij.psi.impl.source.xml.XmlTextImpl) Project(com.intellij.openapi.project.Project) OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) XmlAttributeValueImpl(com.intellij.psi.impl.source.xml.XmlAttributeValueImpl)

Aggregations

Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)1 XmlAttributeValueImpl (com.intellij.psi.impl.source.xml.XmlAttributeValueImpl)1 XmlTextImpl (com.intellij.psi.impl.source.xml.XmlTextImpl)1 OuterLanguageElement (com.intellij.psi.templateLanguages.OuterLanguageElement)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1