Search in sources :

Example 86 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class ExtractIfConditionAction method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final PsiIfStatement ifStatement = PsiTreeUtil.getParentOfType(element, PsiIfStatement.class);
    if (ifStatement == null) {
        return;
    }
    final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    final PsiStatement newIfStatement = create(factory, ifStatement, element);
    if (newIfStatement == null) {
        return;
    }
    ifStatement.replace(codeStyleManager.reformat(newIfStatement));
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 87 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class JavaWithBlockSurrounder 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;
    }
    String text = "{\n}";
    PsiBlockStatement blockStatement = (PsiBlockStatement) factory.createStatementFromText(text, null);
    blockStatement = (PsiBlockStatement) codeStyleManager.reformat(blockStatement);
    blockStatement = (PsiBlockStatement) container.addBefore(blockStatement, statements[0]);
    PsiCodeBlock body = blockStatement.getCodeBlock();
    SurroundWithUtil.indentCommentIfNecessary(body, statements);
    body.addRange(statements[0], statements[statements.length - 1]);
    container.deleteChildRange(statements[0], statements[statements.length - 1]);
    PsiElement firstChild = blockStatement.getFirstChild();
    if (firstChild == null) {
        return null;
    }
    TextRange range = firstChild.getTextRange();
    return new TextRange(range.getEndOffset(), range.getEndOffset());
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) TextRange(com.intellij.openapi.util.TextRange)

Example 88 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class JavaWithDoWhileSurrounder 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 = "do{\n}while(true);";
    PsiDoWhileStatement doWhileStatement = (PsiDoWhileStatement) factory.createStatementFromText(text, null);
    doWhileStatement = (PsiDoWhileStatement) codeStyleManager.reformat(doWhileStatement);
    doWhileStatement = (PsiDoWhileStatement) container.addAfter(doWhileStatement, statements[statements.length - 1]);
    PsiStatement body = doWhileStatement.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 = doWhileStatement.getCondition();
    return condition == null ? null : condition.getTextRange();
}
Also used : NonNls(org.jetbrains.annotations.NonNls) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 89 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class JavaWithIfExpressionSurrounder 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}";
    PsiIfStatement ifStatement = (PsiIfStatement) factory.createStatementFromText(text, null);
    ifStatement = (PsiIfStatement) codeStyleManager.reformat(ifStatement);
    PsiExpression condition = ifStatement.getCondition();
    if (condition != null) {
        condition.replace(expr);
    }
    PsiExpressionStatement statement = (PsiExpressionStatement) expr.getParent();
    ifStatement = (PsiIfStatement) statement.replace(ifStatement);
    PsiStatement thenBranch = ifStatement.getThenBranch();
    if (thenBranch != null && thenBranch instanceof PsiBlockStatement) {
        PsiCodeBlock block = ((PsiBlockStatement) thenBranch).getCodeBlock();
        block = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(block);
        TextRange range = block.getStatements()[0].getTextRange();
        editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
        return TextRange.from(range.getStartOffset(), 0);
    }
    return TextRange.from(editor.getCaretModel().getOffset(), 0);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) TextRange(com.intellij.openapi.util.TextRange)

Example 90 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class JavaWithIfSurrounder 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 = "if(a){\n}";
    PsiIfStatement ifStatement = (PsiIfStatement) factory.createStatementFromText(text, null);
    ifStatement = (PsiIfStatement) codeStyleManager.reformat(ifStatement);
    ifStatement = (PsiIfStatement) container.addAfter(ifStatement, statements[statements.length - 1]);
    final PsiStatement thenBranch = ifStatement.getThenBranch();
    if (thenBranch != 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);
    if (ifStatement == null) {
        return null;
    }
    final PsiExpression condition = ifStatement.getCondition();
    if (condition != null) {
        TextRange range = condition.getTextRange();
        TextRange textRange = new TextRange(range.getStartOffset(), range.getStartOffset());
        editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
        return textRange;
    }
    return ifStatement.getTextRange();
}
Also used : NonNls(org.jetbrains.annotations.NonNls) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) TextRange(com.intellij.openapi.util.TextRange)

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