Search in sources :

Example 6 with UniqueNameGenerator

use of com.intellij.util.text.UniqueNameGenerator in project intellij-community by JetBrains.

the class PsiDiamondTypeImpl method generateStaticFactory.

@Nullable
private static PsiMethod generateStaticFactory(@Nullable PsiMethod constructor, PsiClass containingClass, PsiTypeParameter[] params, PsiJavaCodeReferenceElement reference) {
    final StringBuilder buf = new StringBuilder();
    final String modifier = VisibilityUtil.getVisibilityModifier(constructor != null ? constructor.getModifierList() : containingClass.getModifierList());
    if (!PsiModifier.PACKAGE_LOCAL.equals(modifier)) {
        buf.append(modifier);
        buf.append(" ");
    }
    buf.append("static ");
    buf.append("<");
    //it's possible that constructor type parameters and class type parameters are same named:
    //it's important that class type parameters names are preserved(they are first in the list),
    //though constructor parameters would be renamed in case of conflicts
    final UniqueNameGenerator generator = new UniqueNameGenerator();
    buf.append(StringUtil.join(params, psiTypeParameter -> {
        String extendsList = "";
        if (psiTypeParameter.getLanguage().isKindOf(JavaLanguage.INSTANCE)) {
            final PsiClassType[] extendsListTypes = psiTypeParameter.getExtendsListTypes();
            if (extendsListTypes.length > 0) {
                final Function<PsiClassType, String> canonicalTypePresentationFun = type -> type.getCanonicalText();
                extendsList = " extends " + StringUtil.join(extendsListTypes, canonicalTypePresentationFun, "&");
            }
        }
        return generator.generateUniqueName(psiTypeParameter.getName()) + extendsList;
    }, ", "));
    buf.append(">");
    final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(containingClass.getProject());
    String qualifiedName = containingClass.getQualifiedName();
    PsiElement qualifier = reference != null ? reference.getQualifier() : null;
    if (qualifier instanceof PsiJavaCodeReferenceElement) {
        final JavaResolveResult resolveResult = ((PsiJavaCodeReferenceElement) qualifier).advancedResolve(false);
        final PsiElement element = resolveResult.getElement();
        if (element instanceof PsiClass) {
            final String outerClassSubstitutedQName = elementFactory.createType((PsiClass) element, resolveResult.getSubstitutor()).getInternalCanonicalText();
            qualifiedName = outerClassSubstitutedQName + "." + containingClass.getName();
        }
    } else if (reference != null && qualifier == null && containingClass.getContainingClass() != null) {
        qualifiedName = null;
    }
    buf.append(qualifiedName != null ? qualifiedName : containingClass.getName());
    final PsiTypeParameter[] parameters = containingClass.getTypeParameters();
    buf.append("<");
    buf.append(StringUtil.join(parameters, psiTypeParameter -> psiTypeParameter.getName(), ", "));
    buf.append("> ");
    String staticFactoryName = "staticFactory";
    final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(containingClass.getProject());
    staticFactoryName = styleManager.suggestUniqueVariableName(staticFactoryName, containingClass, false);
    buf.append(staticFactoryName);
    if (constructor == null) {
        buf.append("()");
    } else {
        buf.append("(").append(StringUtil.join(constructor.getParameterList().getParameters(), new Function<PsiParameter, String>() {

            int myIdx;

            @Override
            public String fun(PsiParameter psiParameter) {
                return psiParameter.getType().getCanonicalText() + " p" + myIdx++;
            }
        }, ",")).append(")");
    }
    buf.append("{}");
    try {
        return elementFactory.createMethodFromText(buf.toString(), constructor != null ? constructor : containingClass);
    } catch (IncorrectOperationException e) {
        return null;
    }
}
Also used : java.util(java.util) IncorrectOperationException(com.intellij.util.IncorrectOperationException) StringUtil(com.intellij.openapi.util.text.StringUtil) CandidateInfo(com.intellij.psi.infos.CandidateInfo) com.intellij.psi.util(com.intellij.psi.util) PsiConflictResolver(com.intellij.psi.scope.PsiConflictResolver) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) VisibilityUtil(com.intellij.util.VisibilityUtil) NonNls(org.jetbrains.annotations.NonNls) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) MethodCandidatesProcessor(com.intellij.psi.scope.processor.MethodCandidatesProcessor) UniqueNameGenerator(com.intellij.util.text.UniqueNameGenerator) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) JavaMethodsConflictResolver(com.intellij.psi.scope.conflictResolvers.JavaMethodsConflictResolver) Nullable(org.jetbrains.annotations.Nullable) Comparing(com.intellij.openapi.util.Comparing) InferenceSession(com.intellij.psi.impl.source.resolve.graphInference.InferenceSession) Function(com.intellij.util.Function) Project(com.intellij.openapi.project.Project) Logger(com.intellij.openapi.diagnostic.Logger) NotNull(org.jetbrains.annotations.NotNull) JavaLanguage(com.intellij.lang.java.JavaLanguage) UniqueNameGenerator(com.intellij.util.text.UniqueNameGenerator) Function(com.intellij.util.Function) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with UniqueNameGenerator

use of com.intellij.util.text.UniqueNameGenerator in project intellij-community by JetBrains.

the class FluentIterableConversionUtil method chooseName.

public static String chooseName(@NotNull PsiExpression context, @Nullable PsiType type) {
    final UniqueNameGenerator nameGenerator = new UniqueNameGenerator();
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(context.getProject());
    final String name = codeStyleManager.suggestUniqueVariableName(codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, type).names[0], context, false);
    return nameGenerator.generateUniqueName(name);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) UniqueNameGenerator(com.intellij.util.text.UniqueNameGenerator)

Example 8 with UniqueNameGenerator

use of com.intellij.util.text.UniqueNameGenerator in project intellij-community by JetBrains.

the class InputVariables method wrapInputVariables.

public ArrayList<VariableData> wrapInputVariables(final List<? extends PsiVariable> inputVariables) {
    UniqueNameGenerator nameGenerator = new UniqueNameGenerator();
    final ArrayList<VariableData> inputData = new ArrayList<>(inputVariables.size());
    for (PsiVariable var : inputVariables) {
        final String defaultName = getParameterName(var);
        String name = nameGenerator.generateUniqueName(defaultName);
        PsiType type = GenericsUtil.getVariableTypeByExpressionType(var.getType());
        if (type instanceof PsiEllipsisType) {
            type = ((PsiEllipsisType) type).toArrayType();
        }
        final Map<PsiCodeBlock, PsiType> casts = new HashMap<>();
        for (PsiReference reference : ReferencesSearch.search(var, myScope)) {
            final PsiElement element = reference.getElement();
            final PsiElement parent = element.getParent();
            final PsiCodeBlock block = PsiTreeUtil.getParentOfType(parent, PsiCodeBlock.class);
            if (parent instanceof PsiTypeCastExpression) {
                final PsiType currentType = casts.get(block);
                final PsiType castType = ((PsiTypeCastExpression) parent).getType();
                casts.put(block, casts.containsKey(block) && currentType == null ? null : getBroaderType(currentType, castType));
            } else {
                casts.put(block, null);
            }
        }
        if (!casts.containsValue(null)) {
            PsiType currentType = null;
            for (PsiType psiType : casts.values()) {
                currentType = getBroaderType(currentType, psiType);
                if (currentType == null) {
                    break;
                }
            }
            if (currentType != null) {
                currentType = checkTopLevelInstanceOf(currentType);
                if (currentType != null) {
                    type = currentType;
                }
            }
        }
        VariableData data = new VariableData(var, type);
        data.name = name;
        data.passAsParameter = true;
        inputData.add(data);
        if (myFoldingAvailable)
            myFolding.isParameterFoldable(data, myScope, inputVariables, nameGenerator, defaultName);
    }
    if (myFoldingAvailable) {
        final Set<VariableData> toDelete = new HashSet<>();
        for (int i = inputData.size() - 1; i >= 0; i--) {
            final VariableData data = inputData.get(i);
            if (myFolding.isParameterSafeToDelete(data, myScope)) {
                toDelete.add(data);
            }
        }
        inputData.removeAll(toDelete);
    }
    if (myPassFields && myUsedInstanceFields != null) {
        for (PsiField var : myUsedInstanceFields) {
            final VariableData data = new VariableData(var, var.getType());
            data.name = nameGenerator.generateUniqueName(getParameterName(var));
            data.passAsParameter = true;
            inputData.add(data);
        }
    }
    return inputData;
}
Also used : VariableData(com.intellij.refactoring.util.VariableData) UniqueNameGenerator(com.intellij.util.text.UniqueNameGenerator)

Example 9 with UniqueNameGenerator

use of com.intellij.util.text.UniqueNameGenerator in project intellij-community by JetBrains.

the class JpsModuleRootModelSerializer method loadRootModel.

public static void loadRootModel(JpsModule module, @Nullable Element rootModelComponent, @Nullable JpsSdkType<?> projectSdkType) {
    if (rootModelComponent == null)
        return;
    for (Element contentElement : getChildren(rootModelComponent, CONTENT_TAG)) {
        final String url = contentElement.getAttributeValue(URL_ATTRIBUTE);
        module.getContentRootsList().addUrl(url);
        for (Element sourceElement : getChildren(contentElement, SOURCE_FOLDER_TAG)) {
            module.addSourceRoot(loadSourceRoot(sourceElement));
        }
        for (Element excludeElement : getChildren(contentElement, EXCLUDE_FOLDER_TAG)) {
            module.getExcludeRootsList().addUrl(excludeElement.getAttributeValue(URL_ATTRIBUTE));
        }
    }
    final JpsDependenciesList dependenciesList = module.getDependenciesList();
    dependenciesList.clear();
    final JpsElementFactory elementFactory = JpsElementFactory.getInstance();
    UniqueNameGenerator nameGenerator = new UniqueNameGenerator();
    boolean moduleSourceAdded = false;
    for (Element orderEntry : getChildren(rootModelComponent, ORDER_ENTRY_TAG)) {
        String type = orderEntry.getAttributeValue(TYPE_ATTRIBUTE);
        if (SOURCE_FOLDER_TYPE.equals(type)) {
            dependenciesList.addModuleSourceDependency();
            moduleSourceAdded = true;
        } else if (JDK_TYPE.equals(type)) {
            String sdkName = orderEntry.getAttributeValue(JDK_NAME_ATTRIBUTE);
            String sdkTypeId = orderEntry.getAttributeValue(JDK_TYPE_ATTRIBUTE);
            final JpsSdkType<?> sdkType = JpsSdkTableSerializer.getSdkType(sdkTypeId);
            dependenciesList.addSdkDependency(sdkType);
            JpsSdkTableSerializer.setSdkReference(module.getSdkReferencesTable(), sdkName, sdkType);
            if (sdkType instanceof JpsJavaSdkTypeWrapper) {
                dependenciesList.addSdkDependency(JpsJavaSdkType.INSTANCE);
            }
        } else if (INHERITED_JDK_TYPE.equals(type)) {
            final JpsSdkType<?> sdkType = projectSdkType != null ? projectSdkType : JpsJavaSdkType.INSTANCE;
            dependenciesList.addSdkDependency(sdkType);
            if (sdkType instanceof JpsJavaSdkTypeWrapper) {
                dependenciesList.addSdkDependency(JpsJavaSdkType.INSTANCE);
            }
        } else if (LIBRARY_TYPE.equals(type)) {
            String name = orderEntry.getAttributeValue(NAME_ATTRIBUTE);
            String level = orderEntry.getAttributeValue(LEVEL_ATTRIBUTE);
            final JpsLibraryDependency dependency = dependenciesList.addLibraryDependency(elementFactory.createLibraryReference(name, JpsLibraryTableSerializer.createLibraryTableReference(level)));
            loadModuleDependencyProperties(dependency, orderEntry);
        } else if (MODULE_LIBRARY_TYPE.equals(type)) {
            final Element moduleLibraryElement = orderEntry.getChild(LIBRARY_TAG);
            String name = moduleLibraryElement.getAttributeValue(NAME_ATTRIBUTE);
            if (name == null) {
                name = GENERATED_LIBRARY_NAME_PREFIX;
            }
            String uniqueName = nameGenerator.generateUniqueName(name);
            final JpsLibrary library = JpsLibraryTableSerializer.loadLibrary(moduleLibraryElement, uniqueName);
            module.addModuleLibrary(library);
            final JpsLibraryDependency dependency = dependenciesList.addLibraryDependency(library);
            loadModuleDependencyProperties(dependency, orderEntry);
        } else if (MODULE_TYPE.equals(type)) {
            String name = orderEntry.getAttributeValue(MODULE_NAME_ATTRIBUTE);
            final JpsModuleDependency dependency = dependenciesList.addModuleDependency(elementFactory.createModuleReference(name));
            loadModuleDependencyProperties(dependency, orderEntry);
        }
    }
    if (!moduleSourceAdded) {
        dependenciesList.addModuleSourceDependency();
    }
    for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
        extension.loadRootModel(module, rootModelComponent);
    }
}
Also used : JpsJavaSdkTypeWrapper(org.jetbrains.jps.model.java.JpsJavaSdkTypeWrapper) JpsElement(org.jetbrains.jps.model.JpsElement) JpsCompositeElement(org.jetbrains.jps.model.JpsCompositeElement) Element(org.jdom.Element) UniqueNameGenerator(com.intellij.util.text.UniqueNameGenerator) JpsSdkType(org.jetbrains.jps.model.library.sdk.JpsSdkType) JpsElementFactory(org.jetbrains.jps.model.JpsElementFactory) JpsModelSerializerExtension(org.jetbrains.jps.model.serialization.JpsModelSerializerExtension) JpsLibrary(org.jetbrains.jps.model.library.JpsLibrary)

Example 10 with UniqueNameGenerator

use of com.intellij.util.text.UniqueNameGenerator in project intellij-community by JetBrains.

the class StateSplitterEx method splitState.

@NotNull
protected static List<Pair<Element, String>> splitState(@NotNull Element state, @NotNull String attributeName) {
    UniqueNameGenerator generator = new UniqueNameGenerator();
    List<Pair<Element, String>> result = new SmartList<>();
    for (Element subState : state.getChildren()) {
        result.add(createItem(generator, subState, attributeName));
    }
    return result;
}
Also used : Element(org.jdom.Element) SmartList(com.intellij.util.SmartList) UniqueNameGenerator(com.intellij.util.text.UniqueNameGenerator) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

UniqueNameGenerator (com.intellij.util.text.UniqueNameGenerator)10 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)4 Logger (com.intellij.openapi.diagnostic.Logger)3 NotNull (org.jetbrains.annotations.NotNull)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Project (com.intellij.openapi.project.Project)2 Pair (com.intellij.openapi.util.Pair)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 com.intellij.psi (com.intellij.psi)2 SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)2 VariableKind (com.intellij.psi.codeStyle.VariableKind)2 MethodCandidateInfo (com.intellij.psi.infos.MethodCandidateInfo)2 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)2 PsiUtil (com.intellij.psi.util.PsiUtil)2 VariableData (com.intellij.refactoring.util.VariableData)2 Function (com.intellij.util.Function)2 SmartList (com.intellij.util.SmartList)2 Element (org.jdom.Element)2 Nullable (org.jetbrains.annotations.Nullable)2 JavaPsiEquivalenceUtil (com.intellij.codeInsight.JavaPsiEquivalenceUtil)1