private String createNewVariableName( @NotNull PsiForStatement scope, PsiType type, @Nullable String containerName) { final Project project = scope.getProject(); final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); @NonNls String baseName; if (containerName != null) { baseName = StringUtils.createSingularFromName(containerName); } else { final SuggestedNameInfo suggestions = codeStyleManager.suggestVariableName( VariableKind.LOCAL_VARIABLE, null, null, type); final String[] names = suggestions.names; if (names != null && names.length > 0) { baseName = names[0]; } else { baseName = "value"; } } if (baseName == null || baseName.isEmpty()) { baseName = "value"; } return codeStyleManager.suggestUniqueVariableName(baseName, scope, true); }
// need to shorten references in type argument list public static void shortenReference(final PsiFile file, final int offset) throws IncorrectOperationException { Project project = file.getProject(); final PsiDocumentManager manager = PsiDocumentManager.getInstance(project); Document document = manager.getDocument(file); if (document == null) { PsiUtilCore.ensureValid(file); LOG.error("No document for " + file); return; } manager.commitDocument(document); final PsiReference ref = file.findReferenceAt(offset); if (ref != null) { PsiElement element = ref.getElement(); if (element != null) { JavaCodeStyleManager.getInstance(project).shortenClassReferences(element); PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document); } } }