module grouper: support grouping for ModuleDescription

This is needed to show unloaded modules in a tree.
This commit is contained in:
nik
2017-06-02 20:44:00 +02:00
parent fd04ce89d7
commit 9de2969938
5 changed files with 36 additions and 5 deletions
@@ -37,4 +37,10 @@ interface ModuleDescription {
@ApiStatus.Experimental
interface UnloadedModuleDescription : ModuleDescription {
val contentRoots: List<VirtualFilePointer>
val groupPath: List<String>
}
@ApiStatus.Experimental
interface LoadedModuleDescription : ModuleDescription {
val module: Module
}
@@ -34,6 +34,11 @@ abstract class ModuleGrouper {
*/
abstract fun getGroupPath(module: Module): List<String>
/**
* Returns names of parent groups for a module
*/
abstract fun getGroupPath(description: ModuleDescription): List<String>
/**
* Returns name which should be used for a module when it's shown under its group
*/
@@ -48,6 +53,11 @@ abstract class ModuleGrouper {
*/
abstract fun getModuleAsGroupPath(module: Module): List<String>?
/**
* If [description] itself can be considered as a group, returns its groups. Otherwise returns null.
*/
abstract fun getModuleAsGroupPath(description: ModuleDescription): List<String>?
abstract fun getAllModules(): Array<Module>
companion object {
@@ -78,11 +88,15 @@ private class QualifiedNameGrouper(project: Project, model: ModifiableModuleMode
return getGroupPathByModuleName(getModuleName(module))
}
override fun getGroupPath(description: ModuleDescription) = getGroupPathByModuleName(description.name)
override fun getShortenedNameByFullModuleName(name: String) = StringUtil.getShortName(name)
override fun getGroupPathByModuleName(name: String) = name.split('.').dropLast(1)
override fun getModuleAsGroupPath(module: Module) = getModuleName(module).split('.')
override fun getModuleAsGroupPath(description: ModuleDescription) = description.name.split('.')
}
private class ExplicitModuleGrouper(project: Project, model: ModifiableModuleModel?): ModuleGrouperBase(project, model) {
@@ -91,9 +105,17 @@ private class ExplicitModuleGrouper(project: Project, model: ModifiableModuleMod
return if (path != null) Arrays.asList(*path) else emptyList()
}
override fun getGroupPath(description: ModuleDescription) = when (description) {
is LoadedModuleDescription -> getGroupPath(description.module)
is UnloadedModuleDescription -> description.groupPath
else -> throw IllegalArgumentException(description.javaClass.name)
}
override fun getShortenedNameByFullModuleName(name: String) = name
override fun getGroupPathByModuleName(name: String): List<String> = emptyList()
override fun getModuleAsGroupPath(module: Module) = null
override fun getModuleAsGroupPath(description: ModuleDescription) = null
}
@@ -15,14 +15,14 @@
*/
package com.intellij.openapi.module.impl
import com.intellij.openapi.module.LoadedModuleDescription
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleDescription
import com.intellij.openapi.roots.ModuleRootManager
/**
* @author nik
*/
class LoadedModuleDescription(private val module: Module): ModuleDescription {
class LoadedModuleDescriptionImpl(override val module: Module): LoadedModuleDescription {
override val name: String
get() = module.name
@@ -81,7 +81,7 @@ public abstract class ModuleManagerImpl extends ModuleManager implements Disposa
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.module.impl.ModuleManagerImpl");
private static final Key<String> DISPOSED_MODULE_NAME = Key.create("DisposedNeverAddedModuleName");
private static final String MODULE_GROUP_SEPARATOR = "/";
public static final String MODULE_GROUP_SEPARATOR = "/";
protected final Project myProject;
protected final MessageBus myMessageBus;
@@ -985,7 +985,7 @@ public abstract class ModuleManagerImpl extends ModuleManager implements Disposa
Module[] modules = getModules();
List<ModuleDescription> descriptions = new ArrayList<>(modules.length + myUnloadedModules.size());
for (Module module : modules) {
descriptions.add(new LoadedModuleDescription(module));
descriptions.add(new LoadedModuleDescriptionImpl(module));
}
descriptions.addAll(myUnloadedModules.values());
return descriptions;
@@ -1014,7 +1014,7 @@ public abstract class ModuleManagerImpl extends ModuleManager implements Disposa
else {
Module module = findModuleByName(name);
if (module != null) {
LoadedModuleDescription description = new LoadedModuleDescription(module);
LoadedModuleDescriptionImpl description = new LoadedModuleDescriptionImpl(module);
ModuleSaveItem saveItem = new ModuleSaveItem(module);
ModulePath modulePath = new ModulePath(saveItem.getModuleFilePath(), saveItem.getGroupPathString());
VirtualFilePointerManager pointerManager = VirtualFilePointerManager.getInstance();
@@ -32,6 +32,9 @@ import java.nio.file.Paths
class UnloadedModuleDescriptionImpl(val modulePath: ModulePath,
override val dependencyModuleNames: List<String>,
override val contentRoots: List<VirtualFilePointer>) : UnloadedModuleDescription {
override val groupPath: List<String>
get() = modulePath.group?.split(ModuleManagerImpl.MODULE_GROUP_SEPARATOR) ?: emptyList()
override val name: String
get() = modulePath.moduleName