Search in sources :

Example 21 with JavaCodeStyleManager

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

the class InlineUtil method solveVariableNameConflicts.

public static void solveVariableNameConflicts(final PsiElement scope, final PsiElement placeToInsert, final PsiElement renameScope) throws IncorrectOperationException {
    if (scope instanceof PsiVariable) {
        PsiVariable var = (PsiVariable) scope;
        String name = var.getName();
        String oldName = name;
        final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(scope.getProject());
        while (true) {
            String newName = codeStyleManager.suggestUniqueVariableName(name, placeToInsert, true);
            if (newName.equals(name))
                break;
            name = newName;
            newName = codeStyleManager.suggestUniqueVariableName(name, var, true);
            if (newName.equals(name))
                break;
            name = newName;
        }
        if (!name.equals(oldName)) {
            RefactoringUtil.renameVariableReferences(var, name, new LocalSearchScope(renameScope), true);
            var.getNameIdentifier().replace(JavaPsiFacade.getElementFactory(scope.getProject()).createIdentifier(name));
        }
    }
    PsiElement[] children = scope.getChildren();
    for (PsiElement child : children) {
        solveVariableNameConflicts(child, placeToInsert, renameScope);
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 22 with JavaCodeStyleManager

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

the class ReplaceConstructorWithBuilderDialog method applyNewSetterPrefix.

private void applyNewSetterPrefix() {
    final String setterPrefix = Messages.showInputDialog(myTable, "New setter prefix:", "Rename Setters Prefix", null, mySetterPrefix, new MySetterPrefixInputValidator());
    if (setterPrefix != null) {
        mySetterPrefix = setterPrefix;
        PropertiesComponent.getInstance(myProject).setValue(SETTER_PREFIX_KEY, setterPrefix);
        final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(myProject);
        for (String paramName : myParametersMap.keySet()) {
            final ParameterData data = myParametersMap.get(paramName);
            paramName = data.getParamName();
            final String propertyName = javaCodeStyleManager.variableNameToPropertyName(paramName, VariableKind.PARAMETER);
            data.setSetterName(PropertyUtil.suggestSetterName(propertyName, setterPrefix));
        }
        myTable.revalidate();
        myTable.repaint();
    }
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 23 with JavaCodeStyleManager

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

the class ReplaceConstructorWithBuilderProcessor method createMethodSignature.

private PsiMethod createMethodSignature(String createMethodName) {
    JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(myProject);
    final StringBuffer buf = new StringBuffer();
    final PsiMethod constructor = getWorkingConstructor();
    for (PsiParameter parameter : constructor.getParameterList().getParameters()) {
        final String pureParamName = styleManager.variableNameToPropertyName(parameter.getName(), VariableKind.PARAMETER);
        if (buf.length() > 0)
            buf.append(", ");
        buf.append(myParametersMap.get(pureParamName).getFieldName());
    }
    return myElementFactory.createMethodFromText("public " + constructor.getName() + " " + createMethodName + "(){\n return new " + constructor.getName() + "(" + buf.toString() + ")" + ";\n}", constructor);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 24 with JavaCodeStyleManager

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

the class ReplaceConstructorWithSettersChainInfo method fixUsage.

public void fixUsage() throws IncorrectOperationException {
    final PsiNewExpression expr = (PsiNewExpression) getElement();
    if (expr != null) {
        final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory();
        final PsiMethod constructor = expr.resolveConstructor();
        if (constructor != null) {
            StringBuffer buf = new StringBuffer();
            final PsiExpressionList argumentList = expr.getArgumentList();
            if (argumentList != null) {
                final PsiExpression[] args = argumentList.getExpressions();
                final PsiParameter[] parameters = constructor.getParameterList().getParameters();
                final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(constructor.getProject());
                for (int i = 0; i < Math.min(constructor.getParameterList().getParametersCount(), args.length); i++) {
                    String arg = args[i].getText();
                    if (parameters[i].isVarArgs()) {
                        for (int ia = i + 1; ia < args.length; ia++) {
                            arg += ", " + args[ia].getText();
                        }
                    }
                    final String pureParamName = styleManager.variableNameToPropertyName(parameters[i].getName(), VariableKind.PARAMETER);
                    final ParameterData data = myParametersMap.get(pureParamName);
                    if (!Comparing.strEqual(arg, data.getDefaultValue()) || data.isInsertSetter()) {
                        buf.append(data.getSetterName()).append("(").append(arg).append(").");
                    }
                }
                final PsiExpression settersChain = elementFactory.createExpressionFromText("new " + myBuilderClass + "()." + buf.toString() + "create" + StringUtil.capitalize(constructor.getName()) + "()", null);
                styleManager.shortenClassReferences(expr.replace(settersChain));
            }
        }
    }
}
Also used : ParameterData(com.intellij.refactoring.replaceConstructorWithBuilder.ParameterData) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 25 with JavaCodeStyleManager

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

the class PsiJavaCodeReferenceElementImpl method bindToClass.

private PsiElement bindToClass(@NotNull PsiClass aClass, @NotNull PsiFile containingFile) throws IncorrectOperationException {
    String qName = aClass.getQualifiedName();
    Project project = containingFile.getProject();
    boolean preserveQualification = JavaCodeStyleSettingsFacade.getInstance(project).useFQClassNames() && isFullyQualified(containingFile);
    JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    if (qName == null) {
        qName = aClass.getName();
        assert qName != null : aClass;
        PsiClass psiClass = facade.getResolveHelper().resolveReferencedClass(qName, this);
        if (!getManager().areElementsEquivalent(psiClass, aClass)) {
            throw cannotBindError(aClass);
        }
    } else if (facade.findClass(qName, getResolveScope()) == null && !preserveQualification) {
        return this;
    }
    String text = qName;
    PsiReferenceParameterList parameterList = getParameterList();
    if (parameterList != null) {
        text += parameterList.getText();
    }
    PsiJavaCodeReferenceElement ref;
    try {
        ref = facade.getParserFacade().createReferenceFromText(text, getParent());
    } catch (IncorrectOperationException e) {
        throw new IncorrectOperationException(e.getMessage() + " [qname=" + qName + " class=" + aClass + ";" + aClass.getClass().getName() + "]");
    }
    getTreeParent().replaceChildInternal(this, (TreeElement) ref.getNode());
    if (!preserveQualification) {
        JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
        ref = (PsiJavaCodeReferenceElement) codeStyleManager.shortenClassReferences(ref, JavaCodeStyleManager.INCOMPLETE_CODE);
    }
    return ref;
}
Also used : Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

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