Search in sources :

Example 11 with JavaCodeStyleManager

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

the class IntroduceParameterHandler method createNameSuggestionGenerator.

protected static NameSuggestionsGenerator createNameSuggestionGenerator(final PsiExpression expr, final String propName, final Project project, final String enteredName) {
    return new NameSuggestionsGenerator() {

        public SuggestedNameInfo getSuggestedNameInfo(PsiType type) {
            final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
            SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, propName, expr != null && expr.isValid() ? expr : null, type);
            if (expr != null && expr.isValid()) {
                info = codeStyleManager.suggestUniqueVariableName(info, expr, true);
            }
            final String[] strings = AbstractJavaInplaceIntroducer.appendUnresolvedExprName(JavaCompletionUtil.completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, info), expr);
            return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[] { enteredName }, strings) : strings, info);
        }
    };
}
Also used : NameSuggestionsGenerator(com.intellij.refactoring.ui.NameSuggestionsGenerator) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Example 12 with JavaCodeStyleManager

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

the class PsiReplacementUtil method replaceExpressionWithReferenceTo.

public static void replaceExpressionWithReferenceTo(@NotNull PsiExpression expression, @NotNull PsiMember target) {
    final Project project = expression.getProject();
    final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
    final PsiElementFactory factory = psiFacade.getElementFactory();
    final PsiReferenceExpression newExpression = (PsiReferenceExpression) factory.createExpressionFromText("xxx", expression);
    final PsiReferenceExpression replacementExpression = (PsiReferenceExpression) expression.replace(newExpression);
    final PsiElement element = replacementExpression.bindToElement(target);
    final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
    styleManager.shortenClassReferences(element);
}
Also used : Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 13 with JavaCodeStyleManager

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

the class DataPointHolderConversionIntention method convertToField.

private static PsiField convertToField(final PsiMethod method) {
    final Project project = method.getProject();
    final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
    final String fieldName = codeStyleManager.propertyNameToVariableName(method.getName(), VariableKind.STATIC_FIELD);
    final PsiType returnType = method.getReturnType();
    assert returnType != null;
    final PsiField field = elementFactory.createField(fieldName, returnType);
    final PsiStatement returnStatement = PsiTreeUtil.findChildOfType(method, PsiStatement.class);
    if (returnStatement != null) {
        field.setInitializer(((PsiReturnStatement) returnStatement).getReturnValue());
    }
    return field;
}
Also used : Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 14 with JavaCodeStyleManager

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

the class JavaIntroduceVariableTest method doTest.

protected static void doTest(String expression, VariableKind kind, PsiType type, String... results) throws Exception {
    final PsiElementFactory factory = JavaPsiFacade.getElementFactory(getProject());
    final PsiExpression expr = factory.createExpressionFromText(expression, null);
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(getProject());
    final SuggestedNameInfo info = codeStyleManager.suggestVariableName(kind, null, expr, type);
    assert info.names.length >= results.length : msg("Can't find some variants", info.names, results);
    for (int i = 0; i < results.length; i++) {
        if (!results[i].equals(info.names[i])) {
            throw new Exception(msg("", info.names, results));
        }
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) PsiElementFactory(com.intellij.psi.PsiElementFactory) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Example 15 with JavaCodeStyleManager

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

the class GenerateConstructorHandler method generateConstructorPrototype.

public static PsiMethod generateConstructorPrototype(PsiClass aClass, PsiMethod baseConstructor, boolean copyJavaDoc, PsiField[] fields) throws IncorrectOperationException {
    PsiManager manager = aClass.getManager();
    Project project = aClass.getProject();
    JVMElementFactory factory = JVMElementFactories.requireFactory(aClass.getLanguage(), project);
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
    String className = aClass.getName();
    assert className != null : aClass;
    PsiMethod constructor = factory.createConstructor(className, aClass);
    GenerateMembersUtil.setVisibility(aClass, constructor);
    if (baseConstructor != null) {
        PsiJavaCodeReferenceElement[] throwRefs = baseConstructor.getThrowsList().getReferenceElements();
        for (PsiJavaCodeReferenceElement ref : throwRefs) {
            constructor.getThrowsList().add(ref);
        }
        if (copyJavaDoc) {
            final PsiDocComment docComment = ((PsiMethod) baseConstructor.getNavigationElement()).getDocComment();
            if (docComment != null) {
                constructor.addAfter(docComment, null);
            }
        }
    }
    boolean isNotEnum = false;
    if (baseConstructor != null) {
        PsiClass superClass = aClass.getSuperClass();
        LOG.assertTrue(superClass != null);
        if (!CommonClassNames.JAVA_LANG_ENUM.equals(superClass.getQualifiedName())) {
            isNotEnum = true;
            if (baseConstructor instanceof PsiCompiledElement) {
                // to get some parameter names
                PsiClass dummyClass = JVMElementFactories.requireFactory(baseConstructor.getLanguage(), project).createClass("Dummy");
                baseConstructor = (PsiMethod) dummyClass.add(baseConstructor);
            }
            PsiParameter[] params = baseConstructor.getParameterList().getParameters();
            for (PsiParameter param : params) {
                String name = param.getName();
                assert name != null : param;
                PsiParameter newParam = factory.createParameter(name, param.getType(), aClass);
                GenerateMembersUtil.copyOrReplaceModifierList(param, aClass, newParam);
                constructor.getParameterList().add(newParam);
            }
        }
    }
    JavaCodeStyleManager javaStyle = JavaCodeStyleManager.getInstance(project);
    final PsiMethod dummyConstructor = factory.createConstructor(className);
    dummyConstructor.getParameterList().replace(constructor.getParameterList().copy());
    List<PsiParameter> fieldParams = new ArrayList<>();
    for (PsiField field : fields) {
        String fieldName = field.getName();
        assert fieldName != null : field;
        String name = javaStyle.variableNameToPropertyName(fieldName, VariableKind.FIELD);
        String parmName = javaStyle.propertyNameToVariableName(name, VariableKind.PARAMETER);
        parmName = javaStyle.suggestUniqueVariableName(parmName, dummyConstructor, true);
        PsiParameter parm = factory.createParameter(parmName, field.getType(), aClass);
        NullableNotNullManager.getInstance(project).copyNotNullAnnotation(field, parm);
        if (constructor.isVarArgs()) {
            final PsiParameterList parameterList = constructor.getParameterList();
            parameterList.addBefore(parm, parameterList.getParameters()[parameterList.getParametersCount() - 1]);
            final PsiParameterList dummyParameterList = dummyConstructor.getParameterList();
            dummyParameterList.addBefore(parm.copy(), dummyParameterList.getParameters()[dummyParameterList.getParametersCount() - 1]);
        } else {
            constructor.getParameterList().add(parm);
            dummyConstructor.getParameterList().add(parm.copy());
        }
        fieldParams.add(parm);
    }
    ConstructorBodyGenerator generator = ConstructorBodyGenerator.INSTANCE.forLanguage(aClass.getLanguage());
    if (generator != null) {
        StringBuilder buffer = new StringBuilder();
        generator.start(buffer, constructor.getName(), PsiParameter.EMPTY_ARRAY);
        if (isNotEnum) {
            generator.generateSuperCallIfNeeded(buffer, baseConstructor.getParameterList().getParameters());
        }
        final PsiParameter[] parameters = fieldParams.toArray(new PsiParameter[fieldParams.size()]);
        final List<String> existingNames = ContainerUtil.map(dummyConstructor.getParameterList().getParameters(), parameter -> parameter.getName());
        if (generator instanceof ConstructorBodyGeneratorEx) {
            ((ConstructorBodyGeneratorEx) generator).generateFieldInitialization(buffer, fields, parameters, existingNames);
        } else {
            generator.generateFieldInitialization(buffer, fields, parameters);
        }
        generator.finish(buffer);
        PsiMethod stub = factory.createMethodFromText(buffer.toString(), aClass);
        PsiCodeBlock original = constructor.getBody(), replacement = stub.getBody();
        assert original != null : constructor;
        assert replacement != null : stub;
        original.replace(replacement);
    }
    constructor = (PsiMethod) codeStyleManager.reformat(constructor);
    return constructor;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

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