Search in sources :

Example 1 with XmlAttributeValueImpl

use of com.intellij.psi.impl.source.xml.XmlAttributeValueImpl in project intellij-community by JetBrains.

the class XPathLanguageInjector method getLanguagesToInject.

public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
    final XmlAttribute attribute = (XmlAttribute) context;
    if (!XsltSupport.isXPathAttribute(attribute))
        return;
    XmlAttributeValueImpl value = (XmlAttributeValueImpl) attribute.getValueElement();
    if (value == null)
        return;
    ASTNode type = value.findChildByType(XmlElementType.XML_ENTITY_REF);
    // workaround for inability to inject into text with entity refs (e.g. IDEA-72972) TODO: fix it
    if (type != null)
        return;
    final XsltChecker.LanguageLevel languageLevel = XsltSupport.getXsltLanguageLevel(attribute.getContainingFile());
    final TextRange[] ranges = getInjectionRanges(attribute, languageLevel);
    for (TextRange range : ranges) {
        // workaround for http://www.jetbrains.net/jira/browse/IDEA-10096
        TextRange rangeInsideHost;
        String prefix;
        if (range instanceof AVTRange) {
            if (((AVTRange) range).myComplete) {
                rangeInsideHost = range.shiftRight(2).grown(-2);
                prefix = "";
            } else {
                // we need to keep the "'}' expected" parse error
                rangeInsideHost = range.shiftRight(2).grown(-1);
                prefix = "{";
            }
        } else {
            rangeInsideHost = range;
            prefix = "";
        }
        if (value.getTextRange().contains(rangeInsideHost.shiftRight(value.getTextRange().getStartOffset()))) {
            registrar.startInjecting(languageLevel.getXPathVersion().getLanguage()).addPlace(prefix, "", value, rangeInsideHost).doneInjecting();
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValueImpl(com.intellij.psi.impl.source.xml.XmlAttributeValueImpl)

Example 2 with XmlAttributeValueImpl

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

the class Angular2Injector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
    final Project project = context.getProject();
    if (!AngularIndexUtil.hasAngularJS2(project))
        return;
    final PsiElement parent = context.getParent();
    if (context instanceof JSLiteralExpressionImpl && ((JSLiteralExpressionImpl) context).isQuotedLiteral()) {
        if (injectIntoDirectiveProperty(registrar, context, parent, "template", Angular2HTMLLanguage.INSTANCE))
            return;
        if (injectIntoEmbeddedLiteral(registrar, context, parent))
            return;
        if (parent instanceof JSArrayLiteralExpression) {
            final JSProperty property = ObjectUtils.tryCast(parent.getParent(), JSProperty.class);
            if (injectIntoDirectiveProperty(registrar, context, property, "styles", CSSLanguage.INSTANCE))
                return;
        }
        if (parent instanceof JSProperty && parent.getParent() instanceof JSObjectLiteralExpression) {
            injectIntoDirectiveProperty(registrar, context, parent.getParent().getParent(), "host", AngularJSLanguage.INSTANCE);
        }
        return;
    }
    if (context instanceof XmlAttributeValueImpl && parent instanceof XmlAttribute) {
        final int length = context.getTextLength();
        final String name = ((XmlAttribute) parent).getName();
        if (isInjectableAttribute(project, length, name)) {
            registrar.startInjecting(AngularJSLanguage.INSTANCE).addPlace(null, null, (PsiLanguageInjectionHost) context, ElementManipulators.getValueTextRange(context)).doneInjecting();
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) JSLiteralExpressionImpl(com.intellij.lang.javascript.psi.impl.JSLiteralExpressionImpl) PsiElement(com.intellij.psi.PsiElement) XmlAttributeValueImpl(com.intellij.psi.impl.source.xml.XmlAttributeValueImpl)

Example 3 with XmlAttributeValueImpl

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

Example 4 with XmlAttributeValueImpl

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

the class AngularJSAsExpression method isAsControllerRef.

public static boolean isAsControllerRef(PsiReference ref, PsiElement parent) {
    if (parent instanceof AngularJSAsExpression && ref == parent.getFirstChild()) {
        return true;
    }
    final InjectedLanguageManager injector = InjectedLanguageManager.getInstance(parent.getProject());
    final PsiLanguageInjectionHost host = injector.getInjectionHost(parent);
    final PsiElement hostParent = host instanceof XmlAttributeValueImpl ? host.getParent() : null;
    final String normalized = hostParent instanceof XmlAttribute ? DirectiveUtil.normalizeAttributeName(((XmlAttribute) hostParent).getName()) : null;
    return "ng-controller".equals(normalized);
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiElement(com.intellij.psi.PsiElement) XmlAttributeValueImpl(com.intellij.psi.impl.source.xml.XmlAttributeValueImpl)

Aggregations

XmlAttributeValueImpl (com.intellij.psi.impl.source.xml.XmlAttributeValueImpl)4 XmlAttribute (com.intellij.psi.xml.XmlAttribute)4 PsiElement (com.intellij.psi.PsiElement)3 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)3 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 ASTNode (com.intellij.lang.ASTNode)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 JSLiteralExpressionImpl (com.intellij.lang.javascript.psi.impl.JSLiteralExpressionImpl)1 XmlTextImpl (com.intellij.psi.impl.source.xml.XmlTextImpl)1 OuterLanguageElement (com.intellij.psi.templateLanguages.OuterLanguageElement)1