void (@NotNull PsiReferenceList extendsList, boolean add, int position, @NotNull PsiClassType myTypeToExtendFrom) { PsiClass classToExtendFrom = myTypeToExtendFrom.resolve(); if (classToExtendFrom == null) return; PsiJavaCodeReferenceElement[] referenceElements = extendsList.getReferenceElements(); boolean alreadyExtends = false; for (PsiJavaCodeReferenceElement referenceElement : referenceElements) { if (referenceElement.getManager().areElementsEquivalent(classToExtendFrom, referenceElement.resolve())) { alreadyExtends = true; if (!add) { referenceElement.delete(); } } } PsiReferenceList list = extendsList; Project project = extendsList.getProject(); if (add && !alreadyExtends) { PsiElement anchor; if (position == -1) { anchor = referenceElements.length ==0 ? null : referenceElements[referenceElements.length-1]; } else if (position == 0) { anchor = null; } else { anchor = referenceElements[position - 1]; } PsiJavaCodeReferenceElement classReferenceElement = JavaPsiFacade.getElementFactory(project).createReferenceElementByType(myTypeToExtendFrom); PsiElement element; if (anchor == null) { if (referenceElements.length == 0) { element = extendsList.add(classReferenceElement); } else { element = extendsList.addBefore(classReferenceElement, referenceElements[0]); } } else { element = extendsList.addAfter(classReferenceElement, anchor); } list = (PsiReferenceList) element.getParent(); } //nothing was changed if (!add && !alreadyExtends) { return; } CodeStyleManager.getInstance(project).reformat(JavaCodeStyleManager.getInstance(project).shortenClassReferences(list)); }