use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project qi4j-sdk by Qi4j.
the class Qi4jMixinUtil method addOrReplaceMixinAnnotation.
@NotNull
public static PsiAnnotation addOrReplaceMixinAnnotation(@NotNull PsiModifierListOwner modifierListOwner, @NotNull PsiClass mixinClassToAdd) {
Project project = modifierListOwner.getProject();
JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
PsiElementFactory factory = psiFacade.getElementFactory();
PsiAnnotation existingMixinsAnnotation = findAnnotation(modifierListOwner, QUALIFIED_NAME_MIXINS);
boolean isReplace = false;
PsiAnnotation newMixinsAnnotation;
if (existingMixinsAnnotation != null) {
// Check duplicate
List<PsiAnnotationMemberValue> mixinsValues = getMixinsAnnotationValue(existingMixinsAnnotation);
for (PsiAnnotationMemberValue mixinValue : mixinsValues) {
PsiJavaCodeReferenceElement mixinClassReference = getMixinClassReference(mixinValue);
if (mixinClassReference == null) {
continue;
}
PsiElement mixinClass = mixinClassReference.resolve();
if (mixinClassToAdd.equals(mixinClass)) {
return existingMixinsAnnotation;
}
}
isReplace = true;
}
String mixinsAnnotationText = createMixinsAnnotationText(existingMixinsAnnotation, mixinClassToAdd);
newMixinsAnnotation = factory.createAnnotationFromText(mixinsAnnotationText, modifierListOwner);
if (isReplace) {
// Replace @Mixins instead
existingMixinsAnnotation.replace(newMixinsAnnotation);
} else {
// @Mixins doesn't exists, add it as first child
PsiModifierList modifierList = modifierListOwner.getModifierList();
modifierList.addBefore(newMixinsAnnotation, modifierList.getFirstChild());
}
// Shorten all class references if possible
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
codeStyleManager.shortenClassReferences(newMixinsAnnotation);
return newMixinsAnnotation;
}
Aggregations