oldNameString = args[1];
newNameString = args[2];
if(category == Category.MODULE_NAME) {
ModuleName oldNameAsModuleName = ModuleName.maybeMake(oldNameString);
if(oldNameAsModuleName == null) {
iceLogger.log(Level.INFO, "Error: " + oldNameString + " is not a valid module name");
return;
}
oldName = QualifiedName.make(oldNameAsModuleName, Refactorer.Rename.UNQUALIFIED_NAME_FOR_MODULE_RENAMING);
} else if(QualifiedName.isValidCompoundName(oldNameString)) {
oldName = QualifiedName.makeFromCompoundName(oldNameString);
} else {
iceLogger.log(Level.INFO, "Error: " + oldNameString + " is not a valid qualified name");
return;
}
} else {
oldNameString = args[0];
newNameString = args[1];
ModuleName oldNameAsModuleName = ModuleName.maybeMake(oldNameString);
// the null check for oldNameAsModuleName comes a bit later (a few lines down)
QualifiedName oldNameAsQualifiedName;
if (QualifiedName.isValidCompoundName(oldNameString)) {
oldNameAsQualifiedName = QualifiedName.makeFromCompoundName(oldNameString);
} else {
oldNameAsQualifiedName = null;
}
List<Category> entityCategories = new ArrayList<Category>();
if (oldNameAsModuleName != null && getWorkspaceManager().getModuleTypeInfo(oldNameAsModuleName) != null) {
entityCategories.add(Category.MODULE_NAME);
}
if (oldNameAsQualifiedName != null) {
ModuleName moduleName = oldNameAsQualifiedName.getModuleName();
String unqualifiedName = oldNameAsQualifiedName.getUnqualifiedName();
ModuleTypeInfo moduleTypeInfo = getWorkspaceManager().getModuleTypeInfo(moduleName);
if(moduleTypeInfo != null) {
if(moduleTypeInfo.getFunctionOrClassMethod(unqualifiedName) != null) {
entityCategories.add(Category.TOP_LEVEL_FUNCTION_OR_CLASS_METHOD);
}
if(moduleTypeInfo.getDataConstructor(unqualifiedName) != null) {
entityCategories.add(Category.DATA_CONSTRUCTOR);
}
if(moduleTypeInfo.getTypeConstructor(unqualifiedName) != null) {
entityCategories.add(Category.TYPE_CONSTRUCTOR);
}
if(moduleTypeInfo.getTypeClass(unqualifiedName) != null) {
entityCategories.add(Category.TYPE_CLASS);
}
}
}
if(entityCategories.size() == 0) {
iceLogger.log(Level.INFO, "Error: " + oldNameString + " does not exist");
return;
}
if(entityCategories.size() > 1) {
iceLogger.log(Level.INFO, "Error: " + oldNameString + " is ambiguous; please specify a context");
return;
}
category = entityCategories.get(0);
if (category == Category.MODULE_NAME) {
oldName = QualifiedName.make(oldNameAsModuleName, Refactorer.Rename.UNQUALIFIED_NAME_FOR_MODULE_RENAMING);
} else {
oldName = oldNameAsQualifiedName;
}
}
if (category == Category.MODULE_NAME) {
ModuleName newNameAsModuleName = ModuleName.make(newNameString);
newName = QualifiedName.make(newNameAsModuleName, Refactorer.Rename.UNQUALIFIED_NAME_FOR_MODULE_RENAMING);
} else if(QualifiedName.isValidCompoundName(newNameString)) {
newName = QualifiedName.makeFromCompoundName(newNameString);
} else {
newName = QualifiedName.make(oldName.getModuleName(), newNameString);
newNameString = newName.getQualifiedName();
}
// Error checks
if(!entityExists(oldName, category)) {
iceLogger.log(Level.INFO, "Error: " + oldNameString + " does not exist");
return;
}
if(entityExists(newName, category)) {
iceLogger.log(Level.INFO, "Error: " + newNameString + " already exists");
return;
}
if((Character.isUpperCase(oldName.getUnqualifiedName().charAt(0)) && !Character.isUpperCase(newName.getUnqualifiedName().charAt(0))) ||
(!Character.isUpperCase(oldName.getUnqualifiedName().charAt(0)) && Character.isUpperCase(newName.getUnqualifiedName().charAt(0)))) {
iceLogger.log(Level.INFO, "Error: Old and new names must be of the same type (ie, if one starts with an uppercase letter, so must the other)");
return;
}
if(category != Category.MODULE_NAME && !oldName.getModuleName().equals(newName.getModuleName())) {
iceLogger.log(Level.INFO, "Error: Renamed entities must remain in the same module");
return;
}
// Okay, the basics check out.
System.out.print("Renaming " + oldNameString + " to " + newNameString + "...");
CompilerMessageLogger messageLogger = new MessageLogger();
Refactorer refactorer = new Refactorer.Rename(getWorkspaceManager().getWorkspace().asModuleContainer(), getWorkspaceManager().getTypeChecker(), oldName, newName, category);
refactorer.setStatusListener(new Refactorer.StatusListener() {
public void willUseResource(ModuleSourceDefinition resource) {
System.out.print(".");
}
public void doneUsingResource(ModuleSourceDefinition resource) {
}
});
refactorer.calculateModifications(messageLogger);
if(messageLogger.getMaxSeverity().compareTo(CompilerMessage.Severity.ERROR) >= 0) {
System.out.println("");
dumpCompilerMessages(messageLogger);
return;
}
refactorer.apply(messageLogger);
System.out.println("");
if(messageLogger.getMaxSeverity().compareTo(CompilerMessage.Severity.ERROR) >= 0) {
dumpCompilerMessages(messageLogger);
return;
}
if(!compileWorkspace(false, true, false)) {
System.out.print("Compile failed, rolling back changes...");
refactorer.undo(messageLogger);
System.out.println("");
return;
}
if(category == Category.MODULE_NAME) {
ModuleName oldNameAsModuleName = ModuleName.maybeMake(oldNameString);
if(oldNameAsModuleName == null) {
iceLogger.log(Level.INFO, "Invalid module name '" + oldNameString + "'");
return;
}