Bug 563937 - LanguageServerWrapper#connect() does not watch projects

Change-Id: I4bcb6fa98738f29a29497e430744984656551ac0
Signed-off-by: Brian de Alwis <bsd@acm.org>
diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServerWrapperTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServerWrapperTest.java
index e1077a5..0e031bf 100644
--- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServerWrapperTest.java
+++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServerWrapperTest.java
@@ -15,7 +15,6 @@
 import static org.junit.Assert.assertTrue;
 
 import java.util.Collection;
-
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.CoreException;
@@ -54,9 +53,14 @@
 		assertEquals(1, wrappers.size());
 
 		LanguageServerWrapper wrapper = wrappers.iterator().next();
-
+		for(int i = 0; i < 10 && !wrapper.isActive(); i++) {
+			Thread.sleep(100);
+		}
+		
 		assertTrue(wrapper.isConnectedTo(testFile1.getLocation()));
 		assertTrue(wrapper.isConnectedTo(testFile2.getLocation()));
+		assertTrue(wrapper.watchedProjects().contains(project1));
+		assertTrue(wrapper.watchedProjects().contains(project2));
 
 		TestUtils.closeEditor(editor1, false);
 		TestUtils.closeEditor(editor2, false);
diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java
index a13480a..4e99b86 100644
--- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java
+++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java
@@ -15,6 +15,8 @@
  *******************************************************************************/
 package org.eclipse.lsp4e;
 
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
 import java.io.File;
 import java.io.IOException;
 import java.lang.management.ManagementFactory;
@@ -37,7 +39,6 @@
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.function.Function;
-
 import org.eclipse.core.filebuffers.FileBuffers;
 import org.eclipse.core.filebuffers.IFileBuffer;
 import org.eclipse.core.filebuffers.IFileBufferListener;
@@ -118,9 +119,6 @@
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.PlatformUI;
 
-import com.google.gson.Gson;
-import com.google.gson.JsonObject;
-
 public class LanguageServerWrapper {
 
 	private IFileBufferListener fileBufferListener = new FileBufferListenerAdapter() {
@@ -513,6 +511,14 @@
 	}
 
 	/**
+	 * Return the projects being watched.
+	 */
+	public Set<@NonNull IProject> watchedProjects() {
+		return allWatchedProjects;
+	}
+
+
+	/**
 	 * Check whether this LS is suitable for provided project. Starts the LS if not
 	 * already started.
 	 *
@@ -555,8 +561,8 @@
 	private CompletableFuture<LanguageServer> connect(@NonNull IPath absolutePath, IDocument document) throws IOException {
 		final IPath thePath = Path.fromOSString(absolutePath.toFile().getAbsolutePath()); // should be useless
 
-		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(thePath);
-		if (file.exists()) {
+		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(thePath);
+		if (file != null && file.exists()) {
 			watchProject(file.getProject(), false);
 		}