11package org .digma .intellij .plugin .common ;
22
3+ import com .intellij .openapi .components .Service ;
34import com .intellij .openapi .project .Project ;
4- import com .intellij .openapi .vfs .VirtualFile ;
5- import com .intellij .psi .*;
6- import com .intellij .util .PlatformUtils ;
7- import org .digma .intellij .plugin .persistence .PersistenceService ;
85import org .digma .intellij .plugin .psi .*;
9- import org .jetbrains .annotations .NotNull ;
6+ import org .jetbrains .annotations .* ;
107
11- @ SuppressWarnings ("UnstableApiUsage" )
12- public class IDEUtilsService {
8+ /*
9+ This class is not perfect but it does serve our needs.
10+ see the comments in com.intellij.util.PlatformUtils, PlatformUtils is marked internal API so its better not to user it.
11+ for what we need its probably ok:
12+ isIdeaIDE() is for sure Idea if we have JavaLanguageService installed.
13+ isRiderIDE() is for sure rider if we have CHarpLanguageService installed.
14+ isPyCharmIDE() is for sure pycharm if we have PythonLanguageService installed and it's not Idea.
15+ if we need more capabilities here we need to explore the technics suggested in com.intellij.util.PlatformUtils
16+ */
1317
14- private final IsRider isRider ;
15- private final IsIdea isIdea ;
18+ @ Service (Service .Level .PROJECT )
19+ public final class IDEUtilsService {
20+
21+ private final Idea idea ;
22+ private final Rider rider ;
23+ private final Pycharm pycharm ;
1624
1725
1826 public IDEUtilsService (Project project ) {
19- isRider = new IsRider (project );
20- isIdea = new IsIdea (project );
27+ idea = new Idea (project );
28+ rider = new Rider (project );
29+ pycharm = new Pycharm (project );
2130 }
2231
2332 public static IDEUtilsService getInstance (@ NotNull Project project ){
2433 return project .getService (IDEUtilsService .class );
2534 }
2635
2736
28- public static boolean isAlreadyPassedInstallationWizard (){
29- PersistenceService persistenceService = PersistenceService .getInstance ();
30- return IDEUtilsService .isIdeaIDE () && persistenceService .isAlreadyPassedTheInstallationWizardForIdeaIDE () ||
31- IDEUtilsService .isRiderIDE () && persistenceService .isAlreadyPassedTheInstallationWizardForRiderIDE () ||
32- IDEUtilsService .isPyCharmIDE () && persistenceService .isAlreadyPassedTheInstallationWizardForPyCharmIDE ();
33- }
34-
35-
36- public static boolean shouldOpenWizard (){
37- return !isAlreadyPassedInstallationWizard ();
38- }
39-
40-
4137 /*
4238 There is no easy way to know if a project is a java project. intellij doesn't have a project type. intellij projects
4339 have modules and each module has some nature. there may be a project that contains a java module and python module,
@@ -49,110 +45,76 @@ public static boolean shouldOpenWizard(){
4945 mean that its pycharm.
5046 */
5147 public boolean isJavaProject () {
52- return ( PlatformUtils . isIdeaCommunity () || PlatformUtils . isIdeaUltimate ()) && isIdea . isIdea ();
48+ return idea . is ();
5349 }
5450
51+ //this is not accurate, it may be rider but not a C# project
5552 public boolean isCSharpProject () {
56- return (PlatformUtils .isIdeaCommunity () || PlatformUtils .isIdeaUltimate ()) && isRider .isRider ();
57- }
58-
59-
60- public static boolean isIdeaIDE () {
61- return PlatformUtils .isIdeaCommunity () || PlatformUtils .isIdeaUltimate ();
53+ return rider .is ();
6254 }
6355
64- public static boolean isRiderIDE () {
65- return PlatformUtils .isRider ();
66- }
6756
68- public static boolean isPyCharmIDE () {
69- return PlatformUtils . isPyCharm () || PlatformUtils . isPyCharmCommunity () || PlatformUtils . isPyCharmEducational ();
57+ public boolean isIdeaIDE () {
58+ return idea . is ();
7059 }
7160
72- public boolean isRiderAndCSharpFile (@ NotNull Project project , VirtualFile file ) {
73-
74- //it may be a C# file that was opened from vcs, it doesn't count as C# that CSharpLanguageService should handle
75- if (!VfsUtilsKt .isValidVirtualFile (file )) {
76- return false ;
77- }
78-
79- if (isRider .isRider ()) {
80- LanguageService csharpLanguageService = isRider .getCSharpLanguageService ();
81- PsiFile psiFile = PsiAccessUtilsKt .runInReadAccessWithResult (() -> PsiManager .getInstance (project ).findFile (file ));
82- if (!PsiUtils .isValidPsiFile (psiFile )) {
83- return false ;
84- }
85- return csharpLanguageService .isServiceFor (psiFile .getLanguage ());
86- }
87-
88- return false ;
61+ public boolean isRiderIDE () {
62+ return rider .is ();
8963 }
9064
91- public boolean isRider () {
92- return isRider .isRider ();
65+ public boolean isPyCharmIDE () {
66+ //python plugin may also be installed on Idea
67+ return pycharm .is () && !idea .is ();
9368 }
9469
9570
96-
97-
98- private static class IsRider {
71+ private static abstract class IsIDE {
9972
10073 private LanguageService myLanguageService = null ;
10174
102- public IsRider (Project project ) {
103- init (project );
75+ private IsIDE (Project project , SupportedLanguages language ) {
76+ init (project , language );
10477 }
10578
106- @ SuppressWarnings ("unchecked" )
107- private void init (Project project ) {
108- Class <LanguageService > cshrpLanguageServiceClass ;
79+ private void init (Project project , SupportedLanguages language ) {
10980 try {
110- cshrpLanguageServiceClass = (Class <LanguageService >) Class .forName (SupportedLanguages .CSHARP .getLanguageServiceClassName ());
111- myLanguageService = project .getService (cshrpLanguageServiceClass );
81+ @ SuppressWarnings ("unchecked" ) Class <LanguageService > languageServiceClass = (Class <LanguageService >) Class .forName (language .getLanguageServiceClassName ());
82+ //noinspection IncorrectServiceRetrieving
83+ myLanguageService = project .getService (languageServiceClass );
11284 } catch (Throwable ignored ) {
11385 //catch throwable and not exception because it may be Error like NoClassDefFound
11486 }
11587 }
11688
117- public boolean isRider () {
89+ boolean is () {
11890 return myLanguageService != null ;
11991 }
12092
121- public LanguageService getCSharpLanguageService () {
93+ @ Nullable
94+ LanguageService getLanguageService () {
12295 return myLanguageService ;
12396 }
124- }
125-
12697
98+ }
12799
128- private static class IsIdea {
129100
130- private LanguageService myLanguageService = null ;
131-
132- public IsIdea (Project project ) {
133- init (project );
134- }
135-
136- @ SuppressWarnings ("unchecked" )
137- private void init (Project project ) {
138- Class <LanguageService > javaLanguageServiceClass ;
139- try {
140- javaLanguageServiceClass = (Class <LanguageService >) Class .forName (SupportedLanguages .JAVA .getLanguageServiceClassName ());
141- myLanguageService = project .getService (javaLanguageServiceClass );
142- } catch (Throwable ignored ) {
143- //catch throwable and not exception because it may be Error like NoClassDefFound
144- }
101+ private static class Rider extends IsIDE {
102+ private Rider (Project project ) {
103+ super (project , SupportedLanguages .CSHARP );
145104 }
105+ }
146106
147- public boolean isIdea () {
148- return myLanguageService != null ;
107+ private static class Idea extends IsIDE {
108+ private Idea (Project project ) {
109+ super (project , SupportedLanguages .JAVA );
149110 }
111+ }
150112
151- public LanguageService getJavaLanguageService () {
152- return myLanguageService ;
113+ private static class Pycharm extends IsIDE {
114+ private Pycharm (Project project ) {
115+ super (project , SupportedLanguages .PYTHON );
153116 }
154117 }
155118
156119
157-
158120}
0 commit comments