use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaWithWhileSurrounder method surroundStatements.
@Override
public TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException {
PsiManager manager = PsiManager.getInstance(project);
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
statements = SurroundWithUtil.moveDeclarationsOut(container, statements, true);
if (statements.length == 0) {
return null;
}
@NonNls String text = "while(true){\n}";
PsiWhileStatement whileStatement = (PsiWhileStatement) factory.createStatementFromText(text, null);
whileStatement = (PsiWhileStatement) codeStyleManager.reformat(whileStatement);
whileStatement = (PsiWhileStatement) container.addAfter(whileStatement, statements[statements.length - 1]);
PsiStatement body = whileStatement.getBody();
if (!(body instanceof PsiBlockStatement)) {
return null;
}
PsiCodeBlock bodyBlock = ((PsiBlockStatement) body).getCodeBlock();
SurroundWithUtil.indentCommentIfNecessary(bodyBlock, statements);
bodyBlock.addRange(statements[0], statements[statements.length - 1]);
container.deleteChildRange(statements[0], statements[statements.length - 1]);
PsiExpression condition = whileStatement.getCondition();
return condition == null ? null : condition.getTextRange();
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaWithForSurrounder method surroundStatements.
@Override
public TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException {
PsiManager manager = PsiManager.getInstance(project);
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
statements = SurroundWithUtil.moveDeclarationsOut(container, statements, true);
if (statements.length == 0) {
return null;
}
@NonNls String text = "for(a;b;c){\n}";
PsiForStatement forStatement = (PsiForStatement) factory.createStatementFromText(text, null);
forStatement = (PsiForStatement) codeStyleManager.reformat(forStatement);
forStatement = (PsiForStatement) container.addAfter(forStatement, statements[statements.length - 1]);
PsiStatement body = forStatement.getBody();
if (!(body instanceof PsiBlockStatement)) {
return null;
}
PsiCodeBlock bodyBlock = ((PsiBlockStatement) body).getCodeBlock();
SurroundWithUtil.indentCommentIfNecessary(bodyBlock, statements);
bodyBlock.addRange(statements[0], statements[statements.length - 1]);
container.deleteChildRange(statements[0], statements[statements.length - 1]);
forStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(forStatement);
PsiStatement initialization = forStatement.getInitialization();
if (initialization == null) {
return null;
}
TextRange range1 = initialization.getTextRange();
PsiStatement update = forStatement.getUpdate();
if (update == null) {
return null;
}
TextRange range3 = update.getTextRange();
editor.getDocument().deleteString(range1.getStartOffset(), range3.getEndOffset());
return new TextRange(range1.getStartOffset(), range1.getStartOffset());
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaWithIfElseExpressionSurrounder method surroundExpression.
@Override
public TextRange surroundExpression(Project project, Editor editor, PsiExpression expr) throws IncorrectOperationException {
PsiManager manager = expr.getManager();
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
@NonNls String text = "if(a){\nst;\n}else{\n}";
PsiIfStatement ifStatement = (PsiIfStatement) factory.createStatementFromText(text, null);
ifStatement = (PsiIfStatement) codeStyleManager.reformat(ifStatement);
ifStatement.getCondition().replace(expr);
PsiExpressionStatement statement = (PsiExpressionStatement) expr.getParent();
ifStatement = (PsiIfStatement) statement.replace(ifStatement);
PsiCodeBlock block = ((PsiBlockStatement) ifStatement.getThenBranch()).getCodeBlock();
PsiStatement afterStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(block.getStatements()[0]);
TextRange range = afterStatement.getTextRange();
editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
return new TextRange(range.getStartOffset(), range.getStartOffset());
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class JavaWithIfElseSurrounder method surroundStatements.
@Override
public TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException {
PsiManager manager = PsiManager.getInstance(project);
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
statements = SurroundWithUtil.moveDeclarationsOut(container, statements, false);
if (statements.length == 0) {
return null;
}
@NonNls String text = "if(a){\n}else{\n}";
PsiIfStatement ifStatement = (PsiIfStatement) factory.createStatementFromText(text, null);
ifStatement = (PsiIfStatement) codeStyleManager.reformat(ifStatement);
ifStatement = (PsiIfStatement) container.addAfter(ifStatement, statements[statements.length - 1]);
PsiStatement thenBranch = ifStatement.getThenBranch();
if (!(thenBranch instanceof PsiBlockStatement)) {
return null;
}
PsiCodeBlock thenBlock = ((PsiBlockStatement) thenBranch).getCodeBlock();
SurroundWithUtil.indentCommentIfNecessary(thenBlock, statements);
thenBlock.addRange(statements[0], statements[statements.length - 1]);
container.deleteChildRange(statements[0], statements[statements.length - 1]);
ifStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(ifStatement);
PsiExpression condition = ifStatement.getCondition();
if (condition == null) {
return null;
}
TextRange range = condition.getTextRange();
editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
return new TextRange(range.getStartOffset(), range.getStartOffset());
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class ExtractSuperClassUtil method extractSuperClass.
public static PsiClass extractSuperClass(final Project project, final PsiDirectory targetDirectory, final String superclassName, final PsiClass subclass, final MemberInfo[] selectedMemberInfos, final DocCommentPolicy javaDocPolicy) throws IncorrectOperationException {
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(REFACTORING_EXTRACT_SUPER_ID, createBeforeData(subclass, selectedMemberInfos));
final PsiClass superclass = JavaDirectoryService.getInstance().createClass(targetDirectory, superclassName);
try {
final PsiModifierList superClassModifierList = superclass.getModifierList();
assert superClassModifierList != null;
superClassModifierList.setModifierProperty(PsiModifier.FINAL, false);
final PsiReferenceList subClassExtends = subclass.getExtendsList();
if (subClassExtends != null) {
copyPsiReferenceList(subClassExtends, superclass.getExtendsList());
} else if (subclass instanceof PsiAnonymousClass) {
PsiJavaCodeReferenceElement classReference = ((PsiAnonymousClass) subclass).getBaseClassReference();
PsiElement baseClass = classReference.resolve();
if (baseClass instanceof PsiClass && ((PsiClass) baseClass).isInterface()) {
superclass.getImplementsList().add(classReference);
} else {
superclass.getExtendsList().add(classReference);
}
}
// create constructors if neccesary
PsiMethod[] constructors = getCalledBaseConstructors(subclass);
if (constructors.length > 0) {
createConstructorsByPattern(project, superclass, constructors);
}
// clear original class' "extends" list
if (subClassExtends != null) {
clearPsiReferenceList(subclass.getExtendsList());
}
// make original class extend extracted superclass
PsiJavaCodeReferenceElement ref = createExtendingReference(superclass, subclass, selectedMemberInfos);
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
if (subClassExtends != null) {
codeStyleManager.reformat(subclass.getExtendsList().add(ref));
} else if (subclass instanceof PsiAnonymousClass) {
codeStyleManager.reformat(((PsiAnonymousClass) subclass).getBaseClassReference().replace(ref));
}
PullUpProcessor pullUpHelper = new PullUpProcessor(subclass, superclass, selectedMemberInfos, javaDocPolicy);
pullUpHelper.moveMembersToBase();
pullUpHelper.moveFieldInitializations();
Collection<MethodSignature> toImplement = OverrideImplementExploreUtil.getMethodSignaturesToImplement(superclass);
if (!toImplement.isEmpty()) {
superClassModifierList.setModifierProperty(PsiModifier.ABSTRACT, true);
}
return superclass;
} finally {
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(REFACTORING_EXTRACT_SUPER_ID, createAfterData(superclass));
}
}
Aggregations