11package net .servicestack .idea ;
22
33import com .intellij .openapi .application .ApplicationManager ;
4- import com .intellij .openapi .application .Result ;
5- import com .intellij .openapi .command .WriteCommandAction ;
64import com .intellij .openapi .editor .DocumentRunnable ;
75import com .intellij .openapi .fileEditor .FileDocumentManager ;
86import com .intellij .openapi .module .Module ;
97import com .intellij .openapi .project .Project ;
108import com .intellij .openapi .vfs .LocalFileSystem ;
119import com .intellij .openapi .vfs .VirtualFile ;
12- import com .intellij .openapi .vfs .VirtualFileManager ;
1310import com .intellij .psi .PsiDocumentManager ;
14- import com .intellij .psi .PsiElement ;
1511import com .intellij .psi .PsiFile ;
16- import com .intellij .psi .codeStyle .CodeStyleManager ;
17- import com .intellij .psi .impl .source .codeStyle .CodeEditUtil ;
1812import com .intellij .psi .search .FilenameIndex ;
1913import com .intellij .psi .search .GlobalSearchScope ;
20- import com .intellij .util .xml .GenericDomValue ;
21- import org .jetbrains .annotations .NotNull ;
14+ import org .jetbrains .idea .maven .project .MavenProjectsManager ;
2215import org .w3c .dom .Document ;
2316import org .w3c .dom .Node ;
2417import org .w3c .dom .NodeList ;
@@ -43,7 +36,7 @@ public boolean addMavenDependency(final Module module,File pomFile, String group
4336 boolean dependencyAdded = false ;
4437
4538 try {
46- if (!pomHasMavenDependency (pomFile ,groupId ,packageId , version )) {
39+ if (!pomHasMavenDependency (pomFile ,groupId ,packageId )) {
4740 PomAppendDependency (module , pomFile ,groupId ,packageId ,version );
4841 dependencyAdded = true ;
4942 }
@@ -59,7 +52,7 @@ public boolean addMavenDependency(final Module module,File pomFile, String group
5952 }
6053
6154
62- public String findNearestModulePomFile (Module module ) {
55+ public static String findNearestModulePomFile (Module module ) {
6356 PsiFile [] pomLibFiles = FilenameIndex .getFilesByName (module .getProject (), "pom.xml" , GlobalSearchScope .allScope (module .getProject ()));
6457 String pomFilePath = null ;
6558 for (PsiFile psiPom : pomLibFiles ) {
@@ -104,7 +97,6 @@ private void PomAppendDependency(final Module module, final File pomFile, String
10497 final Project project = module .getProject ();
10598 final VirtualFile virtualFile = LocalFileSystem .getInstance ().findFileByIoFile (pomFile );
10699 final com .intellij .openapi .editor .Document document = FileDocumentManager .getInstance ().getDocument (virtualFile );
107- final PsiFile psiPomFile = PsiDocumentManager .getInstance (project ).getPsiFile (document );
108100 final FileDocumentManager fileDocumentManager = FileDocumentManager .getInstance ();
109101 fileDocumentManager .saveDocument (document ); //when file is edited and editor is closed, it is needed to save the text
110102 PsiDocumentManager .getInstance (project ).commitDocument (document );
@@ -120,11 +112,11 @@ private void PomAppendDependency(final Module module, final File pomFile, String
120112 });
121113 }
122114
123- private boolean pomHasDependenciesNode (Document document ) {
115+ private static boolean pomHasDependenciesNode (Document document ) {
124116 return getMavenDependenciesNode (document ) != null ;
125117 }
126118
127- private Node getMavenDependenciesNode (Document document ) {
119+ private static Node getMavenDependenciesNode (Document document ) {
128120 Node rootNode = document .getFirstChild ();
129121 NodeList firstChildern = rootNode .getChildNodes ();
130122 Node result = null ;
@@ -141,7 +133,7 @@ private Node getMavenDependenciesNode(Document document) {
141133 return result ;
142134 }
143135
144- private boolean pomHasMavenDependency (File pomFile , String groupId , String packageId , String version ) throws ParserConfigurationException , IOException , SAXException {
136+ public static boolean pomHasMavenDependency (File pomFile , String groupId , String packageId ) throws ParserConfigurationException , IOException , SAXException {
145137 boolean hasDependency = false ;
146138 DocumentBuilderFactory docFactory = DocumentBuilderFactory .newInstance ();
147139 DocumentBuilder docBuilder = docFactory .newDocumentBuilder ();
@@ -155,31 +147,54 @@ private boolean pomHasMavenDependency(File pomFile, String groupId, String packa
155147 NodeList depElements = dependencies .getChildNodes ();
156148 for (int i = 0 ; i < depElements .getLength (); i ++) {
157149 Node dependencyElement = depElements .item (i );
158- if (pomDependencyElementMatch (dependencyElement , groupId ,packageId , version )) {
150+ if (pomDependencyElementMatch (dependencyElement , groupId ,packageId )) {
159151 hasDependency = true ;
160152 break ;
161153 }
162154 }
163155 return hasDependency ;
164156 }
165157
166- private boolean pomDependencyElementMatch (Node dependencyElement , String groupId , String packageId , String version ) {
158+ public static boolean pomHasKotlinDependency (Module module ) {
159+ String pomFilePath = findNearestModulePomFile (module );
160+ if (pomFilePath == null ) {
161+ return false ;
162+ }
163+ File pomFile = new File (pomFilePath );
164+ if (!pomFile .exists ()) {
165+ return false ;
166+ }
167+
168+ try {
169+ return pomHasMavenDependency (pomFile ,"org.jetbrains.kotlin" ,"kotlin-stdlib" );
170+ } catch (ParserConfigurationException e ) {
171+ e .printStackTrace ();
172+ } catch (IOException e ) {
173+ e .printStackTrace ();
174+ } catch (SAXException e ) {
175+ e .printStackTrace ();
176+ }
177+ return false ;
178+ }
179+
180+ private static boolean pomDependencyElementMatch (Node dependencyElement , String groupId , String packageId ) {
167181 boolean groupIdMatch = false ;
168182 boolean artifactIdMatch = false ;
169- boolean versionMatch = false ;
170183 NodeList dependencyProperties = dependencyElement .getChildNodes ();
171184 for (int j = 0 ; j < dependencyProperties .getLength (); j ++) {
172185 Node depProp = dependencyProperties .item (j );
173- if (depProp .getNodeName ().equals ("groupId" ) && depProp .getNodeValue () != null && depProp .getNodeValue ().equals (groupId )) {
186+ if (depProp .getNodeName ().equals ("groupId" ) && depProp .getTextContent () != null && depProp .getTextContent ().equals (groupId )) {
174187 groupIdMatch = true ;
175188 }
176- if (depProp .getNodeName ().equals ("artifactId" ) && depProp .getNodeValue () != null && depProp .getNodeValue ().equals (packageId )) {
189+ if (depProp .getNodeName ().equals ("artifactId" ) && depProp .getTextContent () != null && depProp .getTextContent ().equals (packageId )) {
177190 artifactIdMatch = true ;
178191 }
179- if (depProp .getNodeName ().equals ("version" ) && depProp .getNodeValue () != null && depProp .getNodeValue ().equals (version )) {
180- versionMatch = true ;
181- }
182192 }
183- return groupIdMatch && artifactIdMatch && versionMatch ;
193+ return groupIdMatch && artifactIdMatch ;
194+ }
195+
196+ public static boolean isMavenProjectWithKotlin (Module module ) {
197+ final MavenProjectsManager mavenProjectsManager = MavenProjectsManager .getInstance (module .getProject ());
198+ return mavenProjectsManager .isMavenizedModule (module ) && pomHasKotlinDependency (module );
184199 }
185200}
0 commit comments