use of com.intellij.psi.codeStyle.JavaCodeStyleManager 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);
}
};
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class PsiReplacementUtil method replaceExpressionWithReferenceTo.
public static void replaceExpressionWithReferenceTo(@NotNull PsiExpression expression, @NotNull PsiMember target) {
final Project project = expression.getProject();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
final PsiElementFactory factory = psiFacade.getElementFactory();
final PsiReferenceExpression newExpression = (PsiReferenceExpression) factory.createExpressionFromText("xxx", expression);
final PsiReferenceExpression replacementExpression = (PsiReferenceExpression) expression.replace(newExpression);
final PsiElement element = replacementExpression.bindToElement(target);
final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
styleManager.shortenClassReferences(element);
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class DataPointHolderConversionIntention method convertToField.
private static PsiField convertToField(final PsiMethod method) {
final Project project = method.getProject();
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
final String fieldName = codeStyleManager.propertyNameToVariableName(method.getName(), VariableKind.STATIC_FIELD);
final PsiType returnType = method.getReturnType();
assert returnType != null;
final PsiField field = elementFactory.createField(fieldName, returnType);
final PsiStatement returnStatement = PsiTreeUtil.findChildOfType(method, PsiStatement.class);
if (returnStatement != null) {
field.setInitializer(((PsiReturnStatement) returnStatement).getReturnValue());
}
return field;
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager 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));
}
}
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class GenerateConstructorHandler method generateConstructorPrototype.
public static PsiMethod generateConstructorPrototype(PsiClass aClass, PsiMethod baseConstructor, boolean copyJavaDoc, PsiField[] fields) throws IncorrectOperationException {
PsiManager manager = aClass.getManager();
Project project = aClass.getProject();
JVMElementFactory factory = JVMElementFactories.requireFactory(aClass.getLanguage(), project);
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
String className = aClass.getName();
assert className != null : aClass;
PsiMethod constructor = factory.createConstructor(className, aClass);
GenerateMembersUtil.setVisibility(aClass, constructor);
if (baseConstructor != null) {
PsiJavaCodeReferenceElement[] throwRefs = baseConstructor.getThrowsList().getReferenceElements();
for (PsiJavaCodeReferenceElement ref : throwRefs) {
constructor.getThrowsList().add(ref);
}
if (copyJavaDoc) {
final PsiDocComment docComment = ((PsiMethod) baseConstructor.getNavigationElement()).getDocComment();
if (docComment != null) {
constructor.addAfter(docComment, null);
}
}
}
boolean isNotEnum = false;
if (baseConstructor != null) {
PsiClass superClass = aClass.getSuperClass();
LOG.assertTrue(superClass != null);
if (!CommonClassNames.JAVA_LANG_ENUM.equals(superClass.getQualifiedName())) {
isNotEnum = true;
if (baseConstructor instanceof PsiCompiledElement) {
// to get some parameter names
PsiClass dummyClass = JVMElementFactories.requireFactory(baseConstructor.getLanguage(), project).createClass("Dummy");
baseConstructor = (PsiMethod) dummyClass.add(baseConstructor);
}
PsiParameter[] params = baseConstructor.getParameterList().getParameters();
for (PsiParameter param : params) {
String name = param.getName();
assert name != null : param;
PsiParameter newParam = factory.createParameter(name, param.getType(), aClass);
GenerateMembersUtil.copyOrReplaceModifierList(param, aClass, newParam);
constructor.getParameterList().add(newParam);
}
}
}
JavaCodeStyleManager javaStyle = JavaCodeStyleManager.getInstance(project);
final PsiMethod dummyConstructor = factory.createConstructor(className);
dummyConstructor.getParameterList().replace(constructor.getParameterList().copy());
List<PsiParameter> fieldParams = new ArrayList<>();
for (PsiField field : fields) {
String fieldName = field.getName();
assert fieldName != null : field;
String name = javaStyle.variableNameToPropertyName(fieldName, VariableKind.FIELD);
String parmName = javaStyle.propertyNameToVariableName(name, VariableKind.PARAMETER);
parmName = javaStyle.suggestUniqueVariableName(parmName, dummyConstructor, true);
PsiParameter parm = factory.createParameter(parmName, field.getType(), aClass);
NullableNotNullManager.getInstance(project).copyNotNullAnnotation(field, parm);
if (constructor.isVarArgs()) {
final PsiParameterList parameterList = constructor.getParameterList();
parameterList.addBefore(parm, parameterList.getParameters()[parameterList.getParametersCount() - 1]);
final PsiParameterList dummyParameterList = dummyConstructor.getParameterList();
dummyParameterList.addBefore(parm.copy(), dummyParameterList.getParameters()[dummyParameterList.getParametersCount() - 1]);
} else {
constructor.getParameterList().add(parm);
dummyConstructor.getParameterList().add(parm.copy());
}
fieldParams.add(parm);
}
ConstructorBodyGenerator generator = ConstructorBodyGenerator.INSTANCE.forLanguage(aClass.getLanguage());
if (generator != null) {
StringBuilder buffer = new StringBuilder();
generator.start(buffer, constructor.getName(), PsiParameter.EMPTY_ARRAY);
if (isNotEnum) {
generator.generateSuperCallIfNeeded(buffer, baseConstructor.getParameterList().getParameters());
}
final PsiParameter[] parameters = fieldParams.toArray(new PsiParameter[fieldParams.size()]);
final List<String> existingNames = ContainerUtil.map(dummyConstructor.getParameterList().getParameters(), parameter -> parameter.getName());
if (generator instanceof ConstructorBodyGeneratorEx) {
((ConstructorBodyGeneratorEx) generator).generateFieldInitialization(buffer, fields, parameters, existingNames);
} else {
generator.generateFieldInitialization(buffer, fields, parameters);
}
generator.finish(buffer);
PsiMethod stub = factory.createMethodFromText(buffer.toString(), aClass);
PsiCodeBlock original = constructor.getBody(), replacement = stub.getBody();
assert original != null : constructor;
assert replacement != null : stub;
original.replace(replacement);
}
constructor = (PsiMethod) codeStyleManager.reformat(constructor);
return constructor;
}
Aggregations