diff --git a/src/main/java/de/gebit/plugins/autoconfig/ConfigurationLoaderService.java b/src/main/java/de/gebit/plugins/autoconfig/ConfigurationLoaderService.java index 9276a03..992d518 100644 --- a/src/main/java/de/gebit/plugins/autoconfig/ConfigurationLoaderService.java +++ b/src/main/java/de/gebit/plugins/autoconfig/ConfigurationLoaderService.java @@ -13,10 +13,13 @@ import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.components.Service; import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.impl.ProjectImpl; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileManager; import de.gebit.plugins.autoconfig.util.Notifications; import java.io.IOException; +import java.nio.file.Path; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -26,6 +29,11 @@ */ @Service(Service.Level.PROJECT) public final class ConfigurationLoaderService { + /** + * Configuration directory containing yaml sources. + */ + public static final String AUTOCONFIG_DIRECTORY = "autoconfig"; + private final Project project; private final Map configurationOptions = new HashMap<>(); @@ -68,12 +76,21 @@ public boolean hasAutoconfigDir() { return getConfigDirectory().isPresent(); } + @SuppressWarnings("UnstableApiUsage") private Optional getConfigDirectory() { var projectFile = project.getProjectFile(); if (projectFile == null) { + // Fallback to find autoconfig directory. May happen when project is first opened and no misc.xml can be found + if (project instanceof ProjectImpl projectImpl) { + Path directoryStorePath = projectImpl.getComponentStore().getDirectoryStorePath(); + if (directoryStorePath != null) { + return Optional.ofNullable(VirtualFileManager.getInstance().findFileByNioPath(directoryStorePath)) + .map(m -> m.findChild(AUTOCONFIG_DIRECTORY)); + } + } return Optional.empty(); } - return Optional.ofNullable(projectFile.getParent().findChild("autoconfig")); + return Optional.ofNullable(projectFile.getParent().findChild(AUTOCONFIG_DIRECTORY)); } public void resetConfigurationCache() {