Skip to content

reduce Twig template resolving public api #1127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private ContainerCollectionResolver.ServiceCollector getServiceCollector() {

private Map<String, Set<VirtualFile>> getTemplateMap() {
if(this.templateMap == null) {
this.templateMap = TwigUtil.getTwigAndPhpTemplateFiles(this.project);
this.templateMap = TwigUtil.getTemplateMap(this.project, true);
}

return this.templateMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public String[] getNames(Project project, boolean b) {
return new String[0];
}

Collection<String> twigFileNames = TwigUtil.getTwigFileNames(project);
Collection<String> twigFileNames = TwigUtil.getTemplateMap(project).keySet();
return twigFileNames.toArray(new String[twigFileNames.size()]);
}

Expand Down
119 changes: 58 additions & 61 deletions src/fr/adrienbrault/idea/symfony2plugin/templating/util/TwigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -864,42 +864,49 @@ public static String getPresentableTemplateName(@NotNull PsiElement psiElement,
* foo.html.twig => ["views/foo.html.twig", "templates/foo.html.twig"]
*/
@NotNull
private static synchronized Map<String, Set<VirtualFile>> getTemplateMap(@NotNull Project project, boolean useTwig, final boolean usePhp) {
Map<String, Set<VirtualFile>> templateMapProxy = null;
public static Map<String, Set<VirtualFile>> getTemplateMap(@NotNull Project project) {
return getTemplateMap(project, false);
}

/**
* Generate a mapped template name file multiple relation:
*
* foo.html.twig => ["views/foo.html.twig", "templates/foo.html.twig"]
*/
@NotNull
public static synchronized Map<String, Set<VirtualFile>> getTemplateMap(@NotNull Project project, boolean usePhp) {
Map<String, Set<VirtualFile>> templateMapProxy;

// cache twig and all files,
// only PHP files we dont need to cache
if(useTwig && !usePhp) {
if(!usePhp) {
// cache twig files only, most use case
CachedValue<Map<String, Set<VirtualFile>>> cache = project.getUserData(TEMPLATE_CACHE_TWIG);
if (cache == null) {
cache = CachedValuesManager.getManager(project).createCachedValue(new MyTwigOnlyTemplateFileMapCachedValueProvider(project), false);
cache = CachedValuesManager.getManager(project).createCachedValue(new MyAllTemplateFileMapCachedValueProvider(project), false);
project.putUserData(TEMPLATE_CACHE_TWIG, cache);
}

templateMapProxy = cache.getValue();

} else if(useTwig && usePhp) {
} else {
// cache all files
CachedValue<Map<String, Set<VirtualFile>>> cache = project.getUserData(TEMPLATE_CACHE_ALL);
if (cache == null) {
cache = CachedValuesManager.getManager(project).createCachedValue(new MyAllTemplateFileMapCachedValueProvider(project), false);
cache = CachedValuesManager.getManager(project).createCachedValue(new MyAllTemplateFileMapCachedValueProvider(project, true), false);
project.putUserData(TEMPLATE_CACHE_ALL, cache);
}

templateMapProxy = cache.getValue();
}

// cache-less calls
if(templateMapProxy == null) {
templateMapProxy = getTemplateMapProxy(project, useTwig, usePhp);
}

return templateMapProxy;
}

/**
* Cache less visiting template files
*/
@NotNull
private static Map<String, Set<VirtualFile>> getTemplateMapProxy(@NotNull Project project, boolean useTwig, boolean usePhp) {
private static Map<String, Set<VirtualFile>> getTemplateMapProxy(@NotNull Project project, boolean usePhp) {
List<TwigPath> twigPaths = new ArrayList<>(getTwigNamespaces(project));
if(twigPaths.size() == 0) {
return Collections.emptyMap();
Expand All @@ -913,39 +920,29 @@ private static Map<String, Set<VirtualFile>> getTemplateMapProxy(@NotNull Projec
}

VirtualFile virtualDirectoryFile = twigPath.getDirectory(project);
if(virtualDirectoryFile != null) {
MyLimitedVirtualFileVisitor visitor = new MyLimitedVirtualFileVisitor(project, twigPath, usePhp, useTwig, 5, 150);

VfsUtil.visitChildrenRecursively(virtualDirectoryFile, visitor);
if(virtualDirectoryFile == null) {
continue;
}

for (Map.Entry<String, VirtualFile> entry : visitor.getResults().entrySet()) {
if(!templateNames.containsKey(entry.getKey())) {
templateNames.put(entry.getKey(), new HashSet<>());
}
Map<String, VirtualFile> visitor = MyLimitedVirtualFileVisitor.createResult(
virtualDirectoryFile,
project,
twigPath,
usePhp
);

templateNames.get(entry.getKey()).add(entry.getValue());
for (Map.Entry<String, VirtualFile> entry : visitor.entrySet()) {
if(!templateNames.containsKey(entry.getKey())) {
templateNames.put(entry.getKey(), new HashSet<>());
}

templateNames.get(entry.getKey()).add(entry.getValue());
}
}

return templateNames;
}

@NotNull
static Map<String, Set<VirtualFile>> getTwigTemplateFiles(@NotNull Project project) {
return getTemplateMap(project, true, false);
}

@NotNull
public static Collection<String> getTwigFileNames(@NotNull Project project) {
return getTemplateMap(project, true, false).keySet();
}

@NotNull
public static Map<String, Set<VirtualFile>> getTwigAndPhpTemplateFiles(@NotNull Project project) {
return getTemplateMap(project, true, true);
}

@Nullable
private static TwigNamespaceSetting findManagedTwigNamespace(@NotNull Project project, @NotNull TwigPath twigPath) {
List<TwigNamespaceSetting> twigNamespaces = Settings.getInstance(project).twigNamespaces;
Expand Down Expand Up @@ -1481,7 +1478,8 @@ public static Collection<PsiElement> getTwigMacroTargets(final Project project,
public static Collection<LookupElement> getTwigLookupElements(@NotNull Project project) {
VirtualFile baseDir = project.getBaseDir();

return getTwigTemplateFiles(project).entrySet().stream()
return getTemplateMap(project).entrySet()
.stream()
.filter(entry -> entry.getValue().size() > 0)
.map((java.util.function.Function<Map.Entry<String, Set<VirtualFile>>, LookupElement>) entry ->
new TemplateLookupElement(entry.getKey(), entry.getValue().iterator().next(), baseDir)
Expand All @@ -1496,7 +1494,8 @@ public static Collection<LookupElement> getTwigLookupElements(@NotNull Project p
public static Collection<LookupElement> getAllTemplateLookupElements(@NotNull Project project) {
VirtualFile baseDir = project.getBaseDir();

return getTwigAndPhpTemplateFiles(project).entrySet().stream()
return getTemplateMap(project, true).entrySet()
.stream()
.filter(entry -> entry.getValue().size() > 0)
.map((java.util.function.Function<Map.Entry<String, Set<VirtualFile>>, LookupElement>) entry ->
new TemplateLookupElement(entry.getKey(), entry.getValue().iterator().next(), baseDir)
Expand Down Expand Up @@ -2751,33 +2750,25 @@ public String getDomain() {
}
}

private static class MyTwigOnlyTemplateFileMapCachedValueProvider implements CachedValueProvider<Map<String, Set<VirtualFile>>> {
private static class MyAllTemplateFileMapCachedValueProvider implements CachedValueProvider<Map<String, Set<VirtualFile>>> {
@NotNull
private final Project project;

MyTwigOnlyTemplateFileMapCachedValueProvider(@NotNull Project project) {
this.project = project;
}
private final boolean includePhpFiles;

@Nullable
@Override
public Result<Map<String, Set<VirtualFile>>> compute() {
return Result.create(getTemplateMapProxy(project, true, false), PsiModificationTracker.MODIFICATION_COUNT);
MyAllTemplateFileMapCachedValueProvider(@NotNull Project project) {
this(project, false);
}
}

private static class MyAllTemplateFileMapCachedValueProvider implements CachedValueProvider<Map<String, Set<VirtualFile>>> {
@NotNull
private final Project project;

MyAllTemplateFileMapCachedValueProvider(@NotNull Project project) {
MyAllTemplateFileMapCachedValueProvider(@NotNull Project project, boolean includePhpFiles) {
this.project = project;
this.includePhpFiles = includePhpFiles;
}

@Nullable
@Override
public Result<Map<String, Set<VirtualFile>>> compute() {
return Result.create(getTemplateMapProxy(project, true, true), PsiModificationTracker.MODIFICATION_COUNT);
return Result.create(getTemplateMapProxy(project, includePhpFiles), PsiModificationTracker.MODIFICATION_COUNT);
}
}

Expand All @@ -2798,20 +2789,19 @@ private static class MyLimitedVirtualFileVisitor extends VirtualFileVisitor {
@NotNull
private Map<String, VirtualFile> results = new HashMap<>();

private boolean withPhp = false;
private boolean withTwig = true;
private int childrenAllowToVisit = 1000;
final private boolean withPhp;

private int childrenAllowToVisit;

@NotNull
private Set<String> workedOn = new HashSet<>();

MyLimitedVirtualFileVisitor(@NotNull Project project, @NotNull TwigPath twigPath, boolean withPhp, boolean withTwig, int maxDepth, int maxDirs) {
private MyLimitedVirtualFileVisitor(@NotNull Project project, @NotNull TwigPath twigPath, boolean withPhp, int maxDepth, int maxDirs) {
super(VirtualFileVisitor.limit(maxDepth));

this.project = project;
this.twigPath = twigPath;
this.withPhp = withPhp;
this.withTwig = withTwig;
this.childrenAllowToVisit = maxDirs;
}

Expand Down Expand Up @@ -2852,7 +2842,7 @@ private boolean isProcessable(VirtualFile virtualFile) {
return false;
}

if(withTwig && virtualFile.getFileType() instanceof TwigFileType) {
if(virtualFile.getFileType() instanceof TwigFileType) {
return true;
}

Expand All @@ -2864,8 +2854,15 @@ private boolean isProcessable(VirtualFile virtualFile) {
}

@NotNull
public Map<String, VirtualFile> getResults() {
private Map<String, VirtualFile> getResults() {
return results;
}

@NotNull
static Map<String, VirtualFile> createResult(@NotNull VirtualFile virtualFile, @NotNull Project project, @NotNull TwigPath twigPath, boolean withPhp) {
MyLimitedVirtualFileVisitor visitor = new MyLimitedVirtualFileVisitor(project, twigPath, withPhp, 5, 150);
VfsUtil.visitChildrenRecursively(virtualFile, visitor);
return visitor.getResults();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,24 @@ public void testGetTemplateNamesForFile() {
);
}

/**
* @see TwigUtil#getTwigFileNames
*/
public void testGetTwigFileNames() {
createFile("res/foobar/foo.html.twig");

Settings.getInstance(getProject()).twigNamespaces.addAll(createTwigNamespaceSettings());

assertContainsElements(
TwigUtil.getTwigFileNames(getProject()),
TwigUtil.getTemplateMap(getProject()).keySet(),
"@Foo/foobar/foo.html.twig", "FooBundle:foobar:foo.html.twig", ":foobar:foo.html.twig", "foobar/foo.html.twig"
);
}

/**
* @see TwigUtil#getTwigAndPhpTemplateFiles
*/
public void testGetTwigAndPhpTemplateFiles() {
createFiles("res/foobar/foo.html.twig", "res/foobar/foo.php");

Settings.getInstance(getProject()).twigNamespaces.addAll(createTwigNamespaceSettings());

assertContainsElements(
TwigUtil.getTwigAndPhpTemplateFiles(getProject()).keySet(),
TwigUtil.getTemplateMap(getProject(), true).keySet(),
"@Foo/foobar/foo.html.twig", "FooBundle:foobar:foo.html.twig", ":foobar:foo.html.twig", "foobar/foo.html.twig",
"@Foo/foobar/foo.php", "FooBundle:foobar:foo.php", ":foobar:foo.php", "foobar/foo.php"
);
Expand Down