Skip to content
This repository has been archived by the owner on Apr 26, 2019. It is now read-only.

Commit

Permalink
Fix layout resolution and fix #40
Browse files Browse the repository at this point in the history
Layout resoluteion fixed by restricting search for layouts only to the module (and its dependencies).
  • Loading branch information
TomasKypta committed Jul 12, 2015
1 parent be2297e commit 7b62e60
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/com/avast/android/butterknifezelezny/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/com/avast/android/butterknifezelezny/model/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down

0 comments on commit 7b62e60

Please sign in to comment.