use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class IntroduceFieldDialog method createGenerator.
static NameSuggestionsGenerator createGenerator(final boolean willBeDeclaredStatic, final PsiLocalVariable localVariable, final PsiExpression initializerExpression, final boolean isInvokedOnDeclaration, @Nullable final String enteredName, final PsiClass parentClass, final Project project) {
return new NameSuggestionsGenerator() {
private final JavaCodeStyleManager myCodeStyleManager = JavaCodeStyleManager.getInstance(project);
public SuggestedNameInfo getSuggestedNameInfo(PsiType type) {
VariableKind variableKind = willBeDeclaredStatic ? VariableKind.STATIC_FIELD : VariableKind.FIELD;
String propertyName = null;
if (isInvokedOnDeclaration) {
propertyName = myCodeStyleManager.variableNameToPropertyName(localVariable.getName(), VariableKind.LOCAL_VARIABLE);
}
final SuggestedNameInfo nameInfo = myCodeStyleManager.suggestVariableName(variableKind, propertyName, initializerExpression, type);
if (initializerExpression != null) {
String[] names = nameInfo.names;
for (int i = 0, namesLength = names.length; i < namesLength; i++) {
String name = names[i];
if (parentClass.findFieldByName(name, false) != null) {
names[i] = myCodeStyleManager.suggestUniqueVariableName(name, initializerExpression, true);
}
}
}
final String[] strings = AbstractJavaInplaceIntroducer.appendUnresolvedExprName(JavaCompletionUtil.completeVariableNameForRefactoring(myCodeStyleManager, type, VariableKind.LOCAL_VARIABLE, nameInfo), initializerExpression);
return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[] { enteredName }, strings) : strings, nameInfo);
}
};
}
use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class IntroduceVariableBase method getSettings.
public IntroduceVariableSettings getSettings(Project project, Editor editor, PsiExpression expr, PsiExpression[] occurrences, final TypeSelectorManagerImpl typeSelectorManager, boolean declareFinalIfAll, boolean anyAssignmentLHS, final InputValidator validator, PsiElement anchor, final JavaReplaceChoice replaceChoice) {
final boolean replaceAll = replaceChoice.isMultiple();
final SuggestedNameInfo suggestedName = getSuggestedName(typeSelectorManager.getDefaultType(), expr, anchor);
final String variableName = suggestedName.names.length > 0 ? suggestedName.names[0] : "";
final boolean declareFinal = replaceAll && declareFinalIfAll || !anyAssignmentLHS && createFinals(project);
final boolean replaceWrite = anyAssignmentLHS && replaceChoice.isAll();
return new IntroduceVariableSettings() {
@Override
public String getEnteredName() {
return variableName;
}
@Override
public boolean isReplaceAllOccurrences() {
return replaceAll;
}
@Override
public boolean isDeclareFinal() {
return declareFinal;
}
@Override
public boolean isReplaceLValues() {
return replaceWrite;
}
@Override
public PsiType getSelectedType() {
final PsiType selectedType = typeSelectorManager.getTypeSelector().getSelectedType();
return selectedType != null ? selectedType : typeSelectorManager.getDefaultType();
}
@Override
public boolean isOK() {
return true;
}
};
}
use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class ParameterNameExpression method calculateLookupItems.
@Override
public LookupElement[] calculateLookupItems(ExpressionContext context) {
SuggestedNameInfo info = getNameInfo(context);
if (info == null)
return null;
LookupElement[] result = new LookupElement[info.names.length];
int i = 0;
for (String name : info.names) {
result[i++] = LookupElementBuilder.create(name);
}
return result;
}
use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class GrAliasImportIntention method getSuggestedNames.
public static LinkedHashSet<String> getSuggestedNames(PsiElement psiElement, final PsiElement nameSuggestionContext) {
final LinkedHashSet<String> result = new LinkedHashSet<>();
result.add(UsageViewUtil.getShortName(psiElement));
final NameSuggestionProvider[] providers = Extensions.getExtensions(NameSuggestionProvider.EP_NAME);
for (NameSuggestionProvider provider : providers) {
SuggestedNameInfo info = provider.getSuggestedNames(psiElement, nameSuggestionContext, result);
if (info != null) {
if (provider instanceof PreferrableNameSuggestionProvider && !((PreferrableNameSuggestionProvider) provider).shouldCheckOthers()) {
break;
}
}
}
return result;
}
use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class GrMainCompletionProvider method suggestVariableNames.
private static void suggestVariableNames(PsiElement context, CompletionResultSet result) {
final PsiElement parent = context.getParent();
if (GroovyCompletionUtil.isWildcardCompletion(context))
return;
if (parent instanceof GrVariable) {
final GrVariable variable = (GrVariable) parent;
if (context.equals(variable.getNameIdentifierGroovy())) {
final PsiType type = variable.getTypeGroovy();
if (type != null) {
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(context.getProject());
VariableKind kind = variable instanceof GrParameter ? VariableKind.PARAMETER : variable instanceof GrField ? VariableKind.FIELD : VariableKind.LOCAL_VARIABLE;
SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(kind, null, null, type);
String[] names = suggestedNameInfo.names;
if (names.length > 0) {
String name = names[0];
String newName = InlineMethodConflictSolver.suggestNewName(name, null, parent);
if (!name.equals(newName)) {
result.addElement(LookupElementBuilder.create(newName));
return;
}
}
for (String name : names) {
result.addElement(LookupElementBuilder.create(name));
}
}
GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
for (String name : GroovyNameSuggestionUtil.suggestVariableNames(initializer, new DefaultGroovyVariableNameValidator(variable), variable.hasModifierProperty(PsiModifier.STATIC))) {
result.addElement(LookupElementBuilder.create(name));
}
}
}
}
}
Aggregations