5
5
import com .intellij .psi .xml .XmlFile ;
6
6
import com .intellij .psi .xml .XmlTag ;
7
7
import com .intellij .util .Consumer ;
8
- import fr .adrienbrault .idea .symfony2plugin .dic .attribute .value .AttributeValueInterface ;
9
- import fr .adrienbrault .idea .symfony2plugin .dic .attribute .value .DummyAttributeValue ;
10
- import fr .adrienbrault .idea .symfony2plugin .dic .attribute .value .XmlTagAttributeValue ;
11
- import fr .adrienbrault .idea .symfony2plugin .dic .attribute .value .YamlKeyValueAttributeValue ;
12
8
import fr .adrienbrault .idea .symfony2plugin .stubs .dict .FileResource ;
9
+ import fr .adrienbrault .idea .symfony2plugin .stubs .dict .FileResourceContextTypeEnum ;
13
10
import fr .adrienbrault .idea .symfony2plugin .util .yaml .YamlHelper ;
14
11
import org .apache .commons .lang .StringUtils ;
15
12
import org .jetbrains .annotations .NotNull ;
16
- import org .jetbrains .annotations .Nullable ;
17
13
import org .jetbrains .yaml .psi .YAMLFile ;
18
14
import org .jetbrains .yaml .psi .YAMLKeyValue ;
19
15
16
+ import java .util .HashMap ;
17
+ import java .util .Map ;
18
+ import java .util .TreeMap ;
19
+
20
20
/**
21
21
* @author Daniel Espendiller <daniel@espendiller.net>
22
22
*/
23
23
public class FileResourceVisitorUtil {
24
-
25
24
public static void visitFile (@ NotNull PsiFile psiFile , @ NotNull Consumer <FileResourceConsumer > consumer ) {
26
25
if (psiFile instanceof XmlFile ) {
27
26
visitXmlFile ((XmlFile ) psiFile , consumer );
@@ -46,7 +45,26 @@ private static void visitYamlFile(@NotNull YAMLFile yamlFile, @NotNull Consumer<
46
45
continue ;
47
46
}
48
47
49
- consumer .consume (new FileResourceConsumer (resourceKey , yamlKeyValue , normalize (resource )));
48
+ FileResourceContextTypeEnum fileResourceContextType = FileResourceContextTypeEnum .UNKNOWN ;
49
+
50
+ Map <String , String > map = new HashMap <>();
51
+ for (String option : new String [] {"type" , "prefix" , "name_prefix" }) {
52
+ String attributeValue = YamlHelper .getYamlKeyValueAsString (yamlKeyValue , option , true );
53
+ if (StringUtils .isNotBlank (attributeValue ) && attributeValue .length () < 128 ) {
54
+ map .put (option , attributeValue );
55
+ }
56
+ }
57
+
58
+ boolean isRouteContext = map .containsKey ("type" )
59
+ || map .containsKey ("prefix" )
60
+ || map .containsKey ("name_prefix" )
61
+ || YamlHelper .getYamlKeyValue (yamlKeyValue , "requirements" , true ) != null ;
62
+
63
+ if (isRouteContext ) {
64
+ fileResourceContextType = FileResourceContextTypeEnum .ROUTE ;
65
+ }
66
+
67
+ consumer .consume (new FileResourceConsumer (resourceKey , normalize (resource ), fileResourceContextType , map ));
50
68
}
51
69
}
52
70
@@ -65,7 +83,15 @@ private static void visitXmlFile(@NotNull XmlFile psiFile, @NotNull Consumer<Fil
65
83
continue ;
66
84
}
67
85
68
- consumer .consume (new FileResourceConsumer (xmlTag , xmlTag , normalize (resource )));
86
+ Map <String , String > map = new HashMap <>();
87
+ for (String option : new String [] {"type" , "prefix" , "name-prefix" }) {
88
+ String attributeValue = xmlTag .getAttributeValue (option );
89
+ if (StringUtils .isNotBlank (attributeValue ) && attributeValue .length () < 128 ) {
90
+ map .put (option .replace ("-" , "_" ), attributeValue );
91
+ }
92
+ }
93
+
94
+ consumer .consume (new FileResourceConsumer (xmlTag , normalize (resource ), FileResourceContextTypeEnum .ROUTE , map ));
69
95
}
70
96
}
71
97
@@ -75,39 +101,23 @@ public static String normalize(@NotNull String resource) {
75
101
}
76
102
77
103
public static class FileResourceConsumer {
78
-
79
104
@ NotNull
80
105
private final PsiElement psiElement ;
81
106
82
- @ Nullable
83
- private AttributeValueInterface attributeValue = null ;
107
+ @ NotNull
108
+ private final String resource ;
84
109
85
110
@ NotNull
86
- private final PsiElement scope ;
111
+ private final FileResourceContextTypeEnum contextType ;
112
+
87
113
@ NotNull
88
- private final String resource ;
114
+ private final Map < String , String > contextValues ;
89
115
90
- public FileResourceConsumer (@ NotNull PsiElement target , @ NotNull PsiElement scope , @ NotNull String resource ) {
116
+ public FileResourceConsumer (@ NotNull PsiElement target , @ NotNull String resource , @ NotNull FileResourceContextTypeEnum fileResourceContextTypeEnum , @ NotNull Map < String , String > contextValues ) {
91
117
this .psiElement = target ;
92
- this .scope = scope ;
93
118
this .resource = resource ;
94
- }
95
-
96
- @ NotNull
97
- public AttributeValueInterface getAttributeValue () {
98
- if (this .attributeValue != null ) {
99
- return this .attributeValue ;
100
- }
101
-
102
- // We use lazy instances
103
- // @TODO: replace with factory pattern
104
- if (this .psiElement instanceof YAMLKeyValue ) {
105
- return this .attributeValue = new YamlKeyValueAttributeValue ((YAMLKeyValue ) this .scope );
106
- } else if (this .psiElement instanceof XmlTag ) {
107
- return this .attributeValue = new XmlTagAttributeValue ((XmlTag ) this .scope );
108
- }
109
-
110
- return this .attributeValue = new DummyAttributeValue (this .psiElement );
119
+ this .contextType = fileResourceContextTypeEnum ;
120
+ this .contextValues = contextValues ;
111
121
}
112
122
113
123
@ NotNull
@@ -121,14 +131,13 @@ public PsiElement getPsiElement() {
121
131
}
122
132
123
133
@ NotNull
124
- public FileResource createFileResource () {
125
- FileResource fileResource = new FileResource (this .getResource ());
126
- String prefix = this .getAttributeValue ().getString ("prefix" );
127
- if (prefix != null ) {
128
- fileResource .setPrefix (prefix );
129
- }
134
+ public FileResourceContextTypeEnum getContextType () {
135
+ return contextType ;
136
+ }
130
137
131
- return fileResource ;
138
+ @ NotNull
139
+ public FileResource createFileResource () {
140
+ return new FileResource (this .getResource (), this .getContextType (), new TreeMap <>(this .contextValues ));
132
141
}
133
142
}
134
143
}
0 commit comments