cleanup — extract getModuleNameByFilePath

This commit is contained in:
Vladimir Krivosheev
2016-12-21 11:19:38 +01:00
parent 01dcbad42e
commit 3f9b61c709
6 changed files with 35 additions and 43 deletions
@@ -15,7 +15,6 @@
*/
package com.intellij.configurationStore
import com.intellij.ide.highlighter.ModuleFileType
import com.intellij.openapi.components.StateStorage
import com.intellij.openapi.components.StateStorageOperation
import com.intellij.openapi.components.StoragePathMacros
@@ -23,8 +22,8 @@ import com.intellij.openapi.components.TrackingPathMacroSubstitutor
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.impl.ModuleEx
import com.intellij.openapi.module.impl.ModuleManagerImpl
import com.intellij.openapi.module.impl.getModuleNameByFilePath
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
import com.intellij.util.PathUtilRt
import org.jdom.Element
class ModuleStateStorageManager(macroSubstitutor: TrackingPathMacroSubstitutor, module: Module) : StateStorageManagerImpl("module", macroSubstitutor, module) {
@@ -39,7 +38,7 @@ class ModuleStateStorageManager(macroSubstitutor: TrackingPathMacroSubstitutor,
if (requestor == null || requestor !is StateStorage /* not renamed as result of explicit rename */) {
val module = componentManager as ModuleEx
val oldName = module.name
module.rename(PathUtilRt.getFileName(newPath).removeSuffix(ModuleFileType.DOT_DEFAULT_EXTENSION), false)
module.rename(getModuleNameByFilePath(newPath), false)
ModuleManagerImpl.getInstanceImpl(module.project).fireModuleRenamedByVfsEvent(module, oldName)
}
}
@@ -33,11 +33,9 @@ import com.intellij.openapi.module.impl.scopes.ModuleScopeProviderImpl;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.SimpleModificationTracker;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.PathUtil;
import com.intellij.util.xmlb.annotations.MapAnnotation;
import com.intellij.util.xmlb.annotations.Property;
import gnu.trove.THashMap;
@@ -48,6 +46,8 @@ import org.picocontainer.MutablePicoContainer;
import java.util.List;
import java.util.Map;
import static com.intellij.openapi.module.impl.ModulePathKt.getModuleNameByFilePath;
/**
* @author max
*/
@@ -62,14 +62,14 @@ public class ModuleImpl extends PlatformComponentManagerImpl implements ModuleEx
private final ModuleScopeProvider myModuleScopeProvider;
public ModuleImpl(@NotNull String filePath, @NotNull Project project) {
super(project, "Module " + moduleNameByFileName(PathUtil.getFileName(filePath)));
super(project, "Module " + getModuleNameByFilePath(filePath));
getPicoContainer().registerComponentInstance(Module.class, this);
myProject = project;
myModuleScopeProvider = new ModuleScopeProviderImpl(this);
myName = moduleNameByFileName(PathUtil.getFileName(filePath));
myName = getModuleNameByFilePath(filePath);
}
@Override
@@ -310,10 +310,6 @@ public class ModuleImpl extends PlatformComponentManagerImpl implements ModuleEx
return "Module: '" + getName() + "'";
}
private static String moduleNameByFileName(@NotNull String fileName) {
return StringUtil.trimEnd(fileName, ModuleFileType.DOT_DEFAULT_EXTENSION);
}
@NotNull
@Override
public <T> T[] getExtensions(@NotNull final ExtensionPointName<T> extensionPointName) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,24 +19,24 @@ package com.intellij.openapi.module.impl;
import com.intellij.openapi.module.ConfigurationErrorDescription;
import com.intellij.openapi.module.ConfigurationErrorType;
import com.intellij.openapi.project.ProjectBundle;
import java.io.File;
import org.jetbrains.annotations.NotNull;
/**
* @author nik
*/
public class ModuleLoadingErrorDescription extends ConfigurationErrorDescription {
class ModuleLoadingErrorDescription extends ConfigurationErrorDescription {
private static final ConfigurationErrorType INVALID_MODULE = new ConfigurationErrorType(ProjectBundle.message("element.kind.name.module"), false);
private final ModulePath myModulePath;
private final ModuleManagerImpl myModuleManager;
private ModuleLoadingErrorDescription(final String description, final ModulePath modulePath, ModuleManagerImpl moduleManager,
final String elementName) {
super(elementName, description, INVALID_MODULE);
ModuleLoadingErrorDescription(String description, @NotNull ModulePath modulePath, @NotNull ModuleManagerImpl moduleManager) {
super(modulePath.getModuleName(), description, INVALID_MODULE);
myModulePath = modulePath;
myModuleManager = moduleManager;
}
@NotNull
public ModulePath getModulePath() {
return myModulePath;
}
@@ -50,16 +50,4 @@ public class ModuleLoadingErrorDescription extends ConfigurationErrorDescription
public String getIgnoreConfirmationMessage() {
return ProjectBundle.message("module.remove.from.project.confirmation", getElementName());
}
public static ModuleLoadingErrorDescription create(final String description, final ModulePath modulePath,
ModuleManagerImpl moduleManager) {
String path = modulePath.getPath();
int start = path.lastIndexOf(File.separatorChar)+1;
int finish = path.lastIndexOf('.');
if (finish == -1 || finish <= start) {
finish = path.length();
}
final String moduleName = path.substring(start, finish);
return new ModuleLoadingErrorDescription(description, modulePath, moduleManager, moduleName);
}
}
@@ -202,6 +202,7 @@ public abstract class ModuleManagerImpl extends ModuleManager implements Disposa
}
@NotNull
// returns mutable linked hash set
public static LinkedHashSet<ModulePath> getPathsToModuleFiles(@NotNull Element element) {
final LinkedHashSet<ModulePath> paths = new LinkedHashSet<>();
final Element modules = element.getChild(ELEMENT_MODULES);
@@ -209,15 +210,14 @@ public abstract class ModuleManagerImpl extends ModuleManager implements Disposa
for (final Element moduleElement : modules.getChildren(ELEMENT_MODULE)) {
final String fileUrlValue = moduleElement.getAttributeValue(ATTRIBUTE_FILEURL);
final String filepath;
if (fileUrlValue != null) {
filepath = VirtualFileManager.extractPath(fileUrlValue).replace('/', File.separatorChar);
if (fileUrlValue == null) {
// support for older formats
filepath = moduleElement.getAttributeValue(ATTRIBUTE_FILEPATH);
}
else {
// [dsl] support for older formats
filepath = moduleElement.getAttributeValue(ATTRIBUTE_FILEPATH).replace('/', File.separatorChar);
filepath = VirtualFileManager.extractPath(fileUrlValue);
}
final String group = moduleElement.getAttributeValue(ATTRIBUTE_GROUP);
paths.add(new ModulePath(filepath, group));
paths.add(new ModulePath(filepath.replace('/', File.separatorChar), moduleElement.getAttributeValue(ATTRIBUTE_GROUP)));
}
}
return paths;
@@ -243,7 +243,7 @@ public abstract class ModuleManagerImpl extends ModuleManager implements Disposa
ExecutorService service = AppExecutorUtil.createBoundedApplicationPoolExecutor("modules loader", JobSchedulerImpl.CORES_COUNT);
List<Pair<Future<Module>, ModulePath>> tasks = new ArrayList<>();
Set<String> paths = new java.util.HashSet<>();
Set<String> paths = new HashSet<>();
boolean parallel = Registry.is("parallel.modules.loading");
for (ModulePath modulePath : myModulePathsToLoad) {
if (progressIndicator.isCanceled()) {
@@ -316,8 +316,7 @@ public abstract class ModuleManagerImpl extends ModuleManager implements Disposa
}
private void reportError(List<ModuleLoadingErrorDescription> errors, ModulePath modulePath, Exception e) {
errors.add(ModuleLoadingErrorDescription
.create(ProjectBundle.message("module.cannot.load.error", modulePath.getPath(), e.getMessage()), modulePath, this));
errors.add(new ModuleLoadingErrorDescription(ProjectBundle.message("module.cannot.load.error", modulePath.getPath(), e.getMessage()), modulePath, this));
}
public int getModulePathsCount() { return myModulePathsToLoad == null ? 0 : myModulePathsToLoad.size(); }
@@ -15,4 +15,16 @@
*/
package com.intellij.openapi.module.impl
data class ModulePath(val path: String, val group: String?)
import com.intellij.ide.highlighter.ModuleFileType
import com.intellij.util.PathUtil
data class ModulePath(val path: String, val group: String?) {
/**
* Module name (without file extension)
*/
val moduleName: String = getModuleNameByFilePath(path)
}
fun getModuleNameByFilePath(path: String): String {
return PathUtil.getFileName(path).removeSuffix(ModuleFileType.DOT_DEFAULT_EXTENSION)
}
@@ -29,11 +29,9 @@ import com.intellij.openapi.module.impl.ModuleManagerImpl;
import com.intellij.openapi.module.impl.ModulePath;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.testFramework.Parameterized;
import com.intellij.testFramework.TestRunnerUtil;
import com.intellij.util.PathUtil;
import com.intellij.util.containers.ContainerUtil;
import gnu.trove.THashSet;
import junit.framework.TestCase;
@@ -410,7 +408,7 @@ public class PathManagerEx {
Element element = JDomSerializationUtil.findComponent(JDOMUtil.load(modulesXml), ModuleManagerImpl.COMPONENT_NAME);
assert element != null;
for (ModulePath file : ModuleManagerImpl.getPathsToModuleFiles(element)) {
ourCommunityModules.add(FileUtil.getNameWithoutExtension(PathUtil.getFileName(file.getPath())));
ourCommunityModules.add(file.getModuleName());
}
return ourCommunityModules;
}