Search in sources :

Example 96 with JavaCodeStyleManager

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;
}
Also used : Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)96 Project (com.intellij.openapi.project.Project)27 SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)21 VariableKind (com.intellij.psi.codeStyle.VariableKind)19 IncorrectOperationException (com.intellij.util.IncorrectOperationException)15 NotNull (org.jetbrains.annotations.NotNull)13 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)12 Nullable (org.jetbrains.annotations.Nullable)12 ArrayList (java.util.ArrayList)10 NonNls (org.jetbrains.annotations.NonNls)8 StringUtil (com.intellij.openapi.util.text.StringUtil)3 UniqueNameGenerator (com.intellij.util.text.UniqueNameGenerator)3 HashSet (java.util.HashSet)3 JavaLanguage (com.intellij.lang.java.JavaLanguage)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Editor (com.intellij.openapi.editor.Editor)2 Comparing (com.intellij.openapi.util.Comparing)2 com.intellij.psi (com.intellij.psi)2 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)2 FunctionalInterfaceParameterizationUtil (com.intellij.psi.impl.source.resolve.graphInference.FunctionalInterfaceParameterizationUtil)2