use of org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection 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;
}
use of org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection 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);
}
use of org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection in project intellij-community by JetBrains.
the class JavaLanguageInjectionSupport method editInjectionInPlace.
public boolean editInjectionInPlace(final PsiLanguageInjectionHost psiElement) {
if (!isMine(psiElement))
return false;
final HashMap<BaseInjection, Pair<PsiMethod, Integer>> injectionsMap = new HashMap<>();
final ArrayList<PsiElement> annotations = new ArrayList<>();
final PsiLiteralExpression host = (PsiLiteralExpression) psiElement;
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 BaseInjection originalInjection = injectionsMap.keySet().iterator().next();
final MethodParameterInjection methodParameterInjection = createFrom(psiElement.getProject(), originalInjection, injectionsMap.get(originalInjection).first, false);
final MethodParameterInjection copy = methodParameterInjection.copy();
final BaseInjection newInjection = showInjectionUI(project, methodParameterInjection);
if (newInjection != null) {
newInjection.mergeOriginalPlacesFrom(copy, false);
newInjection.mergeOriginalPlacesFrom(originalInjection, true);
configuration.replaceInjectionsWithUndo(project, Collections.singletonList(newInjection), Collections.singletonList(originalInjection), Collections.<PsiAnnotation>emptyList());
}
return true;
}
use of org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection in project intellij-community by JetBrains.
the class JavaLanguageInjectionSupport method createEditAction.
@Override
public AnAction createEditAction(final Project project, final Factory<BaseInjection> producer) {
return new AnAction() {
@Override
public void actionPerformed(final AnActionEvent e) {
final BaseInjection originalInjection = producer.create();
final MethodParameterInjection injection = createFrom(project, originalInjection, null, false);
if (injection != null) {
final boolean mergeEnabled = !project.isInitialized() || JavaPsiFacade.getInstance(project).findClass(injection.getClassName(), GlobalSearchScope.allScope(project)) == null;
final BaseInjection newInjection = showInjectionUI(project, injection);
if (newInjection != null) {
newInjection.mergeOriginalPlacesFrom(originalInjection, mergeEnabled);
originalInjection.copyFrom(newInjection);
}
} else {
createDefaultEditAction(project, producer).actionPerformed(null);
}
}
};
}
use of org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection in project intellij-community by JetBrains.
the class JavaLanguageInjectionSupport method doInjectInJavaMethod.
public static boolean doInjectInJavaMethod(@NotNull final Project project, @Nullable final PsiMethod psiMethod, final int parameterIndex, @NotNull PsiLanguageInjectionHost host, @NotNull final String languageId) {
if (psiMethod == null)
return false;
if (parameterIndex < -1)
return false;
if (parameterIndex >= psiMethod.getParameterList().getParametersCount())
return false;
final PsiModifierList methodModifiers = psiMethod.getModifierList();
if (methodModifiers.hasModifierProperty(PsiModifier.PRIVATE) || methodModifiers.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
return doAddLanguageAnnotation(project, parameterIndex >= 0 ? psiMethod.getParameterList().getParameters()[parameterIndex] : psiMethod, host, languageId);
}
final PsiClass containingClass = psiMethod.getContainingClass();
assert containingClass != null;
final PsiModifierList classModifiers = containingClass.getModifierList();
if (classModifiers != null && (classModifiers.hasModifierProperty(PsiModifier.PRIVATE) || classModifiers.hasModifierProperty(PsiModifier.PACKAGE_LOCAL))) {
return doAddLanguageAnnotation(project, parameterIndex >= 0 ? psiMethod.getParameterList().getParameters()[parameterIndex] : psiMethod, host, languageId);
}
final String className = containingClass.getQualifiedName();
assert className != null;
final MethodParameterInjection injection = new MethodParameterInjection();
injection.setInjectedLanguageId(languageId);
injection.setClassName(className);
final MethodInfo info = createMethodInfo(psiMethod);
if (parameterIndex < 0) {
info.setReturnFlag(true);
} else {
info.getParamFlags()[parameterIndex] = true;
}
injection.setMethodInfos(Collections.singletonList(info));
injection.generatePlaces();
doEditInjection(project, injection, psiMethod);
return true;
}
Aggregations