use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class ExtractMethodSignatureSuggester method detectTopLevelExpressionsToReplaceWithParameters.
@Nullable
private InputVariables detectTopLevelExpressionsToReplaceWithParameters(List<PsiExpression> copies) {
final PsiParameter[] parameters = myExtractedMethod.getParameterList().getParameters();
final List<PsiVariable> inputVariables = new ArrayList<>(Arrays.asList(parameters));
final PsiCodeBlock body = myExtractedMethod.getBody();
LOG.assertTrue(body != null);
final PsiStatement[] pattern = body.getStatements();
final List<PsiExpression> exprs = new ArrayList<>();
for (PsiStatement statement : pattern) {
if (statement instanceof PsiExpressionStatement) {
final PsiExpression expression = ((PsiExpressionStatement) statement).getExpression();
if (expression instanceof PsiIfStatement || expression instanceof PsiLoopStatement) {
continue;
}
}
statement.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitCallExpression(PsiCallExpression callExpression) {
final PsiExpressionList list = callExpression.getArgumentList();
if (list != null) {
for (PsiExpression expression : list.getExpressions()) {
if (expression instanceof PsiReferenceExpression) {
final PsiElement resolve = ((PsiReferenceExpression) expression).resolve();
if (resolve instanceof PsiField) {
exprs.add(expression);
}
} else {
exprs.add(expression);
}
}
}
}
});
}
if (exprs.isEmpty()) {
return null;
}
final UniqueNameGenerator uniqueNameGenerator = new UniqueNameGenerator();
for (PsiParameter parameter : parameters) {
uniqueNameGenerator.addExistingName(parameter.getName());
}
SyntaxTraverser.psiTraverser().withRoot(myExtractedMethod.getBody()).filter(element -> element instanceof PsiVariable).forEach(element -> uniqueNameGenerator.addExistingName(((PsiVariable) element).getName()));
final THashMap<PsiExpression, String> unique = new THashMap<>(ourEquivalenceStrategy);
final Map<PsiExpression, String> replacement = new HashMap<>();
for (PsiExpression expr : exprs) {
String name = unique.get(expr);
if (name == null) {
final PsiType type = GenericsUtil.getVariableTypeByExpressionType(expr.getType());
if (type == null || type == PsiType.NULL || PsiUtil.resolveClassInType(type) instanceof PsiAnonymousClass || LambdaUtil.notInferredType(type))
return null;
copies.add(myElementFactory.createExpressionFromText(expr.getText(), body));
final SuggestedNameInfo info = JavaCodeStyleManager.getInstance(myProject).suggestVariableName(VariableKind.PARAMETER, null, expr, null);
final String paramName = info.names.length > 0 ? info.names[0] : "p";
name = uniqueNameGenerator.generateUniqueName(paramName);
final PsiParameter parameter = (PsiParameter) myExtractedMethod.getParameterList().add(myElementFactory.createParameter(name, type));
inputVariables.add(parameter);
unique.put(expr, name);
}
replacement.put(expr, name);
}
for (PsiExpression expression : replacement.keySet()) {
expression.replace(myElementFactory.createExpressionFromText(replacement.get(expression), null));
}
return new InputVariables(inputVariables, myExtractedMethod.getProject(), new LocalSearchScope(myExtractedMethod), false);
}
use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class InheritanceToDelegationDialog method updateTargetClass.
private void updateTargetClass() {
final PsiClass targetClass = getSelectedTargetClass();
PsiManager psiManager = myClass.getManager();
PsiType superType = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createType(targetClass);
SuggestedNameInfo suggestedNameInfo = JavaCodeStyleManager.getInstance(psiManager.getProject()).suggestVariableName(VariableKind.FIELD, null, null, superType);
myFieldNameField.setSuggestions(suggestedNameInfo.names);
myInnerClassNameField.getComponent().setEnabled(InheritanceToDelegationUtil.isInnerClassNeeded(myClass, targetClass));
@NonNls final String suggestion = "My" + targetClass.getName();
myInnerClassNameField.setSuggestions(new String[] { suggestion });
myDataChangedListener = new NameSuggestionsField.DataChanged() {
public void dataChanged() {
validateButtons();
}
};
myInnerClassNameField.addDataChangedListener(myDataChangedListener);
myFieldNameField.addDataChangedListener(myDataChangedListener);
myMemberSelectionPanel.getTable().setMemberInfos(myBasesToMemberInfos.get(targetClass));
myMemberSelectionPanel.getTable().fireExternalDataChange();
}
use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class MethodParameterFix method getNewParametersInfo.
@NotNull
private ParameterInfoImpl[] getNewParametersInfo(PsiMethod method) throws IncorrectOperationException {
List<ParameterInfoImpl> result = new ArrayList<>();
PsiParameter[] parameters = method.getParameterList().getParameters();
PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory();
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(method.getProject());
SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, myParameterType);
PsiParameter newParameter = factory.createParameter(nameInfo.names[0], myParameterType);
if (method.getContainingClass().isInterface()) {
PsiUtil.setModifierProperty(newParameter, PsiModifier.FINAL, false);
}
for (int i = 0; i < parameters.length; i++) {
PsiParameter parameter = parameters[i];
if (i == myIndex) {
newParameter.setName(parameter.getName());
parameter = newParameter;
}
result.add(new ParameterInfoImpl(i, parameter.getName(), parameter.getType()));
}
if (parameters.length == myIndex) {
result.add(new ParameterInfoImpl(-1, newParameter.getName(), newParameter.getType()));
}
return result.toArray(new ParameterInfoImpl[result.size()]);
}
use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class IntroduceVariableBase method getSuggestedName.
public static SuggestedNameInfo getSuggestedName(@Nullable PsiType type, @NotNull final PsiExpression expression, final PsiElement anchor) {
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(expression.getProject());
final SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, expression, type);
final String[] strings = JavaCompletionUtil.completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, nameInfo);
final SuggestedNameInfo.Delegate delegate = new SuggestedNameInfo.Delegate(strings, nameInfo);
return codeStyleManager.suggestUniqueVariableName(delegate, anchor, true);
}
use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class IntroduceParameterHandler method createNameSuggestionGenerator.
protected static NameSuggestionsGenerator createNameSuggestionGenerator(final PsiExpression expr, final String propName, final Project project, final String enteredName) {
return new NameSuggestionsGenerator() {
public SuggestedNameInfo getSuggestedNameInfo(PsiType type) {
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, propName, expr != null && expr.isValid() ? expr : null, type);
if (expr != null && expr.isValid()) {
info = codeStyleManager.suggestUniqueVariableName(info, expr, true);
}
final String[] strings = AbstractJavaInplaceIntroducer.appendUnresolvedExprName(JavaCompletionUtil.completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, info), expr);
return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[] { enteredName }, strings) : strings, info);
}
};
}
Aggregations