Search in sources :

Example 56 with JavaCodeStyleManager

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

the class BeanPropertyRenameHandler method doRename.

public static void doRename(@NotNull final BeanProperty property, final String newName, final boolean searchInComments, boolean isPreview) {
    final PsiElement psiElement = property.getPsiElement();
    final RenameRefactoring rename = new JavaRenameRefactoringImpl(psiElement.getProject(), psiElement, newName, searchInComments, false);
    rename.setPreviewUsages(isPreview);
    final PsiMethod setter = property.getSetter();
    if (setter != null) {
        final String setterName = PropertyUtil.suggestSetterName(newName);
        rename.addElement(setter, setterName);
        final PsiParameter[] setterParameters = setter.getParameterList().getParameters();
        if (setterParameters.length == 1) {
            final JavaCodeStyleManager manager = JavaCodeStyleManager.getInstance(psiElement.getProject());
            final String suggestedParameterName = manager.propertyNameToVariableName(property.getName(), VariableKind.PARAMETER);
            if (suggestedParameterName.equals(setterParameters[0].getName())) {
                rename.addElement(setterParameters[0], manager.propertyNameToVariableName(newName, VariableKind.PARAMETER));
            }
        }
    }
    final PsiMethod getter = property.getGetter();
    if (getter != null) {
        final String getterName = PropertyUtil.suggestGetterName(newName, getter.getReturnType());
        rename.addElement(getter, getterName);
    }
    rename.run();
}
Also used : JavaRenameRefactoringImpl(com.intellij.refactoring.openapi.impl.JavaRenameRefactoringImpl) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) RenameRefactoring(com.intellij.refactoring.RenameRefactoring)

Example 57 with JavaCodeStyleManager

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

the class JavaNameSuggestionProvider method suggestProperlyCasedName.

@Nullable
private static String[] suggestProperlyCasedName(PsiElement psiElement) {
    if (!(psiElement instanceof PsiNamedElement))
        return null;
    if (psiElement instanceof PsiFile)
        return null;
    String name = ((PsiNamedElement) psiElement).getName();
    if (name == null)
        return null;
    String prefix = "";
    if (psiElement instanceof PsiVariable) {
        final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(psiElement.getProject());
        final VariableKind kind = codeStyleManager.getVariableKind((PsiVariable) psiElement);
        prefix = codeStyleManager.getPrefixByVariableKind(kind);
        if (kind == VariableKind.STATIC_FINAL_FIELD) {
            final String[] words = NameUtil.splitNameIntoWords(name);
            StringBuilder buffer = new StringBuilder();
            for (int i = 0; i < words.length; i++) {
                String word = words[i];
                if (i > 0)
                    buffer.append('_');
                buffer.append(StringUtil.toUpperCase(word));
            }
            return new String[] { buffer.toString() };
        }
    }
    final List<String> result = new ArrayList<>();
    result.add(suggestProperlyCasedName(prefix, NameUtil.splitNameIntoWords(name)));
    if (name.startsWith(prefix) && !prefix.isEmpty()) {
        name = name.substring(prefix.length());
        result.add(suggestProperlyCasedName(prefix, NameUtil.splitNameIntoWords(name)));
    }
    result.add(suggestProperlyCasedName(prefix, NameUtil.splitNameIntoWords(name.toLowerCase())));
    return ArrayUtil.toStringArray(result);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) VariableKind(com.intellij.psi.codeStyle.VariableKind) Nullable(org.jetbrains.annotations.Nullable)

Example 58 with JavaCodeStyleManager

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

the class JavaNameSuggestionProvider method suggestNamesForElement.

@Nullable
private static SuggestedNameInfo suggestNamesForElement(final PsiElement element, PsiElement nameSuggestionContext) {
    PsiVariable var = null;
    if (element instanceof PsiVariable) {
        var = (PsiVariable) element;
    } else if (element instanceof PsiIdentifier) {
        PsiIdentifier identifier = (PsiIdentifier) element;
        if (identifier.getParent() instanceof PsiVariable) {
            var = (PsiVariable) identifier.getParent();
        }
    }
    if (var == null)
        return null;
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(element.getProject());
    VariableKind variableKind = codeStyleManager.getVariableKind(var);
    final SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(variableKind, null, var.getInitializer(), var.getType());
    final PsiExpression expression = PsiTreeUtil.getParentOfType(nameSuggestionContext, PsiExpression.class, false);
    if (expression != null) {
        return new SuggestedNameInfo.Delegate(codeStyleManager.suggestVariableName(variableKind, null, expression, var.getType()).names, nameInfo);
    }
    return nameInfo;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind) Nullable(org.jetbrains.annotations.Nullable)

Example 59 with JavaCodeStyleManager

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

the class MoveInnerProcessor method setup.

public void setup(final PsiClass innerClass, final String className, final boolean passOuterClass, final String parameterName, boolean searchInComments, boolean searchInNonJava, @NotNull final PsiElement targetContainer) {
    myNewClassName = className;
    myInnerClass = innerClass;
    myDescriptiveName = DescriptiveNameUtil.getDescriptiveName(myInnerClass);
    myOuterClass = myInnerClass.getContainingClass();
    myTargetContainer = targetContainer;
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(myProject);
    myParameterNameOuterClass = passOuterClass ? parameterName : null;
    if (myParameterNameOuterClass != null) {
        myFieldNameOuterClass = codeStyleManager.variableNameToPropertyName(myParameterNameOuterClass, VariableKind.PARAMETER);
        myFieldNameOuterClass = codeStyleManager.propertyNameToVariableName(myFieldNameOuterClass, VariableKind.FIELD);
    }
    mySearchInComments = searchInComments;
    mySearchInNonJavaFiles = searchInNonJava;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 60 with JavaCodeStyleManager

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

the class AnonymousCanBeLambdaInspection method generateLambdaByMethod.

/**
   * Try convert given method of given anonymous class into lambda and replace given element.
   *
   * @param anonymousClass      physical anonymous class containing method
   * @param method              physical method to convert with non-empty body
   * @param replacer            an operator which actually inserts a lambda into the file (possibly removing anonymous class)
   *                            and returns an inserted physical lambda
   * @param forceIgnoreTypeCast if false, type cast might be added if necessary
   * @return newly-generated lambda expression (possibly with typecast)
   */
@NotNull
static PsiExpression generateLambdaByMethod(PsiAnonymousClass anonymousClass, PsiMethod method, UnaryOperator<PsiLambdaExpression> replacer, boolean forceIgnoreTypeCast) {
    ChangeContextUtil.encodeContextInfo(anonymousClass, true);
    final String canonicalText = anonymousClass.getBaseClassType().getCanonicalText();
    final PsiCodeBlock body = method.getBody();
    LOG.assertTrue(body != null);
    final Collection<PsiComment> comments = collectCommentsOutsideMethodBody(anonymousClass, body);
    final Project project = anonymousClass.getProject();
    final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
    final String withoutTypesDeclared = ReplaceWithLambdaFix.composeLambdaText(method);
    PsiLambdaExpression lambdaExpression = (PsiLambdaExpression) elementFactory.createExpressionFromText(withoutTypesDeclared, anonymousClass);
    PsiElement lambdaBody = lambdaExpression.getBody();
    LOG.assertTrue(lambdaBody != null);
    lambdaBody.replace(body);
    lambdaExpression = replacer.apply(lambdaExpression);
    final Set<PsiVariable> variables = new HashSet<>();
    final Set<String> usedLocalNames = new HashSet<>();
    collectLocalVariablesDefinedInsideLambda(lambdaExpression, variables, usedLocalNames);
    ReplaceWithLambdaFix.giveUniqueNames(project, elementFactory, lambdaExpression, usedLocalNames, variables.toArray(new PsiVariable[variables.size()]));
    final PsiExpression singleExpr = RedundantLambdaCodeBlockInspection.isCodeBlockRedundant(lambdaExpression.getBody());
    if (singleExpr != null) {
        lambdaExpression.getBody().replace(singleExpr);
    }
    ChangeContextUtil.decodeContextInfo(lambdaExpression, null, null);
    restoreComments(comments, lambdaExpression);
    final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
    if (forceIgnoreTypeCast) {
        return (PsiExpression) javaCodeStyleManager.shortenClassReferences(lambdaExpression);
    }
    PsiTypeCastExpression typeCast = (PsiTypeCastExpression) elementFactory.createExpressionFromText("(" + canonicalText + ")" + withoutTypesDeclared, lambdaExpression);
    final PsiExpression typeCastOperand = typeCast.getOperand();
    LOG.assertTrue(typeCastOperand instanceof PsiLambdaExpression);
    final PsiElement fromText = ((PsiLambdaExpression) typeCastOperand).getBody();
    LOG.assertTrue(fromText != null);
    lambdaBody = lambdaExpression.getBody();
    LOG.assertTrue(lambdaBody != null);
    fromText.replace(lambdaBody);
    ((PsiLambdaExpression) typeCastOperand).getParameterList().replace(lambdaExpression.getParameterList());
    typeCast = (PsiTypeCastExpression) lambdaExpression.replace(typeCast);
    if (RedundantCastUtil.isCastRedundant(typeCast)) {
        final PsiExpression operand = typeCast.getOperand();
        LOG.assertTrue(operand != null);
        return (PsiExpression) typeCast.replace(operand);
    }
    return (PsiExpression) javaCodeStyleManager.shortenClassReferences(typeCast);
}
Also used : Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)96 Project (com.intellij.openapi.project.Project)27 SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)21 VariableKind (com.intellij.psi.codeStyle.VariableKind)19 IncorrectOperationException (com.intellij.util.IncorrectOperationException)15 NotNull (org.jetbrains.annotations.NotNull)13 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)12 Nullable (org.jetbrains.annotations.Nullable)12 ArrayList (java.util.ArrayList)10 NonNls (org.jetbrains.annotations.NonNls)8 StringUtil (com.intellij.openapi.util.text.StringUtil)3 UniqueNameGenerator (com.intellij.util.text.UniqueNameGenerator)3 HashSet (java.util.HashSet)3 JavaLanguage (com.intellij.lang.java.JavaLanguage)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Editor (com.intellij.openapi.editor.Editor)2 Comparing (com.intellij.openapi.util.Comparing)2 com.intellij.psi (com.intellij.psi)2 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)2 FunctionalInterfaceParameterizationUtil (com.intellij.psi.impl.source.resolve.graphInference.FunctionalInterfaceParameterizationUtil)2