8000 add linemarker icon for navigate to "extends" tag of the given Twig · SylApps/idea-php-symfony2-plugin@1dc1ac7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1dc1ac7

Browse files
committed
add linemarker icon for navigate to "extends" tag of the given Twig
1 parent 0f02844 commit 1dc1ac7

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/TwigLineMarkerProvider.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
2727
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
2828
import fr.adrienbrault.idea.symfony2plugin.dic.RelatedPopupGotoLineMarker;
29+
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.TwigExtendsStubIndex;
2930
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.TwigIncludeStubIndex;
3031
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
3132
import fr.adrienbrault.idea.symfony2plugin.twig.loade 8000 r.FileImplementsLazyLoader;
3233
import fr.adrienbrault.idea.symfony2plugin.twig.loader.FileOverwritesLazyLoader;
3334
import fr.adrienbrault.idea.symfony2plugin.twig.utils.TwigBlockUtil;
35+
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
3436
import icons.TwigIcons;
3537
import org.apache.commons.lang.StringUtils;
3638
import org.jetbrains.annotations.NotNull;
@@ -64,6 +66,11 @@ public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNu
6466
results.add(lineIncludes);
6567
}
6668

69+
LineMarkerInfo extending = attachExtends((TwigFile) psiElement);
70+
if(extending != null) {
71+
results.add(extending);
72+
}
73+
6774
// eg bundle overwrites
6875
LineMarkerInfo overwrites = attachOverwrites((TwigFile) psiElement);
6976
if(overwrites != null) {
@@ -113,6 +120,7 @@ private void attachController(@NotNull TwigFile twigFile, @NotNull Collection<?
113120
result.add(builder.createLineMarkerInfo(twigFile));
114121
}
115122

123+
@Nullable
116124
private LineMarkerInfo attachIncludes(@NotNull TwigFile twigFile) {
117125
Collection<String> templateNames = TwigUtil.getTemplateNamesForFile(twigFile);
118126

@@ -143,6 +151,37 @@ private LineMarkerInfo attachIncludes(@NotNull TwigFile twigFile) {
143151
return builder.createLineMarkerInfo(twigFile);
144152
}
145153

154+
@Nullable
155+
private LineMarkerInfo attachExtends(@NotNull TwigFile twigFile) {
156+
Collection<String> templateNames = TwigUtil.getTemplateNamesForFile(twigFile);
157+
158+
boolean found = false;
159+
for(String templateName: templateNames) {
160+
Project project = twigFile.getProject();
161+
162+
Collection<VirtualFile> containingFiles = FileBasedIndex.getInstance().getContainingFiles(
163+
TwigExtendsStubIndex.KEY, templateName, GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.allScope(project), TwigFileType.INSTANCE)
164+
);
165+
166+
// stop on first target, we load them lazily afterwards
167+
if(containingFiles.size() > 0) {
168+
found = true;
169+
break;
170+
}
171+
}
172+
173+
if(!found) {
174+
return null;
175+
}
176+
177+
NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(PhpIcons.IMPLEMENTED)
178+
.setTargets(new TemplateExtendsLazyTargets(twigFile.getProject(), twigFile.getVirtualFile()))
179+
.setTooltipText("Navigate to extends")
180+
.setCellRenderer(new MyFileReferencePsiElementListCellRenderer());
181+
182+
return builder.createLineMarkerInfo(twigFile);
183+
}
184+
146185
@Nullable
147186
private LineMarkerInfo attachOverwrites(@NotNull TwigFile twigFile) {
148187
Collection<PsiFile> targets = new ArrayList<>();
@@ -374,4 +413,25 @@ protected Icon getIcon(PsiElement psiElement) {
374413
return TwigIcons.TwigFileIcon;
375414
}
376415
}
416+
417+
/**
418+
* Find "extends" which are targeting the given template file
419+
*/
420+
private static class TemplateExtendsLazyTargets extends NotNullLazyValue<Collection<? extends PsiElement>> {
421+
@NotNull
422+
private final Project project;
423+
@NotNull
424+
private final VirtualFile virtualFile;
425+
426+
TemplateExtendsLazyTargets(@NotNull Project project, @NotNull VirtualFile virtualFile) {
427+
this.project = project;
428+
this.virtualFile = virtualFile;
429+
}
430+
431+
@NotNull
432+
@Override
433+
protected Collection<? extends PsiElement> compute() {
434+
return PsiElementUtils.convertVirtualFilesToPsiFiles(project, TwigUtil.getTemplatesExtendingFile(project, virtualFile));
435+
}
436+
}
377437
}

0 commit comments

Comments
 (0)
0