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;
}
}
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);
}
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;
}
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);
}
}
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;
}
Aggregations