11package com .axellience .vuegwtplugin ;
22
3- import static com .axellience .vuegwtplugin .util .VueGWTPluginUtil .COMPONENT_QUALIFIED_NAME ;
4- import static com .axellience .vuegwtplugin .util .VueGWTPluginUtil .findHtmlTemplate ;
5-
63import com .axellience .vuegwtplugin .language .htmltemplate .HtmlTemplateLanguage ;
4+ import com .axellience .vuegwtplugin .util .VueGWTComponentAnnotationUtil ;
75import com .axellience .vuegwtplugin .util .VueGWTPluginUtil ;
86import com .intellij .lang .ASTNode ;
9- import com .intellij .openapi .project .Project ;
107import com .intellij .openapi .util .TextRange ;
11- import com .intellij .psi .PsiAnnotation ;
12- import com .intellij .psi .PsiAnnotationMemberValue ;
13- import com .intellij .psi .PsiArrayInitializerMemberValue ;
14- import com .intellij .psi .PsiClass ;
15- import com .intellij .psi .PsiClassObjectAccessExpression ;
16- import com .intellij .psi .PsiClassType ;
178import com .intellij .psi .PsiFile ;
18- import com .intellij .psi .PsiJavaFile ;
19- import com .intellij .psi .PsiManager ;
20- import com .intellij .psi .impl .PsiImplUtil ;
219import com .intellij .psi .impl .source .xml .SchemaPrefix ;
2210import com .intellij .psi .impl .source .xml .TagNameReference ;
2311import com .intellij .psi .xml .XmlAttribute ;
2412import com .intellij .psi .xml .XmlDocument ;
25- import com .intellij .psi .xml .XmlFile ;
2613import com .intellij .psi .xml .XmlTag ;
2714import com .intellij .xml .HtmlXmlExtension ;
28- import java .util .List ;
29- import java .util .Optional ;
3015import org .jetbrains .annotations .NotNull ;
3116import org .jetbrains .annotations .Nullable ;
3217
@@ -38,8 +23,7 @@ public boolean isAvailable(PsiFile psiFile) {
3823 return false ;
3924 }
4025
41- Optional <PsiJavaFile > optionalPsiJavaFile = getJavaFileForTemplate (psiFile );
42- return optionalPsiJavaFile .isPresent ();
26+ return VueGWTPluginUtil .findJavaFromTemplate (psiFile ).isPresent ();
4327 }
4428
4529 @ Override
@@ -84,110 +68,26 @@ public String[][] getNamespacesFromDocument(XmlDocument parent, boolean declarat
8468 @ Nullable
8569 @ Override
8670 public TagNameReference createTagNameReference (ASTNode astNode , boolean b ) {
87- PsiFile templateFile = astNode .getTreeParent ().getPsi ().getContainingFile ();
88-
89- Optional <PsiJavaFile > optionalPsiJavaFile = getJavaFileForTemplate (templateFile );
90- if (!optionalPsiJavaFile .isPresent ()) {
91- return super .createTagNameReference (astNode , b );
92- }
93-
94- PsiJavaFile psiJavaFile = optionalPsiJavaFile .get ();
9571 String tagName = astNode .getText (); // Tag name of the current element
9672
97- return getComponentAnnotationFromJavaFile (psiJavaFile )
98- .flatMap (annotation -> getComponentTemplateFromAnnotation (annotation , tagName ))
99- .map (componentTemplate ->
100- (TagNameReference ) new VueGWTTagNameReference (astNode , componentTemplate , b ))
101- .orElse (super .createTagNameReference (astNode , b ));
102- }
103-
104- private Optional <PsiAnnotation > getComponentAnnotationFromJavaFile (PsiJavaFile psiJavaFile ) {
105- for (PsiClass psiClass : psiJavaFile .getClasses ()) {
106- PsiAnnotation [] annotations = psiClass .getAnnotations ();
107- for (PsiAnnotation annotation : annotations ) {
108- if (COMPONENT_QUALIFIED_NAME .equals (annotation .getQualifiedName ())) {
109- return Optional .of (annotation );
110- }
111- }
112- }
113-
114- return Optional .empty ();
115- }
116-
117- private Optional <PsiFile > getComponentTemplateFromAnnotation (PsiAnnotation componentAnnotation ,
118- String tagName ) {
119- PsiAnnotationMemberValue componentsValue =
120- PsiImplUtil .findAttributeValue (componentAnnotation , "components" );
121-
122- if (componentsValue instanceof PsiClassObjectAccessExpression ) {
123- return getComponentTemplateForClassObjectAccess (
124- (PsiClassObjectAccessExpression ) componentsValue , tagName );
125- }
126-
127- if (componentsValue instanceof PsiArrayInitializerMemberValue ) {
128- return getComponentTemplateFromArrayInitializerMemberValue (
129- (PsiArrayInitializerMemberValue ) componentsValue , tagName );
130- }
131-
132- return Optional .empty ();
133- }
134-
135- private Optional <PsiFile > getComponentTemplateFromArrayInitializerMemberValue (
136- PsiArrayInitializerMemberValue arrayInitializer , String tagName ) {
137- for (PsiAnnotationMemberValue initializer : arrayInitializer .getInitializers ()) {
138- if (!(initializer instanceof PsiClassObjectAccessExpression )) {
139- continue ;
140- }
141-
142- Optional <PsiFile > optionalComponentTemplate = getComponentTemplateForClassObjectAccess (
143- (PsiClassObjectAccessExpression ) initializer , tagName );
144- if (optionalComponentTemplate .isPresent ()) {
145- return optionalComponentTemplate ;
146- }
147- }
148-
149- return Optional .empty ();
150- }
151-
152- private Optional <PsiFile > getComponentTemplateForClassObjectAccess (
153- PsiClassObjectAccessExpression componentsClassAccess , String tagName ) {
154- // classType is Class<MyComponent>
155- PsiClassType classType = (PsiClassType ) PsiImplUtil .getType (componentsClassAccess );
156-
157- // componentClassType is MyComponent
158- PsiClassType componentClassType = (PsiClassType ) classType .getParameters ()[0 ];
159- String componentTagName = VueGWTPluginUtil .componentToTagName (componentClassType );
160- if (!tagName .equals (componentTagName )) {
161- return Optional .empty ();
162- }
163- PsiClass componentClass = componentClassType .resolve ();
164- if (componentClass == null ) {
165- return Optional .empty ();
166- }
167-
168- return findHtmlTemplate (componentClass .getContainingFile ());
73+ PsiFile templateFile = astNode .getTreeParent ().getPsi ().getContainingFile ();
74+ return
75+ VueGWTPluginUtil
76+ .findJavaFromTemplate (templateFile )
77+ .flatMap (VueGWTComponentAnnotationUtil ::getComponentAnnotationFromJavaFile )
78+ .flatMap (
79+ annotation -> VueGWTComponentAnnotationUtil
80+ .getImportedComponentTemplateFromAnnotationComponent (annotation , tagName )
81+ )
82+ .map (
83+ componentTemplate ->
84+ (TagNameReference ) new VueGWTTagNameReference (astNode , componentTemplate , b )
85+ )
86+ .orElse (super .createTagNameReference (astNode , b ));
16987 }
17088
17189 @ Override
17290 public boolean isSelfClosingTagAllowed (@ NotNull XmlTag tag ) {
17391 return true ;
17492 }
175-
176- private Optional <PsiJavaFile > getJavaFileForTemplate (PsiFile templateFile ) {
177- // Get the Java file for the template
178- Optional <PsiFile > optionalJavaFile = VueGWTPluginUtil .findJavaFromTemplate (templateFile );
179- if (!optionalJavaFile .isPresent ()) {
180- return Optional .empty ();
181- }
182-
183- // Find the project for the current file
184- Project project = templateFile .getProject ();
185- PsiFile file = PsiManager .getInstance (project )
186- .findFile (optionalJavaFile .get ().getVirtualFile ());
187- if (!(file instanceof PsiJavaFile )) {
188- return Optional .empty ();
189- }
190-
191- return Optional .of ((PsiJavaFile ) file );
192- }
19393}
0 commit comments