Search in sources :

Example 6 with JavaCodeStyleManager

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

the class GrPullUpHelper method replaceMovedMemberTypeParameters.

public static void replaceMovedMemberTypeParameters(final PsiElement member, final Iterable<PsiTypeParameter> parametersIterable, final PsiSubstitutor substitutor, final GroovyPsiElementFactory factory) {
    final Map<PsiElement, PsiElement> replacement = new LinkedHashMap<>();
    for (PsiTypeParameter parameter : parametersIterable) {
        PsiType substitutedType = substitutor.substitute(parameter);
        PsiType type = substitutedType != null ? substitutedType : TypeConversionUtil.erasure(factory.createType(parameter));
        PsiElement scopeElement = member instanceof GrField ? member.getParent() : member;
        for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(scopeElement))) {
            final PsiElement element = reference.getElement();
            final PsiElement parent = element.getParent();
            if (parent instanceof PsiTypeElement) {
                replacement.put(parent, factory.createTypeElement(type));
            } else if (element instanceof GrCodeReferenceElement && type instanceof PsiClassType) {
                replacement.put(element, factory.createReferenceElementByType((PsiClassType) type));
            }
        }
    }
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(member.getProject());
    for (PsiElement element : replacement.keySet()) {
        if (element.isValid()) {
            final PsiElement replaced = element.replace(replacement.get(element));
            codeStyleManager.shortenClassReferences(replaced);
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 7 with JavaCodeStyleManager

use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project GsonFormat by zzz40500.

the class Processor method formatJavCode.

protected void formatJavCode(PsiClass cls) {
    if (cls == null) {
        return;
    }
    JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(cls.getProject());
    styleManager.optimizeImports(cls.getContainingFile());
    styleManager.shortenClassReferences(cls);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 8 with JavaCodeStyleManager

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

the class IntroduceVariableBase method getSuggestedName.

public static SuggestedNameInfo getSuggestedName(@Nullable PsiType type, @NotNull final PsiExpression expression, final PsiElement anchor) {
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(expression.getProject());
    final SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, expression, type);
    final String[] strings = JavaCompletionUtil.completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, nameInfo);
    final SuggestedNameInfo.Delegate delegate = new SuggestedNameInfo.Delegate(strings, nameInfo);
    return codeStyleManager.suggestUniqueVariableName(delegate, anchor, true);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Example 9 with JavaCodeStyleManager

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

the class MakeClassStaticProcessor method convertToFieldName.

private String convertToFieldName(final String parameterName) {
    JavaCodeStyleManager manager = JavaCodeStyleManager.getInstance(myProject);
    final String propertyName = manager.variableNameToPropertyName(parameterName, VariableKind.PARAMETER);
    final String fieldName = manager.propertyNameToVariableName(propertyName, VariableKind.FIELD);
    return fieldName;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Example 10 with JavaCodeStyleManager

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

the class AbstractJavaInplaceIntroducer method suggestNames.

@Override
protected String[] suggestNames(boolean replaceAll, PsiVariable variable) {
    final PsiType defaultType = getType();
    final String propertyName = variable != null ? JavaCodeStyleManager.getInstance(myProject).variableNameToPropertyName(variable.getName(), VariableKind.LOCAL_VARIABLE) : null;
    final String[] names = suggestNames(defaultType, propertyName);
    if (propertyName != null && names.length > 1) {
        final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(myProject);
        final String paramName = javaCodeStyleManager.propertyNameToVariableName(propertyName, getVariableKind());
        final int idx = ArrayUtil.find(names, paramName);
        if (idx > -1) {
            ArrayUtil.swap(names, 0, idx);
        }
    }
    return names;
}
Also used : 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