Search in sources :

Example 56 with CodeStyleManager

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

the class Intention method replaceExpressionWithNegatedExpressionString.

protected static void replaceExpressionWithNegatedExpressionString(@NotNull String newExpression, @NotNull PsiExpression expression) {
    final Project project = expression.getProject();
    final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
    final PsiElementFactory factory = psiFacade.getElementFactory();
    PsiExpression expressionToReplace = expression;
    final String expString;
    if (BoolUtils.isNegated(expression)) {
        expressionToReplace = BoolUtils.findNegation(expressionToReplace);
        expString = newExpression;
    } else {
        PsiElement parent = expressionToReplace.getParent();
        while (parent instanceof PsiParenthesizedExpression) {
            expressionToReplace = (PsiExpression) parent;
            parent = parent.getParent();
        }
        expString = "!(" + newExpression + ')';
    }
    final PsiExpression newCall = factory.createExpressionFromText(expString, expression);
    assert expressionToReplace != null;
    final PsiElement insertedElement = expressionToReplace.replace(newCall);
    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    codeStyleManager.reformat(insertedElement);
}
Also used : Project(com.intellij.openapi.project.Project) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 57 with CodeStyleManager

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

the class MoveCommentToSeparateLineIntention method processIntention.

public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
    final PsiComment selectedComment = (PsiComment) element;
    PsiElement elementToCheck = selectedComment;
    final PsiWhiteSpace whiteSpace;
    while (true) {
        elementToCheck = PsiTreeUtil.prevLeaf(elementToCheck);
        if (elementToCheck == null) {
            return;
        }
        if (isLineBreakWhiteSpace(elementToCheck)) {
            whiteSpace = (PsiWhiteSpace) elementToCheck;
            break;
        }
    }
    final PsiElement copyWhiteSpace = whiteSpace.copy();
    final PsiElement parent = whiteSpace.getParent();
    assert parent != null;
    final PsiManager manager = selectedComment.getManager();
    final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
    final String commentText = selectedComment.getText();
    final PsiComment newComment = factory.createCommentFromText(commentText, parent);
    final PsiElement insertedComment = parent.addBefore(newComment, whiteSpace);
    parent.addBefore(copyWhiteSpace, insertedComment);
    selectedComment.delete();
    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
    codeStyleManager.reformat(insertedComment);
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 58 with CodeStyleManager

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

the class SurroundAutoCloseableAction method processVariable.

private static void processVariable(Project project, Editor editor, PsiLocalVariable variable) {
    PsiExpression initializer = ObjectUtils.assertNotNull(variable.getInitializer());
    PsiElement declaration = variable.getParent();
    PsiElement codeBlock = declaration.getParent();
    LocalSearchScope scope = new LocalSearchScope(codeBlock);
    PsiElement last = null;
    for (PsiReference reference : ReferencesSearch.search(variable, scope).findAll()) {
        PsiElement usage = PsiTreeUtil.findPrevParent(codeBlock, reference.getElement());
        if ((last == null || usage.getTextOffset() > last.getTextOffset())) {
            last = usage;
        }
    }
    String text = "try (" + variable.getTypeElement().getText() + " " + variable.getName() + " = " + initializer.getText() + ") {}";
    PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
    PsiTryStatement armStatement = (PsiTryStatement) declaration.replace(factory.createStatementFromText(text, codeBlock));
    List<PsiElement> toFormat = null;
    if (last != null) {
        toFormat = moveStatements(last, armStatement);
    }
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    PsiElement formattedElement = codeStyleManager.reformat(armStatement);
    if (toFormat != null) {
        for (PsiElement psiElement : toFormat) {
            codeStyleManager.reformat(psiElement);
        }
    }
    if (last == null) {
        PsiCodeBlock tryBlock = ((PsiTryStatement) formattedElement).getTryBlock();
        if (tryBlock != null) {
            PsiJavaToken brace = tryBlock.getLBrace();
            if (brace != null) {
                editor.getCaretModel().moveToOffset(brace.getTextOffset() + 1);
            }
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 59 with CodeStyleManager

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

the class MoveJavaInnerHandler method copyClass.

@NotNull
@Override
public PsiClass copyClass(@NotNull final MoveInnerOptions options) {
    PsiClass innerClass = options.getInnerClass();
    PsiClass newClass;
    if (options.getTargetContainer() instanceof PsiDirectory) {
        newClass = createNewClass(options);
        PsiDocComment defaultDocComment = newClass.getDocComment();
        if (defaultDocComment != null && innerClass.getDocComment() == null) {
            final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(defaultDocComment.getProject());
            innerClass = (PsiClass) codeStyleManager.reformat(innerClass.addAfter(defaultDocComment, null).getParent());
        }
        newClass = (PsiClass) newClass.replace(innerClass);
        PsiUtil.setModifierProperty(newClass, PsiModifier.STATIC, false);
        PsiUtil.setModifierProperty(newClass, PsiModifier.PRIVATE, false);
        PsiUtil.setModifierProperty(newClass, PsiModifier.PROTECTED, false);
        final boolean makePublic = needPublicAccess(options.getOuterClass(), options.getTargetContainer());
        if (makePublic) {
            PsiUtil.setModifierProperty(newClass, PsiModifier.PUBLIC, true);
        }
    } else {
        newClass = (PsiClass) options.getTargetContainer().add(innerClass);
    }
    newClass.setName(options.getNewClassName());
    return newClass;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) NotNull(org.jetbrains.annotations.NotNull)

Example 60 with CodeStyleManager

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

the class SimplifyBooleanExpressionFix method replaceWithStatements.

private static void replaceWithStatements(@NotNull PsiIfStatement orig, @Nullable PsiStatement statement) throws IncorrectOperationException {
    if (statement == null) {
        orig.delete();
        return;
    }
    PsiElement parent = orig.getParent();
    if (parent == null)
        return;
    if (parent instanceof PsiCodeBlock && blockAlwaysReturns(statement)) {
        removeFollowingStatements(orig, (PsiCodeBlock) parent);
    }
    if (statement instanceof PsiBlockStatement && parent instanceof PsiCodeBlock && !DeclarationSearchUtils.containsConflictingDeclarations(((PsiBlockStatement) statement).getCodeBlock(), (PsiCodeBlock) parent)) {
        // See IDEADEV-24277
        // Code block can only be inlined into another (parent) code block.
        // Code blocks, which are if or loop statement branches should not be inlined.
        PsiCodeBlock codeBlock = ((PsiBlockStatement) statement).getCodeBlock();
        PsiJavaToken lBrace = codeBlock.getLBrace();
        PsiJavaToken rBrace = codeBlock.getRBrace();
        if (lBrace == null || rBrace == null)
            return;
        final PsiElement[] children = codeBlock.getChildren();
        if (children.length > 2) {
            final PsiElement added = parent.addRangeBefore(children[1], children[children.length - 2], orig);
            final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(orig.getManager());
            codeStyleManager.reformat(added);
        }
        orig.delete();
    } else {
        orig.replace(statement);
    }
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) LocalQuickFixOnPsiElement(com.intellij.codeInspection.LocalQuickFixOnPsiElement)

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