use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaIntroduceParameterObjectClassDescriptor method createClass.
@Override
public PsiClass createClass(PsiMethod method, ReadWriteAccessDetector.Access[] accessors) {
if (isUseExistingClass()) {
return getExistingClass();
}
final ParameterObjectBuilder beanClassBuilder = new ParameterObjectBuilder();
beanClassBuilder.setVisibility(isCreateInnerClass() ? PsiModifier.PRIVATE : PsiModifier.PUBLIC);
beanClassBuilder.setProject(method.getProject());
beanClassBuilder.setTypeArguments(getTypeParameters());
beanClassBuilder.setClassName(getClassName());
beanClassBuilder.setPackageName(getPackageName());
PsiParameter[] parameters = method.getParameterList().getParameters();
final ParameterInfoImpl[] parameterInfos = getParamsToMerge();
for (int i = 0; i < parameterInfos.length; i++) {
PsiParameter parameter = parameters[parameterInfos[i].getOldIndex()];
final boolean setterRequired = accessors[i] == ReadWriteAccessDetector.Access.Write;
final String newName = parameterInfos[i].getName();
beanClassBuilder.addField(parameter, newName, parameterInfos[i].getTypeWrapper().getType(method), setterRequired);
}
final String classString = beanClassBuilder.buildBeanClass();
try {
final PsiFileFactory factory = PsiFileFactory.getInstance(method.getProject());
final PsiJavaFile newFile = (PsiJavaFile) factory.createFileFromText(getClassName() + ".java", JavaFileType.INSTANCE, classString);
if (isCreateInnerClass()) {
final PsiClass containingClass = method.getContainingClass();
final PsiClass[] classes = newFile.getClasses();
assert classes.length > 0 : classString;
final PsiClass innerClass = (PsiClass) containingClass.add(classes[0]);
PsiUtil.setModifierProperty(innerClass, PsiModifier.STATIC, true);
return (PsiClass) JavaCodeStyleManager.getInstance(newFile.getProject()).shortenClassReferences(innerClass);
} else {
final PsiFile containingFile = method.getContainingFile();
final PsiDirectory containingDirectory = containingFile.getContainingDirectory();
final PsiDirectory directory;
final MoveDestination moveDestination = getMoveDestination();
if (moveDestination != null) {
directory = moveDestination.getTargetDirectory(containingDirectory);
} else {
final Module module = ModuleUtilCore.findModuleForPsiElement(containingFile);
directory = PackageUtil.findOrCreateDirectoryForPackage(module, getPackageName(), containingDirectory, true, true);
}
if (directory != null) {
PsiFile file = directory.findFile(newFile.getName());
if (file == null) {
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(method.getManager().getProject());
final PsiElement shortenedFile = JavaCodeStyleManager.getInstance(newFile.getProject()).shortenClassReferences(newFile);
final PsiElement reformattedFile = codeStyleManager.reformat(shortenedFile);
file = (PsiFile) directory.add(reformattedFile);
}
return ((PsiJavaFile) file).getClasses()[0];
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return null;
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class MakeMethodStaticProcessor method changeInternalUsage.
protected void changeInternalUsage(InternalUsageInfo usage, PsiElementFactory factory) throws IncorrectOperationException {
if (!mySettings.isChangeSignature())
return;
PsiElement element = usage.getElement();
if (element instanceof PsiReferenceExpression) {
PsiReferenceExpression newRef = null;
if (mySettings.isMakeFieldParameters()) {
PsiElement resolved = ((PsiReferenceExpression) element).resolve();
if (resolved instanceof PsiField) {
String name = mySettings.getNameForField((PsiField) resolved);
if (name != null) {
newRef = (PsiReferenceExpression) factory.createExpressionFromText(name, null);
}
}
}
if (newRef == null && mySettings.isMakeClassParameter()) {
newRef = (PsiReferenceExpression) factory.createExpressionFromText(mySettings.getClassParameterName() + "." + element.getText(), null);
}
if (newRef != null) {
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myProject);
newRef = (PsiReferenceExpression) codeStyleManager.reformat(newRef);
element.replace(newRef);
}
} else if (element instanceof PsiThisExpression && mySettings.isMakeClassParameter()) {
element.replace(factory.createExpressionFromText(mySettings.getClassParameterName(), null));
} else if (element instanceof PsiSuperExpression && mySettings.isMakeClassParameter()) {
element.replace(factory.createExpressionFromText(mySettings.getClassParameterName(), null));
} else if (element instanceof PsiNewExpression && mySettings.isMakeClassParameter()) {
final PsiNewExpression newExpression = ((PsiNewExpression) element);
LOG.assertTrue(newExpression.getQualifier() == null);
final String newText = mySettings.getClassParameterName() + "." + newExpression.getText();
final PsiExpression expr = factory.createExpressionFromText(newText, null);
element.replace(expr);
}
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class PsiElementFactoryImpl method createParameter.
@NotNull
@Override
public PsiParameter createParameter(@NotNull final String name, @NotNull final PsiType type) throws IncorrectOperationException {
PsiUtil.checkIsIdentifier(myManager, name);
if (PsiType.NULL.equals(type)) {
throw new IncorrectOperationException("Cannot create parameter with type \"null\".");
}
final String text = type.getCanonicalText(true) + " " + name;
PsiParameter parameter = createParameterFromText(text, null);
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myManager.getProject());
PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalParameters());
GeneratedMarkerVisitor.markGenerated(parameter);
parameter = (PsiParameter) JavaCodeStyleManager.getInstance(myManager.getProject()).shortenClassReferences(parameter);
return (PsiParameter) codeStyleManager.reformat(parameter);
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class PsiElementFactoryImpl method createCatchSection.
@NotNull
@Override
public PsiCatchSection createCatchSection(@NotNull final PsiType exceptionType, @NotNull final String exceptionName, @Nullable final PsiElement context) throws IncorrectOperationException {
if (!(exceptionType instanceof PsiClassType || exceptionType instanceof PsiDisjunctionType)) {
throw new IncorrectOperationException("Unexpected type:" + exceptionType);
}
@NonNls final String text = "catch (" + exceptionType.getCanonicalText(true) + " " + exceptionName + ") {}";
final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, CATCH_SECTION, level(context)), context);
final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode());
if (!(element instanceof PsiCatchSection)) {
throw new IncorrectOperationException("Incorrect catch section '" + text + "'. Parsed element: " + element);
}
final Project project = myManager.getProject();
final JavaPsiImplementationHelper helper = JavaPsiImplementationHelper.getInstance(project);
helper.setupCatchBlock(exceptionName, exceptionType, context, (PsiCatchSection) element);
final CodeStyleManager styleManager = CodeStyleManager.getInstance(project);
final PsiCatchSection catchSection = (PsiCatchSection) styleManager.reformat(element);
GeneratedMarkerVisitor.markGenerated(catchSection);
return catchSection;
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JsonStringPropertyInsertHandler method reformat.
private static void reformat(@NotNull InsertionContext context, int startOffset, int endOffset) {
PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getDocument());
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(context.getProject());
codeStyleManager.reformatText(context.getFile(), startOffset, endOffset);
}
Aggregations