Added JDT command for getting Java project
Also added a JDT Project cache in the Spring Boot LS
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2018 Pivotal, Inc.
|
||||
* 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:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.commons.languageserver;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ClasspathResponse {
|
||||
|
||||
public static final String ENTRY_KIND_SOURCE = "source";
|
||||
public static final String ENTRY_KIND_BINARY = "binary";
|
||||
|
||||
private List<Entry> entries;
|
||||
private String defaultOutputFolder;
|
||||
|
||||
public ClasspathResponse(List<Entry> entries, String defaultOutputFolder) {
|
||||
super();
|
||||
this.entries = entries;
|
||||
this.defaultOutputFolder = defaultOutputFolder;
|
||||
}
|
||||
|
||||
public List<Entry> getEntries() {
|
||||
return entries;
|
||||
}
|
||||
|
||||
public void setEntries(List<Entry> entries) {
|
||||
this.entries = entries;
|
||||
}
|
||||
|
||||
public String getDefaultOutputFolder() {
|
||||
return defaultOutputFolder;
|
||||
}
|
||||
|
||||
public void setDefaultOutputFolder(String defaultOutputFolder) {
|
||||
this.defaultOutputFolder = defaultOutputFolder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Classpath [entries=" + entries + ", defaultOutputFolder=" + defaultOutputFolder + "]";
|
||||
}
|
||||
|
||||
public static class Entry {
|
||||
|
||||
private String kind;
|
||||
private String path;
|
||||
|
||||
public Entry(String kind, String path) {
|
||||
super();
|
||||
this.kind = kind;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public String getKind() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
public void setKind(String kind) {
|
||||
this.kind = kind;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CPE [kind=" + kind + ", path=" + path + "]\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2018 Pivotal, Inc.
|
||||
* 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:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.commons.languageserver;
|
||||
|
||||
public class ProjectResponse {
|
||||
|
||||
private String name;
|
||||
private String uri;
|
||||
|
||||
public ProjectResponse(String name, String uri) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
public void setUri(String uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ProjectResponse [name=" + name + ", uri=" + uri + "]";
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,8 @@ public interface STS4LanguageClient extends LanguageClient {
|
||||
CompletableFuture<Object> moveCursor(CursorMovement cursorMovement);
|
||||
|
||||
@JsonRequest("sts/classpath")
|
||||
CompletableFuture<Object> classpath(ClasspathParams classpathEvent);
|
||||
CompletableFuture<ClasspathResponse> classpath(ClasspathParams classpathParams);
|
||||
|
||||
@JsonRequest("sts/project")
|
||||
CompletableFuture<ProjectResponse> project(String uri);
|
||||
}
|
||||
|
||||
@@ -91,8 +91,10 @@ import org.eclipse.lsp4j.WorkspaceEdit;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.Either;
|
||||
import org.eclipse.lsp4j.services.LanguageClientAware;
|
||||
import org.springframework.ide.vscode.commons.languageserver.ClasspathParams;
|
||||
import org.springframework.ide.vscode.commons.languageserver.ClasspathResponse;
|
||||
import org.springframework.ide.vscode.commons.languageserver.HighlightParams;
|
||||
import org.springframework.ide.vscode.commons.languageserver.ProgressParams;
|
||||
import org.springframework.ide.vscode.commons.languageserver.ProjectResponse;
|
||||
import org.springframework.ide.vscode.commons.languageserver.STS4LanguageClient;
|
||||
import org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits;
|
||||
import org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixEdit.CursorMovement;
|
||||
@@ -294,7 +296,12 @@ public class LanguageServerHarness<S extends SimpleLanguageServerWrapper> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Object> classpath(ClasspathParams classpathEvent) {
|
||||
public CompletableFuture<ClasspathResponse> classpath(ClasspathParams classpathEvent) {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ProjectResponse> project(String uri) {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,5 +6,10 @@
|
||||
<command id="sts.java.resolveClasspath"/>
|
||||
</delegateCommandHandler>
|
||||
</extension>
|
||||
<extension point="org.eclipse.jdt.ls.core.delegateCommandHandler">
|
||||
<delegateCommandHandler class="org.springframework.tooling.jdt.ls.extension.ResolveProjectHandler">
|
||||
<command id="sts.java.resolveProject"/>
|
||||
</delegateCommandHandler>
|
||||
</extension>
|
||||
</plugin>
|
||||
|
||||
@@ -12,37 +12,31 @@ package org.springframework.tooling.jdt.ls.extension;
|
||||
|
||||
import static org.springframework.tooling.jdt.ls.extension.Logger.log;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.core.resources.IContainer;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.jdt.core.IClasspathEntry;
|
||||
import org.eclipse.jdt.core.IJavaProject;
|
||||
import org.eclipse.jdt.core.IPackageFragmentRoot;
|
||||
import org.eclipse.jdt.core.JavaCore;
|
||||
import org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler;
|
||||
|
||||
@SuppressWarnings("restriction")
|
||||
public class ResolveClasspathHandler implements IDelegateCommandHandler {
|
||||
|
||||
public static final String ENTRY_CONTENT_SOURCE = "source";
|
||||
public static final String ENTRY_CONTENT_BINARY = "binary";
|
||||
public static final String ENTRY_KIND_SOURCE = "source";
|
||||
public static final String ENTRY_KIND_BINARY = "binary";
|
||||
public static final String OUTPUT_LOCATION = "output_location";
|
||||
|
||||
@Override
|
||||
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor monitor) throws Exception {
|
||||
log("ResolveClasspathHandler=" + commandId);
|
||||
|
||||
try {
|
||||
URI resourceUri = getResourceUri(arguments);
|
||||
URI resourceUri = ResourceUtils.getResourceUri(arguments);
|
||||
|
||||
IJavaProject javaProject = getJavaProject(resourceUri);
|
||||
IJavaProject javaProject = ResourceUtils.getJavaProject(resourceUri);
|
||||
|
||||
return resolveClasspathHandler(javaProject);
|
||||
} catch (Exception e) {
|
||||
@@ -64,62 +58,21 @@ public class ResolveClasspathHandler implements IDelegateCommandHandler {
|
||||
}
|
||||
}
|
||||
Classpath classpath = new Classpath(cpEntries, javaProject.getOutputLocation().toString());
|
||||
log("classpath=" + classpath);
|
||||
// log("classpath=" + classpath);
|
||||
return classpath;
|
||||
}
|
||||
|
||||
private String toContentKind(IClasspathEntry entry) {
|
||||
switch (entry.getContentKind()) {
|
||||
case IPackageFragmentRoot.K_BINARY:
|
||||
return ENTRY_CONTENT_BINARY;
|
||||
return ENTRY_KIND_BINARY;
|
||||
case IPackageFragmentRoot.K_SOURCE:
|
||||
return ENTRY_CONTENT_SOURCE;
|
||||
return ENTRY_KIND_SOURCE;
|
||||
default:
|
||||
return "unknown: " + entry.getContentKind();
|
||||
}
|
||||
}
|
||||
|
||||
private IJavaProject getJavaProject(URI resourceUri) throws Exception {
|
||||
IContainer[] containers = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocationURI(resourceUri);
|
||||
log("containers=" + containers);
|
||||
if (containers.length > 0) {
|
||||
log("containers.length=" + containers.length);
|
||||
Optional<IContainer> shortest = Arrays.stream(containers)
|
||||
.min((f1, f2) -> f1.getFullPath().segmentCount() - f2.getFullPath().segmentCount());
|
||||
log("shortest=" + shortest.isPresent());
|
||||
|
||||
if (shortest.isPresent()) {
|
||||
log("shortest.fullpath=" + shortest.get().getFullPath());
|
||||
|
||||
IProject project = shortest.get().getProject();
|
||||
log("project=" + project.getName());
|
||||
|
||||
if (project.isAccessible() && project.hasNature(JavaCore.NATURE_ID)) {
|
||||
IJavaProject javaProject = JavaCore.create(project);
|
||||
log("javaProject=" + javaProject.getElementName());
|
||||
|
||||
return javaProject;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception("Resolve classpath: unable to resolve a Java project from : " + resourceUri);
|
||||
}
|
||||
|
||||
private URI getResourceUri(List<Object> arguments) throws Exception {
|
||||
if (arguments == null || arguments.size() == 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Resolve classpath: Invalid number of arguments. A resource URI is required.");
|
||||
}
|
||||
URI uri = URI.create((String) arguments.get(0));
|
||||
File resource = new File(uri);
|
||||
if (!resource.exists()) {
|
||||
throw new IllegalArgumentException("Resolve classpath: Resource does not exist: " + resource);
|
||||
}
|
||||
|
||||
return uri;
|
||||
}
|
||||
|
||||
public static class CPE {
|
||||
private String kind;
|
||||
private String path;
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2018 Pivotal, Inc.
|
||||
* 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:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.tooling.jdt.ls.extension;
|
||||
|
||||
import static org.springframework.tooling.jdt.ls.extension.Logger.log;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.jdt.core.IJavaProject;
|
||||
import org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler;
|
||||
|
||||
@SuppressWarnings("restriction")
|
||||
public class ResolveProjectHandler implements IDelegateCommandHandler {
|
||||
|
||||
@Override
|
||||
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor monitor) throws Exception {
|
||||
|
||||
log("ResolveProjectHandler=" + commandId);
|
||||
try {
|
||||
URI resourceUri = ResourceUtils.getResourceUri(arguments);
|
||||
|
||||
IJavaProject javaProject = ResourceUtils.getJavaProject(resourceUri);
|
||||
|
||||
ProjectResponse projectResponse = new ProjectResponse(javaProject.getElementName(), javaProject.getProject().getLocationURI().toString());
|
||||
|
||||
log("projectResponse="+projectResponse);
|
||||
|
||||
return projectResponse;
|
||||
} catch (Exception e) {
|
||||
log(e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public class ProjectResponse {
|
||||
|
||||
private String name;
|
||||
private String uri;
|
||||
|
||||
public ProjectResponse(String name, String uri) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
public void setUri(String uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ProjectResponse [name=" + name + ", uri=" + uri + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2018 Pivotal, Inc.
|
||||
* 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:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.tooling.jdt.ls.extension;
|
||||
|
||||
import static org.springframework.tooling.jdt.ls.extension.Logger.log;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.core.resources.IContainer;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.jdt.core.IJavaProject;
|
||||
import org.eclipse.jdt.core.JavaCore;
|
||||
|
||||
public final class ResourceUtils {
|
||||
|
||||
/**
|
||||
* Resolves a Java project given the resource URI. The resource URI could be any resource contained in the project.
|
||||
* @param resourceUri
|
||||
* @return Java project
|
||||
* @throws Exception if unable to resolve Java project from given resource URI
|
||||
*/
|
||||
public static IJavaProject getJavaProject(URI resourceUri) throws Exception {
|
||||
IContainer[] containers = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocationURI(resourceUri);
|
||||
log("containers=" + containers);
|
||||
if (containers.length > 0) {
|
||||
log("containers.length=" + containers.length);
|
||||
Optional<IContainer> shortest = Arrays.stream(containers)
|
||||
.min((f1, f2) -> f1.getFullPath().segmentCount() - f2.getFullPath().segmentCount());
|
||||
log("shortest=" + shortest.isPresent());
|
||||
|
||||
if (shortest.isPresent()) {
|
||||
log("shortest.fullpath=" + shortest.get().getFullPath());
|
||||
|
||||
IProject project = shortest.get().getProject();
|
||||
log("project=" + project.getName());
|
||||
|
||||
if (project.isAccessible() && project.hasNature(JavaCore.NATURE_ID)) {
|
||||
IJavaProject javaProject = JavaCore.create(project);
|
||||
log("javaProject=" + javaProject.getElementName());
|
||||
|
||||
return javaProject;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception("Resolve classpath: unable to resolve a Java project from : " + resourceUri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the resource URI, IFF it exists. Throws exception if it doesn't exist.
|
||||
* @param arguments
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static URI getResourceUri(List<Object> arguments) throws Exception {
|
||||
if (arguments == null || arguments.size() == 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Resolve classpath: Invalid number of arguments. A resource URI is required.");
|
||||
}
|
||||
URI uri = URI.create((String) arguments.get(0));
|
||||
File resource = new File(uri);
|
||||
if (!resource.exists()) {
|
||||
throw new IllegalArgumentException("Resolve classpath: Resource does not exist: " + resource);
|
||||
}
|
||||
|
||||
return uri;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import java.util.Arrays;
|
||||
import org.eclipse.lsp4j.TextDocumentIdentifier;
|
||||
import org.springframework.ide.vscode.boot.java.handlers.RunningAppProvider;
|
||||
import org.springframework.ide.vscode.boot.java.utils.SpringLiveHoverWatchdog;
|
||||
import org.springframework.ide.vscode.boot.jdt.ls.JdtLsProjectCache;
|
||||
import org.springframework.ide.vscode.boot.metadata.DefaultSpringPropertyIndexProvider;
|
||||
import org.springframework.ide.vscode.boot.metadata.SpringPropertyIndexProvider;
|
||||
import org.springframework.ide.vscode.boot.metadata.types.TypeUtil;
|
||||
@@ -80,13 +81,16 @@ public class BootLanguageServerParams {
|
||||
return (SimpleLanguageServer server) -> {
|
||||
// Initialize project finders, project caches and project observers
|
||||
CompositeJavaProjectFinder javaProjectFinder = new CompositeJavaProjectFinder();
|
||||
JdtLsProjectCache jdtProjectCache = new JdtLsProjectCache(server);
|
||||
javaProjectFinder.addJavaProjectFinder(jdtProjectCache);
|
||||
|
||||
MavenProjectCache mavenProjectCache = new MavenProjectCache(server, MavenCore.getDefault(), true, Paths.get(IJavaProject.PROJECT_CACHE_FOLDER));
|
||||
javaProjectFinder.addJavaProjectFinder(new MavenProjectFinder(mavenProjectCache));
|
||||
|
||||
GradleProjectCache gradleProjectCache = new GradleProjectCache(server, GradleCore.getDefault(), true, Paths.get(IJavaProject.PROJECT_CACHE_FOLDER));
|
||||
javaProjectFinder.addJavaProjectFinder(new GradleProjectFinder(gradleProjectCache));
|
||||
|
||||
CompositeProjectOvserver projectObserver = new CompositeProjectOvserver(Arrays.asList(mavenProjectCache, gradleProjectCache));
|
||||
CompositeProjectOvserver projectObserver = new CompositeProjectOvserver(Arrays.asList(jdtProjectCache, mavenProjectCache, gradleProjectCache));
|
||||
|
||||
DefaultSpringPropertyIndexProvider indexProvider = new DefaultSpringPropertyIndexProvider(javaProjectFinder, projectObserver);
|
||||
indexProvider.setProgressService(server.getProgressService());
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2018 Pivotal, Inc.
|
||||
* 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:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.jdt.ls;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.eclipse.lsp4j.TextDocumentIdentifier;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.ide.vscode.commons.jandex.JandexClasspath;
|
||||
import org.springframework.ide.vscode.commons.java.ClasspathData;
|
||||
import org.springframework.ide.vscode.commons.java.IClasspath;
|
||||
import org.springframework.ide.vscode.commons.java.IJavaProject;
|
||||
import org.springframework.ide.vscode.commons.java.IJavadocProvider;
|
||||
import org.springframework.ide.vscode.commons.languageserver.ClasspathResponse;
|
||||
import org.springframework.ide.vscode.commons.languageserver.ProjectResponse;
|
||||
import org.springframework.ide.vscode.commons.languageserver.ClasspathParams;
|
||||
import org.springframework.ide.vscode.commons.languageserver.STS4LanguageClient;
|
||||
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
|
||||
import org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
|
||||
import org.springframework.ide.vscode.commons.util.CollectorUtil;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.base.Suppliers;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
public class JdtLsProjectCache implements JavaProjectFinder, ProjectObserver {
|
||||
|
||||
private SimpleLanguageServer server;
|
||||
private Map<String, JdtLsProject> table = new HashMap<String, JdtLsProject>();
|
||||
private Logger log = LoggerFactory.getLogger(JdtLsProjectCache.class);
|
||||
|
||||
public JdtLsProjectCache(SimpleLanguageServer server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(Listener listener) {
|
||||
// throw new Error("not implemented yet");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(Listener listener) {
|
||||
// throw new Error("not implemented yet");
|
||||
|
||||
}
|
||||
|
||||
private synchronized IJavaProject project(ProjectResponse project) {
|
||||
if (project == null) {
|
||||
return null;
|
||||
}
|
||||
return table.computeIfAbsent(project.getUri(), uri ->
|
||||
new JdtLsProject(project)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<IJavaProject> find(TextDocumentIdentifier doc) {
|
||||
try {
|
||||
ProjectResponse projectUri = getClient().project(doc.getUri()).get();
|
||||
return Optional.of(project(projectUri));
|
||||
} catch (Exception e) {
|
||||
log.error("Problems finding project for {}", doc.getUri(), e);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private STS4LanguageClient getClient() {
|
||||
return ((SimpleLanguageServer) server).getClient();
|
||||
}
|
||||
|
||||
private class JdtLsProject implements IJavaProject {
|
||||
|
||||
private Supplier<IClasspath> classpath = Suppliers.memoize(this::computeClasspath);
|
||||
private ProjectResponse projectResponse;
|
||||
|
||||
public JdtLsProject(ProjectResponse projectResponse) {
|
||||
this.projectResponse = projectResponse;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IClasspath getClasspath() {
|
||||
return classpath.get();
|
||||
}
|
||||
|
||||
private IClasspath computeClasspath() {
|
||||
try {
|
||||
ClasspathResponse response = getClient().classpath(new ClasspathParams(projectResponse.getUri())).get();
|
||||
return new JdtClasspath(response, projectResponse);
|
||||
} catch (Exception e) {
|
||||
log.error("", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class JdtClasspath extends JandexClasspath {
|
||||
|
||||
private ClasspathResponse response;
|
||||
private String name;
|
||||
private String projectUri;
|
||||
|
||||
public JdtClasspath(ClasspathResponse response, ProjectResponse projectResponse) {
|
||||
this.response = response;
|
||||
this.name = projectResponse.getName();
|
||||
this.projectUri = projectResponse.getUri();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return new File(projectUri).exists();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path getOutputFolder() {
|
||||
return Paths.get(response.getDefaultOutputFolder());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImmutableList<Path> getClasspathEntries() throws Exception {
|
||||
return response
|
||||
.getEntries()
|
||||
.stream()
|
||||
.filter(cpe -> cpe.getKind().equals(ClasspathResponse.ENTRY_KIND_BINARY))
|
||||
.map(cpe -> Paths.get(cpe.getPath()))
|
||||
.collect(CollectorUtil.toImmutableList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImmutableList<String> getClasspathResources() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImmutableList<String> getSourceFolders() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClasspathData createClasspathData() throws Exception {
|
||||
// We should not be needing this as we dont use DelegatingCachedClasspath
|
||||
throw new UnsupportedOperationException("Not supported for JDT classpath: ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<URL> sourceContainer(File classpathResource) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected IJavadocProvider createHtmlJavdocProvider(File classpathResource) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,7 +15,8 @@ import {HighlightService, HighlightParams} from './highlight-service';
|
||||
import { log } from 'util';
|
||||
import { tmpdir } from 'os';
|
||||
import { JVM, findJvm, findJdk } from '@pivotal-tools/jvm-launch-utils';
|
||||
import { registerClasspathService } from './classpath';
|
||||
import { registerClasspathService } from './classpath-service';
|
||||
import { registerProjectService } from './project-service';
|
||||
|
||||
let p2c = P2C.createConverter();
|
||||
|
||||
@@ -215,6 +216,7 @@ function setupLanguageClient(context: VSCode.ExtensionContext, createServer: Ser
|
||||
}
|
||||
return { applied: true};
|
||||
});
|
||||
registerProjectService(client);
|
||||
registerClasspathService(client);
|
||||
return client;
|
||||
});
|
||||
|
||||
22
vscode-extensions/commons-vscode/src/project-service.ts
Normal file
22
vscode-extensions/commons-vscode/src/project-service.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
|
||||
import * as VSCode from 'vscode';
|
||||
import { LanguageClient, RequestType } from 'vscode-languageclient';
|
||||
|
||||
export function registerProjectService(client : LanguageClient) : void {
|
||||
let projectRequest = new RequestType<string, ProjectResponse, void, void>("sts/project");
|
||||
|
||||
client.onRequest(projectRequest, async (uri: string) => {
|
||||
return await executeProjectCommand(uri);
|
||||
});
|
||||
}
|
||||
|
||||
async function executeProjectCommand(resourceUri : string) : Promise<ProjectResponse> {
|
||||
return <ProjectResponse> (await VSCode.commands.executeCommand("java.execute.workspaceCommand", "sts.java.resolveProject", resourceUri));
|
||||
}
|
||||
|
||||
interface ProjectResponse {
|
||||
name: string,
|
||||
uri : string
|
||||
}
|
||||
Reference in New Issue
Block a user