66import com .intellij .psi .PsiElement ;
77import com .intellij .psi .PsiElementVisitor ;
88import com .intellij .psi .util .PsiTreeUtil ;
9- import com .intellij .util .containers .ContainerUtil ;
109import com .jetbrains .php .lang .documentation .phpdoc .psi .PhpDocComment ;
1110import com .jetbrains .php .lang .documentation .phpdoc .psi .tags .PhpDocTag ;
1211import com .jetbrains .php .lang .psi .elements .Method ;
1918import fr .adrienbrault .idea .symfony2plugin .templating .util .TwigUtil ;
2019import fr .adrienbrault .idea .symfony2plugin .util .PhpElementsUtil ;
2120import fr .adrienbrault .idea .symfony2plugin .util .PhpPsiAttributesUtil ;
22- import org .apache .commons .lang .StringUtils ;
2321import org .jetbrains .annotations .NotNull ;
2422import 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