use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.
the class ForEachMigration method migrate.
@Override
PsiElement migrate(@NotNull Project project, @NotNull PsiStatement body, @NotNull TerminalBlock tb) {
PsiLoopStatement loopStatement = tb.getMainLoop();
restoreComments(loopStatement, body);
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
PsiExpression mapExpression = tryExtractMapExpression(tb);
if (mapExpression != null) {
PsiMethodCallExpression call = tb.getSingleMethodCall();
LOG.assertTrue(call != null);
PsiType addedType = getAddedElementType(call);
if (addedType == null)
addedType = call.getType();
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, addedType, false);
if (suggestedNameInfo.names.length == 0) {
suggestedNameInfo = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, "item", null, null, false);
}
String varName = codeStyleManager.suggestUniqueVariableName(suggestedNameInfo, call, false).names[0];
String streamText = tb.add(new StreamApiMigrationInspection.MapOp(mapExpression, tb.getVariable(), addedType)).generate();
String forEachBody = varName + "->" + call.getMethodExpression().getText() + "(" + varName + ")";
String callText = streamText + "." + getReplacement() + "(" + forEachBody + ");";
return loopStatement.replace(factory.createStatementFromText(callText, loopStatement));
}
String stream = tb.generate(true) + "." + getReplacement() + "(";
PsiElement block = tb.convertToElement(factory);
final String functionalExpressionText = tb.getVariable().getName() + " -> " + wrapInBlock(block);
PsiExpressionStatement callStatement = (PsiExpressionStatement) factory.createStatementFromText(stream + functionalExpressionText + ");", loopStatement);
callStatement = (PsiExpressionStatement) loopStatement.replace(callStatement);
final PsiExpressionList argumentList = ((PsiCallExpression) callStatement.getExpression()).getArgumentList();
LOG.assertTrue(argumentList != null, callStatement.getText());
final PsiExpression[] expressions = argumentList.getExpressions();
LOG.assertTrue(expressions.length == 1);
if (expressions[0] instanceof PsiFunctionalExpression && ((PsiFunctionalExpression) expressions[0]).getFunctionalInterfaceType() == null) {
callStatement = (PsiExpressionStatement) callStatement.replace(factory.createStatementFromText(stream + "(" + tb.getVariable().getText() + ") -> " + wrapInBlock(block) + ");", callStatement));
}
return callStatement;
}
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 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));
}
}
}
}
}
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-plugins by JetBrains.
the class ActionNameCustomReferenceConverter method createReferences.
@NotNull
@Override
public PsiReference[] createReferences(final GenericDomValue<String> genericDomValue, final PsiElement psiElement, final ConvertContext convertContext) {
final PsiReferenceBase<PsiElement> ref = new PsiReferenceBase<PsiElement>(psiElement) {
@SuppressWarnings({ "ConstantConditions" })
public PsiElement resolve() {
return genericDomValue.getParent().getXmlTag();
}
public boolean isSoft() {
return true;
}
// do nothing. the element will be renamed via PsiMetaData
public PsiElement handleElementRename(final String newElementName) throws IncorrectOperationException {
return getElement();
}
@NotNull
public Object[] getVariants() {
final DomElement invocationElement = convertContext.getInvocationElement();
final Action action = invocationElement.getParentOfType(Action.class, true);
assert action != null;
final PsiClass psiClass = action.searchActionClass();
if (psiClass == null) {
return EMPTY_ARRAY;
}
final Project project = psiClass.getProject();
final PsiClassType classType = PsiTypesUtil.getClassType(psiClass);
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
final SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, classType);
final Set<String> variants = new HashSet<>(Arrays.asList(info.names));
variants.remove(ACTION_SUFFIX);
// remove existing action-names
final List<Action> actions = action.getStrutsPackage().getActions();
ContainerUtil.process(actions, action1 -> {
variants.remove(action1.getName().getStringValue());
return true;
});
return ContainerUtil.map2Array(variants, ACTION_NAME_FUNCTION);
}
};
return new PsiReference[] { ref };
}
Aggregations