14
14
package com .microsoft .java .debug .plugin .internal ;
15
15
16
16
import java .util .ArrayList ;
17
+ import java .util .LinkedList ;
17
18
import java .util .List ;
18
19
import java .util .logging .Logger ;
19
20
28
29
import org .eclipse .core .resources .ResourcesPlugin ;
29
30
import org .eclipse .core .runtime .CoreException ;
30
31
import org .eclipse .core .runtime .IProgressMonitor ;
31
- import org .eclipse .core .runtime .OperationCanceledException ;
32
32
import org .eclipse .jdt .core .IJavaProject ;
33
33
import org .eclipse .jdt .core .IType ;
34
34
import org .eclipse .jdt .ls .core .internal .BuildWorkspaceStatus ;
35
35
import org .eclipse .jdt .ls .core .internal .JavaLanguageServerPlugin ;
36
36
import org .eclipse .jdt .ls .core .internal .ProjectUtils ;
37
37
import org .eclipse .jdt .ls .core .internal .ResourceUtils ;
38
+ import org .eclipse .jdt .ls .core .internal .handlers .BuildWorkspaceHandler ;
38
39
import org .eclipse .jdt .ls .core .internal .managers .ProjectsManager ;
40
+ import org .eclipse .lsp4j .TextDocumentIdentifier ;
41
+ import org .eclipse .lsp4j .extended .ProjectBuildParams ;
39
42
40
43
import com .microsoft .java .debug .core .Configuration ;
41
44
@@ -45,20 +48,12 @@ public class Compile {
45
48
private static final int GRADLE_BS_COMPILATION_ERROR = 100 ;
46
49
47
50
public static Object compile (CompileParams params , IProgressMonitor monitor ) {
48
- IProject mainProject = params == null ? null : ProjectUtils .getProject (params .getProjectName ());
49
- if (mainProject == null ) {
50
- try {
51
- // Q: is infer project by main class name necessary? perf impact?
52
- List <IJavaProject > javaProjects = ResolveClasspathsHandler .getJavaProjectFromType (params .getMainClass ());
53
- if (javaProjects .size () == 1 ) {
54
- mainProject = javaProjects .get (0 ).getProject ();
55
- }
56
- } catch (CoreException e ) {
57
- JavaLanguageServerPlugin .logException ("Failed to resolve project from main class name." , e );
58
- }
51
+ if (params == null ) {
52
+ throw new IllegalArgumentException ("The compile parameters should not be null." );
59
53
}
60
54
61
- if (isBspProject (mainProject ) && !ProjectUtils .isGradleProject (mainProject )) {
55
+ IProject mainProject = JdtUtils .getMainProject (params .getProjectName (), params .getMainClass ());
56
+ if (JdtUtils .isBspProject (mainProject ) && !ProjectUtils .isGradleProject (mainProject )) {
62
57
// Just need to trigger a build for the target project, the Gradle build server will
63
58
// handle the build dependencies for us.
64
59
try {
@@ -78,20 +73,37 @@ public static Object compile(CompileParams params, IProgressMonitor monitor) {
78
73
return BuildWorkspaceStatus .SUCCEED ;
79
74
}
80
75
81
- try {
82
- if (monitor .isCanceled ()) {
83
- return BuildWorkspaceStatus .CANCELLED ;
84
- }
76
+ if (monitor .isCanceled ()) {
77
+ return BuildWorkspaceStatus .CANCELLED ;
78
+ }
85
79
86
- long compileAt = System . currentTimeMillis ();
87
- if ( params != null && params . isFullBuild ()) {
88
- ResourcesPlugin . getWorkspace (). build ( IncrementalProjectBuilder . CLEAN_BUILD , monitor );
89
- ResourcesPlugin . getWorkspace (). build ( IncrementalProjectBuilder . FULL_BUILD , monitor );
90
- } else {
91
- ResourcesPlugin . getWorkspace (). build ( IncrementalProjectBuilder . INCREMENTAL_BUILD , monitor ) ;
80
+ ProjectBuildParams buildParams = new ProjectBuildParams ();
81
+ List < TextDocumentIdentifier > identifiers = new LinkedList <>();
82
+ buildParams . setFullBuild ( params . isFullBuild );
83
+ for ( IJavaProject javaProject : ProjectUtils . getJavaProjects ()) {
84
+ if ( ProjectsManager . getDefaultProject (). equals ( javaProject . getProject ())) {
85
+ continue ;
92
86
}
93
- logger .info ("Time cost for ECJ: " + (System .currentTimeMillis () - compileAt ) + "ms" );
87
+ // we only build project which is not a BSP project, in case that the compile request is triggered by
88
+ // HCR with auto-build disabled, the build for BSP projects will be triggered by JavaHotCodeReplaceProvider.
89
+ if (!JdtUtils .isBspProject (javaProject .getProject ())) {
90
+ identifiers .add (new TextDocumentIdentifier (javaProject .getProject ().getLocationURI ().toString ()));
91
+ }
92
+ }
93
+ if (identifiers .size () == 0 ) {
94
+ return BuildWorkspaceStatus .SUCCEED ;
95
+ }
94
96
97
+ buildParams .setIdentifiers (identifiers );
98
+ long compileAt = System .currentTimeMillis ();
99
+ BuildWorkspaceHandler buildWorkspaceHandler = new BuildWorkspaceHandler (JavaLanguageServerPlugin .getProjectsManager ());
100
+ BuildWorkspaceStatus status = buildWorkspaceHandler .buildProjects (buildParams , monitor );
101
+ logger .info ("Time cost for ECJ: " + (System .currentTimeMillis () - compileAt ) + "ms" );
102
+ if (status == BuildWorkspaceStatus .FAILED || status == BuildWorkspaceStatus .CANCELLED ) {
103
+ return status ;
104
+ }
105
+
106
+ try {
95
107
IResource currentResource = mainProject ;
96
108
if (isUnmanagedFolder (mainProject ) && StringUtils .isNotBlank (params .getMainClass ())) {
97
109
IType mainType = ProjectUtils .getJavaProject (mainProject ).findType (params .getMainClass ());
@@ -135,29 +147,21 @@ public static Object compile(CompileParams params, IProgressMonitor monitor) {
135
147
}
136
148
}
137
149
138
- if (problemMarkers .isEmpty ()) {
139
- return BuildWorkspaceStatus .SUCCEED ;
150
+ if (! problemMarkers .isEmpty ()) {
151
+ return BuildWorkspaceStatus .WITH_ERROR ;
140
152
}
141
-
142
- return BuildWorkspaceStatus .WITH_ERROR ;
143
153
} catch (CoreException e ) {
144
- JavaLanguageServerPlugin .logException ("Failed to build workspace." , e );
145
- return BuildWorkspaceStatus .FAILED ;
146
- } catch (OperationCanceledException e ) {
147
- return BuildWorkspaceStatus .CANCELLED ;
154
+ JavaLanguageServerPlugin .log (e );
148
155
}
156
+
157
+ return BuildWorkspaceStatus .SUCCEED ;
149
158
}
150
159
151
160
private static boolean isUnmanagedFolder (IProject project ) {
152
161
return project != null && ProjectUtils .isUnmanagedFolder (project )
153
162
&& ProjectUtils .isJavaProject (project );
154
163
}
155
164
156
- private static boolean isBspProject (IProject project ) {
157
- return project != null && ProjectUtils .isJavaProject (project )
158
- && ProjectUtils .hasNature (project , "com.microsoft.gradle.bs.importer.GradleBuildServerProjectNature" );
159
- }
160
-
161
165
private static IProject getDefaultProject () {
162
166
return getWorkspaceRoot ().getProject (ProjectsManager .DEFAULT_PROJECT_NAME );
163
167
}
0 commit comments