Search in sources :

Example 1 with InjectedLanguage

use of org.intellij.plugins.intelliLang.inject.InjectedLanguage in project intellij-community by JetBrains.

the class PyTemporaryInjector method getInjectedLanguage.

@Nullable
@Override
public Language getInjectedLanguage(@NotNull PsiElement context) {
    final TemporaryPlacesRegistry registry = TemporaryPlacesRegistry.getInstance(context.getProject());
    if (context instanceof PsiLanguageInjectionHost) {
        final PsiFile file = context.getContainingFile();
        final InjectedLanguage injectedLanguage = registry.getLanguageFor((PsiLanguageInjectionHost) context, file);
        if (injectedLanguage != null) {
            return injectedLanguage.getLanguage();
        }
    }
    return null;
}
Also used : TemporaryPlacesRegistry(org.intellij.plugins.intelliLang.inject.TemporaryPlacesRegistry) InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with InjectedLanguage

use of org.intellij.plugins.intelliLang.inject.InjectedLanguage in project intellij-community by JetBrains.

the class ConcatenationInjector method getLanguagesToInject.

public void getLanguagesToInject(@NotNull final MultiHostRegistrar registrar, @NotNull PsiElement... operands) {
    if (operands.length == 0)
        return;
    boolean hasLiteral = false;
    InjectedLanguage tempInjectedLanguage = null;
    PsiFile containingFile = null;
    for (PsiElement operand : operands) {
        if (PsiUtilEx.isStringOrCharacterLiteral(operand)) {
            hasLiteral = true;
            if (containingFile == null) {
                containingFile = operands[0].getContainingFile();
            }
            tempInjectedLanguage = myTemporaryPlacesRegistry.getLanguageFor((PsiLanguageInjectionHost) operand, containingFile);
            if (tempInjectedLanguage != null)
                break;
        }
    }
    if (!hasLiteral)
        return;
    final Language tempLanguage = tempInjectedLanguage == null ? null : tempInjectedLanguage.getLanguage();
    final PsiFile finalContainingFile = containingFile;
    InjectionProcessor injectionProcessor = new InjectionProcessor(myConfiguration, mySupport, operands) {

        @Override
        protected void processInjection(Language language, List<Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange>> list, boolean settingsAvailable, boolean unparsable) {
            InjectorUtils.registerInjection(language, list, finalContainingFile, registrar);
            InjectorUtils.registerSupport(mySupport, settingsAvailable, registrar);
            InjectorUtils.putInjectedFileUserData(registrar, InjectedLanguageUtil.FRANKENSTEIN_INJECTION, unparsable ? Boolean.TRUE : null);
        }

        @Override
        protected boolean areThereInjectionsWithName(String methodName, boolean annoOnly) {
            if (methodName == null)
                return false;
            if (getAnnotatedElementsValue().contains(methodName)) {
                return true;
            }
            if (!annoOnly && getXmlAnnotatedElementsValue().contains(methodName)) {
                return true;
            }
            return false;
        }
    };
    if (tempLanguage != null) {
        BaseInjection baseInjection = new BaseInjection(JavaLanguageInjectionSupport.JAVA_SUPPORT_ID);
        baseInjection.setInjectedLanguageId(tempInjectedLanguage.getID());
        injectionProcessor.processInjectionInner(baseInjection, false);
        InjectorUtils.putInjectedFileUserData(registrar, LanguageInjectionSupport.TEMPORARY_INJECTED_LANGUAGE, tempInjectedLanguage);
    } else {
        injectionProcessor.processInjections();
    }
}
Also used : InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) Language(com.intellij.lang.Language) InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) TextRange(com.intellij.openapi.util.TextRange) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection)

Example 3 with InjectedLanguage

use of org.intellij.plugins.intelliLang.inject.InjectedLanguage in project intellij-community by JetBrains.

the class XmlLanguageInjector method getLanguagesToInject.

public void getLanguagesToInject(@NotNull final MultiHostRegistrar registrar, @NotNull PsiElement host) {
    final XmlElement xmlElement = (XmlElement) host;
    if (!isInIndex(xmlElement))
        return;
    final TreeSet<TextRange> ranges = new TreeSet<>(InjectorUtils.RANGE_COMPARATOR);
    final PsiFile containingFile = xmlElement.getContainingFile();
    final Ref<Boolean> unparsableRef = Ref.create();
    getInjectedLanguage(xmlElement, unparsableRef, (language, list) -> {
        for (Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange> trinity : list) {
            if (ranges.contains(trinity.third.shiftRight(trinity.first.getTextRange().getStartOffset())))
                return true;
        }
        for (Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange> trinity : list) {
            final PsiLanguageInjectionHost host1 = trinity.first;
            if (host1.getContainingFile() != containingFile)
                continue;
            final TextRange textRange = trinity.third;
            ranges.add(textRange.shiftRight(host1.getTextRange().getStartOffset()));
        }
        InjectorUtils.registerInjection(language, list, containingFile, registrar);
        InjectorUtils.registerSupport(mySupport, true, registrar);
        if (Boolean.TRUE.equals(unparsableRef.get())) {
            InjectorUtils.putInjectedFileUserData(registrar, InjectedLanguageUtil.FRANKENSTEIN_INJECTION, Boolean.TRUE);
        }
        return true;
    });
}
Also used : InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) TextRange(com.intellij.openapi.util.TextRange) PsiFile(com.intellij.psi.PsiFile)

Example 4 with InjectedLanguage

use of org.intellij.plugins.intelliLang.inject.InjectedLanguage in project intellij-community by JetBrains.

the class XmlLanguageInjector method getInjectedLanguage.

void getInjectedLanguage(final PsiElement place, final Ref<Boolean> unparsableRef, final PairProcessor<Language, List<Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange>>> processor) {
    if (place instanceof XmlTag) {
        final XmlTag xmlTag = (XmlTag) place;
        List<BaseInjection> injections = myConfiguration.getInjections(XmlLanguageInjectionSupport.XML_SUPPORT_ID);
        //noinspection ForLoopReplaceableByForEach
        for (int i = 0, injectionsSize = injections.size(); i < injectionsSize; i++) {
            final BaseInjection injection = injections.get(i);
            if (injection.acceptsPsiElement(xmlTag)) {
                final Language language = InjectedLanguage.findLanguageById(injection.getInjectedLanguageId());
                if (language == null)
                    continue;
                final boolean separateFiles = !injection.isSingleFile() && StringUtil.isNotEmpty(injection.getValuePattern());
                final List<Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange>> result = ContainerUtil.newArrayList();
                xmlTag.acceptChildren(new PsiElementVisitor() {

                    @Override
                    public void visitElement(final PsiElement element) {
                        if (element instanceof XmlText) {
                            if (!(element instanceof PsiLanguageInjectionHost) || element.getTextLength() == 0)
                                return;
                            final List<TextRange> list = injection.getInjectedArea(element);
                            final InjectedLanguage l = InjectedLanguage.create(injection.getInjectedLanguageId(), injection.getPrefix(), injection.getSuffix(), false);
                            for (TextRange textRange : list) {
                                result.add(Trinity.create((PsiLanguageInjectionHost) element, l, textRange));
                            }
                        } else if (element instanceof XmlTag) {
                            if (!separateFiles)
                                unparsableRef.set(Boolean.TRUE);
                            if (injection instanceof AbstractTagInjection && ((AbstractTagInjection) injection).isApplyToSubTags()) {
                                element.acceptChildren(this);
                            }
                        }
                    }
                });
                if (!result.isEmpty()) {
                    if (separateFiles) {
                        for (Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange> trinity : result) {
                            processor.process(language, Collections.singletonList(trinity));
                        }
                    } else {
                        processor.process(language, result);
                    }
                }
                if (injection.isTerminal()) {
                    break;
                }
            }
        }
    } else if (place instanceof XmlAttributeValue && place.getParent() instanceof XmlAttribute) {
        final XmlAttribute attribute = (XmlAttribute) place.getParent();
        final XmlAttributeValue value = (XmlAttributeValue) place;
        //if (value == null) return;
        // Check that we don't inject anything into embedded (e.g. JavaScript) content:
        // XmlToken: "
        // JSEmbeddedContent
        // XmlToken "
        // Actually IDEA shouldn't ask for injected languages at all in this case.
        final PsiElement[] children = value.getChildren();
        if (children.length < 3 || !(children[1] instanceof XmlToken) || ((XmlToken) children[1]).getTokenType() != XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN) {
            return;
        }
        List<BaseInjection> injections = myConfiguration.getInjections(XmlLanguageInjectionSupport.XML_SUPPORT_ID);
        //noinspection ForLoopReplaceableByForEach
        for (int i = 0, size = injections.size(); i < size; i++) {
            BaseInjection injection = injections.get(i);
            if (injection.acceptsPsiElement(attribute)) {
                final Language language = InjectedLanguage.findLanguageById(injection.getInjectedLanguageId());
                if (language == null)
                    continue;
                final boolean separateFiles = !injection.isSingleFile() && StringUtil.isNotEmpty(injection.getValuePattern());
                final List<TextRange> ranges = injection.getInjectedArea(value);
                if (ranges.isEmpty())
                    continue;
                final List<Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange>> result = new ArrayList<>();
                final InjectedLanguage l = InjectedLanguage.create(injection.getInjectedLanguageId(), injection.getPrefix(), injection.getSuffix(), false);
                for (TextRange textRange : ranges) {
                    result.add(Trinity.create((PsiLanguageInjectionHost) value, l, textRange));
                }
                if (separateFiles) {
                    for (Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange> trinity : result) {
                        processor.process(language, Collections.singletonList(trinity));
                    }
                } else {
                    processor.process(language, result);
                }
                if (injection.isTerminal()) {
                    break;
                }
            }
        }
    }
}
Also used : Trinity(com.intellij.openapi.util.Trinity) TextRange(com.intellij.openapi.util.TextRange) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) Language(com.intellij.lang.Language) InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) AbstractTagInjection(org.intellij.plugins.intelliLang.inject.config.AbstractTagInjection) PsiElement(com.intellij.psi.PsiElement)

Example 5 with InjectedLanguage

use of org.intellij.plugins.intelliLang.inject.InjectedLanguage in project intellij-plugins by JetBrains.

the class CodeFenceTemporaryLangInjector method findLangForInjection.

@Nullable
@Override
protected Language findLangForInjection(@NotNull MarkdownCodeFenceImpl element) {
    final TemporaryPlacesRegistry registry = TemporaryPlacesRegistry.getInstance(element.getProject());
    final InjectedLanguage language = registry.getLanguageFor(element, element.getContainingFile());
    if (language != null) {
        return language.getLanguage();
    }
    return null;
}
Also used : TemporaryPlacesRegistry(org.intellij.plugins.intelliLang.inject.TemporaryPlacesRegistry) InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

InjectedLanguage (org.intellij.plugins.intelliLang.inject.InjectedLanguage)5 TextRange (com.intellij.openapi.util.TextRange)3 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)3 Language (com.intellij.lang.Language)2 PsiFile (com.intellij.psi.PsiFile)2 TemporaryPlacesRegistry (org.intellij.plugins.intelliLang.inject.TemporaryPlacesRegistry)2 BaseInjection (org.intellij.plugins.intelliLang.inject.config.BaseInjection)2 Nullable (org.jetbrains.annotations.Nullable)2 Trinity (com.intellij.openapi.util.Trinity)1 PsiElement (com.intellij.psi.PsiElement)1 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)1 AbstractTagInjection (org.intellij.plugins.intelliLang.inject.config.AbstractTagInjection)1