Search in sources :

Example 71 with CodeStyleManager

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

the class ReturnStatementReturnValue method createReplacement.

public PsiStatement createReplacement(final PsiMethod extractedMethod, final PsiMethodCallExpression methodCallExpression) throws IncorrectOperationException {
    final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(methodCallExpression.getProject()).getElementFactory();
    final CodeStyleManager styleManager = CodeStyleManager.getInstance(methodCallExpression.getProject());
    PsiReturnStatement returnStatement = (PsiReturnStatement) elementFactory.createStatementFromText("return x;", null);
    returnStatement = (PsiReturnStatement) styleManager.reformat(returnStatement);
    returnStatement.getReturnValue().replace(methodCallExpression);
    return returnStatement;
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 72 with CodeStyleManager

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

the class PsiAdapter method addOrReplaceJavadoc.

/**
     * Add or replaces the javadoc comment to the given method.
     *
     * @param method           the method the javadoc should be added/set to.
     * @param javadoc          the javadoc comment.
     * @param replace          true if any existing javadoc should be replaced. false will not replace any existing javadoc and thus leave the javadoc untouched.
     * @return the added/replace javadoc comment, null if the was an existing javadoc and it should <b>not</b> be replaced.
     * @throws IncorrectOperationException is thrown if error adding/replacing the javadoc comment.
     */
@Nullable
public static PsiComment addOrReplaceJavadoc(PsiMethod method, String javadoc, boolean replace) {
    final Project project = method.getProject();
    PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
    PsiComment comment = factory.createCommentFromText(javadoc, null);
    // does a method already exists?
    PsiDocComment doc = method.getDocComment();
    if (doc != null) {
        if (replace) {
            // javadoc already exists, so replace
            doc.replace(comment);
            final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
            // to reformat javadoc
            codeStyleManager.reformat(method);
            return comment;
        } else {
            // do not replace existing javadoc
            return null;
        }
    } else {
        // add new javadoc
        method.addBefore(comment, method.getFirstChild());
        final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
        // to reformat javadoc
        codeStyleManager.reformat(method);
        return comment;
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) Nullable(org.jetbrains.annotations.Nullable)

Example 73 with CodeStyleManager

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

the class MakeClassStaticProcessor method changeInternalUsage.

protected void changeInternalUsage(final InternalUsageInfo usage, final PsiElementFactory factory) throws IncorrectOperationException {
    if (!mySettings.isChangeSignature())
        return;
    PsiElement element = usage.getElement();
    if (element instanceof PsiReferenceExpression) {
        PsiReferenceExpression newRef = null;
        if (mySettings.isMakeFieldParameters()) {
            PsiElement resolved = ((PsiReferenceExpression) element).resolve();
            if (resolved instanceof PsiField) {
                String name = mySettings.getNameForField((PsiField) resolved);
                if (name != null) {
                    name = convertToFieldName(name);
                    if (name != null) {
                        newRef = (PsiReferenceExpression) factory.createExpressionFromText(name, null);
                    }
                }
            }
        }
        if (newRef == null && mySettings.isMakeClassParameter()) {
            newRef = (PsiReferenceExpression) factory.createExpressionFromText(convertToFieldName(mySettings.getClassParameterName()) + "." + element.getText(), null);
        }
        if (newRef != null) {
            CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myProject);
            newRef = (PsiReferenceExpression) codeStyleManager.reformat(newRef);
            element.replace(newRef);
        }
    } else if (mySettings.isMakeClassParameter() && (element instanceof PsiThisExpression || element instanceof PsiSuperExpression)) {
        final PsiElement replace = element.replace(factory.createExpressionFromText(convertToFieldName(mySettings.getClassParameterName()), null));
        final PsiField field = PsiTreeUtil.getParentOfType(replace, PsiField.class);
        if (field != null) {
            myFieldsToSplit.add(field);
        }
    } else if (element instanceof PsiNewExpression && mySettings.isMakeClassParameter()) {
        final PsiNewExpression newExpression = ((PsiNewExpression) element);
        LOG.assertTrue(newExpression.getQualifier() == null);
        final String newText = convertToFieldName(mySettings.getClassParameterName()) + "." + newExpression.getText();
        final PsiExpression expr = factory.createExpressionFromText(newText, null);
        element.replace(expr);
    }
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 74 with CodeStyleManager

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

the class RefactoringChangeUtil method qualifyReference.

public static PsiReferenceExpression qualifyReference(@NotNull PsiReferenceExpression referenceExpression, @NotNull PsiMember member, @Nullable final PsiClass qualifyingClass) throws IncorrectOperationException {
    PsiManager manager = referenceExpression.getManager();
    PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(referenceExpression, PsiMethodCallExpression.class, true);
    while (methodCallExpression != null) {
        if (isSuperOrThisMethodCall(methodCallExpression)) {
            return referenceExpression;
        }
        methodCallExpression = PsiTreeUtil.getParentOfType(methodCallExpression, PsiMethodCallExpression.class, true);
    }
    PsiReferenceExpression expressionFromText;
    final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
    if (qualifyingClass == null) {
        PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class);
        final PsiClass containingClass = member.getContainingClass();
        if (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
            while (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
                parentClass = PsiTreeUtil.getParentOfType(parentClass, PsiClass.class, true);
            }
            LOG.assertTrue(parentClass != null);
            expressionFromText = (PsiReferenceExpression) factory.createExpressionFromText("A.this." + member.getName(), null);
            ((PsiThisExpression) expressionFromText.getQualifierExpression()).getQualifier().replace(factory.createClassReferenceElement(parentClass));
        } else {
            final PsiModifierListOwner staticElement = PsiUtil.getEnclosingStaticElement(referenceExpression, null);
            if (staticElement != null && containingClass != null && !PsiTreeUtil.isAncestor(staticElement, containingClass, false)) {
                return referenceExpression;
            } else {
                expressionFromText = (PsiReferenceExpression) factory.createExpressionFromText("this." + member.getName(), null);
            }
        }
    } else {
        expressionFromText = (PsiReferenceExpression) factory.createExpressionFromText("A." + member.getName(), null);
        expressionFromText.setQualifierExpression(factory.createReferenceExpression(qualifyingClass));
    }
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
    expressionFromText = (PsiReferenceExpression) codeStyleManager.reformat(expressionFromText);
    return (PsiReferenceExpression) referenceExpression.replace(expressionFromText);
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 75 with CodeStyleManager

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

the class FieldConflictsResolver method qualifyReference.

public static GrReferenceExpression qualifyReference(GrReferenceExpression referenceExpression, final PsiMember member, @Nullable final PsiClass qualifyingClass) throws IncorrectOperationException {
    PsiManager manager = referenceExpression.getManager();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(manager.getProject());
    GrReferenceExpression expressionFromText;
    if (qualifyingClass == null) {
        final PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class);
        final PsiClass containingClass = member.getContainingClass();
        if (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
            expressionFromText = (GrReferenceExpression) factory.createExpressionFromText(containingClass.getQualifiedName() + ".this." + member.getName());
        } else {
            expressionFromText = (GrReferenceExpression) factory.createExpressionFromText("this." + member.getName());
        }
    } else {
        expressionFromText = (GrReferenceExpression) factory.createExpressionFromText(qualifyingClass.getQualifiedName() + '.' + member.getName());
    }
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
    expressionFromText = (GrReferenceExpression) codeStyleManager.reformat(expressionFromText);
    return (GrReferenceExpression) referenceExpression.replace(expressionFromText);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

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