use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class ParametersFolder method isParameterFoldable.
boolean isParameterFoldable(@NotNull VariableData data, @NotNull LocalSearchScope scope, @NotNull final List<? extends PsiVariable> inputVariables, UniqueNameGenerator nameGenerator, String defaultName) {
final List<PsiExpression> mentionedInExpressions = getMentionedExpressions(data.variable, scope, inputVariables);
if (mentionedInExpressions == null)
return false;
int currentRank = 0;
PsiExpression mostRanked = null;
for (int i = mentionedInExpressions.size() - 1; i >= 0; i--) {
PsiExpression expression = mentionedInExpressions.get(i);
boolean arrayAccess = expression instanceof PsiArrayAccessExpression && !isConditional(expression, scope);
if (arrayAccess) {
myFoldingSelectedByDefault = true;
}
final int r = findUsedVariables(data, inputVariables, expression).size();
if (currentRank < r || arrayAccess && currentRank == r) {
currentRank = r;
mostRanked = expression;
}
}
if (mostRanked != null) {
myExpressions.put(data.variable, mostRanked);
myArgs.put(data.variable, mostRanked.getText());
data.type = RefactoringChangeUtil.getTypeByExpression(mostRanked);
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(mostRanked.getProject());
final SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, mostRanked, data.type);
if (nameInfo.names.length > 0 && !Comparing.equal(nameInfo.names[0], data.name) && !Comparing.equal(nameInfo.names[0], defaultName)) {
data.name = nameInfo.names[0];
setUniqueName(data, nameGenerator, scope, mostRanked);
}
}
return mostRanked != null;
}
use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class ExpressionUtil method getNamesForIdentifier.
@Nullable
private static String[] getNamesForIdentifier(Project project, PsiIdentifier identifier) {
if (identifier.getParent() instanceof PsiVariable) {
PsiVariable var = (PsiVariable) identifier.getParent();
if (identifier.equals(var.getNameIdentifier())) {
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
VariableKind variableKind = codeStyleManager.getVariableKind(var);
PsiExpression initializer = var.getInitializer();
if (var instanceof PsiParameter && ((PsiParameter) var).getDeclarationScope() instanceof PsiForeachStatement) {
//synthesize initializer
PsiForeachStatement foreachStatement = (PsiForeachStatement) ((PsiParameter) var).getDeclarationScope();
final PsiExpression iteratedValue = foreachStatement.getIteratedValue();
if (iteratedValue != null) {
try {
final PsiArrayAccessExpression expr = (PsiArrayAccessExpression) JavaPsiFacade.getInstance(iteratedValue.getProject()).getElementFactory().createExpressionFromText("a[0]", var);
expr.getArrayExpression().replace(iteratedValue);
//note: non physical with no parent
initializer = expr;
} catch (IncorrectOperationException e) {
//do nothing
}
}
}
SuggestedNameInfo suggestedInfo = codeStyleManager.suggestVariableName(variableKind, null, initializer, var.getType());
return suggestedInfo.names;
}
}
return null;
}
use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class NameSuggestionsManager method updateSuggestions.
private void updateSuggestions(PsiType selectedType) {
SuggestedNameInfo nameInfo = myTypesToSuggestions.get(selectedType);
if (nameInfo == null) {
nameInfo = myGenerator.getSuggestedNameInfo(selectedType);
myTypesToSuggestions.put(selectedType, nameInfo);
}
myNameField.setSuggestions(nameInfo.names);
}
use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class MoveInstanceMethodHandler method suggestParameterNameForThisClass.
public static String suggestParameterNameForThisClass(final PsiClass thisClass) {
PsiManager manager = thisClass.getManager();
PsiType type = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().createType(thisClass);
final SuggestedNameInfo suggestedNameInfo = JavaCodeStyleManager.getInstance(manager.getProject()).suggestVariableName(VariableKind.PARAMETER, null, null, type);
return suggestedNameInfo.names.length > 0 ? suggestedNameInfo.names[0] : "";
}
use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class JavaIntroduceVariableTest method doTest.
protected static void doTest(String expression, VariableKind kind, PsiType type, String... results) throws Exception {
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(getProject());
final PsiExpression expr = factory.createExpressionFromText(expression, null);
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(getProject());
final SuggestedNameInfo info = codeStyleManager.suggestVariableName(kind, null, expr, type);
assert info.names.length >= results.length : msg("Can't find some variants", info.names, results);
for (int i = 0; i < results.length; i++) {
if (!results[i].equals(info.names[i])) {
throw new Exception(msg("", info.names, results));
}
}
}
Aggregations