use of org.eclipse.jdt.ls.core.internal.FileSystemWatcher in project eclipse.jdt.ls by eclipse.
the class InitHandlerTest method testWatchers.
@Test
public void testWatchers() throws Exception {
ClientPreferences mockCapabilies = mock(ClientPreferences.class);
when(mockCapabilies.isWorkspaceChangeWatchedFilesDynamicRegistered()).thenReturn(Boolean.TRUE);
when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies);
importProjects(Arrays.asList("maven/salut", "gradle/simple-gradle"));
newEmptyProject();
List<FileSystemWatcher> watchers = projectsManager.registerWatchers();
Collections.sort(watchers, new Comparator<FileSystemWatcher>() {
@Override
public int compare(FileSystemWatcher o1, FileSystemWatcher o2) {
return o1.getGlobPattern().compareTo(o2.getGlobPattern());
}
});
assertEquals(watchers.size(), 5);
assertEquals(watchers.get(0).getGlobPattern(), ResourcesPlugin.getWorkspace().getRoot().getLocation().toString() + "/TestProject/src/**");
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("simple-gradle");
String location = project.getLocation().toString();
assertEquals(watchers.get(1).getGlobPattern(), location + "/src/main/java/**");
assertEquals(watchers.get(2).getGlobPattern(), location + "/src/test/java/**");
project = ResourcesPlugin.getWorkspace().getRoot().getProject("salut");
location = project.getLocation().toString();
assertEquals(watchers.get(3).getGlobPattern(), location + "/src/main/java/**");
assertEquals(watchers.get(4).getGlobPattern(), location + "/src/main/resources/**");
IJavaProject javaProject = JavaCore.create(project);
// for test purposes only
removeExclusionPattern(javaProject);
File outputDir = new File(new File(location), javaProject.getOutputLocation().removeFirstSegments(1).toOSString());
File outputFile = new File(outputDir, "test.properties");
String resourceName = location + "/src/main/resources/test.properties";
String uri = "file://" + resourceName;
File sourceFile = new Path(resourceName).toFile();
assertTrue(FileUtils.contentEquals(sourceFile, outputFile));
FileUtils.writeStringToFile(sourceFile, TEST_CONTENT);
FileEvent fileEvent = new FileEvent(uri, FileChangeType.Changed);
DidChangeWatchedFilesParams params = new DidChangeWatchedFilesParams();
params.getChanges().add(fileEvent);
server.didChangeWatchedFiles(params);
JobHelpers.waitForJobsToComplete();
assertTrue(FileUtils.contentEquals(sourceFile, outputFile));
verify(client, times(1)).registerCapability(any());
List<FileSystemWatcher> newWatchers = projectsManager.registerWatchers();
verify(client, times(1)).registerCapability(any());
Collections.sort(newWatchers, new Comparator<FileSystemWatcher>() {
@Override
public int compare(FileSystemWatcher o1, FileSystemWatcher o2) {
return o1.getGlobPattern().compareTo(o2.getGlobPattern());
}
});
assertEquals(newWatchers, watchers);
}
use of org.eclipse.jdt.ls.core.internal.FileSystemWatcher in project eclipse.jdt.ls by eclipse.
the class ProjectsManager method registerWatchers.
public List<FileSystemWatcher> registerWatchers() {
if (preferenceManager.getClientPreferences().isWorkspaceChangeWatchedFilesDynamicRegistered()) {
Set<String> sources = new HashSet<>();
try {
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (IProject project : projects) {
if (DEFAULT_PROJECT_NAME.equals(project.getName())) {
continue;
}
IJavaProject javaProject = JavaCore.create(project);
if (javaProject != null && javaProject.exists()) {
IClasspathEntry[] classpath = javaProject.getRawClasspath();
for (IClasspathEntry entry : classpath) {
if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
IPath path = entry.getPath();
if (path != null) {
IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(path);
if (folder.exists() && !folder.isDerived()) {
IPath location = folder.getLocation();
if (location != null) {
sources.add(location.toString() + "/**");
}
}
}
}
}
}
}
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
List<FileSystemWatcher> fileWatchers = new ArrayList<>();
for (String pattern : sources) {
FileSystemWatcher watcher = new FileSystemWatcher(pattern, FileSystemWatcher.WATCH_KIND_DEFAULT);
fileWatchers.add(watcher);
}
if (!sources.equals(watchers)) {
logInfo(">> registerFeature 'workspace/didChangeWatchedFiles'");
DidChangeWatchedFilesRegistrationOptions didChangeWatchedFilesRegistrationOptions = new DidChangeWatchedFilesRegistrationOptions(fileWatchers);
JavaLanguageServerPlugin.getInstance().unregisterCapability(Preferences.WORKSPACE_WATCHED_FILES_ID, Preferences.WORKSPACE_WATCHED_FILES);
JavaLanguageServerPlugin.getInstance().registerCapability(Preferences.WORKSPACE_WATCHED_FILES_ID, Preferences.WORKSPACE_WATCHED_FILES, didChangeWatchedFilesRegistrationOptions);
watchers.clear();
watchers.addAll(sources);
}
return fileWatchers;
}
return null;
}
Aggregations