Search in sources :

Example 31 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 32 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 33 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)

Example 34 with JavaCodeStyleManager

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

the class JavaMemberNameCompletionContributor method completeFieldName.

private static void completeFieldName(Set<LookupElement> set, PsiField var, final PrefixMatcher matcher, boolean includeOverlapped) {
    FeatureUsageTracker.getInstance().triggerFeatureUsed("editing.completion.variable.name");
    Project project = var.getProject();
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
    final VariableKind variableKind = JavaCodeStyleManager.getInstance(project).getVariableKind(var);
    final String prefix = matcher.getPrefix();
    if (PsiType.VOID.equals(var.getType()) || psiField().inClass(psiClass().isInterface().andNot(psiClass().isAnnotationType())).accepts(var)) {
        completeVariableNameForRefactoring(project, set, matcher, var.getType(), variableKind, includeOverlapped, true);
        return;
    }
    SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(variableKind, null, null, var.getType());
    final String[] suggestedNames = suggestedNameInfo.names;
    addLookupItems(set, suggestedNameInfo, matcher, project, suggestedNames);
    if (!hasStartMatches(set, matcher) && includeOverlapped) {
        // use suggested names as suffixes
        final String requiredSuffix = codeStyleManager.getSuffixByVariableKind(variableKind);
        if (variableKind != VariableKind.STATIC_FINAL_FIELD) {
            for (int i = 0; i < suggestedNames.length; i++) suggestedNames[i] = codeStyleManager.variableNameToPropertyName(suggestedNames[i], variableKind);
        }
        addLookupItems(set, null, matcher, project, getOverlappedNameVersions(prefix, suggestedNames, requiredSuffix));
    }
    addLookupItems(set, suggestedNameInfo, matcher, project, getUnresolvedReferences(var.getParent(), false));
    PsiExpression initializer = var.getInitializer();
    PsiClass containingClass = var.getContainingClass();
    if (initializer != null && containingClass != null) {
        SuggestedNameInfo initializerSuggestions = InplaceIntroduceFieldPopup.suggestFieldName(var.getType(), null, initializer, var.hasModifierProperty(PsiModifier.STATIC), containingClass);
        addLookupItems(set, initializerSuggestions, matcher, project, initializerSuggestions.names);
    }
}
Also used : Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 35 with JavaCodeStyleManager

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

the class JavaMemberNameCompletionContributor method completeVariableNameForRefactoring.

public static void completeVariableNameForRefactoring(Project project, Set<LookupElement> set, PrefixMatcher matcher, PsiType varType, VariableKind varKind, final boolean includeOverlapped, final boolean methodPrefix) {
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
    SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(varKind, null, null, varType);
    final String[] strings = completeVariableNameForRefactoring(codeStyleManager, matcher, varType, varKind, suggestedNameInfo, includeOverlapped, methodPrefix);
    addLookupItems(set, suggestedNameInfo, matcher, project, strings);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

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