diff --git a/java/idea-ui/src/com/intellij/ide/util/importProject/ModuleInsight.java b/java/idea-ui/src/com/intellij/ide/util/importProject/ModuleInsight.java index bd442e91114c..14e31698ac51 100644 --- a/java/idea-ui/src/com/intellij/ide/util/importProject/ModuleInsight.java +++ b/java/idea-ui/src/com/intellij/ide/util/importProject/ModuleInsight.java @@ -1,7 +1,6 @@ // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.ide.util.importProject; -import com.google.common.collect.ImmutableSet; import com.intellij.ide.util.projectWizard.importSources.DetectedProjectRoot; import com.intellij.ide.util.projectWizard.importSources.DetectedSourceRoot; import com.intellij.ide.util.projectWizard.importSources.impl.ProjectFromSourcesBuilderImpl; @@ -45,7 +44,6 @@ public abstract class ModuleInsight { private List myLibraries; private final Set myExistingModuleNames; private final Set myExistingProjectLibraryNames; - private static final Set ourModuleUndesirableNames = ImmutableSet.of("src"); public ModuleInsight(@Nullable final ProgressIndicator progress, Set existingModuleNames, Set existingProjectLibraryNames) { myExistingModuleNames = existingModuleNames; @@ -113,7 +111,7 @@ public abstract class ModuleInsight { Map rootToModule = new HashMap<>(); for (DetectedSourceRoot sourceRoot : processedRoots) { final File srcRoot = sourceRoot.getDirectory(); - final File moduleContentRoot = suggestModuleRoot(srcRoot); + final File moduleContentRoot = isEntryPointRoot(srcRoot) ? srcRoot : srcRoot.getParentFile(); rootToModule.computeIfAbsent(moduleContentRoot, file -> new ModuleCandidate(moduleContentRoot)).myRoots.add(sourceRoot); } maximizeModuleFolders(rootToModule.values()); @@ -150,41 +148,26 @@ public abstract class ModuleInsight { if (!dirToChildRootCount.adjustValue(file, 1)) { dirToChildRootCount.put(file, 1); } - }, true); + }); } for (ModuleCandidate module : modules) { File moduleRoot = module.myFolder; Ref adjustedRootRef = new Ref<>(module.myFolder); - walkParents(moduleRoot, - file -> isEntryPointRoot(file) || dirToChildRootCount.get(file) != 1, - file -> adjustedRootRef.set(file), - false - ); + File current = moduleRoot; + while (dirToChildRootCount.get(current) == 1) { + adjustedRootRef.set(current); + if (isEntryPointRoot(current)) break; + current = current.getParentFile(); + } module.myFolder = adjustedRootRef.get(); } } - private static void walkParents(@NotNull File file, Predicate stopCondition, @NotNull Consumer fileConsumer, boolean includeStop) { + private static void walkParents(@NotNull File file, Predicate stopCondition, @NotNull Consumer fileConsumer) { File current = file; while (true) { - if (!includeStop) { - if (stopCondition.test(current)) break; - fileConsumer.consume(current); - } else { - fileConsumer.consume(current); - if (stopCondition.test(current)) break; - } - current = current.getParentFile(); - } - } - - @NotNull - private File suggestModuleRoot(@NotNull File srcRoot) { - File current = isEntryPointRoot(srcRoot) ? srcRoot : srcRoot.getParentFile(); - while (true) { - if (isEntryPointRoot(current) || !ourModuleUndesirableNames.contains(current.getName())) { - return current; - } + fileConsumer.consume(current); + if (stopCondition.test(current)) break; current = current.getParentFile(); } } diff --git a/java/idea-ui/testSrc/com/intellij/ide/projectWizard/ImportActionTest.java b/java/idea-ui/testSrc/com/intellij/ide/projectWizard/ImportActionTest.java index a6a723a7f8ca..a034533509cd 100644 --- a/java/idea-ui/testSrc/com/intellij/ide/projectWizard/ImportActionTest.java +++ b/java/idea-ui/testSrc/com/intellij/ide/projectWizard/ImportActionTest.java @@ -68,6 +68,15 @@ public class ImportActionTest extends ProjectWizardTestCase { assertEquals(path + "/inner/m2", contentRoots.get(1).getPath()); } + public void testSingleModuleInProject() { + String path = getModuleMaximizationPath("single"); + Project project = importProjectFrom(path, null, new ImportFromSourcesProvider()).getProject(); + Module[] modules = ModuleManager.getInstance(project).getModules(); + assertEquals(1, modules.length); + List contentRoots = getSingleContentRoots(modules); + assertEquals(path, contentRoots.get(0).getPath()); + } + private static String getModuleMaximizationPath(String projectName) { String basePath = "/ide/importAction/moduleMaximization"; return PathManagerEx.getTestDataPath(basePath + "/" + projectName); diff --git a/java/java-tests/testData/ide/importAction/moduleMaximization/single/m1/src/main/f1.java b/java/java-tests/testData/ide/importAction/moduleMaximization/single/m1/src/main/f1.java new file mode 100644 index 000000000000..f72abf33fa2a --- /dev/null +++ b/java/java-tests/testData/ide/importAction/moduleMaximization/single/m1/src/main/f1.java @@ -0,0 +1 @@ +class Foo {} \ No newline at end of file diff --git a/java/java-tests/testData/ide/importAction/moduleMaximization/single/m1/src/test/f2.java b/java/java-tests/testData/ide/importAction/moduleMaximization/single/m1/src/test/f2.java new file mode 100644 index 000000000000..31eebff6972e --- /dev/null +++ b/java/java-tests/testData/ide/importAction/moduleMaximization/single/m1/src/test/f2.java @@ -0,0 +1 @@ +class FooTest {} \ No newline at end of file