use of org.intellij.plugins.intelliLang.Configuration in project intellij-community by JetBrains.
the class JavaLanguageInjectionSupport method doAddLanguageAnnotation.
public static boolean doAddLanguageAnnotation(Project project, @Nullable final PsiModifierListOwner modifierListOwner, @NotNull final PsiLanguageInjectionHost host, final String languageId, Processor<PsiLanguageInjectionHost> annotationFixer) {
final boolean addAnnotation = isAnnotationsJarInPath(ModuleUtilCore.findModuleForPsiElement(modifierListOwner)) && PsiUtil.isLanguageLevel5OrHigher(modifierListOwner) && modifierListOwner.getModifierList() != null;
final PsiStatement statement = PsiTreeUtil.getParentOfType(host, PsiStatement.class);
if (!addAnnotation && statement == null)
return false;
Configuration.AdvancedConfiguration configuration = Configuration.getProjectInstance(project).getAdvancedConfiguration();
if (!configuration.isSourceModificationAllowed()) {
host.putUserData(InjectLanguageAction.FIX_KEY, annotationFixer);
return false;
}
new WriteCommandAction(modifierListOwner.getProject(), modifierListOwner.getContainingFile()) {
protected void run(@NotNull Result result) throws Throwable {
PsiElementFactory javaFacade = JavaPsiFacade.getElementFactory(getProject());
if (addAnnotation) {
JVMElementFactory factory = ObjectUtils.chooseNotNull(JVMElementFactories.getFactory(modifierListOwner.getLanguage(), getProject()), javaFacade);
PsiAnnotation annotation = factory.createAnnotationFromText("@" + AnnotationUtil.LANGUAGE + "(\"" + languageId + "\")", modifierListOwner);
PsiModifierList list = ObjectUtils.assertNotNull(modifierListOwner.getModifierList());
final PsiAnnotation existingAnnotation = list.findAnnotation(AnnotationUtil.LANGUAGE);
if (existingAnnotation != null) {
existingAnnotation.replace(annotation);
} else {
list.addAfter(annotation, null);
}
JavaCodeStyleManager.getInstance(getProject()).shortenClassReferences(list);
} else {
statement.getParent().addBefore(javaFacade.createCommentFromText("//language=" + languageId, host), statement);
}
}
}.execute();
return true;
}
use of org.intellij.plugins.intelliLang.Configuration 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.Configuration in project intellij-community by JetBrains.
the class XmlLanguageInjectionSupport method doEditInjection.
private static void doEditInjection(final Project project, final XmlAttributeInjection template) {
final Configuration configuration = InjectorUtils.getEditableInstance(project);
final BaseInjection originalInjection = configuration.findExistingInjection(template);
final BaseInjection newInjection = originalInjection == null ? template : originalInjection.copy();
configuration.replaceInjectionsWithUndo(project, Collections.singletonList(newInjection), ContainerUtil.createMaybeSingletonList(originalInjection), Collections.<PsiElement>emptyList());
}
use of org.intellij.plugins.intelliLang.Configuration in project intellij-community by JetBrains.
the class XmlLanguageInjectionSupport method editInjectionInPlace.
public boolean editInjectionInPlace(final PsiLanguageInjectionHost host) {
if (!isMine(host))
return false;
final Project project = host.getProject();
final Configuration configuration = Configuration.getProjectInstance(project);
final ArrayList<BaseInjection> injections = collectInjections(host, configuration);
if (injections.isEmpty())
return false;
final BaseInjection originalInjection = injections.get(0);
final BaseInjection xmlInjection = createFrom(originalInjection);
final BaseInjection newInjection = xmlInjection == null ? showDefaultInjectionUI(project, originalInjection.copy()) : showInjectionUI(project, xmlInjection);
if (newInjection != null) {
configuration.replaceInjectionsWithUndo(project, Collections.singletonList(newInjection), Collections.singletonList(originalInjection), Collections.<PsiElement>emptyList());
}
return true;
}
use of org.intellij.plugins.intelliLang.Configuration 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;
}
Aggregations