Repository navigation
Add debug execute command handler. #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <?eclipse version="3.4"?> | ||
| <plugin> | ||
| <extension | ||
| point="org.eclipse.m2e.core.lifecycleMappingMetadataSource"> | ||
| <extension point="org.eclipse.jdt.ls.core.delegateCommandHandler"> | ||
| <delegateCommandHandler class="org.eclipse.jdt.ls.core.internal.DebugDelegateCommandHandlerFactory"> | ||
| <command id="vscode.java.startDebugSession"/> | ||
| <command id="vscode.java.resolveClasspath"/> | ||
| <command id="vscode.java.buildWorkspace"/> | ||
| </delegateCommandHandler> | ||
| </extension> | ||
| </plugin> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...c/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugDelegateCommandHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.microsoft.java.debug.plugin.internal; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler; | ||
|
|
||
|
|
||
| public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler { | ||
|
|
||
| public static String DEBUG_STARTSESSION = "vscode.java.startDebugSession"; | ||
|
|
||
| public static String RESOLVE_CLASSPATH = "vscode.java.resolveClasspath"; | ||
|
|
||
| public static String BUILD_WORKSPACE = "vscode.java.buildWorkspace"; | ||
|
|
||
| @Override | ||
| public Object executeCommand(String commandId, List<Object> arguments) { | ||
| if (DEBUG_STARTSESSION.equals(commandId)) { | ||
|
|
||
| } else if (RESOLVE_CLASSPATH.equals(commandId)) { | ||
| ResolveClasspathsHandler handler = new ResolveClasspathsHandler(); | ||
| return handler.resolveClasspaths(arguments); | ||
| } else if (BUILD_WORKSPACE.equals(commandId)) { | ||
|
|
||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public static JavaDebugDelegateCommandHandler getInstance() { | ||
| return new JavaDebugDelegateCommandHandler(); | ||
| } | ||
| } | ||
|
|
152 changes: 152 additions & 0 deletions
152
...ugin/src/main/java/com/microsoft/java/debug/plugin/internal/ResolveClasspathsHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2017 Red Hat Inc. 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 | ||
| * http://www.eclipse.org/legal/epl-v10.html | ||
| * | ||
| * Contributors: | ||
| * Microsoft Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
| package com.microsoft.java.debug.plugin.internal; | ||
|
|
||
| import static org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logException; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import org.eclipse.core.resources.IProject; | ||
| import org.eclipse.core.resources.IWorkspaceRoot; | ||
| import org.eclipse.core.resources.ResourcesPlugin; | ||
| import org.eclipse.core.runtime.CoreException; | ||
| import org.eclipse.core.runtime.IStatus; | ||
| import org.eclipse.core.runtime.Status; | ||
| import org.eclipse.jdt.core.IJavaElement; | ||
| import org.eclipse.jdt.core.IJavaProject; | ||
| import org.eclipse.jdt.core.JavaCore; | ||
| import org.eclipse.jdt.core.search.IJavaSearchConstants; | ||
| import org.eclipse.jdt.core.search.IJavaSearchScope; | ||
| import org.eclipse.jdt.core.search.SearchEngine; | ||
| import org.eclipse.jdt.core.search.SearchMatch; | ||
| import org.eclipse.jdt.core.search.SearchParticipant; | ||
| import org.eclipse.jdt.core.search.SearchPattern; | ||
| import org.eclipse.jdt.core.search.SearchRequestor; | ||
| import org.eclipse.jdt.launching.JavaRuntime; | ||
| import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; | ||
|
|
||
| public class ResolveClasspathsHandler { | ||
|
|
||
| public String[] resolveClasspaths(List<Object> arguments) { | ||
| try { | ||
| return computeClassPath((String)arguments.get(0), (String)arguments.get(1)); | ||
| } catch (CoreException e) { | ||
| logException("Failed to resolve classpath.", e); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Get java project from name. | ||
| * | ||
| * @param projectName | ||
| * project name | ||
| * @return java project | ||
| * @throws CoreException | ||
| * CoreException | ||
| */ | ||
| private static IJavaProject getJavaProjectFromName(String projectName) throws CoreException { | ||
| IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); | ||
| IProject project = root.getProject(projectName); | ||
| if (!project.exists()) { | ||
| throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID, | ||
| String.format("Cannot find the project with name '%s'.", projectName))); | ||
| } | ||
| if (!project.isNatureEnabled("org.eclipse.jdt.core.javanature")) { | ||
| throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID, | ||
| String.format("The project '%s' does not have java nature enabled.", projectName))); | ||
| } | ||
| IJavaProject javaProject = JavaCore.create(project); | ||
| return javaProject; | ||
| } | ||
|
|
||
| /** | ||
| * Get java project from type. | ||
| * | ||
| * @param typeFullyQualifiedName | ||
| * fully qualified name of type | ||
| * @return java project | ||
| * @throws CoreException | ||
| * CoreException | ||
| */ | ||
| private static List<IJavaProject> getJavaProjectFromType(String typeFullyQualifiedName) throws CoreException { | ||
| SearchPattern pattern = SearchPattern.createPattern(typeFullyQualifiedName, IJavaSearchConstants.TYPE, | ||
| IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH); | ||
| IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); | ||
| ArrayList<IJavaProject> projects = new ArrayList<>(); | ||
| SearchRequestor requestor = new SearchRequestor() { | ||
| @Override | ||
| public void acceptSearchMatch(SearchMatch match) { | ||
| Object element = match.getElement(); | ||
| if (element instanceof IJavaElement) { | ||
| projects.add(((IJavaElement) element).getJavaProject()); | ||
| } | ||
| } | ||
| }; | ||
| SearchEngine searchEngine = new SearchEngine(); | ||
| searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, | ||
| requestor, null /* progress monitor */); | ||
|
|
||
| return projects; | ||
| } | ||
|
|
||
| /** | ||
| * Accord to the project name and the main class, compute runtime classpath. | ||
| * | ||
| * @param projectName | ||
| * project name | ||
| * @param mainClass | ||
| * full qualified class name | ||
| * @return class path | ||
| * @throws CoreException | ||
| * CoreException | ||
| */ | ||
| private static String[] computeClassPath(String projectName, String mainClass) throws CoreException { | ||
| IJavaProject project = null; | ||
| // if type exists in multiple projects, debug configuration need provide | ||
| // project name. | ||
| if (projectName != null) { | ||
| project = getJavaProjectFromName(projectName); | ||
| } else { | ||
| List<IJavaProject> projects = getJavaProjectFromType(mainClass); | ||
| if (projects.size() == 0) { | ||
| throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID, | ||
| String.format("Main class '%s' doesn't exist in the workspace.", mainClass))); | ||
| } | ||
| if (projects.size() > 1) { | ||
| throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID, | ||
| String.format( | ||
| "Main class '%s' isn't unique in the workspace, please pass in specified projectname.", | ||
| mainClass))); | ||
| } | ||
| project = projects.get(0); | ||
| } | ||
| return computeClassPath(project); | ||
| } | ||
|
|
||
| /** | ||
| * Compute runtime classpath of a java project. | ||
| * | ||
| * @param javaProject | ||
| * java project | ||
| * @return class path | ||
| * @throws CoreException | ||
| * CoreException | ||
| */ | ||
| private static String[] computeClassPath(IJavaProject javaProject) throws CoreException { | ||
| if (javaProject == null) { | ||
| throw new IllegalArgumentException("javaProject is null"); | ||
| } | ||
| return JavaRuntime.computeDefaultRuntimeClassPath(javaProject); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not ref this class.