Search in sources :

Example 1 with FileSystemWatcher

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/**"));
}
Also used : FileSystemWatcher(org.eclipse.lsp4j.FileSystemWatcher) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) File(java.io.File) IProject(org.eclipse.core.resources.IProject)

Example 2 with FileSystemWatcher

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/**"));
}
Also used : Arrays(java.util.Arrays) TestVMType(org.eclipse.jdt.ls.core.internal.TestVMType) IFolder(org.eclipse.core.resources.IFolder) ProjectUtils(org.eclipse.jdt.ls.core.internal.ProjectUtils) CoreException(org.eclipse.core.runtime.CoreException) ArrayList(java.util.ArrayList) JavaRuntime(org.eclipse.jdt.launching.JavaRuntime) IProject(org.eclipse.core.resources.IProject) IPath(org.eclipse.core.runtime.IPath) FileSystemWatcher(org.eclipse.lsp4j.FileSystemWatcher) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaCore(org.eclipse.jdt.core.JavaCore) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) File(java.io.File) List(java.util.List) JavaProjectHelper(org.eclipse.jdt.ls.core.internal.JavaProjectHelper) Preferences(org.eclipse.jdt.ls.core.internal.preferences.Preferences) Assert.assertFalse(org.junit.Assert.assertFalse) Path(org.eclipse.core.runtime.Path) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) FileSystemWatcher(org.eclipse.lsp4j.FileSystemWatcher) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) File(java.io.File) IProject(org.eclipse.core.resources.IProject) Test(org.junit.Test)

Example 3 with FileSystemWatcher

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();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) URI(java.net.URI) IProject(org.eclipse.core.resources.IProject) DidChangeWatchedFilesRegistrationOptions(org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions) FileSystemWatcher(org.eclipse.lsp4j.FileSystemWatcher) IJavaProject(org.eclipse.jdt.core.IJavaProject) ResourceUtils(org.eclipse.jdt.ls.core.internal.ResourceUtils) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) IFolder(org.eclipse.core.resources.IFolder)

Example 4 with FileSystemWatcher

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);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) FileEvent(org.eclipse.lsp4j.FileEvent) FileSystemWatcher(org.eclipse.lsp4j.FileSystemWatcher) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) IJavaProject(org.eclipse.jdt.core.IJavaProject) File(java.io.File) IProject(org.eclipse.core.resources.IProject) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 5 with FileSystemWatcher

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();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JavaModelException(org.eclipse.jdt.core.JavaModelException) FileSystemWatcher(org.eclipse.lsp4j.FileSystemWatcher) IPath(org.eclipse.core.runtime.IPath) ResourceUtils(org.eclipse.jdt.ls.core.internal.ResourceUtils) ArrayList(java.util.ArrayList) DidChangeWatchedFilesRegistrationOptions(org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions)

Aggregations

FileSystemWatcher (org.eclipse.lsp4j.FileSystemWatcher)5 IProject (org.eclipse.core.resources.IProject)4 IPath (org.eclipse.core.runtime.IPath)4 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)3 ClientPreferences (org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences)3 LinkedHashSet (java.util.LinkedHashSet)2 IFolder (org.eclipse.core.resources.IFolder)2 Path (org.eclipse.core.runtime.Path)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 ResourceUtils (org.eclipse.jdt.ls.core.internal.ResourceUtils)2 DidChangeWatchedFilesRegistrationOptions (org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions)2 Test (org.junit.Test)2 URI (java.net.URI)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1