Implemented find project and table for JDT project cache
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2016, 2017 Pivotal, Inc.
|
||||
* Copyright (c) 2016, 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
|
||||
@@ -18,11 +18,33 @@ public class UriUtil {
|
||||
|
||||
public static URI toUri(File file) {
|
||||
try {
|
||||
return new URI("file", "", file.getAbsoluteFile().toURI().getPath(), null); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
return new URI("file", "", file.getAbsoluteFile().toURI().getPath(), null);
|
||||
} catch (URISyntaxException e) {
|
||||
Log.log(e);
|
||||
return file.getAbsoluteFile().toURI();
|
||||
}
|
||||
}
|
||||
|
||||
public static String normalize(String uriVal) {
|
||||
try {
|
||||
if (uriVal != null && uriVal.startsWith("file:")) {
|
||||
File file = new File(URI.create(uriVal));
|
||||
return file.toURI().toString();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return uriVal;
|
||||
}
|
||||
|
||||
public static boolean contains(String projectUri, String uri) {
|
||||
if (projectUri.length() < uri.length()) {
|
||||
return uri.startsWith(projectUri) && uri.charAt(projectUri.length()) == '/';
|
||||
} else if (projectUri.length() > uri.length()) {
|
||||
return false;
|
||||
} else {
|
||||
return projectUri.equals(uri);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
package org.springframework.ide.vscode.boot.jdt.ls;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Classpath {
|
||||
|
||||
public static final String ENTRY_KIND_SOURCE = "source";
|
||||
public static final String ENTRY_KIND_BINARY = "binary";
|
||||
public static final String OUTPUT_LOCATION = "output_location";
|
||||
|
||||
private List<CPE> entries;
|
||||
private String defaultOutputFolder;
|
||||
|
||||
public Classpath(List<CPE> entries, String defaultOutputFolder) {
|
||||
super();
|
||||
this.entries = entries;
|
||||
this.defaultOutputFolder = defaultOutputFolder;
|
||||
}
|
||||
|
||||
public List<CPE> getEntries() {
|
||||
return entries;
|
||||
}
|
||||
|
||||
public void setEntries(List<CPE> 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 CPE {
|
||||
private String kind;
|
||||
private String path;
|
||||
|
||||
public CPE(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";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,8 +14,11 @@ import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@@ -29,9 +32,11 @@ import org.springframework.ide.vscode.commons.java.IJavaProject;
|
||||
import org.springframework.ide.vscode.commons.java.IJavadocProvider;
|
||||
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.jdt.ls.Classpath;
|
||||
import org.springframework.ide.vscode.commons.languageserver.jdt.ls.ClasspathListener;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
|
||||
import org.springframework.ide.vscode.commons.util.CollectorUtil;
|
||||
import org.springframework.ide.vscode.commons.util.UriUtil;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
@@ -42,6 +47,7 @@ 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);
|
||||
private List<Listener> listeners = new ArrayList<>();
|
||||
|
||||
public JdtLsProjectCache(SimpleLanguageServer server) {
|
||||
this.server = server;
|
||||
@@ -50,7 +56,22 @@ public class JdtLsProjectCache implements JavaProjectFinder, ProjectObserver {
|
||||
disposable.complete(server.addClasspathListener(new ClasspathListener() {
|
||||
@Override
|
||||
public void changed(Event event) {
|
||||
log.info("Classpath changed: "+ event.projectUri);
|
||||
synchronized (table) {
|
||||
log.info("Classpath changed: " + event.projectUri);
|
||||
String uri = UriUtil.normalize(event.projectUri);
|
||||
if (event.deleted) {
|
||||
JdtLsProject deleted = table.remove(uri);
|
||||
notifyDelete(deleted);
|
||||
} else {
|
||||
JdtLsProject newProject = new JdtLsProject(event.name, uri, event.classpath);
|
||||
JdtLsProject oldProject = table.put(event.projectUri, newProject);
|
||||
if (oldProject != null) {
|
||||
notifyChanged(newProject);
|
||||
} else {
|
||||
notifyCreated(newProject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
);
|
||||
@@ -61,18 +82,58 @@ public class JdtLsProjectCache implements JavaProjectFinder, ProjectObserver {
|
||||
|
||||
@Override
|
||||
public void addListener(Listener listener) {
|
||||
// throw new Error("not implemented yet");
|
||||
|
||||
synchronized (listeners) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(Listener listener) {
|
||||
// throw new Error("not implemented yet");
|
||||
|
||||
synchronized (listeners) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyDelete(JdtLsProject deleted) {
|
||||
synchronized (listeners) {
|
||||
for (Listener listener : listeners) {
|
||||
listener.deleted(deleted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyChanged(JdtLsProject newProject) {
|
||||
synchronized (listeners) {
|
||||
for (Listener listener : listeners) {
|
||||
listener.changed(newProject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyCreated(JdtLsProject newProject) {
|
||||
synchronized (listeners) {
|
||||
for (Listener listener : listeners) {
|
||||
listener.created(newProject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<IJavaProject> find(TextDocumentIdentifier doc) {
|
||||
String uri = UriUtil.normalize(doc.getUri());
|
||||
System.out.println("finding project from = " + uri);
|
||||
|
||||
synchronized (table) {
|
||||
for (Entry<String, JdtLsProject> e : table.entrySet()) {
|
||||
String projectUri = e.getKey();
|
||||
if (UriUtil.contains(projectUri, uri) ) {
|
||||
System.out.println("find project found = " + projectUri);
|
||||
return Optional.of(e.getValue());
|
||||
}
|
||||
}
|
||||
System.out.println("no project found for = " + uri);
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@@ -80,8 +141,8 @@ public class JdtLsProjectCache implements JavaProjectFinder, ProjectObserver {
|
||||
|
||||
private final IClasspath classpath;
|
||||
|
||||
public JdtLsProject(String name, String uri, Classpath jdtClasspath) {
|
||||
this.classpath = new JdtClasspath(name, uri, jdtClasspath);
|
||||
public JdtLsProject(String name, String projectUri, Classpath classpath) {
|
||||
this.classpath = new JdtClasspath(name, projectUri, classpath);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user