Skip to content

Commit c9c3a4d

Browse files
authored
Merge pull request #1923 from Haehnchen/feature/template-annotation-create
underscore should be the preferred template creation quickfix
2 parents 62c7a29 + d4cbc0d commit c9c3a4d

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/inspection/TemplateCreateByNameLocalQuickFix.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@
2727
* @author Daniel Espendiller <daniel@espendiller.net>
2828
*/
2929
public class TemplateCreateByNameLocalQuickFix extends IntentionAndQuickFixAction implements HighPriorityAction {
30-
@NotNull
31-
private final String templateName;
30+
private final @NotNull String[] templateNames;
3231

33-
public TemplateCreateByNameLocalQuickFix(@NotNull String templateName) {
34-
this.templateName = templateName;
32+
public TemplateCreateByNameLocalQuickFix(@NotNull String... templateNames) {
33+
this.templateNames = templateNames;
3534
}
3635

3736
@Nls
@@ -59,7 +58,11 @@ public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor proble
5958
}
6059

6160
private void applyFix(@NotNull Project project) {
62-
Collection<String> templatePaths = TwigUtil.getCreateAbleTemplatePaths(project, templateName);
61+
Collection<String> templatePaths = new LinkedHashSet<>();
62+
63+
for (String templateName : templateNames) {
64+
templatePaths.addAll(TwigUtil.getCreateAbleTemplatePaths(project, templateName));
65+
}
6366

6467
if(templatePaths.size() == 0) {
6568
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/inspection/TemplateExistsAnnotationPhpAttributeLocalInspection.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.intellij.psi.PsiElement;
77
import com.intellij.psi.PsiElementVisitor;
88
import com.intellij.psi.util.PsiTreeUtil;
9-
import com.intellij.util.containers.ContainerUtil;
109
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment;
1110
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
1211
import com.jetbrains.php.lang.psi.elements.Method;
@@ -19,14 +18,10 @@
1918
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
2019
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
2120
import fr.adrienbrault.idea.symfony2plugin.util.PhpPsiAttributesUtil;
22-
import org.apache.commons.lang.StringUtils;
2321
import org.jetbrains.annotations.NotNull;
2422
import org.jetbrains.annotations.Nullable;
2523

26-
import java.util.ArrayList;
27-
import java.util.Arrays;
28-
import java.util.Collection;
29-
import java.util.HashSet;
24+
import java.util.*;
3025

3126
/**
3227
* @author Daniel Espendiller <daniel@espendiller.net>
@@ -54,9 +49,11 @@ public void visitElement(@NotNull PsiElement element) {
5449
}
5550

5651
private void annotate(@NotNull PhpAttribute phpAttribute, @NotNull ProblemsHolder holder) {
57-
Collection<String> templateNames = new HashSet<>();
52+
LinkedHashSet<String> templateNames = new LinkedHashSet<>();
5853

59-
if (phpAttribute.getArguments().isEmpty()) {
54+
55+
boolean isEmptyTemplateAndGuess = phpAttribute.getArguments().isEmpty();
56+
if (isEmptyTemplateAndGuess) {
6057
PsiElement phpAttributesList = phpAttribute.getParent();
6158
if (phpAttributesList instanceof PhpAttributesList) {
6259
PsiElement method = phpAttributesList.getParent();
@@ -72,7 +69,7 @@ private void annotate(@NotNull PhpAttribute phpAttribute, @NotNull ProblemsHolde
7269
}
7370

7471
if(!templateNames.isEmpty()) {
75-
extracted(phpAttribute, holder, templateNames);
72+
attachProblemForMissingTemplatesWithSuggestions(phpAttribute, holder, templateNames, isEmptyTemplateAndGuess);
7673
}
7774
}
7875

@@ -91,18 +88,22 @@ private void annotate(@NotNull PhpDocTag phpDocTag, @NotNull ProblemsHolder hold
9188
return;
9289
}
9390

94-
Collection<String> templateNames = new HashSet<>();
91+
LinkedHashSet<String> templateNames = new LinkedHashSet<>();
92+
93+
boolean isEmptyTemplateAndGuess = false;
9594

9695
@Nullable String matcher = AnnotationUtil.getPropertyValueOrDefault(phpDocTag, "template");
9796
if (matcher != null) {
9897
templateNames.add(matcher);
9998
} else {
99+
isEmptyTemplateAndGuess = true;
100100

101101
// find template name on last method
102102
PhpDocComment docComment = PsiTreeUtil.getParentOfType(phpDocTag, PhpDocComment.class);
103103
if(null == docComment) {
104104
return;
105105
}
106+
106107
Method method = PsiTreeUtil.getNextSiblingOfType(docComment, Method.class);
107108
if(null == method) {
108109
return;
@@ -112,11 +113,11 @@ private void annotate(@NotNull PhpDocTag phpDocTag, @NotNull ProblemsHolder hold
112113
}
113114

114115
if(!templateNames.isEmpty()) {
115-
extracted(phpDocTag, holder, templateNames);
116+
attachProblemForMissingTemplatesWithSuggestions(phpDocTag, holder, templateNames, isEmptyTemplateAndGuess);
116117
}
117118
}
118119

119-
private void extracted(@NotNull PsiElement target, @NotNull ProblemsHolder holder, @NotNull Collection<String> templateNames) {
120+
private void attachProblemForMissingTemplatesWithSuggestions(@NotNull PsiElement target, @NotNull ProblemsHolder holder, @NotNull LinkedHashSet<String> templateNames, boolean isEmptyTemplateAndGuess) {
120121
if(templateNames.size() == 0) {
121122
return;
122123
}
@@ -128,18 +129,16 @@ private void extracted(@NotNull PsiElement target, @NotNull ProblemsHolder holde
128129
}
129130

130131
// find html target, as this this our first priority for end users condition
131-
String templateName = ContainerUtil.find(templateNames, s -> s.toLowerCase().endsWith(".html.twig"));
132-
133-
// fallback on first item
134-
if(templateName == null) {
135-
templateName = templateNames.iterator().next();
136-
}
132+
// or fallback on first item
133+
String[] templates = templateNames.stream()
134+
.filter(s -> s.toLowerCase().endsWith(".html.twig")).toArray(String[]::new);
137135

138136
Collection<LocalQuickFix> quickFixes = new ArrayList<>();
139-
quickFixes.add(new TemplateCreateByNameLocalQuickFix(templateName));
137+
quickFixes.add(new TemplateCreateByNameLocalQuickFix(templates));
140138

141-
if (StringUtils.isNotBlank(templateName)) {
142-
quickFixes.add(new TemplateGuessTypoQuickFix(templateName));
139+
if (!isEmptyTemplateAndGuess && templates.length > 0) {
140+
// use first as underscore is higher priority and common way by framework bundle
141+
quickFixes.add(new TemplateGuessTypoQuickFix(templates[0]));
143142
}
144143

145144
holder.registerProblem(target, "Twig: Missing Template", quickFixes.toArray(new LocalQuickFix[0]));

0 commit comments

Comments
 (0)