PsiElement (@NotNull PsiClass aClass, @NotNull PsiMember member) { CodeStyleSettings settings = CodeStyle.getSettings(aClass.getContainingFile()); MemberOrderService service = ApplicationManager.getApplication().getService(MemberOrderService.class); PsiElement anchor = service.getAnchor(member, settings.getCommonSettings(JavaLanguage.INSTANCE), aClass); PsiElement newAnchor = skipWhitespaces(aClass, anchor); if (newAnchor != null) { return newAnchor; } if (anchor != null && anchor != aClass) { anchor = anchor.getNextSibling(); while (anchor instanceof PsiJavaToken && (anchor.getText().equals(",") || anchor.getText().equals(";"))) { final boolean afterComma = anchor.getText().equals(","); anchor = anchor.getNextSibling(); if (afterComma) { newAnchor = skipWhitespaces(aClass, anchor); if (newAnchor != null) return newAnchor; } } if (anchor != null) { return anchor; } } // The main idea is to avoid to anchor to 'white space' element because that causes reformatting algorithm // to perform incorrectly. The algorithm is encapsulated at the PostprocessReformattingAspect.doPostponedFormattingInner(). final PsiElement lBrace = aClass.getLBrace(); if (lBrace != null) { PsiElement result = lBrace.getNextSibling(); while (result instanceof PsiWhiteSpace) { result = result.getNextSibling(); } return result; } return aClass.getRBrace(); }