use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class PsiReplacementUtil method replaceStatementAndShortenClassNames.
public static void replaceStatementAndShortenClassNames(@NotNull PsiStatement statement, @NotNull @NonNls String newStatementText) {
final Project project = statement.getProject();
final CodeStyleManager styleManager = CodeStyleManager.getInstance(project);
final JavaCodeStyleManager javaStyleManager = JavaCodeStyleManager.getInstance(project);
if (FileTypeUtils.isInServerPageFile(statement)) {
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final PsiFile jspFile = PsiUtilCore.getTemplateLanguageFile(statement);
if (jspFile == null) {
return;
}
final Document document = documentManager.getDocument(jspFile);
if (document == null) {
return;
}
documentManager.doPostponedOperationsAndUnblockDocument(document);
final TextRange textRange = statement.getTextRange();
document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), newStatementText);
documentManager.commitDocument(document);
final FileViewProvider viewProvider = jspFile.getViewProvider();
PsiElement elementAt = viewProvider.findElementAt(textRange.getStartOffset(), JavaLanguage.INSTANCE);
if (elementAt == null) {
return;
}
final int endOffset = textRange.getStartOffset() + newStatementText.length();
while (elementAt.getTextRange().getEndOffset() < endOffset || !(elementAt instanceof PsiStatement)) {
elementAt = elementAt.getParent();
if (elementAt == null) {
LOG.error("Cannot decode statement");
return;
}
}
final PsiStatement newStatement = (PsiStatement) elementAt;
javaStyleManager.shortenClassReferences(newStatement);
final TextRange newTextRange = newStatement.getTextRange();
final Language baseLanguage = viewProvider.getBaseLanguage();
final PsiFile element = viewProvider.getPsi(baseLanguage);
if (element != null) {
styleManager.reformatRange(element, newTextRange.getStartOffset(), newTextRange.getEndOffset());
}
} else {
final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
final PsiElementFactory factory = facade.getElementFactory();
PsiStatement newStatement = factory.createStatementFromText(newStatementText, statement);
newStatement = (PsiStatement) statement.replace(newStatement);
javaStyleManager.shortenClassReferences(newStatement);
styleManager.reformat(newStatement);
}
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class AnonymousToInnerHandler method createClass.
private PsiClass createClass(String name) throws IncorrectOperationException {
PsiElementFactory factory = JavaPsiFacade.getInstance(myAnonClass.getProject()).getElementFactory();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myProject);
final PsiNewExpression newExpression = (PsiNewExpression) myAnonClass.getParent();
final PsiMethod superConstructor = newExpression.resolveConstructor();
PsiClass aClass = factory.createClass(name);
final PsiTypeParameterList typeParameterList = aClass.getTypeParameterList();
LOG.assertTrue(typeParameterList != null);
for (PsiTypeParameter typeParameter : myTypeParametersToCreate) {
typeParameterList.add((typeParameter));
}
if (!myTargetClass.isInterface()) {
PsiUtil.setModifierProperty(aClass, PsiModifier.PRIVATE, true);
PsiModifierListOwner owner = PsiTreeUtil.getParentOfType(myAnonClass, PsiModifierListOwner.class);
if (owner != null && owner.hasModifierProperty(PsiModifier.STATIC)) {
PsiUtil.setModifierProperty(aClass, PsiModifier.STATIC, true);
}
} else {
PsiUtil.setModifierProperty(aClass, PsiModifier.PACKAGE_LOCAL, true);
}
PsiJavaCodeReferenceElement baseClassRef = myAnonClass.getBaseClassReference();
PsiClass baseClass = (PsiClass) baseClassRef.resolve();
if (baseClass == null || !CommonClassNames.JAVA_LANG_OBJECT.equals(baseClass.getQualifiedName())) {
PsiReferenceList refList = baseClass != null && baseClass.isInterface() ? aClass.getImplementsList() : aClass.getExtendsList();
if (refList != null)
refList.add(baseClassRef);
}
renameReferences(myAnonClass);
copyClassBody(myAnonClass, aClass, myVariableInfos.length > 0);
if (myVariableInfos.length > 0) {
createFields(aClass);
}
PsiExpressionList argList = newExpression.getArgumentList();
assert argList != null;
PsiExpression[] originalExpressions = argList.getExpressions();
final PsiReferenceList superConstructorThrowsList = superConstructor != null && superConstructor.getThrowsList().getReferencedTypes().length > 0 ? superConstructor.getThrowsList() : null;
if (myVariableInfos.length > 0 || originalExpressions.length > 0 || superConstructorThrowsList != null) {
PsiMethod constructor = factory.createConstructor();
if (superConstructorThrowsList != null) {
constructor.getThrowsList().replace(superConstructorThrowsList);
}
if (originalExpressions.length > 0) {
createSuperStatement(constructor, originalExpressions);
}
if (myVariableInfos.length > 0) {
fillParameterList(constructor);
createAssignmentStatements(constructor);
appendInitializers(constructor);
}
constructor = (PsiMethod) codeStyleManager.reformat(constructor);
aClass.add(constructor);
}
if (!needsThis() && myMakeStatic && !myTargetClass.isInterface()) {
PsiUtil.setModifierProperty(aClass, PsiModifier.STATIC, true);
}
PsiElement lastChild = aClass.getLastChild();
if (lastChild instanceof PsiJavaToken && ((PsiJavaToken) lastChild).getTokenType() == JavaTokenType.SEMICOLON) {
lastChild.delete();
}
return aClass;
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaSourceUtil method addParenthToReplacedChild.
public static TreeElement addParenthToReplacedChild(@NotNull IElementType parenthType, @NotNull TreeElement newChild, @NotNull PsiManager manager) {
CompositeElement parenthExpr = ASTFactory.composite(parenthType);
TreeElement dummyExpr = (TreeElement) newChild.clone();
final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(newChild);
new DummyHolder(manager, parenthExpr, null, charTableByTree);
parenthExpr.putUserData(CharTable.CHAR_TABLE_KEY, charTableByTree);
parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.LPARENTH, "("));
parenthExpr.rawAddChildren(dummyExpr);
parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.RPARENTH, ")"));
try {
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
PsiElement formatted = codeStyleManager.reformat(SourceTreeToPsiMap.treeToPsiNotNull(parenthExpr));
parenthExpr = (CompositeElement) SourceTreeToPsiMap.psiToTreeNotNull(formatted);
} catch (IncorrectOperationException e) {
// should not happen
LOG.error(e);
}
newChild.putUserData(CharTable.CHAR_TABLE_KEY, SharedImplUtil.findCharTableByTree(newChild));
dummyExpr.getTreeParent().replaceChild(dummyExpr, newChild);
// TODO remove explicit caches drop since this should be ok if we will use ChangeUtil for the modification
TreeUtil.clearCaches(TreeUtil.getFileElement(parenthExpr));
return parenthExpr;
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class WrapReturnValueProcessor method buildClass.
private boolean buildClass() {
final PsiManager manager = myMethod.getManager();
final Project project = myMethod.getProject();
final ReturnValueBeanBuilder beanClassBuilder = new ReturnValueBeanBuilder();
beanClassBuilder.setProject(project);
beanClassBuilder.setTypeArguments(myTypeParameters);
beanClassBuilder.setClassName(myClassName);
beanClassBuilder.setPackageName(myPackageName);
beanClassBuilder.setStatic(myCreateInnerClass && myMethod.hasModifierProperty(PsiModifier.STATIC));
final PsiType returnType = myMethod.getReturnType();
beanClassBuilder.setValueType(returnType);
final String classString;
try {
classString = beanClassBuilder.buildBeanClass();
} catch (IOException e) {
LOG.error(e);
return false;
}
try {
final PsiFileFactory factory = PsiFileFactory.getInstance(project);
final PsiJavaFile psiFile = (PsiJavaFile) factory.createFileFromText(myClassName + ".java", JavaFileType.INSTANCE, classString);
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
if (myCreateInnerClass) {
final PsiClass containingClass = myMethod.getContainingClass();
final PsiElement innerClass = containingClass.add(psiFile.getClasses()[0]);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(innerClass);
} else {
final PsiFile containingFile = myMethod.getContainingFile();
final PsiDirectory containingDirectory = containingFile.getContainingDirectory();
final PsiDirectory directory;
if (myMoveDestination != null) {
directory = myMoveDestination.getTargetDirectory(containingDirectory);
} else {
final Module module = ModuleUtilCore.findModuleForPsiElement(containingFile);
directory = PackageUtil.findOrCreateDirectoryForPackage(module, myPackageName, containingDirectory, true, true);
}
if (directory != null) {
final PsiElement shortenedFile = JavaCodeStyleManager.getInstance(project).shortenClassReferences(psiFile);
final PsiElement reformattedFile = codeStyleManager.reformat(shortenedFile);
directory.add(reformattedFile);
} else {
return false;
}
}
} catch (IncorrectOperationException e) {
LOG.info(e);
return false;
}
return true;
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JsonBySchemaObjectCompletionContributor method formatInsertedString.
public static void formatInsertedString(@NotNull InsertionContext context, int offset) {
Project project = context.getProject();
PsiDocumentManager.getInstance(project).commitDocument(context.getDocument());
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
codeStyleManager.reformatText(context.getFile(), context.getStartOffset(), context.getTailOffset() + offset);
}
Aggregations