Search in sources :

Example 16 with PsiLanguageInjectionHost

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

the class EditInjectionSettingsAction method invokeImpl.

private static void invokeImpl(Project project, Editor editor, PsiFile file) {
    final PsiFile psiFile = InjectedLanguageUtil.findInjectedPsiNoCommit(file, editor.getCaretModel().getOffset());
    if (psiFile == null)
        return;
    final PsiLanguageInjectionHost host = InjectedLanguageManager.getInstance(project).getInjectionHost(psiFile);
    if (host == null)
        return;
    final LanguageInjectionSupport support = psiFile.getUserData(LanguageInjectionSupport.SETTINGS_EDITOR);
    if (support == null)
        return;
    try {
        if (!support.editInjectionInPlace(host)) {
            ShowSettingsUtil.getInstance().editConfigurable(project, new InjectionsSettingsUI(project, Configuration.getProjectInstance(project)));
        }
    } finally {
        FileContentUtil.reparseFiles(project, Collections.<VirtualFile>emptyList(), true);
    }
}
Also used : PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiFile(com.intellij.psi.PsiFile) InjectionsSettingsUI(org.intellij.plugins.intelliLang.InjectionsSettingsUI)

Example 17 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-plugins by JetBrains.

the class MxmlLanguageInjector method injectInMxmlFile.

private static void injectInMxmlFile(final MultiHostRegistrar registrar, final PsiElement host, final PsiMetaData descriptor, XmlTag tag) {
    int offset = host instanceof XmlText ? 0 : 1;
    if (descriptor instanceof AnnotationBackedDescriptor && ((XmlElementDescriptorWithCDataContent) descriptor).requiresCdataBracesInContext(tag)) {
        final int length = host.getTextLength();
        if (length < 2 * offset)
            return;
        String type = ((AnnotationBackedDescriptor) descriptor).getType();
        if (type == null)
            type = "*";
        @NonNls String prefix = "(function (event:" + type + ") {";
        @NonNls String suffix = "})(null);";
        if (host instanceof XmlText) {
            JSLanguageInjector.injectToXmlText(registrar, host, JavaScriptSupportLoader.ECMA_SCRIPT_L4, prefix, suffix);
        } else {
            if (JSCommonTypeNames.FUNCTION_CLASS_NAME.equals(type) && host.textContains('{')) {
                final String text = StringUtil.stripQuotesAroundValue(host.getText());
                if (text.startsWith("{") && text.endsWith("}")) {
                    prefix = FUNCTION_CALL_PREFIX;
                    suffix = FUNCTION_CALL_SUFFIX;
                    offset++;
                }
            }
            TextRange range = new TextRange(offset, length - offset);
            registrar.startInjecting(JavaScriptSupportLoader.ECMA_SCRIPT_L4).addPlace(prefix, (host instanceof XmlAttributeValue ? "\n" : "") + suffix, (PsiLanguageInjectionHost) host, range).doneInjecting();
        }
    } else if (!(host instanceof XmlText) || !hasCDATA((XmlText) host)) {
        final String text = StringUtil.stripQuotesAroundValue(host.getText());
        int openedBraces = 0;
        int start = -1;
        boolean addedSomething = false;
        boolean quoted = false;
        for (int i = 0; i < text.length(); ++i) {
            final char ch = text.charAt(i);
            if (quoted) {
                quoted = false;
                continue;
            }
            if (ch == '\\') {
                quoted = true;
            } else if (ch == '{') {
                if (openedBraces == 0)
                    start = i + 1;
                openedBraces++;
            } else if (ch == '}') {
                openedBraces--;
                if (openedBraces == 0 && start != -1) {
                    registrar.startInjecting(JavaScriptSupportLoader.ECMA_SCRIPT_L4).addPlace(FUNCTION_CALL_PREFIX, FUNCTION_CALL_SUFFIX, (PsiLanguageInjectionHost) host, new TextRange(offset + start, i + offset)).doneInjecting();
                    addedSomething = true;
                    start = -1;
                }
            }
        }
        if (!addedSomething) {
            final String trimmedText = text.trim();
            start = trimmedText.indexOf("@");
            if (start == 0 && trimmedText.length() > 1 && Character.isUpperCase(trimmedText.charAt(1))) {
                // @id can be reference to attribute
                offset += text.indexOf(trimmedText);
                registrar.startInjecting(JavaScriptSupportLoader.ECMA_SCRIPT_L4).addPlace(null, null, (PsiLanguageInjectionHost) host, new TextRange(offset, trimmedText.length() + offset)).doneInjecting();
            }
        }
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) TextRange(com.intellij.openapi.util.TextRange) AnnotationBackedDescriptor(com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)

Example 18 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-plugins by JetBrains.

the class AngularJSProcessor method eventScopeMatches.

private static boolean eventScopeMatches(InjectedLanguageManager injector, PsiElement element, PsiElement parent) {
    XmlAttribute attribute = PsiTreeUtil.getNonStrictParentOfType(element, XmlAttribute.class);
    if (attribute == null) {
        final PsiLanguageInjectionHost elementContainer = injector.getInjectionHost(element);
        attribute = PsiTreeUtil.getNonStrictParentOfType(elementContainer, XmlAttribute.class);
    }
    return attribute != null && CompletionUtil.getOriginalOrSelf(attribute) == CompletionUtil.getOriginalOrSelf(parent);
}
Also used : PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost)

Example 19 with PsiLanguageInjectionHost

use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-plugins by JetBrains.

the class TilesOgnlInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
    final PsiFile containingFile = context.getContainingFile();
    if (!JamCommonUtil.isPlainXmlFile(containingFile)) {
        return;
    }
    assert context instanceof XmlAttributeValue;
    if (!((XmlAttributeValue) context).getValue().startsWith(OGNL_PREFIX)) {
        return;
    }
    PsiElement parent = context.getParent();
    if (parent instanceof XmlAttribute) {
        String name = ((XmlAttribute) parent).getLocalName();
        if ("expression".equals(name) || "templateExpression".equals(name)) {
            DomElement domElement = DomManager.getDomManager(context.getProject()).getDomElement((XmlTag) parent.getParent());
            if (domElement instanceof Put || domElement instanceof Add || domElement instanceof Definition) {
                final TextRange attributeTextRange = ElementManipulators.getValueTextRange(context);
                final TextRange ognlTextRange = TextRange.from(attributeTextRange.getStartOffset() + OGNL_PREFIX.length(), attributeTextRange.getLength() - OGNL_PREFIX.length());
                registrar.startInjecting(OgnlLanguage.INSTANCE).addPlace(OgnlLanguage.EXPRESSION_PREFIX, OgnlLanguage.EXPRESSION_SUFFIX, (PsiLanguageInjectionHost) context, ognlTextRange).doneInjecting();
            }
        }
    }
}
Also used : Add(com.intellij.struts.dom.tiles.Add) XmlAttribute(com.intellij.psi.xml.XmlAttribute) DomElement(com.intellij.util.xml.DomElement) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) Definition(com.intellij.struts.dom.tiles.Definition) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement) Put(com.intellij.struts.dom.tiles.Put)

Example 20 with PsiLanguageInjectionHost

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

the class JavaCheckRegexpWithFlagsTest method setUpRegexpInjectionAndGetRegexpFile.

@NotNull
private PsiFile setUpRegexpInjectionAndGetRegexpFile() {
    final PsiFile file = getFile();
    int offsetWithRegexp = file.getText().indexOf("Pattern.compile(\"") + "Pattern.compile(\"".length();
    final PsiElement stringLiteralLeaf = file.findElementAt(offsetWithRegexp);
    assertNotNull(stringLiteralLeaf);
    assertNotNull(stringLiteralLeaf.getParent());
    assertTrue(stringLiteralLeaf.getParent() instanceof PsiLanguageInjectionHost);
    final PsiLanguageInjectionHost elementWithInjection = ((PsiLanguageInjectionHost) stringLiteralLeaf.getParent());
    InjectedLanguageUtil.enumerate(elementWithInjection, file, false, new PsiLanguageInjectionHost.InjectedPsiVisitor() {

        @Override
        public void visit(@NotNull final PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
        }
    });
    assertTrue(InjectedLanguageUtil.hasInjections(elementWithInjection));
    final PsiElement elementInInjected = InjectedLanguageUtil.findElementInInjected(elementWithInjection, offsetWithRegexp);
    final PsiFile regexpFile = PsiTreeUtil.getParentOfType(elementInInjected, PsiFile.class);
    assertNotNull(regexpFile);
    return regexpFile;
}
Also used : PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)41 PsiElement (com.intellij.psi.PsiElement)23 TextRange (com.intellij.openapi.util.TextRange)19 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)10 NotNull (org.jetbrains.annotations.NotNull)10 PsiFile (com.intellij.psi.PsiFile)9 Pair (com.intellij.openapi.util.Pair)8 Language (com.intellij.lang.Language)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)5 List (java.util.List)4 Nullable (org.jetbrains.annotations.Nullable)4 ASTNode (com.intellij.lang.ASTNode)3 XmlAttributeValueImpl (com.intellij.psi.impl.source.xml.XmlAttributeValueImpl)3 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)3 InjectedLanguage (org.intellij.plugins.intelliLang.inject.InjectedLanguage)3 NonNls (org.jetbrains.annotations.NonNls)3 Project (com.intellij.openapi.project.Project)2 HtmlTag (com.intellij.psi.html.HtmlTag)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 AnnotationBackedDescriptor (com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)1