diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusModuleUtil.java b/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusModuleUtil.java index fce810a7c..38b9f9a3c 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusModuleUtil.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusModuleUtil.java @@ -31,10 +31,7 @@ import org.slf4j.LoggerFactory; import java.io.File; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -52,6 +49,7 @@ public class QuarkusModuleUtil { public static final Pattern APPLICATION_YAML = Pattern.compile("application(-.+)?\\.ya?ml"); + private static final Comparator ROOT_COMPARATOR = Comparator.comparingInt(r -> r.getPath().length()); /** * Check if the module is a Quarkus project. Should check if some class if present @@ -152,6 +150,34 @@ private static boolean isQuarkusModule(VirtualFile file, Project project) { } public static @Nullable VirtualFile getModuleDirPath(@NotNull Module module) { - return LocalFileSystem.getInstance().findFileByPath(ModuleUtilCore.getModuleDirPath(module)); + VirtualFile[] roots = getContentRoots(module); + if (roots.length > 0) { + return roots[0]; + } + return VfsUtil.findFileByIoFile(new File(ModuleUtilCore.getModuleDirPath(module)), true); + } + + /** + * Returns an array of content roots of the given module sorted with smallest path first (to eliminate generated sources roots) from all content entries. + * + * @param module the module + * @return the array of content roots. + */ + public static VirtualFile[] getContentRoots(Module module) { + VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots(); + if (roots.length <= 1) { + return roots; + } + // put root with smallest path first (eliminates generated sources roots) + sortRoot(roots); + return roots; + } + + public static void sortRoot(List roots) { + Collections.sort(roots, ROOT_COMPARATOR); // put root with smallest path first (eliminates generated sources roots) + } + + public static void sortRoot(VirtualFile[] roots) { + Arrays.sort(roots, ROOT_COMPARATOR); // put root with smallest path first (eliminates generated sources roots) } } diff --git a/src/main/java/com/redhat/devtools/intellij/qute/psi/core/command/QuteAction.java b/src/main/java/com/redhat/devtools/intellij/qute/psi/core/command/QuteAction.java index 90c1a0179..06147e259 100644 --- a/src/main/java/com/redhat/devtools/intellij/qute/psi/core/command/QuteAction.java +++ b/src/main/java/com/redhat/devtools/intellij/qute/psi/core/command/QuteAction.java @@ -11,14 +11,9 @@ package com.redhat.devtools.intellij.qute.psi.core.command; import com.google.gson.JsonPrimitive; -import com.intellij.openapi.actionSystem.AnAction; -import com.intellij.openapi.actionSystem.AnActionEvent; -import com.redhat.devtools.lsp4ij.commands.CommandExecutor; import com.redhat.devtools.lsp4ij.commands.LSPCommand; import com.redhat.devtools.lsp4ij.commands.LSPCommandAction; -import java.util.List; - public abstract class QuteAction extends LSPCommandAction { protected String getURL(LSPCommand command) { diff --git a/src/main/java/com/redhat/devtools/intellij/qute/psi/core/command/QuteGenerateTemplateAction.java b/src/main/java/com/redhat/devtools/intellij/qute/psi/core/command/QuteGenerateTemplateAction.java index a846ad8de..260816984 100644 --- a/src/main/java/com/redhat/devtools/intellij/qute/psi/core/command/QuteGenerateTemplateAction.java +++ b/src/main/java/com/redhat/devtools/intellij/qute/psi/core/command/QuteGenerateTemplateAction.java @@ -17,9 +17,7 @@ import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.redhat.devtools.lsp4ij.LanguageServiceAccessor; -import com.redhat.devtools.lsp4ij.commands.CommandExecutor; import com.redhat.devtools.lsp4ij.commands.LSPCommand; -import org.eclipse.lsp4j.Command; import org.eclipse.lsp4j.ExecuteCommandOptions; import org.eclipse.lsp4j.ExecuteCommandParams; import org.eclipse.lsp4j.services.LanguageServer; diff --git a/src/main/java/com/redhat/devtools/intellij/qute/psi/utils/PsiQuteProjectUtils.java b/src/main/java/com/redhat/devtools/intellij/qute/psi/utils/PsiQuteProjectUtils.java index 2e215c224..6bd83da60 100644 --- a/src/main/java/com/redhat/devtools/intellij/qute/psi/utils/PsiQuteProjectUtils.java +++ b/src/main/java/com/redhat/devtools/intellij/qute/psi/utils/PsiQuteProjectUtils.java @@ -44,7 +44,6 @@ public class PsiQuteProjectUtils { * Value for Qute annotations indicating behaviour should be using the default */ private static final String DEFAULTED = "<>"; - private static final Comparator ROOT_COMPARATOR = Comparator.comparingInt(r -> r.getPath().length()); private PsiQuteProjectUtils() { } @@ -104,7 +103,7 @@ private static String getTemplateBaseDir(Module javaProject) { public static @Nullable VirtualFile findBestResourcesDir(@NotNull Module javaProject) { List resourcesDirs = ModuleRootManager.getInstance(javaProject).getSourceRoots(JavaResourceRootType.RESOURCE); if (!resourcesDirs.isEmpty()) { - Collections.sort(resourcesDirs, ROOT_COMPARATOR); // put root with smallest path first (eliminates generated sources roots) + QuarkusModuleUtil.sortRoot(resourcesDirs); // put root with smallest path first (eliminates generated sources roots) // The module configure 'Resources folder' // 1) loop for each configured resources dir and returns the first which contains 'templates' folder. for (var dir : resourcesDirs) { @@ -117,10 +116,6 @@ private static String getTemplateBaseDir(Module javaProject) { return resourcesDirs.get(0); } // Corner usecase, the module doesn't configure 'Resources folder', use the first content roots - VirtualFile[] roots = getContentRoots(javaProject); - if (roots.length > 0) { - return roots[0]; - } return QuarkusModuleUtil.getModuleDirPath(javaProject); } @@ -131,12 +126,7 @@ private static String getTemplateBaseDir(Module javaProject) { * @return the array of content roots. */ public static VirtualFile[] getContentRoots(Module module) { - VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots(); - if (roots.length <= 1) { - return roots; - } - Arrays.sort(roots, ROOT_COMPARATOR); // put root with smallest path first (eliminates generated sources roots) - return roots; + return QuarkusModuleUtil.getContentRoots(module); } /**