From 110ce4475dcb618d8e1c83c5dfecd94f775d5a3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kypta?= Date: Mon, 13 Jul 2015 12:55:12 +0200 Subject: [PATCH] Improve layout resolution Added fallback to find the layout file even though the project is incorrectly configured (no resource directory is set). --- .../butterknifezelezny/common/Utils.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/com/avast/android/butterknifezelezny/common/Utils.java b/src/com/avast/android/butterknifezelezny/common/Utils.java index 61f3827..49cc8f0 100644 --- a/src/com/avast/android/butterknifezelezny/common/Utils.java +++ b/src/com/avast/android/butterknifezelezny/common/Utils.java @@ -27,7 +27,6 @@ import java.util.ArrayList; import java.util.regex.Matcher; -import java.util.regex.Pattern; public class Utils { @@ -90,12 +89,22 @@ public static PsiFile findLayoutResource(PsiElement element) { Project project = element.getProject(); String name = String.format("%s.xml", element.getText()); + return resolveLayoutResourceFile(element, project, name); + + } + + private static PsiFile resolveLayoutResourceFile(PsiElement element, Project project, String name) { // restricting the search to the current module - searching the whole project could return wrong layouts GlobalSearchScope moduleScope = ModuleUtil.findModuleForPsiElement(element).getModuleWithDependenciesAndLibrariesScope(false); PsiFile[] files = FilenameIndex.getFilesByName(project, name, moduleScope); if (files.length <= 0) { - return null; //no matching files + // fallback to search through the whole project + // useful when the project is not properly configured - when the resource directory is not configured + files = FilenameIndex.getFilesByName(project, name, new EverythingGlobalScope(project)); + if (files.length <= 0) { + return null; //no matching files + } } // TODO - we have a problem here - we still can have multiple layouts (some coming from a dependency) @@ -114,16 +123,7 @@ public static PsiFile findLayoutResource(PsiElement element) { public static PsiFile findLayoutResource(PsiFile file, Project project, String fileName) { String name = String.format("%s.xml", fileName); // restricting the search to the module of layout that includes the layout we are seaching for - // - searching the whole project could return wrong layouts - GlobalSearchScope moduleScope = ModuleUtil.findModuleForPsiElement(file).getModuleWithDependenciesAndLibrariesScope(false); - PsiFile[] files = FilenameIndex.getFilesByName(project, name, moduleScope); - if (files.length <= 0) { - return null; //no matching files - } - - // TODO - we have a problem here - we still can have multiple layouts (some coming from a dependency) - // we need to resolve R class properly and find the proper layout for the R class - return files[0]; + return resolveLayoutResourceFile(file, project, name); } /**