Search in sources :

Example 21 with BaseInjection

use of org.intellij.plugins.intelliLang.inject.config.BaseInjection 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 22 with BaseInjection

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

the class GrConcatenationInjector method calcLanguageParams.

@Nullable
private static BaseInjection calcLanguageParams(PsiModifierListOwner annotationOwner) {
    final Pair<String, ? extends Set<String>> pair = Configuration.getInstance().getAdvancedConfiguration().getLanguageAnnotationPair();
    final PsiAnnotation[] annotations = getAnnotationFrom(annotationOwner, pair, true, true);
    if (annotations.length > 0) {
        String prefix = StringUtil.notNullize(AnnotationUtilEx.calcAnnotationValue(annotations, "prefix"));
        String suffix = StringUtil.notNullize(AnnotationUtilEx.calcAnnotationValue(annotations, "suffix"));
        String id = StringUtil.notNullize(AnnotationUtilEx.calcAnnotationValue(annotations, "value"));
        if (!StringUtil.isEmpty(id)) {
            BaseInjection injection = new BaseInjection(GroovyLanguageInjectionSupport.GROOVY_SUPPORT_ID);
            injection.setPrefix(prefix);
            injection.setSuffix(suffix);
            injection.setInjectedLanguageId(id);
            return injection;
        }
    }
    if (annotationOwner instanceof PsiParameter && annotationOwner.getParent() instanceof PsiParameterList && annotationOwner.getParent().getParent() instanceof PsiMethod) {
        List<BaseInjection> injections = Configuration.getInstance().getInjections(JavaLanguageInjectionSupport.JAVA_SUPPORT_ID);
        for (BaseInjection injection : injections) {
            if (injection.acceptsPsiElement(annotationOwner)) {
                return injection;
            }
        }
    }
    return null;
}
Also used : BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with BaseInjection

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

the class GroovyLanguageInjectionSupport method removeInjectionInPlace.

@Override
public boolean removeInjectionInPlace(@Nullable final PsiLanguageInjectionHost psiElement) {
    if (!isStringLiteral(psiElement))
        return false;
    GrLiteralContainer host = (GrLiteralContainer) psiElement;
    final HashMap<BaseInjection, Pair<PsiMethod, Integer>> injectionsMap = ContainerUtil.newHashMap();
    final ArrayList<PsiElement> annotations = new ArrayList<>();
    final Project project = host.getProject();
    final Configuration configuration = Configuration.getProjectInstance(project);
    collectInjections(host, configuration, this, injectionsMap, annotations);
    if (injectionsMap.isEmpty() && annotations.isEmpty())
        return false;
    final ArrayList<BaseInjection> originalInjections = new ArrayList<>(injectionsMap.keySet());
    final List<BaseInjection> newInjections = ContainerUtil.mapNotNull(originalInjections, (NullableFunction<BaseInjection, BaseInjection>) injection -> {
        final Pair<PsiMethod, Integer> pair = injectionsMap.get(injection);
        final String placeText = JavaLanguageInjectionSupport.getPatternStringForJavaPlace(pair.first, pair.second);
        final BaseInjection newInjection = injection.copy();
        newInjection.setPlaceEnabled(placeText, false);
        return InjectorUtils.canBeRemoved(newInjection) ? null : newInjection;
    });
    configuration.replaceInjectionsWithUndo(project, newInjections, originalInjections, annotations);
    return true;
}
Also used : Language(com.intellij.lang.Language) GrLiteralImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrLiteralImpl) IElementType(com.intellij.psi.tree.IElementType) GrStringContent(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent) NonNls(org.jetbrains.annotations.NonNls) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) HashMap(java.util.HashMap) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) ContainerUtil(com.intellij.util.containers.ContainerUtil) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) ArrayList(java.util.ArrayList) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) TokenSets(org.jetbrains.plugins.groovy.lang.lexer.TokenSets) Project(com.intellij.openapi.project.Project) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrLiteralContainer(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteralContainer) AbstractLanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.AbstractLanguageInjectionSupport) InjectorUtils(org.intellij.plugins.intelliLang.inject.InjectorUtils) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) NullableFunction(com.intellij.util.NullableFunction) LightElement(com.intellij.psi.impl.light.LightElement) Nullable(org.jetbrains.annotations.Nullable) Contract(org.jetbrains.annotations.Contract) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer) List(java.util.List) Configuration(org.intellij.plugins.intelliLang.Configuration) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) Processor(com.intellij.util.Processor) org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions) Pair(com.intellij.openapi.util.Pair) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) LanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.LanguageInjectionSupport) com.intellij.psi(com.intellij.psi) JavaLanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.java.JavaLanguageInjectionSupport) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) NotNull(org.jetbrains.annotations.NotNull) GroovyPatterns(org.jetbrains.plugins.groovy.lang.psi.patterns.GroovyPatterns) Configuration(org.intellij.plugins.intelliLang.Configuration) ArrayList(java.util.ArrayList) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) Project(com.intellij.openapi.project.Project) GrLiteralContainer(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteralContainer) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) Pair(com.intellij.openapi.util.Pair)

Aggregations

BaseInjection (org.intellij.plugins.intelliLang.inject.config.BaseInjection)23 InjectionPlace (org.intellij.plugins.intelliLang.inject.config.InjectionPlace)7 Nullable (org.jetbrains.annotations.Nullable)7 Language (com.intellij.lang.Language)6 Project (com.intellij.openapi.project.Project)6 LanguageInjectionSupport (org.intellij.plugins.intelliLang.inject.LanguageInjectionSupport)6 InjectorUtils (org.intellij.plugins.intelliLang.inject.InjectorUtils)5 NotNull (org.jetbrains.annotations.NotNull)5 AnAction (com.intellij.openapi.actionSystem.AnAction)4 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)4 ContainerUtil (com.intellij.util.containers.ContainerUtil)4 THashSet (gnu.trove.THashSet)4 Pair (com.intellij.openapi.util.Pair)3 PsiElement (com.intellij.psi.PsiElement)3 IOException (java.io.IOException)3 Configuration (org.intellij.plugins.intelliLang.Configuration)3 MethodParameterInjection (org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection)3 Element (org.jdom.Element)3 NonNls (org.jetbrains.annotations.NonNls)3 SplitterProportionsDataImpl (com.intellij.ide.ui.SplitterProportionsDataImpl)2