use of org.eclipse.lsp4j.FileSystemWatcher in project eclipse.jdt.ls by eclipse.
the class InvisibleProjectImporterTest method automaticJarDetection.
public void automaticJarDetection() throws Exception {
ClientPreferences mockCapabilies = mock(ClientPreferences.class);
when(mockCapabilies.isWorkspaceChangeWatchedFilesDynamicRegistered()).thenReturn(Boolean.TRUE);
when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies);
File projectFolder = createSourceFolderWithLibs("automaticJarDetection", "src", true);
IProject invisibleProject = importRootFolder(projectFolder, "Test.java");
assertNoErrors(invisibleProject);
IJavaProject javaProject = JavaCore.create(invisibleProject);
IClasspathEntry[] classpath = javaProject.getRawClasspath();
assertEquals("Unexpected classpath:\n" + JavaProjectHelper.toString(classpath), 3, classpath.length);
assertEquals("foo.jar", classpath[2].getPath().lastSegment());
assertEquals("foo-sources.jar", classpath[2].getSourceAttachmentPath().lastSegment());
List<FileSystemWatcher> watchers = projectsManager.registerWatchers();
watchers.sort((a, b) -> a.getGlobPattern().compareTo(b.getGlobPattern()));
assertEquals(10, watchers.size());
String srcGlobPattern = watchers.get(7).getGlobPattern();
assertTrue("Unexpected source glob pattern: " + srcGlobPattern, srcGlobPattern.equals("**/src/**"));
String libGlobPattern = watchers.get(9).getGlobPattern();
assertTrue("Unexpected lib glob pattern: " + libGlobPattern, libGlobPattern.endsWith(projectFolder.getName() + "/lib/**"));
}
use of org.eclipse.lsp4j.FileSystemWatcher in project eclipse.jdt.ls by eclipse.
the class InvisibleProjectImporterTest method automaticJarDetectionLibUnderSource.
@Test
public void automaticJarDetectionLibUnderSource() throws Exception {
ClientPreferences mockCapabilies = mock(ClientPreferences.class);
when(mockCapabilies.isWorkspaceChangeWatchedFilesDynamicRegistered()).thenReturn(Boolean.TRUE);
when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies);
File projectFolder = createSourceFolderWithLibs("automaticJarDetectionLibUnderSource");
IProject invisibleProject = importRootFolder(projectFolder, "Test.java");
assertNoErrors(invisibleProject);
IJavaProject javaProject = JavaCore.create(invisibleProject);
IClasspathEntry[] classpath = javaProject.getRawClasspath();
assertEquals("Unexpected classpath:\n" + JavaProjectHelper.toString(classpath), 3, classpath.length);
assertEquals("foo.jar", classpath[2].getPath().lastSegment());
assertEquals("foo-sources.jar", classpath[2].getSourceAttachmentPath().lastSegment());
List<FileSystemWatcher> watchers = projectsManager.registerWatchers();
// watchers.sort((a, b) -> a.getGlobPattern().compareTo(b.getGlobPattern()));
// basic(9) + project(1) + library(1)
assertEquals(12, watchers.size());
String srcGlobPattern = watchers.stream().filter(w -> "**/src/**".equals(w.getGlobPattern())).findFirst().get().getGlobPattern();
assertTrue("Unexpected source glob pattern: " + srcGlobPattern, srcGlobPattern.equals("**/src/**"));
String projGlobPattern = watchers.stream().filter(w -> w.getGlobPattern().endsWith(projectFolder.getName() + "/**")).findFirst().get().getGlobPattern();
assertTrue("Unexpected project glob pattern: " + projGlobPattern, projGlobPattern.endsWith(projectFolder.getName() + "/**"));
String libGlobPattern = watchers.stream().filter(w -> w.getGlobPattern().endsWith(projectFolder.getName() + "/lib/**")).findFirst().get().getGlobPattern();
assertTrue("Unexpected library glob pattern: " + libGlobPattern, libGlobPattern.endsWith(projectFolder.getName() + "/lib/**"));
}
use of org.eclipse.lsp4j.FileSystemWatcher in project eclipse.jdt.ls by eclipse.
the class StandardProjectsManager method registerWatchers.
@Override
public List<FileSystemWatcher> registerWatchers() {
logInfo(">> registerWatchers'");
if (preferenceManager.getClientPreferences().isWorkspaceChangeWatchedFilesDynamicRegistered()) {
Set<String> patterns = new LinkedHashSet<>(basicWatchers);
buildSupports().forEach(e -> e.getWatchPatterns().forEach(patterns::add));
Set<IPath> sources = new HashSet<>();
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
try {
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 && !path.toString().contains("/src/") && !path.toString().endsWith("/src")) {
IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(path);
if (folder.exists() && !folder.isDerived()) {
IPath location = folder.getLocation();
if (location != null && !isContainedIn(location, sources)) {
sources.add(location);
}
}
}
}
if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
IPath path = entry.getPath();
IFile resource = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
if (resource != null && !resource.isDerived()) {
IPath location = resource.getLocation();
if (location != null && !isContainedIn(location, sources)) {
sources.add(location);
}
}
}
}
if (!ProjectUtils.isVisibleProject(project)) {
// Invisible project will watch referenced libraries' include patterns
IPath projectFolder = ProjectUtils.getProjectRealFolder(project);
Set<String> libraries = preferenceManager.getPreferences().getReferencedLibraries().getInclude();
for (String pattern : libraries) {
patterns.add(ProjectUtils.resolveGlobPath(projectFolder, pattern).toPortableString());
}
patterns.add("**/.settings");
}
}
}
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
List<FileSystemWatcher> fileWatchers = new ArrayList<>();
patterns.addAll(sources.stream().map(ResourceUtils::toGlobPattern).collect(Collectors.toList()));
sources.clear();
URI formatter = preferenceManager.getPreferences().getFormatterAsURI();
if (formatter == null && preferenceManager.getPreferences().getFormatterUrl() != null) {
List<URI> uris = getURIs(preferenceManager.getPreferences().getFormatterUrl());
for (URI uri : uris) {
addWatcher(uri, sources);
}
} else {
addWatcher(formatter, sources);
}
URI settings = preferenceManager.getPreferences().getSettingsAsURI();
if (settings == null && preferenceManager.getPreferences().getSettingsUrl() != null) {
List<URI> uris = getURIs(preferenceManager.getPreferences().getSettingsUrl());
for (URI uri : uris) {
addWatcher(uri, sources);
}
} else {
addWatcher(settings, sources);
}
patterns.addAll(sources.stream().map(p -> ResourceUtils.toGlobPattern(p, false)).collect(Collectors.toList()));
for (String pattern : patterns) {
FileSystemWatcher watcher = new FileSystemWatcher(pattern);
fileWatchers.add(watcher);
}
// Watch on project root folders.
for (IProject project : projects) {
if (ProjectUtils.isVisibleProject(project) && project.exists()) {
FileSystemWatcher watcher = new FileSystemWatcher(ResourceUtils.toGlobPattern(project.getLocation(), false), WatchKind.Delete);
fileWatchers.add(watcher);
}
}
if (!patterns.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(patterns);
}
return fileWatchers;
}
return Collections.emptyList();
}
use of org.eclipse.lsp4j.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();
// 8 basic + 3 project roots
assertEquals("Unexpected watchers:\n" + toString(watchers), 12, watchers.size());
List<FileSystemWatcher> projectWatchers = watchers.subList(9, 12);
assertTrue(projectWatchers.get(0).getGlobPattern().endsWith("/TestProject"));
assertTrue(WatchKind.Delete == projectWatchers.get(0).getKind());
assertTrue(projectWatchers.get(1).getGlobPattern().endsWith("/maven/salut"));
assertTrue(projectWatchers.get(2).getGlobPattern().endsWith("/gradle/simple-gradle"));
watchers = watchers.subList(0, 9);
Collections.sort(watchers, new Comparator<FileSystemWatcher>() {
@Override
public int compare(FileSystemWatcher o1, FileSystemWatcher o2) {
return o1.getGlobPattern().compareTo(o2.getGlobPattern());
}
});
assertEquals("**/*.gradle", watchers.get(0).getGlobPattern());
assertEquals("**/*.gradle.kts", watchers.get(1).getGlobPattern());
assertEquals("**/*.java", watchers.get(2).getGlobPattern());
assertEquals("**/.classpath", watchers.get(3).getGlobPattern());
assertEquals("**/.project", watchers.get(4).getGlobPattern());
assertEquals("**/.settings/*.prefs", watchers.get(5).getGlobPattern());
assertEquals("**/gradle.properties", watchers.get(6).getGlobPattern());
assertEquals("**/pom.xml", watchers.get(7).getGlobPattern());
assertEquals("**/src/**", watchers.get(8).getGlobPattern());
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("salut");
String location = project.getLocation().toString();
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());
assertEquals("Unexpected watchers:\n" + toString(watchers), 12, newWatchers.size());
projectWatchers = newWatchers.subList(9, 12);
assertTrue(projectWatchers.get(0).getGlobPattern().endsWith("/TestProject"));
assertTrue(projectWatchers.get(1).getGlobPattern().endsWith("/maven/salut"));
assertTrue(projectWatchers.get(2).getGlobPattern().endsWith("/gradle/simple-gradle"));
newWatchers = watchers.subList(0, 9);
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.lsp4j.FileSystemWatcher in project eclipse.jdt.ls by eclipse.
the class SyntaxProjectsManager method registerWatchers.
@Override
public List<FileSystemWatcher> registerWatchers() {
logInfo(">> registerFeature 'workspace/didChangeWatchedFiles'");
if (JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isWorkspaceChangeWatchedFilesDynamicRegistered()) {
IPath[] sources = new IPath[0];
try {
sources = listAllSourcePaths();
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
List<FileSystemWatcher> fileWatchers = new ArrayList<>();
Set<String> patterns = new LinkedHashSet<>(basicWatchers);
patterns.addAll(Stream.of(sources).map(ResourceUtils::toGlobPattern).collect(Collectors.toList()));
for (String pattern : patterns) {
FileSystemWatcher watcher = new FileSystemWatcher(pattern);
fileWatchers.add(watcher);
}
if (!patterns.equals(watchers)) {
JavaLanguageServerPlugin.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(patterns);
}
return fileWatchers;
}
return Collections.emptyList();
}
Aggregations