Search in sources :

Example 16 with CodeStyleManager

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

the class InvertIfConditionAction method wrapWithCodeBlock.

private static PsiStatement wrapWithCodeBlock(@NotNull PsiStatement statement) {
    final Project project = statement.getProject();
    final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
    final CodeStyleManager codeStyle = CodeStyleManager.getInstance(project);
    PsiCodeBlock codeBlock = factory.createCodeBlockFromText("{}", statement);
    codeBlock = (PsiCodeBlock) codeStyle.reformat(codeBlock);
    codeBlock.add(statement);
    codeBlock = (PsiCodeBlock) statement.replace(codeBlock);
    return codeBlock.getStatements()[0];
}
Also used : Project(com.intellij.openapi.project.Project) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 17 with CodeStyleManager

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

the class InvertIfConditionAction method formatIf.

private static void formatIf(PsiIfStatement ifStatement) throws IncorrectOperationException {
    final Project project = ifStatement.getProject();
    PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
    PsiElement thenBranch = ifStatement.getThenBranch().copy();
    PsiElement elseBranch = ifStatement.getElseBranch() != null ? ifStatement.getElseBranch().copy() : null;
    PsiElement condition = ifStatement.getCondition().copy();
    final CodeStyleManager codeStyle = CodeStyleManager.getInstance(project);
    PsiBlockStatement codeBlock = (PsiBlockStatement) factory.createStatementFromText("{}", ifStatement);
    codeBlock = (PsiBlockStatement) codeStyle.reformat(codeBlock);
    ifStatement.getThenBranch().replace(codeBlock);
    if (elseBranch != null) {
        ifStatement.getElseBranch().replace(codeBlock);
    }
    ifStatement.getCondition().replace(factory.createExpressionFromText("true", null));
    ifStatement = (PsiIfStatement) codeStyle.reformat(ifStatement);
    if (!(thenBranch instanceof PsiBlockStatement)) {
        PsiBlockStatement codeBlock1 = (PsiBlockStatement) ifStatement.getThenBranch().replace(codeBlock);
        codeBlock1 = (PsiBlockStatement) codeStyle.reformat(codeBlock1);
        codeBlock1.getCodeBlock().add(thenBranch);
    } else {
        ifStatement.getThenBranch().replace(thenBranch);
    }
    if (elseBranch != null) {
        if (!(elseBranch instanceof PsiBlockStatement)) {
            PsiBlockStatement codeBlock1 = (PsiBlockStatement) ifStatement.getElseBranch().replace(codeBlock);
            codeBlock1 = (PsiBlockStatement) codeStyle.reformat(codeBlock1);
            codeBlock1.getCodeBlock().add(elseBranch);
        } else {
            elseBranch = ifStatement.getElseBranch().replace(elseBranch);
            if (emptyBlock(((PsiBlockStatement) elseBranch).getCodeBlock())) {
                ifStatement.getElseBranch().delete();
            }
        }
    }
    ifStatement.getCondition().replace(condition);
}
Also used : Project(com.intellij.openapi.project.Project) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 18 with CodeStyleManager

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

the class InvertIfConditionAction method setupBranches.

private static PsiIfStatement setupBranches(PsiIfStatement ifStatement, ControlFlow flow) throws IncorrectOperationException {
    PsiElementFactory factory = JavaPsiFacade.getInstance(ifStatement.getProject()).getElementFactory();
    Project project = ifStatement.getProject();
    PsiStatement thenBranch = ifStatement.getThenBranch();
    PsiStatement elseBranch = ifStatement.getElseBranch();
    if (elseBranch != null) {
        elseBranch = (PsiStatement) elseBranch.copy();
        setElseBranch(ifStatement, thenBranch, flow);
        ifStatement.getThenBranch().replace(elseBranch);
        return ifStatement;
    }
    final CodeStyleManager codeStyle = CodeStyleManager.getInstance(project);
    if (flow.getSize() == 0) {
        ifStatement.setElseBranch(thenBranch);
        PsiStatement statement = factory.createStatementFromText("{}", ifStatement);
        statement = (PsiStatement) codeStyle.reformat(statement);
        statement = (PsiStatement) ifStatement.getThenBranch().replace(statement);
        codeStyle.reformat(statement);
        return ifStatement;
    }
    int endOffset = calcEndOffset(flow, ifStatement);
    LOG.assertTrue(endOffset >= 0);
    if (endOffset >= flow.getSize()) {
        PsiStatement statement = factory.createStatementFromText("return;", ifStatement);
        statement = (PsiStatement) codeStyle.reformat(statement);
        if (thenBranch instanceof PsiBlockStatement) {
            PsiStatement[] statements = ((PsiBlockStatement) thenBranch).getCodeBlock().getStatements();
            if (statements.length > 0) {
                PsiElement firstElement = statements[0];
                while (firstElement.getPrevSibling() instanceof PsiWhiteSpace || firstElement.getPrevSibling() instanceof PsiComment) {
                    firstElement = firstElement.getPrevSibling();
                }
                ifStatement.getParent().addRangeAfter(firstElement, statements[statements.length - 1], ifStatement);
            }
        } else {
            if (!(thenBranch instanceof PsiReturnStatement)) {
                ifStatement = addAfterWithinCodeBlock(ifStatement, thenBranch);
            }
        }
        ifStatement.getThenBranch().replace(statement);
        return ifStatement;
    }
    PsiElement element = flow.getElement(endOffset);
    while (element != null && !(element instanceof PsiStatement)) element = element.getParent();
    if (element != null && element.getParent() instanceof PsiForStatement && ((PsiForStatement) element.getParent()).getUpdate() == element || element instanceof PsiWhileStatement && flow.getStartOffset(element) == endOffset || element instanceof PsiForeachStatement && flow.getStartOffset(element) + 1 == endOffset) {
        PsiStatement statement = factory.createStatementFromText("continue;", ifStatement);
        statement = (PsiStatement) codeStyle.reformat(statement);
        ifStatement = addAfterWithinCodeBlock(ifStatement, thenBranch);
        ifStatement.getThenBranch().replace(statement);
        return ifStatement;
    }
    if (element instanceof PsiReturnStatement) {
        PsiReturnStatement returnStatement = (PsiReturnStatement) element;
        ifStatement = addAfterWithinCodeBlock(ifStatement, thenBranch);
        ifStatement.getThenBranch().replace(returnStatement.copy());
        ControlFlow flow2 = buildControlFlow(findCodeBlock(ifStatement));
        if (!ControlFlowUtil.isInstructionReachable(flow2, flow2.getStartOffset(returnStatement), 0))
            returnStatement.delete();
        return ifStatement;
    }
    boolean nextUnreachable = flow.getEndOffset(ifStatement) == flow.getSize();
    if (!nextUnreachable) {
        PsiElement parent = ifStatement.getParent();
        if (parent != null) {
            if (!(parent instanceof PsiCodeBlock)) {
                ifStatement = (PsiIfStatement) wrapWithCodeBlock(ifStatement);
                parent = ifStatement.getParent();
                thenBranch = ifStatement.getThenBranch();
            }
            ControlFlow localFlow = buildControlFlow(parent);
            int startThenOffset = getThenOffset(localFlow, ifStatement);
            int afterIfOffset = localFlow.getEndOffset(ifStatement);
            nextUnreachable = !ControlFlowUtil.isInstructionReachable(localFlow, afterIfOffset, startThenOffset);
        }
    }
    if (nextUnreachable) {
        setElseBranch(ifStatement, thenBranch, flow);
        PsiElement first = ifStatement.getNextSibling();
        if (first != null) {
            PsiElement last = first;
            PsiElement next = last.getNextSibling();
            while (next != null && !(next instanceof PsiSwitchLabelStatement)) {
                last = next;
                next = next.getNextSibling();
            }
            while (first != last && (last instanceof PsiWhiteSpace || last instanceof PsiJavaToken && ((PsiJavaToken) last).getTokenType() == JavaTokenType.RBRACE)) last = last.getPrevSibling();
            PsiBlockStatement codeBlock = (PsiBlockStatement) factory.createStatementFromText("{}", ifStatement);
            codeBlock.getCodeBlock().addRange(first, last);
            first.getParent().deleteChildRange(first, last);
            ifStatement.getThenBranch().replace(codeBlock);
        }
        codeStyle.reformat(ifStatement);
        return ifStatement;
    }
    setElseBranch(ifStatement, thenBranch, flow);
    PsiStatement statement = factory.createStatementFromText("{}", ifStatement);
    statement = (PsiStatement) codeStyle.reformat(statement);
    statement = (PsiStatement) ifStatement.getThenBranch().replace(statement);
    codeStyle.reformat(statement);
    return ifStatement;
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) Project(com.intellij.openapi.project.Project)

Example 19 with CodeStyleManager

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

the class ReplaceConstructorWithBuilderProcessor method createBuilderClass.

@Nullable
private PsiClass createBuilderClass() {
    final PsiClass psiClass = myConstructors[0].getContainingClass();
    assert psiClass != null;
    final PsiTypeParameterList typeParameterList = psiClass.getTypeParameterList();
    final String text = "public class " + myClassName + (typeParameterList != null ? typeParameterList.getText() : "") + "{}";
    final PsiFileFactory factory = PsiFileFactory.getInstance(myProject);
    final PsiJavaFile newFile = (PsiJavaFile) factory.createFileFromText(myClassName + ".java", JavaFileType.INSTANCE, text);
    final PsiFile containingFile = myConstructors[0].getContainingFile();
    final PsiDirectory containingDirectory = containingFile.getContainingDirectory();
    final PsiDirectory directory;
    if (myMoveDestination != null) {
        directory = myMoveDestination.getTargetDirectory(containingDirectory);
    } else {
        final Module module = ModuleUtil.findModuleForPsiElement(containingFile);
        assert module != null;
        directory = PackageUtil.findOrCreateDirectoryForPackage(module, myPackageName, containingDirectory, true, true);
    }
    if (directory != null) {
        final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(PsiManager.getInstance(myProject).getProject());
        final PsiJavaFile reformattedFile = (PsiJavaFile) codeStyleManager.reformat(JavaCodeStyleManager.getInstance(newFile.getProject()).shortenClassReferences(newFile));
        if (directory.findFile(reformattedFile.getName()) != null)
            return reformattedFile.getClasses()[0];
        return ((PsiJavaFile) directory.add(reformattedFile)).getClasses()[0];
    }
    return null;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with CodeStyleManager

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

the class ReplaceWithTernaryOperatorFix method replaceWthConditionalExpression.

@NotNull
private static PsiConditionalExpression replaceWthConditionalExpression(@NotNull Project project, @NotNull String condition, @NotNull PsiExpression expression, @NotNull String defaultValue) {
    final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
    final PsiElement parent = expression.getParent();
    final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression) factory.createExpressionFromText(condition + " ? " + expression.getText() + " : " + defaultValue, parent);
    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    return (PsiConditionalExpression) expression.replace(codeStyleManager.reformat(conditionalExpression));
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) NotNull(org.jetbrains.annotations.NotNull)

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