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

Commit

Permalink
Improve layout resolution
Browse files Browse the repository at this point in the history
Added fallback to find the layout file even though the project is incorrectly configured (no resource directory is set).
  • Loading branch information
TomasKypta committed Jul 13, 2015
1 parent 7b62e60 commit 110ce44
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/com/avast/android/butterknifezelezny/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

Expand Down Expand Up @@ -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)
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 110ce44

Please sign in to comment.