Search in sources :

Example 26 with CodeStyleManager

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();
}
Also used : NonNls(org.jetbrains.annotations.NonNls) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 27 with CodeStyleManager

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());
}
Also used : NonNls(org.jetbrains.annotations.NonNls) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) TextRange(com.intellij.openapi.util.TextRange)

Example 28 with CodeStyleManager

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());
}
Also used : NonNls(org.jetbrains.annotations.NonNls) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) TextRange(com.intellij.openapi.util.TextRange)

Example 29 with CodeStyleManager

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());
}
Also used : NonNls(org.jetbrains.annotations.NonNls) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) TextRange(com.intellij.openapi.util.TextRange)

Example 30 with CodeStyleManager

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));
    }
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) MethodSignature(com.intellij.psi.util.MethodSignature) PullUpProcessor(com.intellij.refactoring.memberPullUp.PullUpProcessor)

Aggregations

CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)97 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)29 Project (com.intellij.openapi.project.Project)26 TextRange (com.intellij.openapi.util.TextRange)19 NonNls (org.jetbrains.annotations.NonNls)18 IncorrectOperationException (com.intellij.util.IncorrectOperationException)16 NotNull (org.jetbrains.annotations.NotNull)8 Document (com.intellij.openapi.editor.Document)6 PsiFile (com.intellij.psi.PsiFile)6 Module (com.intellij.openapi.module.Module)5 PsiElement (com.intellij.psi.PsiElement)4 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)4 Nullable (org.jetbrains.annotations.Nullable)4 CaretModel (com.intellij.openapi.editor.CaretModel)3 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)3 JavaLanguage (com.intellij.lang.java.JavaLanguage)2 FileType (com.intellij.openapi.fileTypes.FileType)2 Comparing (com.intellij.openapi.util.Comparing)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 com.intellij.psi (com.intellij.psi)2