Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
case VALIDATE_LAUNCHCONFIG:
return new ResolveMainClassHandler().validateLaunchConfig(arguments);
case RESOLVE_MAINMETHOD:
return ResolveMainMethodHandler.resolveMainMethods(arguments);
return ResolveMainMethodHandler.resolveMainMethods(arguments, progress);
case INFER_LAUNCH_COMMAND_LENGTH:
return LaunchCommandHandler.getLaunchCommandLength(JsonUtils.fromJson((String) arguments.get(0), LaunchArguments.class));
case CHECK_PROJECT_SETTINGS:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Microsoft Corporation and others.
* Copyright (c) 2018-2020 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -18,7 +18,7 @@

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.Flags;
Expand All @@ -42,25 +42,29 @@ public class ResolveMainMethodHandler {
* Resolve the main methods from the current file.
* @return an array of main methods.
*/
public static Object resolveMainMethods(List<Object> arguments) throws DebugException {
if (arguments == null || arguments.isEmpty()) {
public static Object resolveMainMethods(List<Object> arguments, IProgressMonitor monitor) throws DebugException {
if (monitor.isCanceled() || arguments == null || arguments.isEmpty()) {
return Collections.emptyList();
}

// When the current document is changed, the language server will receive a didChange request about the changed text and then
// trigger a background job to update the change to the CompilationUnit. Because of race condition, the resolveMainMethods may read
// an old CompilationUnit. So add some waiting logic to wait the Document Update to finish first.
try {
Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, new NullProgressMonitor());
} catch (OperationCanceledException ignorable) {
// Do nothing.
Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, monitor);
} catch (OperationCanceledException e) {
return Collections.emptyList();
} catch (InterruptedException e) {
// Do nothing.
}

if (monitor.isCanceled()) {
return Collections.emptyList();
}

String uri = (String) arguments.get(0);
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
if (unit == null || unit.getResource() == null || !unit.getResource().exists()) {
if (monitor.isCanceled() || unit == null || unit.getResource() == null || !unit.getResource().exists()) {
return Collections.emptyList();
}

Expand Down