Search in sources :

Example 1 with FileSystemWatcher

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);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) FileEvent(org.eclipse.lsp4j.FileEvent) FileSystemWatcher(org.eclipse.jdt.ls.core.internal.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 2 with FileSystemWatcher

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

Aggregations

IProject (org.eclipse.core.resources.IProject)2 IPath (org.eclipse.core.runtime.IPath)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 FileSystemWatcher (org.eclipse.jdt.ls.core.internal.FileSystemWatcher)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 IFolder (org.eclipse.core.resources.IFolder)1 Path (org.eclipse.core.runtime.Path)1 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 DidChangeWatchedFilesRegistrationOptions (org.eclipse.jdt.ls.core.internal.DidChangeWatchedFilesRegistrationOptions)1 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)1 ClientPreferences (org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences)1 DidChangeWatchedFilesParams (org.eclipse.lsp4j.DidChangeWatchedFilesParams)1 FileEvent (org.eclipse.lsp4j.FileEvent)1 Test (org.junit.Test)1