Search in sources :

Example 1 with DidChangeWatchedFilesRegistrationOptions

use of org.eclipse.jdt.ls.core.internal.DidChangeWatchedFilesRegistrationOptions 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

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 IFolder (org.eclipse.core.resources.IFolder)1 IProject (org.eclipse.core.resources.IProject)1 IPath (org.eclipse.core.runtime.IPath)1 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 DidChangeWatchedFilesRegistrationOptions (org.eclipse.jdt.ls.core.internal.DidChangeWatchedFilesRegistrationOptions)1 FileSystemWatcher (org.eclipse.jdt.ls.core.internal.FileSystemWatcher)1