Search in sources :

Example 1 with InjectionPlace

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

the class JavaLanguageInjectionSupport method createFrom.

private static MethodParameterInjection createFrom(final Project project, final BaseInjection injection, final PsiMethod contextMethod, final boolean includeAllPlaces) {
    final PsiClass[] classes;
    final String className;
    if (contextMethod != null) {
        final PsiClass psiClass = contextMethod.getContainingClass();
        className = psiClass == null ? "" : StringUtil.notNullize(psiClass.getQualifiedName());
        classes = psiClass == null ? PsiClass.EMPTY_ARRAY : new PsiClass[] { psiClass };
    } else {
        String found = null;
        final Pattern pattern = Pattern.compile(".*definedInClass\\(\"([^\"]*)\"\\)+");
        for (InjectionPlace place : injection.getInjectionPlaces()) {
            final Matcher matcher = pattern.matcher(place.getText());
            if (matcher.matches()) {
                found = matcher.group(1);
            }
        }
        if (found == null) {
            // hack to guess at least the class name
            final Matcher matcher = ourPresentationPattern.matcher(injection.getDisplayName());
            if (matcher.matches()) {
                final String pkg = matcher.group(2);
                found = pkg.substring(1, pkg.length() - 1) + "." + matcher.group(1);
            }
        }
        classes = found != null && project.isInitialized() ? JavaPsiFacade.getInstance(project).findClasses(found, GlobalSearchScope.allScope(project)) : PsiClass.EMPTY_ARRAY;
        className = StringUtil.notNullize(classes.length == 0 ? found : classes[0].getQualifiedName());
    }
    final MethodParameterInjection result = new MethodParameterInjection();
    result.copyFrom(injection);
    result.setInjectionPlaces(InjectionPlace.EMPTY_ARRAY);
    result.setClassName(className);
    final ArrayList<MethodInfo> infos = new ArrayList<>();
    if (classes.length > 0) {
        final THashSet<String> visitedSignatures = new THashSet<>();
        final PatternCompiler<PsiElement> compiler = injection.getCompiler();
        for (PsiClass psiClass : classes) {
            for (PsiMethod method : psiClass.getMethods()) {
                final PsiModifierList modifiers = method.getModifierList();
                if (modifiers.hasModifierProperty(PsiModifier.PRIVATE) || modifiers.hasModifierProperty(PsiModifier.PACKAGE_LOCAL))
                    continue;
                boolean add = false;
                final MethodInfo methodInfo = createMethodInfo(method);
                if (!visitedSignatures.add(methodInfo.getMethodSignature()))
                    continue;
                if (isInjectable(method.getReturnType(), method.getProject())) {
                    final int parameterIndex = -1;
                    int index = ArrayUtilRt.find(injection.getInjectionPlaces(), new InjectionPlace(compiler.compileElementPattern(getPatternStringForJavaPlace(method, parameterIndex)), true));
                    final InjectionPlace place = index > -1 ? injection.getInjectionPlaces()[index] : null;
                    methodInfo.setReturnFlag(place != null && place.isEnabled() || includeAllPlaces);
                    add = true;
                }
                final PsiParameter[] parameters = method.getParameterList().getParameters();
                for (int i = 0; i < parameters.length; i++) {
                    final PsiParameter p = parameters[i];
                    if (isInjectable(p.getType(), p.getProject())) {
                        int index = ArrayUtilRt.find(injection.getInjectionPlaces(), new InjectionPlace(compiler.compileElementPattern(getPatternStringForJavaPlace(method, i)), true));
                        final InjectionPlace place = index > -1 ? injection.getInjectionPlaces()[index] : null;
                        methodInfo.getParamFlags()[i] = place != null && place.isEnabled() || includeAllPlaces;
                        add = true;
                    }
                }
                if (add) {
                    infos.add(methodInfo);
                }
            }
        }
    }
    //    else {
    // todo tbd
    //for (InjectionPlace place : injection.getInjectionPlaces()) {
    //  final Matcher matcher = pattern.matcher(place.getText());
    //  if (matcher.matches()) {
    //
    //  }
    //}
    //    }
    result.setMethodInfos(infos);
    result.generatePlaces();
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InjectionPlace(org.intellij.plugins.intelliLang.inject.config.InjectionPlace) THashSet(gnu.trove.THashSet) MethodParameterInjection(org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection)

Example 2 with InjectionPlace

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

the class JavaLanguageInjectionSupport method doEditInjection.

private static void doEditInjection(final Project project, final MethodParameterInjection template, final PsiMethod contextMethod) {
    final Configuration configuration = InjectorUtils.getEditableInstance(project);
    final BaseInjection baseTemplate = new BaseInjection(template.getSupportId()).copyFrom(template);
    final MethodParameterInjection allMethodParameterInjection = createFrom(project, baseTemplate, contextMethod, true);
    // find existing injection for this class.
    final BaseInjection originalInjection = configuration.findExistingInjection(allMethodParameterInjection);
    final MethodParameterInjection methodParameterInjection;
    if (originalInjection == null) {
        methodParameterInjection = template;
    } else {
        final BaseInjection originalCopy = originalInjection.copy();
        final InjectionPlace currentPlace = template.getInjectionPlaces()[0];
        originalCopy.mergeOriginalPlacesFrom(template, true);
        originalCopy.setPlaceEnabled(currentPlace.getText(), true);
        methodParameterInjection = createFrom(project, originalCopy, contextMethod, false);
    }
    mergePlacesAndAddToConfiguration(project, configuration, methodParameterInjection, originalInjection);
}
Also used : Configuration(org.intellij.plugins.intelliLang.Configuration) MethodParameterInjection(org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection) InjectionPlace(org.intellij.plugins.intelliLang.inject.config.InjectionPlace) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection)

Example 3 with InjectionPlace

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

the class XmlLanguageInjector method getXmlAnnotatedElementsValue.

private Trinity<Long, Pattern, Collection<String>> getXmlAnnotatedElementsValue() {
    Trinity<Long, Pattern, Collection<String>> index = myXmlIndex;
    if (index == null || myConfiguration.getModificationCount() != index.first.longValue()) {
        final Map<ElementPattern<?>, BaseInjection> map = new THashMap<>();
        for (BaseInjection injection : myConfiguration.getInjections(XmlLanguageInjectionSupport.XML_SUPPORT_ID)) {
            for (InjectionPlace place : injection.getInjectionPlaces()) {
                if (!place.isEnabled() || place.getElementPattern() == null)
                    continue;
                map.put(place.getElementPattern(), injection);
            }
        }
        final Collection<String> stringSet = PatternValuesIndex.buildStringIndex(map.keySet());
        index = Trinity.create(myConfiguration.getModificationCount(), buildPattern(stringSet), stringSet);
        myXmlIndex = index;
    }
    return index;
}
Also used : ElementPattern(com.intellij.patterns.ElementPattern) Pattern(java.util.regex.Pattern) THashMap(gnu.trove.THashMap) ElementPattern(com.intellij.patterns.ElementPattern) InjectionPlace(org.intellij.plugins.intelliLang.inject.config.InjectionPlace) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection)

Example 4 with InjectionPlace

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

the class BaseInjectionPanel method resetImpl.

protected void resetImpl() {
    final StringBuilder sb = new StringBuilder();
    for (InjectionPlace place : myOrigInjection.getInjectionPlaces()) {
        sb.append(place.isEnabled() ? "+ " : "- ").append(place.getText()).append("\n");
    }
    myTextArea.setText(sb.toString());
    myNameTextField.setText(myOrigInjection.getDisplayName());
}
Also used : InjectionPlace(org.intellij.plugins.intelliLang.inject.config.InjectionPlace)

Example 5 with InjectionPlace

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

the class Configuration method setHostInjectionEnabled.

public boolean setHostInjectionEnabled(final PsiLanguageInjectionHost host, final Collection<String> languages, final boolean enabled) {
    List<BaseInjection> originalInjections = new ArrayList<>();
    List<BaseInjection> newInjections = new ArrayList<>();
    for (LanguageInjectionSupport support : InjectorUtils.getActiveInjectionSupports()) {
        for (BaseInjection injection : getInjections(support.getId())) {
            if (!languages.contains(injection.getInjectedLanguageId()))
                continue;
            boolean replace = false;
            final ArrayList<InjectionPlace> newPlaces = new ArrayList<>();
            for (InjectionPlace place : injection.getInjectionPlaces()) {
                if (place.isEnabled() != enabled && place.getElementPattern() != null && (place.getElementPattern().accepts(host) || place.getElementPattern().accepts(host.getParent()))) {
                    newPlaces.add(place.enabled(enabled));
                    replace = true;
                } else
                    newPlaces.add(place);
            }
            if (replace) {
                originalInjections.add(injection);
                final BaseInjection newInjection = injection.copy();
                newInjection.setInjectionPlaces(newPlaces.toArray(new InjectionPlace[newPlaces.size()]));
                newInjections.add(newInjection);
            }
        }
    }
    if (!originalInjections.isEmpty()) {
        replaceInjectionsWithUndo(host.getProject(), newInjections, originalInjections, Collections.emptyList());
        return true;
    }
    return false;
}
Also used : LanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.LanguageInjectionSupport) InjectionPlace(org.intellij.plugins.intelliLang.inject.config.InjectionPlace) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection)

Aggregations

InjectionPlace (org.intellij.plugins.intelliLang.inject.config.InjectionPlace)8 BaseInjection (org.intellij.plugins.intelliLang.inject.config.BaseInjection)4 PsiElement (com.intellij.psi.PsiElement)2 THashMap (gnu.trove.THashMap)2 Pattern (java.util.regex.Pattern)2 LanguageInjectionSupport (org.intellij.plugins.intelliLang.inject.LanguageInjectionSupport)2 MethodParameterInjection (org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection)2 ElementPattern (com.intellij.patterns.ElementPattern)1 PsiCompiledElement (com.intellij.psi.PsiCompiledElement)1 THashSet (gnu.trove.THashSet)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Configuration (org.intellij.plugins.intelliLang.Configuration)1 Element (org.jdom.Element)1