2020import org .eclipse .core .resources .IProject ;
2121import org .eclipse .core .resources .IResource ;
2222import org .eclipse .core .runtime .CoreException ;
23+ import org .eclipse .core .runtime .IPath ;
2324import org .eclipse .jdt .core .IJavaProject ;
2425import org .eclipse .jdt .core .IMethod ;
2526import org .eclipse .jdt .core .JavaModelException ;
3031import org .eclipse .jdt .core .search .SearchParticipant ;
3132import org .eclipse .jdt .core .search .SearchPattern ;
3233import org .eclipse .jdt .core .search .SearchRequestor ;
34+ import org .eclipse .jdt .ls .core .internal .ResourceUtils ;
3335import org .eclipse .jdt .ls .core .internal .managers .ProjectsManager ;
3436
3537import com .microsoft .java .debug .core .Configuration ;
@@ -42,11 +44,19 @@ public class ResolveMainClassHandler {
4244 * @return an array of main class and project name
4345 * @throws CoreException when there are errors when resolving main class.
4446 */
45- public Object resolveMainClass () throws CoreException {
46- return resolveMainClassCore ();
47+ public Object resolveMainClass (List < Object > arguments ) throws CoreException {
48+ return resolveMainClassCore (arguments );
4749 }
4850
49- private List <ResolutionItem > resolveMainClassCore () throws CoreException {
51+ private List <ResolutionItem > resolveMainClassCore (List <Object > arguments ) throws CoreException {
52+ IPath rootPath = null ;
53+ if (arguments != null && arguments .size () > 0 ) {
54+ rootPath = ResourceUtils .filePathFromURI ((String ) arguments .get (0 ));
55+ }
56+ final ArrayList <IPath > targetProjectPath = new ArrayList <>();
57+ if (rootPath != null ) {
58+ targetProjectPath .add (rootPath );
59+ }
5060 IJavaSearchScope searchScope = SearchEngine .createWorkspaceScope ();
5161 SearchPattern pattern = SearchPattern .createPattern ("main(String[]) void" , IJavaSearchConstants .METHOD ,
5262 IJavaSearchConstants .DECLARATIONS , SearchPattern .R_CASE_SENSITIVE | SearchPattern .R_EXACT_MATCH );
@@ -72,7 +82,11 @@ public void acceptSearchMatch(SearchMatch match) {
7282 }
7383 }
7484 String projectName = ProjectsManager .DEFAULT_PROJECT_NAME .equals (project .getName ()) ? null : project .getName ();
75- res .add (new ResolutionItem (mainClass , projectName ));
85+ if (projectName == null
86+ || targetProjectPath .isEmpty ()
87+ || ResourceUtils .isContainedIn (project .getLocation (), targetProjectPath )) {
88+ res .add (new ResolutionItem (mainClass , projectName ));
89+ }
7690 }
7791 }
7892 }
0 commit comments