diff --git a/src/com/avast/android/butterknifezelezny/common/Utils.java b/src/com/avast/android/butterknifezelezny/common/Utils.java index d673dbf..61f3827 100644 --- a/src/com/avast/android/butterknifezelezny/common/Utils.java +++ b/src/com/avast/android/butterknifezelezny/common/Utils.java @@ -90,28 +90,39 @@ public static PsiFile findLayoutResource(PsiElement element) { Project project = element.getProject(); String name = String.format("%s.xml", element.getText()); - PsiFile[] files = FilenameIndex.getFilesByName(project, name, new EverythingGlobalScope(project)); + + // 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 } + // 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]; } /** * Try to find layout XML file by name * + * @param file * @param project * @param fileName * @return */ - public static PsiFile findLayoutResource(Project project, String fileName) { + public static PsiFile findLayoutResource(PsiFile file, Project project, String fileName) { String name = String.format("%s.xml", fileName); - PsiFile[] files = FilenameIndex.getFilesByName(project, name, new EverythingGlobalScope(project)); + // 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]; } @@ -148,7 +159,7 @@ public void visitElement(final PsiElement element) { if (layout != null) { Project project = file.getProject(); - PsiFile include = findLayoutResource(project, getLayoutName(layout.getValue())); + PsiFile include = findLayoutResource(file, project, getLayoutName(layout.getValue())); if (include != null) { getIDsFromLayout(include, elements); diff --git a/src/com/avast/android/butterknifezelezny/model/Element.java b/src/com/avast/android/butterknifezelezny/model/Element.java index 373ffc6..25d0c4a 100644 --- a/src/com/avast/android/butterknifezelezny/model/Element.java +++ b/src/com/avast/android/butterknifezelezny/model/Element.java @@ -73,6 +73,10 @@ private String getFieldName() { sb.append(Utils.getPrefix()); for (int i = 0; i < words.length; i++) { + if (words[i].isEmpty()) { + // fixing issues with double underscores - see issue #40 + continue; + } String[] idTokens = words[i].split("\\."); char[] chars = idTokens[idTokens.length - 1].toCharArray(); if (i > 0 || !Utils.isEmptyString(Utils.getPrefix())) {